Browse Source

update: shadowsocks bootstrap framework

master
dnomd343 2 years ago
parent
commit
26b45b9b21
  1. 23
      include/common.h
  2. 9
      include/help.h
  3. 16
      include/load.h
  4. 7
      include/sip003.h
  5. 0
      include/utils/cJSON.h
  6. 0
      include/utils/dns.h
  7. 2
      include/utils/logger.h
  8. 0
      include/utils/network.h
  9. 0
      include/utils/process.h
  10. 19
      src/CMakeLists.txt
  11. 173
      src/common.c
  12. 66
      src/help.c
  13. 82
      src/load.c
  14. 38
      src/local.c
  15. 35
      src/server.c
  16. 60
      src/sip003.c
  17. 2
      src/utils/CMakeLists.txt
  18. 0
      src/utils/cJSON.c
  19. 0
      src/utils/dns.c
  20. 4
      src/utils/logger.c
  21. 0
      src/utils/network.c
  22. 5
      src/utils/process.c

23
include/common.h

@ -1,29 +1,16 @@
#ifndef _COMMON_H_ #ifndef _COMMON_H_
#define _COMMON_H_ #define _COMMON_H_
#define VERSION "0.9.1" #define VERSION "0.9.2"
#define RANDOM_PORT_START 41952 #define RANDOM_PORT_START 41952
#define RANDOM_PORT_END 65535 #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* new_string(char *str);
char* int_to_string(int num);
char* read_file(char *file_name); char* read_file(char *file_name);
void params_load(char *ss_default, bootstrap_info *info); char** string_list_append(char **string_list, char *data);
void args_decode(int argc, char **argv, bootstrap_info *info);
void init(int argc, char **argv, char *help_msg);
#endif #endif

9
include/help.h

@ -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

16
include/load.h

@ -1,6 +1,20 @@
#ifndef _LOAD_H_ #ifndef _LOAD_H_
#define _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 #endif

7
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

0
include/cJSON.h → include/utils/cJSON.h

0
include/dns.h → include/utils/dns.h

2
include/logger.h → include/utils/logger.h

@ -19,7 +19,7 @@ enum {
#define log_error(...) log_printf(LOG_ERROR, __VA_ARGS__) #define log_error(...) log_printf(LOG_ERROR, __VA_ARGS__)
#define log_fatal(...) log_printf(LOG_FATAL, __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, ...); void log_printf(int level, const char *fmt, ...);
#endif #endif

0
include/network.h → include/utils/network.h

0
include/process.h → include/utils/process.h

19
src/CMakeLists.txt

@ -1,19 +1,12 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 2.8.12)
include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(/usr/lib/i386-linux-gnu/glib-2.0/include) include_directories(${PROJECT_SOURCE_DIR}/include/utils)
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)
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC) add_subdirectory(utils)
list(REMOVE_ITEM SRC ${PROJECT_SOURCE_DIR}/src/local.c)
list(REMOVE_ITEM SRC ${PROJECT_SOURCE_DIR}/src/server.c)
add_executable(ss-bootstrap-local local.c ${SRC}) add_executable(ss-bootstrap-local local.c common.c load.c)
target_link_libraries(ss-bootstrap-local glib-2.0 pthread) target_link_libraries(ss-bootstrap-local utils pthread)
add_executable(ss-bootstrap-server server.c ${SRC}) add_executable(ss-bootstrap-server local.c common.c load.c)
target_link_libraries(ss-bootstrap-server glib-2.0 pthread) target_link_libraries(ss-bootstrap-server utils pthread)

173
src/common.c

@ -1,39 +1,11 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "cJSON.h"
#include "common.h"
#include "network.h"
#include "process.h"
#include "logger.h" #include "logger.h"
#include "common.h"
//int is_udp_proxy = 1; char* new_string(char *str) {
//char *server_addr = NULL, *client_addr = NULL; return strcpy((char*)malloc(strlen(str) + 1), str);
//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* int_to_string(int num) { // int -> string char* int_to_string(int num) { // int -> string
if (num < 0) { if (num < 0) {
@ -50,46 +22,15 @@ char* int_to_string(int num) { // int -> string
return str; return str;
} }
void params_load(char *ss_default, bootstrap_info *info) { // load shadowsocks and plugin params char** string_list_append(char **string_list, char *data) {
if (info->shadowsocks == NULL) { int num = 0;
info->shadowsocks = ss_default; while(string_list[num++] != NULL); // get string list size
} string_list = (char**)realloc(string_list, sizeof(char**) * (num + 1));
info->shadowsocks_opts[0] = info->shadowsocks; // fill with file name string_list[num - 1] = new_string(data);
if (info->plugin != NULL) { // with plugin string_list[num] = NULL; // list end sign
char *rand_port = int_to_string(get_available_port(RANDOM_PORT_START, RANDOM_PORT_END)); return string_list;
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 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 char* read_file(char *file_name) { // read file content
log_debug("Start read file -> %s", file_name); log_debug("Start read file -> %s", file_name);
FILE *pfile = fopen(file_name, "rb"); FILE *pfile = fopen(file_name, "rb");
@ -110,86 +51,18 @@ char* read_file(char *file_name) { // read file content
return file_content; return file_content;
} }
void init(int argc, char **argv, char *help_msg) {
if (argc <= 1) { // with only one argument
void add_shadowsocks_option(char *option, char **opts) { // add shadowsocks options printf("%s", help_msg);
int opt_num = 0; exit(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");
} }
printf("server_addr = %s\n", info->server_addr); for (int i = 0; i < argc; ++i) {
printf("client_addr = %s\n", info->client_addr); if (!strcmp(argv[i], "--debug")) { // include `--debug`
printf("server_port = %s\n", info->server_port); LOG_LEVEL = LOG_DEBUG;
printf("client_port = %s\n", info->client_port); }
printf("password = %s\n", info->password); if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { // include `-h` or `--help`
printf("method = %s\n", info->method); printf("%s", help_msg);
printf("timeout = %s\n", info->timeout); exit(0);
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++;
} }
} }
//void args_dump() { // show parameter's content
//
//}

66
src/help.c

@ -1,66 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 <server_host> Host name or IP address of your remote server.\n\
-p <server_port> Port number of your remote server.\n\
-b <local_address> Local address to bind.\n\
-l <local_port> Port number of your local server.\n\
\n\
-c <config_file> Path to JSON config file.\n\
-k <password> Password of your remote server.\n\
-m <method> Encrypt method.\n\
-t <timeout> Socket timeout in seconds.\n\
--fast-open Enable TCP fast open (with Linux kernel 3.7+).\n\
--plugin <name> Enable SIP003 plugin.\n\
--plugin-opts <options> Set SIP003 plugin options.\n\
--shadowsocks <ssservre> 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 <server_host> Host name or IP address of your remote server.\n\
-p <server_port> Port number of your remote server.\n\
-b <local_address> Local address to bind.\n\
-l <local_port> Port number of your local server.\n\
\n\
-c <config_file> Path to JSON config file.\n\
-k <password> Password of your remote server.\n\
-m <method> Encrypt method.\n\
-t <timeout> Socket timeout in seconds.\n\
--fast-open Enable TCP fast open (with Linux kernel 3.7+).\n\
--plugin <name> Enable SIP003 plugin.\n\
--plugin-opts <options> Set SIP003 plugin options.\n\
--shadowsocks <sslocal> 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);
}
}
}

82
src/load.c

@ -1,11 +1,17 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "load.h"
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#include "cJSON.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->is_udp_proxy = 1; // enabled udp proxy
info->server_addr = info->client_addr = NULL; info->server_addr = info->client_addr = NULL;
info->server_port = info->client_port = NULL; info->server_port = info->client_port = NULL;
@ -22,7 +28,7 @@ void init_info(bootstrap_info *info) {
info->shadowsocks_opts[1] = NULL; info->shadowsocks_opts[1] = NULL;
} }
void dump_info(bootstrap_info *info) { void dump_info(boot_info *info) {
if (info->is_udp_proxy) { if (info->is_udp_proxy) {
log_debug("is_udp_proxy = true"); log_debug("is_udp_proxy = true");
} else { } else {
@ -51,30 +57,7 @@ void dump_info(bootstrap_info *info) {
} }
} }
char** string_list_append(char **string_list, char *data) { char** add_extra_opts(char **opts, char *extra_opts_str) { // split shadowsocks extra options
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
log_debug("Split extra options -> `%s`", extra_opts_str); 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 char *tmp = (char*)calloc(strlen(extra_opts_str) + 1, 1); // new memory and set as 0x00
for (int i = 0, num = 0;; ++num) { 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; 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); cJSON* json = cJSON_Parse(json_content);
if (json == NULL) { if (json == NULL) {
log_fatal("JSON format error"); 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)) { if (!cJSON_IsString(json)) {
log_fatal("`extra_opts` must be string"); 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 } else { // unknown field => ERROR
log_fatal("Unknown JSON field `%s`", json->string); log_fatal("Unknown JSON field `%s`", json->string);
} }
json = json->next; // next field json = json->next; // next field
} }
cJSON_free(json); // free JSON struct 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 boot_info* load_info(int argc, char **argv) { // load info from input parameters
bootstrap_info *info = (bootstrap_info*)malloc(sizeof(bootstrap_info)); boot_info *info = (boot_info*)malloc(sizeof(boot_info));
log_debug("Start to load input arguments"); log_debug("Start to load input arguments");
// TODO: output input args // TODO: output input args
init_info(info); init_info(info);
char **arg_limit = argv + argc; char **arg_limit = argv + argc;
for (char **arg = argv + 1; arg < arg_limit; ++arg) { for (char **arg = argv + 1; arg < arg_limit; ++arg) {
// log_debug("Get argument -> `%s`", *arg);
if (!strcmp(*arg, "--debug")) { // skip debug flag if (!strcmp(*arg, "--debug")) { // skip debug flag
continue; continue;
} else if (!strcmp(*arg, "--fast-open")) { // --fast-open => fastopen } 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 json_decode(json_content, info); // decode json content
free(json_content); free(json_content);
} else if ( } else if (
!load_field("-s", &info->server_addr, &arg, &arg_limit) && !add_field("-s", &info->server_addr, &arg, &arg_limit) &&
!load_field("-p", &info->server_port, &arg, &arg_limit) && !add_field("-p", &info->server_port, &arg, &arg_limit) &&
!load_field("-b", &info->client_addr, &arg, &arg_limit) && !add_field("-b", &info->client_addr, &arg, &arg_limit) &&
!load_field("-l", &info->client_port, &arg, &arg_limit) && !add_field("-l", &info->client_port, &arg, &arg_limit) &&
!load_field("-k", &info->password, &arg, &arg_limit) && !add_field("-k", &info->password, &arg, &arg_limit) &&
!load_field("-m", &info->method, &arg, &arg_limit) && !add_field("-m", &info->method, &arg, &arg_limit) &&
!load_field("-t", &info->timeout, &arg, &arg_limit) && !add_field("-t", &info->timeout, &arg, &arg_limit) &&
!load_field("--plugin", &info->plugin, &arg, &arg_limit) && !add_field("--plugin", &info->plugin, &arg, &arg_limit) &&
!load_field("--plugin-opts", &info->plugin_opts, &arg, &arg_limit) && !add_field("--plugin-opts", &info->plugin_opts, &arg, &arg_limit) &&
!load_field("--shadowsocks", &info->shadowsocks, &arg, &arg_limit) !add_field("--shadowsocks", &info->shadowsocks, &arg, &arg_limit)
) { // archive unknown options ) { // archive unknown options
log_info("Extra field -> %s", *arg); log_info("Extra field -> %s", *arg);
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, *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"; info->server_addr = "127.0.0.1";
} }
dump_info(info); dump_info(info);
return info;
} }

38
src/local.c

@ -1,20 +1,38 @@
#include <stdio.h> #include "load.h"
#include <stdlib.h> #include "logger.h"
#include <string.h>
#include "common.h" #include "common.h"
#include "process.h" #include "process.h"
#include "help.h"
#include "logger.h" char *help_msg = "\n\
#include "load.h" ss-bootstrap-local\n\
\n\
A simple program to make the original shadowsocks support SIP003 plugins.\n\
\n\
-s <server_host> Host name or IP address of your remote server.\n\
-p <server_port> Port number of your remote server.\n\
-b <local_address> Local address to bind.\n\
-l <local_port> Port number of your local server.\n\
\n\
-c <config_file> Path to JSON config file.\n\
-k <password> Password of your remote server.\n\
-m <method> Encrypt method.\n\
-t <timeout> Socket timeout in seconds.\n\
--fast-open Enable TCP fast open (with Linux kernel 3.7+).\n\
--plugin <name> Enable SIP003 plugin.\n\
--plugin-opts <options> Set SIP003 plugin options.\n\
--shadowsocks <sslocal> 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[]) { 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); 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 // params_load("sslocal"); // default file name
// start_bootstrap("sslocal", is_udp_proxy); // local or server mode // start_bootstrap("sslocal", is_udp_proxy); // local or server mode
return 0; return 0;
} }

35
src/server.c

@ -1,17 +1,36 @@
#include <stdio.h> #include "load.h"
#include <stdlib.h> #include "logger.h"
#include <string.h>
#include "common.h" #include "common.h"
#include "process.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 <server_host> Host name or IP address of your remote server.\n\
-p <server_port> Port number of your remote server.\n\
-b <local_address> Local address to bind.\n\
-l <local_port> Port number of your local server.\n\
\n\
-c <config_file> Path to JSON config file.\n\
-k <password> Password of your remote server.\n\
-m <method> Encrypt method.\n\
-t <timeout> Socket timeout in seconds.\n\
--fast-open Enable TCP fast open (with Linux kernel 3.7+).\n\
--plugin <name> Enable SIP003 plugin.\n\
--plugin-opts <options> Set SIP003 plugin options.\n\
--shadowsocks <ssservre> 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[]) { 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); 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 // params_load("ssserver"); // default file name
// start_bootstrap("ssserver", is_udp_proxy); // local or server mode // start_bootstrap("ssserver", is_udp_proxy); // local or server mode
return 0; return 0;

60
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");
}
}

2
src/utils/CMakeLists.txt

@ -0,0 +1,2 @@
aux_source_directory(. utils_src)
add_library(utils ${utils_src})

0
src/cJSON.c → src/utils/cJSON.c

0
src/dns.c → src/utils/dns.c

4
src/logger.c → src/utils/logger.c

@ -1,7 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "logger.h" #include "logger.h"
int log_level = LOG_DEBUG; // default log level int LOG_LEVEL = LOG_INFO; // default log level
static const char *log_string[] = { static const char *log_string[] = {
"[DEBUG]", "[DEBUG]",
@ -20,7 +20,7 @@ static const char *log_color[] = {
}; };
void log_printf(int level, const char *fmt, ...) { 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; return;
} }
time_t t = time(NULL); time_t t = time(NULL);

0
src/network.c → src/utils/network.c

5
src/process.c → src/utils/process.c

@ -1,7 +1,3 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
//#include <glib.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -237,6 +233,5 @@ void start_bootstrap(char *ss_type, int is_udp_proxy) { // start shadowsocks and
printf("!!! get signal !!!\n"); printf("!!! get signal !!!\n");
} }
// g_main_loop_run(main_loop); // into main loop for wait
exit_with_child(); exit_with_child();
} }
Loading…
Cancel
Save