Browse Source

update: perror log output

master
dnomd343 2 years ago
parent
commit
d1d5fc96d1
  1. 2
      include/common.h
  2. 1
      src/local.c
  3. 4
      src/server.c
  4. 12
      src/utils/network.c
  5. 33
      src/utils/process.c

2
include/common.h

@ -1,7 +1,7 @@
#ifndef _COMMON_H_ #ifndef _COMMON_H_
#define _COMMON_H_ #define _COMMON_H_
#define VERSION "0.9.3" #define VERSION "0.9.4"
#define RANDOM_PORT_START 41952 #define RANDOM_PORT_START 41952
#define RANDOM_PORT_END 65535 #define RANDOM_PORT_END 65535

1
src/local.c

@ -1,5 +1,4 @@
#include "load.h" #include "load.h"
#include "sip003.h"
#include "logger.h" #include "logger.h"
#include "common.h" #include "common.h"
#include "process.h" #include "process.h"

4
src/server.c

@ -31,8 +31,6 @@ int main(int argc, char *argv[]) {
init(argc, argv, help_msg); init(argc, argv, help_msg);
log_info("Shadowsocks bootstrap server (%s)", VERSION); log_info("Shadowsocks bootstrap server (%s)", VERSION);
bootstrap *info = load_info(argc, argv); bootstrap *info = load_info(argc, argv);
load_sip003("ssserver", info); start_bootstrap(0, load_sip003("ssserver", info));
// start_bootstrap("ssserver", is_udp_proxy); // local or server mode
return 0; return 0;
} }

12
src/utils/network.c

@ -154,7 +154,7 @@ int create_ipv4_udp_sock(char *address, int port) { // 创建并绑定IPv4 UDP
ipv4_udp_addr.sin_addr.s_addr = inet_addr(address); // 监听地址 ipv4_udp_addr.sin_addr.s_addr = inet_addr(address); // 监听地址
} }
if (bind(ipv4_udp_sock, (struct sockaddr*)&ipv4_udp_addr, sizeof(ipv4_udp_addr)) < 0) { // 绑定接口 if (bind(ipv4_udp_sock, (struct sockaddr*)&ipv4_udp_addr, sizeof(ipv4_udp_addr)) < 0) { // 绑定接口
perror("[Shadowsocks Bootstrap] IPv4 UDP Sock bind error"); log_perror("IPv4 UDP Sock bind error");
return -1; // 端口被占用 return -1; // 端口被占用
} }
return ipv4_udp_sock; return ipv4_udp_sock;
@ -172,7 +172,7 @@ int create_ipv6_udp_sock(char *address, int port) { // 创建并绑定IPv6 UDP
inet_pton(AF_INET6, address, &ipv6_udp_addr.sin6_addr); // 监听地址 inet_pton(AF_INET6, address, &ipv6_udp_addr.sin6_addr); // 监听地址
} }
if (bind(ipv6_udp_sock, (struct sockaddr*)&ipv6_udp_addr, sizeof(ipv6_udp_addr)) < 0) { // 绑定接口 if (bind(ipv6_udp_sock, (struct sockaddr*)&ipv6_udp_addr, sizeof(ipv6_udp_addr)) < 0) { // 绑定接口
perror("[Shadowsocks Bootstrap] IPv6 UDP Sock bind error"); log_perror("IPv6 UDP Sock bind error");
return -1; // 端口被占用 return -1; // 端口被占用
} }
return ipv6_udp_sock; return ipv6_udp_sock;
@ -222,7 +222,7 @@ long ipv4_send_and_receive(char *ipv4_server_ip, int ipv4_server_port, char *sen
ipv4_server_addr.sin_port = htons(ipv4_server_port); // 目标端口 ipv4_server_addr.sin_port = htons(ipv4_server_port); // 目标端口
ipv4_server_addr.sin_addr.s_addr = inet_addr(ipv4_server_ip); // 目标IP ipv4_server_addr.sin_addr.s_addr = inet_addr(ipv4_server_ip); // 目标IP
if (sendto(ipv4_server_fd, send_buffer, send_len, 0, (struct sockaddr*)&ipv4_server_addr, sizeof(ipv4_server_addr)) < 0) { // 发送缓冲区数据 if (sendto(ipv4_server_fd, send_buffer, send_len, 0, (struct sockaddr*)&ipv4_server_addr, sizeof(ipv4_server_addr)) < 0) { // 发送缓冲区数据
perror("[Shadowsocks Bootstrap] IPv4 UDP send failed"); log_perror("IPv4 UDP send failed");
} }
long recv_len = ipv4_receive(ipv4_server_fd, recv_buffer, BUFFER_SIZE, TIMEOUT, ipv4_server_addr); // 接收数据到缓冲区 long recv_len = ipv4_receive(ipv4_server_fd, recv_buffer, BUFFER_SIZE, TIMEOUT, ipv4_server_addr); // 接收数据到缓冲区
close(ipv4_server_fd); // 关闭描述符 close(ipv4_server_fd); // 关闭描述符
@ -237,7 +237,7 @@ long ipv6_send_and_receive(char *ipv6_server_ip, int ipv6_server_port, char *sen
ipv6_server_addr.sin6_port = htons(ipv6_server_port); // 目标端口 ipv6_server_addr.sin6_port = htons(ipv6_server_port); // 目标端口
inet_pton(AF_INET6, ipv6_server_ip, &ipv6_server_addr.sin6_addr); // 目标IP inet_pton(AF_INET6, ipv6_server_ip, &ipv6_server_addr.sin6_addr); // 目标IP
if (sendto(ipv6_server_fd, send_buffer, send_len, 0, (struct sockaddr*)&ipv6_server_addr, sizeof(ipv6_server_addr)) < 0) { // 发送缓冲区数据 if (sendto(ipv6_server_fd, send_buffer, send_len, 0, (struct sockaddr*)&ipv6_server_addr, sizeof(ipv6_server_addr)) < 0) { // 发送缓冲区数据
perror("[Shadowsocks Bootstrap] IPv6 UDP send failed"); log_perror("IPv6 UDP send failed");
} }
long recv_len = ipv6_receive(ipv6_server_fd, recv_buffer, BUFFER_SIZE, TIMEOUT, ipv6_server_addr); // 接收数据到缓冲区 long recv_len = ipv6_receive(ipv6_server_fd, recv_buffer, BUFFER_SIZE, TIMEOUT, ipv6_server_addr); // 接收数据到缓冲区
close(ipv6_server_fd); // 关闭描述符 close(ipv6_server_fd); // 关闭描述符
@ -260,7 +260,7 @@ void ipv4_proxy(void *ipv4_info) { // 代理IPv4客户端
log_warn("UDP Proxy: server return timeout"); log_warn("UDP Proxy: server return timeout");
} else { } else {
if (sendto(info->ipv4_client_fd, recv_buffer, recv_len, 0, (struct sockaddr*)&(info->ipv4_client_addr), sizeof(info->ipv4_client_addr)) < 0) { // 服务端数据返回给客户端 if (sendto(info->ipv4_client_fd, recv_buffer, recv_len, 0, (struct sockaddr*)&(info->ipv4_client_addr), sizeof(info->ipv4_client_addr)) < 0) { // 服务端数据返回给客户端
perror("[Shadowsocks Bootstrap] IPv4 UDP return failed"); log_perror("IPv4 UDP return failed");
} else { } else {
log_info("UDP Proxy: ↑ %ld bytes ↓ %ld bytes", info->len, recv_len); log_info("UDP Proxy: ↑ %ld bytes ↓ %ld bytes", info->len, recv_len);
} }
@ -278,7 +278,7 @@ void ipv6_proxy(void *ipv6_info) { // 代理IPv6客户端
log_warn("UDP Proxy: Server return timeout"); log_warn("UDP Proxy: Server return timeout");
} else { } else {
if (sendto(info->ipv6_client_fd, recv_buffer, recv_len, 0, (struct sockaddr*)&(info->ipv6_client_addr), sizeof(info->ipv6_client_addr)) < 0) { // 服务端数据返回给客户端 if (sendto(info->ipv6_client_fd, recv_buffer, recv_len, 0, (struct sockaddr*)&(info->ipv6_client_addr), sizeof(info->ipv6_client_addr)) < 0) { // 服务端数据返回给客户端
perror("[Shadowsocks Bootstrap] IPv6 UDP return failed"); log_perror("IPv6 UDP return failed");
} else { } else {
log_info("UDP Proxy: ↑ %ld bytes ↓ %ld bytes", info->len, recv_len); log_info("UDP Proxy: ↑ %ld bytes ↓ %ld bytes", info->len, recv_len);
} }

33
src/utils/process.c

@ -1,7 +1,5 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/prctl.h> #include <sys/prctl.h>
@ -21,16 +19,14 @@ pid_t ss_pid = 0, plugin_pid = 0;
int exiting = 0; // sub process exiting int exiting = 0; // sub process exiting
int exited = 0; // all sub process exited int exited = 0; // all sub process exited
//void process_exec();
//void get_sub_exit();
//void exit_with_child();
//void plugin_env_load();
//void show_exit_info(exit_info info);
//exit_info get_exit_info(int status, pid_t pid);
void error_exit(); void error_exit();
void normal_exit(); void normal_exit();
void get_sub_exit();
void kill_sub_process(); void kill_sub_process();
void show_exit_info(exit_info info, char *prefix);
exit_info get_exit_info(int status, pid_t pid);
char** load_plugin_env(sip003 *service);
void process_exec(sip003 *service);
void normal_exit() { // exit normally void normal_exit() { // exit normally
kill_sub_process(); kill_sub_process();
@ -97,7 +93,7 @@ void get_sub_exit() { // catch child process die
if (ss_pid != 0) { if (ss_pid != 0) {
ss_ret = waitpid(ss_pid, &ss_status, WNOHANG); // non-blocking ss_ret = waitpid(ss_pid, &ss_status, WNOHANG); // non-blocking
if (ss_ret == -1) { if (ss_ret == -1) {
perror("[Shadowsocks Bootstrap] shadowsocks waitpid error"); log_perror("Shadowsocks waitpid error");
error_exit(); error_exit();
} else if (ss_ret) { // ss exit } else if (ss_ret) { // ss exit
sub_exit_info = get_exit_info(ss_status, ss_pid); sub_exit_info = get_exit_info(ss_status, ss_pid);
@ -108,7 +104,7 @@ void get_sub_exit() { // catch child process die
if (plugin != NULL && plugin_pid != 0) { // with plugin if (plugin != NULL && plugin_pid != 0) { // with plugin
plugin_ret = waitpid(plugin_pid, &plugin_status, WNOHANG); // non-blocking plugin_ret = waitpid(plugin_pid, &plugin_status, WNOHANG); // non-blocking
if (plugin_ret == -1) { if (plugin_ret == -1) {
perror("[Shadowsocks Bootstrap] plugin waitpid error"); log_perror("Plugin waitpid error");
error_exit(); error_exit();
} else if (plugin_ret) { // plugin exit } else if (plugin_ret) { // plugin exit
sub_exit_info = get_exit_info(plugin_status, plugin_pid); sub_exit_info = get_exit_info(plugin_status, plugin_pid);
@ -119,7 +115,7 @@ void get_sub_exit() { // catch child process die
exited = 1; exited = 1;
} }
char** plugin_env_load(sip003 *service) { // load plugin's environment variable char** load_plugin_env(sip003 *service) { // load plugin's environment variable
char *remote_host = "SS_REMOTE_HOST="; char *remote_host = "SS_REMOTE_HOST=";
char *remote_port = "SS_REMOTE_PORT="; char *remote_port = "SS_REMOTE_PORT=";
char *local_host = "SS_LOCAL_HOST="; char *local_host = "SS_LOCAL_HOST=";
@ -146,15 +142,12 @@ char** plugin_env_load(sip003 *service) { // load plugin's environment variable
void process_exec(sip003 *service) { // run shadowsocks main process and plugin void process_exec(sip003 *service) { // run shadowsocks main process and plugin
if ((ss_pid = fork()) < 0) { if ((ss_pid = fork()) < 0) {
perror("[Shadowsocks Bootstrap] fork error"); log_perror("Shadowsocks fork error");
error_exit(); error_exit();
} else if (ss_pid == 0) { // child process } else if (ss_pid == 0) { // child process
prctl(PR_SET_PDEATHSIG, SIGKILL); // child die with his father prctl(PR_SET_PDEATHSIG, SIGKILL); // child die with his father
if (execvp(service->shadowsocks_cmd[0], service->shadowsocks_cmd) < 0) { if (execvp(service->shadowsocks_cmd[0], service->shadowsocks_cmd) < 0) {
log_perror("Shadowsocks exec error"); log_perror("Shadowsocks exec error");
// perror("[Shadowsocks Bootstrap] shadowsocks exec error");
exit(2); exit(2);
} }
} }
@ -167,14 +160,14 @@ void process_exec(sip003 *service) { // run shadowsocks main process and plugin
return; return;
} }
if ((plugin_pid = fork()) < 0) { if ((plugin_pid = fork()) < 0) {
perror("[Shadowsocks Bootstrap] fork error"); log_perror("Plugin fork error");
error_exit(); error_exit();
} else if (plugin_pid == 0) { // child process } else if (plugin_pid == 0) { // child process
prctl(PR_SET_PDEATHSIG, SIGKILL); // child die with his father prctl(PR_SET_PDEATHSIG, SIGKILL); // child die with his father
char **plugin_env = plugin_env_load(service); char **plugin_env = load_plugin_env(service);
char *plugin_arg[] = { plugin, NULL }; char *plugin_arg[] = { plugin, NULL };
if (execvpe(plugin, plugin_arg, plugin_env) < 0) { if (execvpe(plugin, plugin_arg, plugin_env) < 0) {
perror("[Shadowsocks Bootstrap] plugin exec error"); log_perror("Plugin exec error");
exit(2); exit(2);
} }
} }

Loading…
Cancel
Save