Browse Source

update: sip003 arguments

master
dnomd343 2 years ago
parent
commit
c305225734
  1. 5
      include/common.h
  2. 4
      include/load.h
  3. 15
      include/sip003.h
  4. 4
      src/CMakeLists.txt
  5. 10
      src/common.c
  6. 39
      src/load.c
  7. 6
      src/local.c
  8. 5
      src/server.c
  9. 98
      src/sip003.c
  10. 19
      src/utils/process.c

5
include/common.h

@ -6,11 +6,12 @@
#define RANDOM_PORT_START 41952 #define RANDOM_PORT_START 41952
#define RANDOM_PORT_END 65535 #define RANDOM_PORT_END 65535
void init(int argc, char **argv, char *help_msg);
char* new_string(char *str); char* new_string(char *str);
char* int_to_string(int num); char* int_to_string(int num);
char* read_file(char *file_name); char* read_file(char *file_name);
char* string_list_join(char **string_list);
char** string_list_append(char **string_list, char *data); char** string_list_append(char **string_list, char *data);
void init(int argc, char **argv, char *help_msg);
#endif #endif

4
include/load.h

@ -13,8 +13,8 @@ typedef struct {
char *plugin_opts; char *plugin_opts;
char *shadowsocks; char *shadowsocks;
char **shadowsocks_opts; char **shadowsocks_opts;
} boot_info; } bootstrap;
boot_info* load_info(int argc, char **argv); bootstrap* load_info(int argc, char **argv);
#endif #endif

15
include/sip003.h

@ -1,7 +1,18 @@
#ifndef _SIP003_H_ #ifndef _SIP003_H_
#define _SIP003_H_ #define _SIP003_H_
//void params_load(char *ss_default, bootstrap_info *info); #include "load.h"
//void args_decode(int argc, char **argv, bootstrap_info *info);
typedef struct {
char *SS_REMOTE_HOST;
char *SS_REMOTE_PORT;
char *SS_LOCAL_HOST;
char *SS_LOCAL_PORT;
char *SS_PLUGIN_OPTIONS;
char *plugin_file;
char **shadowsocks_cmd;
} sip003;
sip003* load_sip003(char *ss_default, bootstrap *info);
#endif #endif

4
src/CMakeLists.txt

@ -5,8 +5,8 @@ include_directories(${PROJECT_SOURCE_DIR}/include/utils)
add_subdirectory(utils) add_subdirectory(utils)
add_executable(ss-bootstrap-local local.c common.c load.c) add_executable(ss-bootstrap-local local.c common.c load.c sip003.c)
target_link_libraries(ss-bootstrap-local utils pthread) target_link_libraries(ss-bootstrap-local utils pthread)
add_executable(ss-bootstrap-server local.c common.c load.c) add_executable(ss-bootstrap-server local.c common.c load.c sip003.c)
target_link_libraries(ss-bootstrap-server utils pthread) target_link_libraries(ss-bootstrap-server utils pthread)

10
src/common.c

@ -31,6 +31,16 @@ char** string_list_append(char **string_list, char *data) {
return string_list; return string_list;
} }
char* string_list_join(char **string_list) { // combine string list -> `str_1` `str_2` `str_3` ...
char *join_str = (char*)malloc(0);
for (char **str = string_list; *str != NULL; ++str) {
join_str = (char*)realloc(join_str, strlen(join_str) + strlen(*str) + 4);
join_str = strcat(strcat(join_str, "`"), *str);
join_str = strcat(join_str, "` ");
}
return join_str;
}
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");

39
src/load.c

@ -5,13 +5,13 @@
#include "cJSON.h" #include "cJSON.h"
#include "load.h" #include "load.h"
void init_info(boot_info *info); void init_info(bootstrap *info);
void dump_info(boot_info *info); void dump_info(bootstrap *info);
char** add_extra_opts(char **opts, char *extra_opts_str); char** add_extra_opts(char **opts, char *extra_opts_str);
void json_decode(char *json_content, boot_info *info); void json_decode(char *json_content, bootstrap *info);
int add_field(char *field, char **target, char ***arg, char ***arg_limit); int add_field(char *field, char **target, char ***arg, char ***arg_limit);
void init_info(boot_info *info) { void init_info(bootstrap *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;
@ -24,11 +24,10 @@ void init_info(boot_info *info) {
info->shadowsocks = NULL; info->shadowsocks = NULL;
info->shadowsocks_opts = (char**)malloc(sizeof(char*) * 2); // two arguments info->shadowsocks_opts = (char**)malloc(sizeof(char*) * 2); // two arguments
info->shadowsocks_opts[0] = ""; // reserved for program name info->shadowsocks_opts[0] = ""; // reserved for program name
// TODO: add reserved field after load
info->shadowsocks_opts[1] = NULL; info->shadowsocks_opts[1] = NULL;
} }
void dump_info(boot_info *info) { void dump_info(bootstrap *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 {
@ -49,12 +48,10 @@ void dump_info(boot_info *info) {
log_debug("plugin = %s", info->plugin); log_debug("plugin = %s", info->plugin);
log_debug("plugin_opts = %s", info->plugin_opts); log_debug("plugin_opts = %s", info->plugin_opts);
log_debug("shadowsocks = %s", info->shadowsocks); log_debug("shadowsocks = %s", info->shadowsocks);
log_debug("shadowsocks_opts:"); // TODO: combine as one line output
char **option = info->shadowsocks_opts; char *opts_str = string_list_join(info->shadowsocks_opts + 1);
while(*option != NULL) { log_debug("shadowsocks_opts: %s", opts_str); // combine output
printf(" '%s'\n", *option); free(opts_str);
++option;
}
} }
char** add_extra_opts(char **opts, char *extra_opts_str) { // split shadowsocks extra options char** add_extra_opts(char **opts, char *extra_opts_str) { // split shadowsocks extra options
@ -80,7 +77,7 @@ char** add_extra_opts(char **opts, char *extra_opts_str) { // split shadowsocks
return opts; return opts;
} }
void json_decode(char *json_content, boot_info *info) { // decode JSON content void json_decode(char *json_content, bootstrap *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");
@ -217,12 +214,19 @@ int add_field(char *field, char **target, char ***arg, char ***arg_limit) {
return 1; return 1;
} }
boot_info* load_info(int argc, char **argv) { // load info from input parameters bootstrap* load_info(int argc, char **argv) { // load info from input parameters
boot_info *info = (boot_info*)malloc(sizeof(boot_info)); bootstrap *info = (bootstrap*)malloc(sizeof(bootstrap));
char **arg_limit = argv + argc;
log_debug("Start to load input arguments"); log_debug("Start to load input arguments");
// TODO: output input args char *arg_str = (char*)malloc(0);
for (char **arg = argv + 1; arg < arg_limit; ++arg) {
arg_str = (char*)realloc(arg_str, strlen(arg_str) + strlen(*arg) + 4);
arg_str = strcat(strcat(strcat(arg_str, "`"), *arg), "` ");
}
log_debug("Input arguments -> %s", arg_str);
init_info(info); init_info(info);
char **arg_limit = argv + argc;
for (char **arg = argv + 1; arg < arg_limit; ++arg) { for (char **arg = argv + 1; arg < arg_limit; ++arg) {
if (!strcmp(*arg, "--debug")) { // skip debug flag if (!strcmp(*arg, "--debug")) { // skip debug flag
continue; continue;
@ -249,7 +253,6 @@ boot_info* load_info(int argc, char **argv) { // load info from input parameters
!add_field("--plugin-opts", &info->plugin_opts, &arg, &arg_limit) && !add_field("--plugin-opts", &info->plugin_opts, &arg, &arg_limit) &&
!add_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);
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, *arg); info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, *arg);
} }
} }

6
src/local.c

@ -1,4 +1,5 @@
#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"
@ -22,6 +23,7 @@ ss-bootstrap-local\n\
--plugin-opts <options> Set SIP003 plugin options.\n\ --plugin-opts <options> Set SIP003 plugin options.\n\
--shadowsocks <sslocal> Set shadowsocks local program.\n\ --shadowsocks <sslocal> Set shadowsocks local program.\n\
--no-udp Do not use UDP proxy.\n\ --no-udp Do not use UDP proxy.\n\
--debug Enable debug mode.\n\
-h, --help Print this message.\n\ -h, --help Print this message.\n\
\n\ \n\
"; ";
@ -29,9 +31,9 @@ ss-bootstrap-local\n\
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
init(argc, argv, 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); bootstrap *info = load_info(argc, argv);
load_sip003("sslocal", info);
// 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;

5
src/server.c

@ -22,6 +22,7 @@ ss-bootstrap-server\n\
--plugin-opts <options> Set SIP003 plugin options.\n\ --plugin-opts <options> Set SIP003 plugin options.\n\
--shadowsocks <ssservre> Set shadowsocks server program.\n\ --shadowsocks <ssservre> Set shadowsocks server program.\n\
--no-udp Do not use UDP proxy.\n\ --no-udp Do not use UDP proxy.\n\
--debug Enable debug mode.\n\
-h, --help Print this message.\n\ -h, --help Print this message.\n\
\n\ \n\
"; ";
@ -29,9 +30,9 @@ ss-bootstrap-server\n\
int main(int argc, char *argv[]) { 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);
boot_info *info = load_info(argc, argv); bootstrap *info = load_info(argc, argv);
load_sip003("ssserver", info);
// 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;
} }

98
src/sip003.c

@ -1,60 +1,66 @@
#include <stdlib.h>
#include "sip003.h" #include "sip003.h"
#include "load.h" #include "logger.h"
#include "common.h"
#include "network.h"
void params_load(char *ss_default, boot_info *info) { // load shadowsocks and plugin params void dump_sip003(sip003 *service);
if (info->shadowsocks == NULL) { void add_sip003_arg(bootstrap *info, char *key, char *value);
info->shadowsocks = ss_default;
} void dump_sip003(sip003 *service) { // show shadowsocks and plugin params
info->shadowsocks_opts[0] = info->shadowsocks; // fill with file name char *cmd_str = string_list_join(service->shadowsocks_cmd);
if (info->plugin != NULL) { // with plugin log_info("Shadowsocks -> %s", cmd_str);
char *rand_port = int_to_string(get_available_port(RANDOM_PORT_START, RANDOM_PORT_END)); free(cmd_str);
SS_REMOTE_HOST = info->server_addr; if (service->plugin_file == NULL) {
SS_REMOTE_PORT = info->server_port; log_info("Plugin no need");
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 { } else {
plugin_file = info->plugin; log_info("Plugin -> `%s`", service->plugin_file);
log_info("SS_REMOTE_HOST -> `%s`", service->SS_REMOTE_HOST);
log_info("SS_REMOTE_PORT -> `%s`", service->SS_REMOTE_PORT);
log_info("SS_LOCAL_HOST -> `%s`", service->SS_LOCAL_HOST);
log_info("SS_LOCAL_PORT -> `%s`", service->SS_LOCAL_PORT);
log_info("SS_PLUGIN_OPTIONS -> `%s`", service->SS_PLUGIN_OPTIONS);
} }
} }
void pack_shadowsocks_params(boot_info *info) { // packaging shadowsocks parameters void add_sip003_arg(bootstrap *info, char *key, char *value) {
if (info->server_addr != NULL) { if (value != NULL) {
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "-s"); info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, key);
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->server_addr); info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, value);
}
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"); sip003* load_sip003(char *ss_default, bootstrap *info) { // load shadowsocks and plugin params
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->password); sip003 *service = (sip003*)malloc(sizeof(sip003));
} if (info->shadowsocks == NULL) {
if (info->method != NULL) { info->shadowsocks = ss_default; // load default shadowsocks name
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[0] = info->shadowsocks; // fill with file name
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "-t");
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, info->timeout); service->plugin_file = NULL;
if (info->plugin != NULL) { // with sip003 plugin
char *rand_port = int_to_string(get_available_port(RANDOM_PORT_START, RANDOM_PORT_END));
service->SS_REMOTE_HOST = info->server_addr;
service->SS_REMOTE_PORT = info->server_port;
service->SS_LOCAL_HOST = "127.0.0.1";
service->SS_LOCAL_PORT = rand_port;
service->SS_PLUGIN_OPTIONS = info->plugin_opts;
info->server_addr = service->SS_LOCAL_HOST;
info->server_port = service->SS_LOCAL_PORT;
service->plugin_file = info->plugin;
} }
add_sip003_arg(info, "-s", info->server_addr);
add_sip003_arg(info, "-b", info->client_addr);
add_sip003_arg(info, "-p", info->server_port);
add_sip003_arg(info, "-l", info->client_port);
add_sip003_arg(info, "-k", info->password);
add_sip003_arg(info, "-m", info->method);
add_sip003_arg(info, "-t", info->timeout);
if (info->fastopen) { if (info->fastopen) {
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "--fast-open"); info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, "--fast-open");
} }
service->shadowsocks_cmd = info->shadowsocks_opts;
dump_sip003(service);
return service;
} }

19
src/utils/process.c

@ -176,24 +176,7 @@ void exit_with_child() { // exit and kill his child process
exit(1); exit(1);
} }
void show_params() { // show shadowsocks and plugin params
int num = 0;
printf("[Shadowsocks Bootstrap]");
while(shadowsocks_args[num] != NULL) {
printf(" %s", shadowsocks_args[num++]);
}
printf("\n");
if (plugin_file == NULL) {
printf("[Shadowsocks Bootstrap] Plugin no need.\n");
return;
}
printf("[Shadowsocks Bootstrap] Plugin -> %s\n", plugin_file);
printf("[Shadowsocks Bootstrap] SS_REMOTE_HOST -> %s\n", SS_REMOTE_HOST);
printf("[Shadowsocks Bootstrap] SS_REMOTE_PORT -> %s\n", SS_REMOTE_PORT);
printf("[Shadowsocks Bootstrap] SS_LOCAL_HOST -> %s\n", SS_LOCAL_HOST);
printf("[Shadowsocks Bootstrap] SS_LOCAL_PORT -> %s\n", SS_LOCAL_PORT);
printf("[Shadowsocks Bootstrap] SS_PLUGIN_OPTIONS -> %s\n", SS_PLUGIN_OPTIONS);
}
void start_bootstrap(char *ss_type, int is_udp_proxy) { // start shadowsocks and plugin (optional) void start_bootstrap(char *ss_type, int is_udp_proxy) { // start shadowsocks and plugin (optional)
show_params(); show_params();

Loading…
Cancel
Save