From 26b45b9b21876cf4e929aadf5a68636d26d15723 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sun, 4 Sep 2022 13:25:35 +0800 Subject: [PATCH] update: shadowsocks bootstrap framework --- include/common.h | 23 +---- include/help.h | 9 -- include/load.h | 16 +++- include/sip003.h | 7 ++ include/{ => utils}/cJSON.h | 0 include/{ => utils}/dns.h | 0 include/{ => utils}/logger.h | 2 +- include/{ => utils}/network.h | 0 include/{ => utils}/process.h | 0 src/CMakeLists.txt | 19 ++-- src/common.c | 173 +++++----------------------------- src/help.c | 66 ------------- src/load.c | 82 ++++++++-------- src/local.c | 38 ++++++-- src/server.c | 35 +++++-- src/sip003.c | 60 ++++++++++++ src/utils/CMakeLists.txt | 2 + src/{ => utils}/cJSON.c | 0 src/{ => utils}/dns.c | 0 src/{ => utils}/logger.c | 4 +- src/{ => utils}/network.c | 0 src/{ => utils}/process.c | 5 - 22 files changed, 214 insertions(+), 327 deletions(-) delete mode 100644 include/help.h create mode 100644 include/sip003.h rename include/{ => utils}/cJSON.h (100%) rename include/{ => utils}/dns.h (100%) rename include/{ => utils}/logger.h (95%) rename include/{ => utils}/network.h (100%) rename include/{ => utils}/process.h (100%) delete mode 100644 src/help.c create mode 100644 src/sip003.c create mode 100644 src/utils/CMakeLists.txt rename src/{ => utils}/cJSON.c (100%) rename src/{ => utils}/dns.c (100%) rename src/{ => utils}/logger.c (90%) rename src/{ => utils}/network.c (100%) rename src/{ => utils}/process.c (98%) diff --git a/include/common.h b/include/common.h index f21f812..9a69dcd 100644 --- a/include/common.h +++ b/include/common.h @@ -1,29 +1,16 @@ #ifndef _COMMON_H_ #define _COMMON_H_ -#define VERSION "0.9.1" +#define VERSION "0.9.2" #define RANDOM_PORT_START 41952 #define RANDOM_PORT_END 65535 -typedef struct { - int is_udp_proxy; - char *server_addr, *client_addr; - char *server_port, *client_port; - char *password; - char *method; - char *timeout; - int fastopen; - char *plugin; - char *plugin_opts; - char *shadowsocks; - char **shadowsocks_opts; -} bootstrap_info; - -char* int_to_string(int num); char* new_string(char *str); +char* int_to_string(int num); char* read_file(char *file_name); -void params_load(char *ss_default, bootstrap_info *info); -void args_decode(int argc, char **argv, bootstrap_info *info); +char** string_list_append(char **string_list, char *data); + +void init(int argc, char **argv, char *help_msg); #endif diff --git a/include/help.h b/include/help.h deleted file mode 100644 index 0f8743e..0000000 --- a/include/help.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _HELP_H_ -#define _HELP_H_ - -extern char *local_help_msg; -extern char *server_help_msg; - -void is_show_help(int argc, char **argv, char *help_msg); - -#endif diff --git a/include/load.h b/include/load.h index 3927676..b17f7ec 100644 --- a/include/load.h +++ b/include/load.h @@ -1,6 +1,20 @@ #ifndef _LOAD_H_ #define _LOAD_H_ -void load_info(int argc, char **argv); +typedef struct { + int is_udp_proxy; + char *server_addr, *client_addr; + char *server_port, *client_port; + char *password; + char *method; + char *timeout; + int fastopen; + char *plugin; + char *plugin_opts; + char *shadowsocks; + char **shadowsocks_opts; +} boot_info; + +boot_info* load_info(int argc, char **argv); #endif diff --git a/include/sip003.h b/include/sip003.h new file mode 100644 index 0000000..7f88d21 --- /dev/null +++ b/include/sip003.h @@ -0,0 +1,7 @@ +#ifndef _SIP003_H_ +#define _SIP003_H_ + +//void params_load(char *ss_default, bootstrap_info *info); +//void args_decode(int argc, char **argv, bootstrap_info *info); + +#endif diff --git a/include/cJSON.h b/include/utils/cJSON.h similarity index 100% rename from include/cJSON.h rename to include/utils/cJSON.h diff --git a/include/dns.h b/include/utils/dns.h similarity index 100% rename from include/dns.h rename to include/utils/dns.h diff --git a/include/logger.h b/include/utils/logger.h similarity index 95% rename from include/logger.h rename to include/utils/logger.h index 1108984..2c08be1 100644 --- a/include/logger.h +++ b/include/utils/logger.h @@ -19,7 +19,7 @@ enum { #define log_error(...) log_printf(LOG_ERROR, __VA_ARGS__) #define log_fatal(...) log_printf(LOG_FATAL, __VA_ARGS__) -extern int log_level; +extern int LOG_LEVEL; void log_printf(int level, const char *fmt, ...); #endif diff --git a/include/network.h b/include/utils/network.h similarity index 100% rename from include/network.h rename to include/utils/network.h diff --git a/include/process.h b/include/utils/process.h similarity index 100% rename from include/process.h rename to include/utils/process.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea3ac09..0e6c4ef 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,19 +1,12 @@ cmake_minimum_required(VERSION 2.8.12) include_directories(${PROJECT_SOURCE_DIR}/include) -include_directories(/usr/lib/i386-linux-gnu/glib-2.0/include) -include_directories(/usr/lib/x86_64-linux-gnu/glib-2.0/include) -include_directories(/usr/lib/aarch64-linux-gnu/glib-2.0/include) -include_directories(/usr/lib64/glib-2.0/include) -include_directories(/usr/lib/glib-2.0/include) -include_directories(/usr/include/glib-2.0) +include_directories(${PROJECT_SOURCE_DIR}/include/utils) -aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC) -list(REMOVE_ITEM SRC ${PROJECT_SOURCE_DIR}/src/local.c) -list(REMOVE_ITEM SRC ${PROJECT_SOURCE_DIR}/src/server.c) +add_subdirectory(utils) -add_executable(ss-bootstrap-local local.c ${SRC}) -target_link_libraries(ss-bootstrap-local glib-2.0 pthread) +add_executable(ss-bootstrap-local local.c common.c load.c) +target_link_libraries(ss-bootstrap-local utils pthread) -add_executable(ss-bootstrap-server server.c ${SRC}) -target_link_libraries(ss-bootstrap-server glib-2.0 pthread) +add_executable(ss-bootstrap-server local.c common.c load.c) +target_link_libraries(ss-bootstrap-server utils pthread) diff --git a/src/common.c b/src/common.c index 8489c36..87f53d9 100644 --- a/src/common.c +++ b/src/common.c @@ -1,39 +1,11 @@ -#include #include #include -#include "cJSON.h" -#include "common.h" -#include "network.h" -#include "process.h" #include "logger.h" +#include "common.h" -//int is_udp_proxy = 1; -//char *server_addr = NULL, *client_addr = NULL; -//char *server_port = NULL, *client_port = NULL; -//char *password = NULL; -//char *method = NULL; -//char *timeout = NULL; -//int fastopen = 0; -//char *plugin = NULL; -//char *plugin_opts = NULL; -//char *shadowsocks = NULL; -//char **shadowsocks_opts; // init before usage - -void args_dump(); -//void error_exit(char *msg); -char* int_to_string(int num); -void pack_shadowsocks_params(bootstrap_info *info); -char* read_file(char *file_name); -//void json_decode(char *json_content); -void add_shadowsocks_option(char *option, char **opts); -void extra_options_decode(char *extra_opts, char **opts); -//void params_load(char *ss_default, bootstrap_info *info); - -//void error_exit(char *msg) { // throw error message with exit-code 1 -// printf("[Shadowsocks Bootstrap] ERROR: %s.\n", msg); -// printf("[Shadowsocks Bootstrap] exit with error.\n"); -// exit(1); -//} +char* new_string(char *str) { + return strcpy((char*)malloc(strlen(str) + 1), str); +} char* int_to_string(int num) { // int -> string if (num < 0) { @@ -50,46 +22,15 @@ char* int_to_string(int num) { // int -> string return str; } -void params_load(char *ss_default, bootstrap_info *info) { // load shadowsocks and plugin params - if (info->shadowsocks == NULL) { - info->shadowsocks = ss_default; - } - info->shadowsocks_opts[0] = info->shadowsocks; // fill with file name - if (info->plugin != NULL) { // with plugin - char *rand_port = int_to_string(get_available_port(RANDOM_PORT_START, RANDOM_PORT_END)); - SS_REMOTE_HOST = info->server_addr; - SS_REMOTE_PORT = info->server_port; - SS_LOCAL_HOST = "127.0.0.1"; - SS_LOCAL_PORT = rand_port; - info->server_addr = SS_LOCAL_HOST; - info->server_port = SS_LOCAL_PORT; - SS_PLUGIN_OPTIONS = info->plugin_opts; - } - pack_shadowsocks_params(info); - shadowsocks_args = info->shadowsocks_opts; - if (info->plugin == NULL) { - plugin_file = NULL; - } else { - plugin_file = info->plugin; - } +char** string_list_append(char **string_list, char *data) { + int num = 0; + while(string_list[num++] != NULL); // get string list size + string_list = (char**)realloc(string_list, sizeof(char**) * (num + 1)); + string_list[num - 1] = new_string(data); + string_list[num] = NULL; // list end sign + return string_list; } -//void args_init() { // init bootstrap arguments -// is_udp_proxy = 1; // enable udp proxy in default -// server_addr = client_addr = NULL; -// server_port = client_port = NULL; -// password = NULL; -// method = NULL; -// timeout = NULL; -// fastopen = 0; -// plugin = NULL; -// plugin_opts = NULL; -// shadowsocks = NULL; -// shadowsocks_opts = (char**)malloc(sizeof(char*) * 2); -// shadowsocks_opts[0] = ""; // reserved for program name -// shadowsocks_opts[1] = NULL; -//} - char* read_file(char *file_name) { // read file content log_debug("Start read file -> %s", file_name); FILE *pfile = fopen(file_name, "rb"); @@ -110,86 +51,18 @@ char* read_file(char *file_name) { // read file content return file_content; } - - -void add_shadowsocks_option(char *option, char **opts) { // add shadowsocks options - int opt_num = 0; - while(opts[opt_num++] != NULL); // get options number - opts = (char**)realloc(opts, sizeof(char**) * (opt_num + 1)); - opts[opt_num - 1] = strcpy((char*)malloc(strlen(option) + 1), option); - opts[opt_num] = NULL; // end sign -} - -void pack_shadowsocks_params(bootstrap_info *info) { // packaging shadowsocks parameters - if (info->server_addr != NULL) { - add_shadowsocks_option("-s", info->shadowsocks_opts); - add_shadowsocks_option(info->server_addr, info->shadowsocks_opts); - } - if (info->client_addr != NULL) { - add_shadowsocks_option("-b", info->shadowsocks_opts); - add_shadowsocks_option(info->client_addr, info->shadowsocks_opts); - } - if (info->server_port != NULL) { - add_shadowsocks_option("-p", info->shadowsocks_opts); - add_shadowsocks_option(info->server_port, info->shadowsocks_opts); - } - if (info->client_port != NULL) { - add_shadowsocks_option("-l", info->shadowsocks_opts); - add_shadowsocks_option(info->client_port, info->shadowsocks_opts); - } - if (info->password != NULL) { - add_shadowsocks_option("-k", info->shadowsocks_opts); - add_shadowsocks_option(info->password, info->shadowsocks_opts); - } - if (info->method != NULL) { - add_shadowsocks_option("-m", info->shadowsocks_opts); - add_shadowsocks_option(info->method, info->shadowsocks_opts); - } - if (info->timeout != NULL) { - add_shadowsocks_option("-t", info->shadowsocks_opts); - add_shadowsocks_option(info->timeout, info->shadowsocks_opts); - } - if (info->fastopen) { - add_shadowsocks_option("--fast-open", info->shadowsocks_opts); - } -} - - - -char* new_string(char *str) { - return strcpy((char*)malloc(strlen(str) + 1), str); -} - -void args_debug(bootstrap_info *info) { - if (info->is_udp_proxy) { - log_debug("is_udp_proxy = true"); - } else { - log_debug("is_udp_proxy = false"); +void init(int argc, char **argv, char *help_msg) { + if (argc <= 1) { // with only one argument + printf("%s", help_msg); + exit(0); } - printf("server_addr = %s\n", info->server_addr); - printf("client_addr = %s\n", info->client_addr); - printf("server_port = %s\n", info->server_port); - printf("client_port = %s\n", info->client_port); - printf("password = %s\n", info->password); - printf("method = %s\n", info->method); - printf("timeout = %s\n", info->timeout); - if (info->fastopen) { - printf("fastopen = true\n"); - } else { - printf("fastopen = false\n"); - } - printf("shadowsocks = %s\n", info->shadowsocks); - printf("plugin = %s\n", info->plugin); - printf("plugin_opts = %s\n", info->plugin_opts); - int num = 0; - printf("options:\n"); - while(info->shadowsocks_opts[num] != NULL) { - printf(" '%s'\n", info->shadowsocks_opts[num]); - num++; + for (int i = 0; i < argc; ++i) { + if (!strcmp(argv[i], "--debug")) { // include `--debug` + LOG_LEVEL = LOG_DEBUG; + } + if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { // include `-h` or `--help` + printf("%s", help_msg); + exit(0); + } } } - - -//void args_dump() { // show parameter's content -// -//} diff --git a/src/help.c b/src/help.c deleted file mode 100644 index 462de90..0000000 --- a/src/help.c +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include -#include "help.h" - -char *server_help_msg = "\n\ -ss-bootstrap-server\n\ -\n\ - A simple program to make the original shadowsocks support SIP003 plugins.\n\ -\n\ - -s Host name or IP address of your remote server.\n\ - -p Port number of your remote server.\n\ - -b Local address to bind.\n\ - -l Port number of your local server.\n\ -\n\ - -c Path to JSON config file.\n\ - -k Password of your remote server.\n\ - -m Encrypt method.\n\ - -t Socket timeout in seconds.\n\ - --fast-open Enable TCP fast open (with Linux kernel 3.7+).\n\ - --plugin Enable SIP003 plugin.\n\ - --plugin-opts Set SIP003 plugin options.\n\ - --shadowsocks Set shadowsocks server program.\n\ - --no-udp Do not use UDP proxy.\n\ - -h, --help Print this message.\n\ -\n\ -"; - -char *local_help_msg = "\n\ -ss-bootstrap-local\n\ -\n\ - A simple program to make the original shadowsocks support SIP003 plugins.\n\ -\n\ - -s Host name or IP address of your remote server.\n\ - -p Port number of your remote server.\n\ - -b Local address to bind.\n\ - -l Port number of your local server.\n\ -\n\ - -c Path to JSON config file.\n\ - -k Password of your remote server.\n\ - -m Encrypt method.\n\ - -t Socket timeout in seconds.\n\ - --fast-open Enable TCP fast open (with Linux kernel 3.7+).\n\ - --plugin Enable SIP003 plugin.\n\ - --plugin-opts Set SIP003 plugin options.\n\ - --shadowsocks Set shadowsocks local program.\n\ - --no-udp Do not use UDP proxy.\n\ - -h, --help Print this message.\n\ -\n\ -"; - -void show_help(char *help_msg) { - printf("%s", help_msg); - exit(0); // exit normally -} - -void is_show_help(int argc, char **argv, char *help_msg) { // output help when necessary - if (argc <= 1) { // with only one argument - show_help(help_msg); - } - for (int i = 0; i < argc; ++i) { - if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { // include `-h` or `--help` - show_help(help_msg); - } - } -} diff --git a/src/load.c b/src/load.c index 8c3334c..93ee564 100644 --- a/src/load.c +++ b/src/load.c @@ -1,11 +1,17 @@ #include #include -#include "load.h" #include "common.h" #include "logger.h" #include "cJSON.h" +#include "load.h" -void init_info(bootstrap_info *info) { +void init_info(boot_info *info); +void dump_info(boot_info *info); +char** add_extra_opts(char **opts, char *extra_opts_str); +void json_decode(char *json_content, boot_info *info); +int add_field(char *field, char **target, char ***arg, char ***arg_limit); + +void init_info(boot_info *info) { info->is_udp_proxy = 1; // enabled udp proxy info->server_addr = info->client_addr = NULL; info->server_port = info->client_port = NULL; @@ -22,7 +28,7 @@ void init_info(bootstrap_info *info) { info->shadowsocks_opts[1] = NULL; } -void dump_info(bootstrap_info *info) { +void dump_info(boot_info *info) { if (info->is_udp_proxy) { log_debug("is_udp_proxy = true"); } else { @@ -51,30 +57,7 @@ void dump_info(bootstrap_info *info) { } } -char** string_list_append(char **string_list, char *data) { - int num = 0; - while(string_list[num++] != NULL); // get string list size - string_list = (char**)realloc(string_list, sizeof(char**) * (num + 1)); - string_list[num - 1] = new_string(data); - string_list[num] = NULL; // list end sign - return string_list; -} - -int load_field(char *field, char **target, char ***arg, char ***arg_limit) { - if (strcmp(**arg, field)) { // field not match - return 0; - } - if (++(*arg) == *arg_limit) { // without next argument - log_fatal("`%s` require a parameter", field); - } - if (*target != NULL) { - free(*target); // override target field - } - *target = new_string(**arg); - return 1; -} - -char** add_extra_options(char **opts, char *extra_opts_str) { // split shadowsocks extra options +char** add_extra_opts(char **opts, char *extra_opts_str) { // split shadowsocks extra options log_debug("Split extra options -> `%s`", extra_opts_str); char *tmp = (char*)calloc(strlen(extra_opts_str) + 1, 1); // new memory and set as 0x00 for (int i = 0, num = 0;; ++num) { @@ -97,7 +80,7 @@ char** add_extra_options(char **opts, char *extra_opts_str) { // split shadowsoc return opts; } -void json_decode(char *json_content, bootstrap_info *info) { // decode JSON content +void json_decode(char *json_content, boot_info *info) { // decode JSON content cJSON* json = cJSON_Parse(json_content); if (json == NULL) { log_fatal("JSON format error"); @@ -211,26 +194,36 @@ void json_decode(char *json_content, bootstrap_info *info) { // decode JSON cont if (!cJSON_IsString(json)) { log_fatal("`extra_opts` must be string"); } - info->shadowsocks_opts = add_extra_options(info->shadowsocks_opts, json->valuestring); + info->shadowsocks_opts = add_extra_opts(info->shadowsocks_opts, json->valuestring); } else { // unknown field => ERROR - log_fatal("Unknown JSON field `%s`", json->string); - } json = json->next; // next field } cJSON_free(json); // free JSON struct } +int add_field(char *field, char **target, char ***arg, char ***arg_limit) { + if (strcmp(**arg, field)) { // field not match + return 0; + } + if (++(*arg) == *arg_limit) { // without next argument + log_fatal("`%s` require a parameter", field); + } + if (*target != NULL) { + free(*target); // override target field + } + *target = new_string(**arg); + return 1; +} -void load_info(int argc, char **argv) { // load info from input parameters - bootstrap_info *info = (bootstrap_info*)malloc(sizeof(bootstrap_info)); +boot_info* load_info(int argc, char **argv) { // load info from input parameters + boot_info *info = (boot_info*)malloc(sizeof(boot_info)); log_debug("Start to load input arguments"); // TODO: output input args init_info(info); char **arg_limit = argv + argc; for (char **arg = argv + 1; arg < arg_limit; ++arg) { -// log_debug("Get argument -> `%s`", *arg); if (!strcmp(*arg, "--debug")) { // skip debug flag continue; } else if (!strcmp(*arg, "--fast-open")) { // --fast-open => fastopen @@ -245,16 +238,16 @@ void load_info(int argc, char **argv) { // load info from input parameters json_decode(json_content, info); // decode json content free(json_content); } else if ( - !load_field("-s", &info->server_addr, &arg, &arg_limit) && - !load_field("-p", &info->server_port, &arg, &arg_limit) && - !load_field("-b", &info->client_addr, &arg, &arg_limit) && - !load_field("-l", &info->client_port, &arg, &arg_limit) && - !load_field("-k", &info->password, &arg, &arg_limit) && - !load_field("-m", &info->method, &arg, &arg_limit) && - !load_field("-t", &info->timeout, &arg, &arg_limit) && - !load_field("--plugin", &info->plugin, &arg, &arg_limit) && - !load_field("--plugin-opts", &info->plugin_opts, &arg, &arg_limit) && - !load_field("--shadowsocks", &info->shadowsocks, &arg, &arg_limit) + !add_field("-s", &info->server_addr, &arg, &arg_limit) && + !add_field("-p", &info->server_port, &arg, &arg_limit) && + !add_field("-b", &info->client_addr, &arg, &arg_limit) && + !add_field("-l", &info->client_port, &arg, &arg_limit) && + !add_field("-k", &info->password, &arg, &arg_limit) && + !add_field("-m", &info->method, &arg, &arg_limit) && + !add_field("-t", &info->timeout, &arg, &arg_limit) && + !add_field("--plugin", &info->plugin, &arg, &arg_limit) && + !add_field("--plugin-opts", &info->plugin_opts, &arg, &arg_limit) && + !add_field("--shadowsocks", &info->shadowsocks, &arg, &arg_limit) ) { // archive unknown options log_info("Extra field -> %s", *arg); info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, *arg); @@ -264,4 +257,5 @@ void load_info(int argc, char **argv) { // load info from input parameters info->server_addr = "127.0.0.1"; } dump_info(info); + return info; } diff --git a/src/local.c b/src/local.c index 2e06bc8..d0f5deb 100644 --- a/src/local.c +++ b/src/local.c @@ -1,20 +1,38 @@ -#include -#include -#include +#include "load.h" +#include "logger.h" #include "common.h" #include "process.h" -#include "help.h" -#include "logger.h" -#include "load.h" + +char *help_msg = "\n\ +ss-bootstrap-local\n\ +\n\ + A simple program to make the original shadowsocks support SIP003 plugins.\n\ +\n\ + -s Host name or IP address of your remote server.\n\ + -p Port number of your remote server.\n\ + -b Local address to bind.\n\ + -l Port number of your local server.\n\ +\n\ + -c Path to JSON config file.\n\ + -k Password of your remote server.\n\ + -m Encrypt method.\n\ + -t Socket timeout in seconds.\n\ + --fast-open Enable TCP fast open (with Linux kernel 3.7+).\n\ + --plugin Enable SIP003 plugin.\n\ + --plugin-opts Set SIP003 plugin options.\n\ + --shadowsocks Set shadowsocks local program.\n\ + --no-udp Do not use UDP proxy.\n\ + -h, --help Print this message.\n\ +\n\ +"; int main(int argc, char *argv[]) { - is_show_help(argc, argv, local_help_msg); + init(argc, argv, help_msg); log_info("Shadowsocks bootstrap local (%s)", VERSION); + boot_info *info = load_info(argc, argv); - // set log level -// bootstrap_info info; - load_info(argc, argv); // params_load("sslocal"); // default file name // start_bootstrap("sslocal", is_udp_proxy); // local or server mode + return 0; } diff --git a/src/server.c b/src/server.c index d130472..b56217e 100644 --- a/src/server.c +++ b/src/server.c @@ -1,17 +1,36 @@ -#include -#include -#include +#include "load.h" +#include "logger.h" #include "common.h" #include "process.h" -#include "help.h" -#include "logger.h" + +char *help_msg = "\n\ +ss-bootstrap-server\n\ +\n\ + A simple program to make the original shadowsocks support SIP003 plugins.\n\ +\n\ + -s Host name or IP address of your remote server.\n\ + -p Port number of your remote server.\n\ + -b Local address to bind.\n\ + -l Port number of your local server.\n\ +\n\ + -c Path to JSON config file.\n\ + -k Password of your remote server.\n\ + -m Encrypt method.\n\ + -t Socket timeout in seconds.\n\ + --fast-open Enable TCP fast open (with Linux kernel 3.7+).\n\ + --plugin Enable SIP003 plugin.\n\ + --plugin-opts Set SIP003 plugin options.\n\ + --shadowsocks Set shadowsocks server program.\n\ + --no-udp Do not use UDP proxy.\n\ + -h, --help Print this message.\n\ +\n\ +"; int main(int argc, char *argv[]) { - is_show_help(argc, argv, server_help_msg); + init(argc, argv, help_msg); log_info("Shadowsocks bootstrap server (%s)", VERSION); + boot_info *info = load_info(argc, argv); -// bootstrap_info info; -// args_decode(argc, argv, &info); // params_load("ssserver"); // default file name // start_bootstrap("ssserver", is_udp_proxy); // local or server mode return 0; diff --git a/src/sip003.c b/src/sip003.c new file mode 100644 index 0000000..0c74c5d --- /dev/null +++ b/src/sip003.c @@ -0,0 +1,60 @@ +#include "sip003.h" +#include "load.h" + +void params_load(char *ss_default, boot_info *info) { // load shadowsocks and plugin params + if (info->shadowsocks == NULL) { + info->shadowsocks = ss_default; + } + info->shadowsocks_opts[0] = info->shadowsocks; // fill with file name + if (info->plugin != NULL) { // with plugin + char *rand_port = int_to_string(get_available_port(RANDOM_PORT_START, RANDOM_PORT_END)); + SS_REMOTE_HOST = info->server_addr; + SS_REMOTE_PORT = info->server_port; + SS_LOCAL_HOST = "127.0.0.1"; + SS_LOCAL_PORT = rand_port; + info->server_addr = SS_LOCAL_HOST; + info->server_port = SS_LOCAL_PORT; + SS_PLUGIN_OPTIONS = info->plugin_opts; + } + pack_shadowsocks_params(info); + shadowsocks_args = info->shadowsocks_opts; + if (info->plugin == NULL) { + plugin_file = NULL; + } else { + plugin_file = info->plugin; + } +} + +void pack_shadowsocks_params(boot_info *info) { // packaging shadowsocks parameters + if (info->server_addr != NULL) { + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "-s"); + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->server_addr); + } + if (info->client_addr != NULL) { + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "-b"); + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->client_addr); + } + if (info->server_port != NULL) { + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "-p"); + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->server_port); + } + if (info->client_port != NULL) { + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "-l"); + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->client_port); + } + if (info->password != NULL) { + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "-k"); + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->password); + } + if (info->method != NULL) { + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "-m"); + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->method); + } + if (info->timeout != NULL) { + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "-t"); + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->timeout); + } + if (info->fastopen) { + info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "--fast-open"); + } +} \ No newline at end of file diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt new file mode 100644 index 0000000..1a5b281 --- /dev/null +++ b/src/utils/CMakeLists.txt @@ -0,0 +1,2 @@ +aux_source_directory(. utils_src) +add_library(utils ${utils_src}) diff --git a/src/cJSON.c b/src/utils/cJSON.c similarity index 100% rename from src/cJSON.c rename to src/utils/cJSON.c diff --git a/src/dns.c b/src/utils/dns.c similarity index 100% rename from src/dns.c rename to src/utils/dns.c diff --git a/src/logger.c b/src/utils/logger.c similarity index 90% rename from src/logger.c rename to src/utils/logger.c index 9c9f6dc..57fcd81 100644 --- a/src/logger.c +++ b/src/utils/logger.c @@ -1,7 +1,7 @@ #include #include "logger.h" -int log_level = LOG_DEBUG; // default log level +int LOG_LEVEL = LOG_INFO; // default log level static const char *log_string[] = { "[DEBUG]", @@ -20,7 +20,7 @@ static const char *log_color[] = { }; void log_printf(int level, const char *fmt, ...) { - if (level < log_level) { // skip low log level + if (level < LOG_LEVEL) { // skip low log level return; } time_t t = time(NULL); diff --git a/src/network.c b/src/utils/network.c similarity index 100% rename from src/network.c rename to src/utils/network.c diff --git a/src/process.c b/src/utils/process.c similarity index 98% rename from src/process.c rename to src/utils/process.c index d78b6a0..55f60bb 100644 --- a/src/process.c +++ b/src/utils/process.c @@ -1,7 +1,3 @@ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif -//#include #include #include #include @@ -237,6 +233,5 @@ void start_bootstrap(char *ss_type, int is_udp_proxy) { // start shadowsocks and printf("!!! get signal !!!\n"); } -// g_main_loop_run(main_loop); // into main loop for wait exit_with_child(); }