Browse Source

fix: several fixes

master
dnomd343 3 years ago
parent
commit
a12c12958c
  1. 3
      .gitignore
  2. 1
      include/process.h
  3. 53
      src/common.c
  4. 2
      src/local.c
  5. 9
      src/process.c
  6. 2
      src/server.c

3
.gitignore

@ -1,2 +1,5 @@
/bin/ /bin/
/build/ /build/
/.idea/
/cmake-build-debug/
/cmake-build-release/

1
include/process.h

@ -1,7 +1,6 @@
#ifndef _PROCESS_H_ #ifndef _PROCESS_H_
#define _PROCESS_H_ #define _PROCESS_H_
extern char *shadowsocks_file;
extern char **shadowsocks_args; extern char **shadowsocks_args;
extern char *plugin_file; extern char *plugin_file;
extern char *SS_REMOTE_HOST; extern char *SS_REMOTE_HOST;

53
src/common.c

@ -23,9 +23,7 @@ void error_exit(char *msg);
char* int_to_string(int num); char* int_to_string(int num);
void pack_shadowsocks_params(); void pack_shadowsocks_params();
char* read_file(char *file_name); char* read_file(char *file_name);
void params_load(char *ss_default);
void json_decode(char *json_content); void json_decode(char *json_content);
void args_decode(int argc, char **argv);
void add_shadowsocks_option(char *option); void add_shadowsocks_option(char *option);
void extra_options_decode(char *extra_opts); void extra_options_decode(char *extra_opts);
@ -66,7 +64,6 @@ void params_load(char *ss_default) { // load shadowsocks and plugin params
SS_PLUGIN_OPTIONS = plugin_opts; SS_PLUGIN_OPTIONS = plugin_opts;
} }
pack_shadowsocks_params(); pack_shadowsocks_params();
shadowsocks_file = shadowsocks;
shadowsocks_args = shadowsocks_opts; shadowsocks_args = shadowsocks_opts;
if (plugin == NULL) { if (plugin == NULL) {
plugin_file = NULL; plugin_file = NULL;
@ -100,7 +97,7 @@ char* read_file(char *file_name) { // read file content
error_exit(strcat(strcat(msg, file_name), msg_suffix)); // merge error message error_exit(strcat(strcat(msg, file_name), msg_suffix)); // merge error message
} }
fseek(pfile, 0, SEEK_END); fseek(pfile, 0, SEEK_END);
int file_length = ftell(pfile); // get file length long file_length = ftell(pfile); // get file length
char *file_content = (char*)malloc(file_length + 1); // malloc new memory char *file_content = (char*)malloc(file_length + 1); // malloc new memory
if (file_content == NULL) { if (file_content == NULL) {
error_exit("no enough memory"); // file too large error_exit("no enough memory"); // file too large
@ -114,7 +111,7 @@ char* read_file(char *file_name) { // read file content
void extra_options_decode(char *extra_opts) { // decode shadowsocks extra options void extra_options_decode(char *extra_opts) { // decode shadowsocks extra options
int num, i; int num, i;
char *tmp = (char*)calloc(strlen(extra_opts) + 1, 1); // new memery and set as 0x00 char *tmp = (char*)calloc(strlen(extra_opts) + 1, 1); // new memory and set as 0x00
num = i = 0; num = i = 0;
for (;;) { for (;;) {
if (extra_opts[num] == '\0' || extra_opts[num] == ' ') { // string end or find a space if (extra_opts[num] == '\0' || extra_opts[num] == ' ') { // string end or find a space
@ -285,8 +282,8 @@ void json_decode(char *json_content) { // decode JSON content
error_exit("`extra_opts` must be a string.\n"); error_exit("`extra_opts` must be a string.\n");
} }
extra_options_decode(json->valuestring); extra_options_decode(json->valuestring);
} else { // unknow field => ERROR } else { // unknown field => ERROR
char *msg_prefix = "Unknow JSON field `"; char *msg_prefix = "Unknown JSON field `";
char *msg_suffix = "`.\n"; char *msg_suffix = "`.\n";
char *msg = (char*)malloc(strlen(msg_prefix) + strlen(json->string) + strlen(msg_suffix) + 1); char *msg = (char*)malloc(strlen(msg_prefix) + strlen(json->string) + strlen(msg_suffix) + 1);
strcpy(msg, msg_prefix); strcpy(msg, msg_prefix);
@ -300,8 +297,11 @@ void json_decode(char *json_content) { // decode JSON content
void args_decode(int argc, char **argv) { // decode the input parameters void args_decode(int argc, char **argv) { // decode the input parameters
args_init(); args_init();
int i; int i;
int debug_flag = 0;
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-c")) { // -c => CONFIG_JSON if (!strcmp(argv[i], "--debug")) { // --debug => dump args
debug_flag = 1;
} else if (!strcmp(argv[i], "-c")) { // -c => CONFIG_JSON
if (i + 1 == argc) { if (i + 1 == argc) {
error_exit("`-c` require a parameter"); error_exit("`-c` require a parameter");
} }
@ -313,7 +313,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (server_addr != NULL) { if (server_addr != NULL) {
free(server_addr); free(server_addr);
} }
server_addr = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
server_addr = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
} else if (!strcmp(argv[i], "-p")) { // -p => server_port } else if (!strcmp(argv[i], "-p")) { // -p => server_port
if (i + 1 == argc) { if (i + 1 == argc) {
error_exit("`-p` require a parameter"); error_exit("`-p` require a parameter");
@ -321,7 +322,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (server_port != NULL) { if (server_port != NULL) {
free(server_port); free(server_port);
} }
server_port = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
server_port = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
} else if (!strcmp(argv[i], "-b")) { // -b => client_addr } else if (!strcmp(argv[i], "-b")) { // -b => client_addr
if (i + 1 == argc) { if (i + 1 == argc) {
error_exit("`-b` require a parameter"); error_exit("`-b` require a parameter");
@ -329,7 +331,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (client_addr != NULL) { if (client_addr != NULL) {
free(client_addr); free(client_addr);
} }
client_addr = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
client_addr = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
} else if (!strcmp(argv[i], "-l")) { // -l => client_port } else if (!strcmp(argv[i], "-l")) { // -l => client_port
if (i + 1 == argc) { if (i + 1 == argc) {
error_exit("`-l` require a parameter"); error_exit("`-l` require a parameter");
@ -337,7 +340,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (client_port != NULL) { if (client_port != NULL) {
free(client_port); free(client_port);
} }
client_port = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
client_port = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
} else if (!strcmp(argv[i], "-k")) { // -k => password } else if (!strcmp(argv[i], "-k")) { // -k => password
if (i + 1 == argc) { if (i + 1 == argc) {
error_exit("`-k` require a parameter"); error_exit("`-k` require a parameter");
@ -345,7 +349,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (password != NULL) { if (password != NULL) {
free(password); free(password);
} }
password = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
password = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
} else if (!strcmp(argv[i], "-m")) { // -m => method } else if (!strcmp(argv[i], "-m")) { // -m => method
if (i + 1 == argc) { if (i + 1 == argc) {
error_exit("`-m` require a parameter"); error_exit("`-m` require a parameter");
@ -353,7 +358,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (method != NULL) { if (method != NULL) {
free(method); free(method);
} }
method = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
method = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
} else if (!strcmp(argv[i], "-t")) { // -t => timeout } else if (!strcmp(argv[i], "-t")) { // -t => timeout
if (i + 1 == argc) { if (i + 1 == argc) {
error_exit("`-t` require a parameter"); error_exit("`-t` require a parameter");
@ -361,7 +367,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (timeout != NULL) { if (timeout != NULL) {
free(timeout); free(timeout);
} }
timeout = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
timeout = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
} else if (!strcmp(argv[i], "--fast-open")) { // --fast-open } else if (!strcmp(argv[i], "--fast-open")) { // --fast-open
fastopen = 1; fastopen = 1;
} else if (!strcmp(argv[i], "--plugin")) { // --plugin => plugin } else if (!strcmp(argv[i], "--plugin")) { // --plugin => plugin
@ -371,7 +378,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (plugin != NULL) { if (plugin != NULL) {
free(plugin); free(plugin);
} }
plugin = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
plugin = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
} else if (!strcmp(argv[i], "--plugin-opts")) { // --plugin-opts => plugin_opts } else if (!strcmp(argv[i], "--plugin-opts")) { // --plugin-opts => plugin_opts
if (i + 1 == argc) { if (i + 1 == argc) {
error_exit("`--plugin-opts` require a parameter"); error_exit("`--plugin-opts` require a parameter");
@ -379,7 +387,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (plugin_opts != NULL) { if (plugin_opts != NULL) {
free(plugin_opts); free(plugin_opts);
} }
plugin_opts = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
plugin_opts = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
} else if (!strcmp(argv[i], "--shadowsocks")) { // --shadowsocks => shadowsocks } else if (!strcmp(argv[i], "--shadowsocks")) { // --shadowsocks => shadowsocks
if (i + 1 == argc) { if (i + 1 == argc) {
error_exit("`--shadowsocks` require a parameter"); error_exit("`--shadowsocks` require a parameter");
@ -387,11 +396,15 @@ void args_decode(int argc, char **argv) { // decode the input parameters
if (shadowsocks != NULL) { if (shadowsocks != NULL) {
free(shadowsocks); free(shadowsocks);
} }
shadowsocks = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[++i]); ++i;
} else { // unknow option => archive shadowsocks = strcpy((char*)malloc(strlen(argv[i]) + 1), argv[i]);
add_shadowsocks_option(argv[i]); // archive unknow options } else { // unknown option => archive
add_shadowsocks_option(argv[i]); // archive unknown options
} }
} }
if (debug_flag) { // show args for debug
args_dump();
}
} }
void args_dump() { // show parameter's content void args_dump() { // show parameter's content

2
src/local.c

@ -30,7 +30,7 @@ ss-bootstrap-local\n\
"; ";
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int i = 0; int i;
for (i = 0; i < argc; ++i) { for (i = 0; i < argc; ++i) {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
printf("%s", help_msg); // show help message printf("%s", help_msg); // show help message

9
src/process.c

@ -6,7 +6,6 @@
#include <sys/prctl.h> #include <sys/prctl.h>
#include "process.h" #include "process.h"
char *shadowsocks_file;
char **shadowsocks_args; char **shadowsocks_args;
char *plugin_file; char *plugin_file;
char *SS_REMOTE_HOST; char *SS_REMOTE_HOST;
@ -44,7 +43,7 @@ void show_exit_info(exit_info info) { // show info of child process death
} else if (info.exit_signal != -1) { // abnormal exit } else if (info.exit_signal != -1) { // abnormal exit
printf("killed by signal %d.\n", info.exit_signal); printf("killed by signal %d.\n", info.exit_signal);
} else { } else {
printf("unknow reason.\n"); printf("unknown reason.\n");
} }
} }
@ -52,7 +51,7 @@ exit_info get_exit_info(int status, pid_t pid) { // get why the child process de
exit_info temp; exit_info temp;
temp.pid = pid; temp.pid = pid;
temp.exit_code = temp.exit_signal = -1; temp.exit_code = temp.exit_signal = -1;
if (WIFEXITED(status)) { // exit normally (with a exit-code) if (WIFEXITED(status)) { // exit normally (with an exit-code)
temp.exit_code = WEXITSTATUS(status); temp.exit_code = WEXITSTATUS(status);
} }
if (WIFSIGNALED(status)) { // abnormal exit (with a signal) if (WIFSIGNALED(status)) { // abnormal exit (with a signal)
@ -153,11 +152,9 @@ void process_exec() { // run shadowsocks main process and plugin (as child proce
} }
void exit_with_child() { // exit and kill his child process void exit_with_child() { // exit and kill his child process
if (exiting) { while (exiting) {
for (;;) {
sleep(1); // block sleep(1); // block
} }
}
exiting = 1; exiting = 1;
if (ss_pid != 0) { if (ss_pid != 0) {
kill(ss_pid, SIGKILL); kill(ss_pid, SIGKILL);

2
src/server.c

@ -30,7 +30,7 @@ ss-bootstrap-server\n\
"; ";
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int i = 0; int i;
for (i = 0; i < argc; ++i) { for (i = 0; i < argc; ++i) {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
printf("%s", help_msg); // show help message printf("%s", help_msg); // show help message

Loading…
Cancel
Save