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_END 65535
void init(int argc, char **argv, char *help_msg);
char* new_string(char *str);
char* int_to_string(int num);
char* read_file(char *file_name);
char* string_list_join(char **string_list);
char** string_list_append(char **string_list, char *data);
void init(int argc, char **argv, char *help_msg);
#endif

4
include/load.h

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

15
include/sip003.h

@ -1,7 +1,18 @@
#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);
#include "load.h"
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

4
src/CMakeLists.txt

@ -5,8 +5,8 @@ include_directories(${PROJECT_SOURCE_DIR}/include/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)
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)

10
src/common.c

@ -31,6 +31,16 @@ char** string_list_append(char **string_list, char *data) {
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
log_debug("Start read file -> %s", file_name);
FILE *pfile = fopen(file_name, "rb");

39
src/load.c

@ -5,13 +5,13 @@
#include "cJSON.h"
#include "load.h"
void init_info(boot_info *info);
void dump_info(boot_info *info);
void init_info(bootstrap *info);
void dump_info(bootstrap *info);
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);
void init_info(boot_info *info) {
void init_info(bootstrap *info) {
info->is_udp_proxy = 1; // enabled udp proxy
info->server_addr = info->client_addr = NULL;
info->server_port = info->client_port = NULL;
@ -24,11 +24,10 @@ void init_info(boot_info *info) {
info->shadowsocks = NULL;
info->shadowsocks_opts = (char**)malloc(sizeof(char*) * 2); // two arguments
info->shadowsocks_opts[0] = ""; // reserved for program name
// TODO: add reserved field after load
info->shadowsocks_opts[1] = NULL;
}
void dump_info(boot_info *info) {
void dump_info(bootstrap *info) {
if (info->is_udp_proxy) {
log_debug("is_udp_proxy = true");
} else {
@ -49,12 +48,10 @@ void dump_info(boot_info *info) {
log_debug("plugin = %s", info->plugin);
log_debug("plugin_opts = %s", info->plugin_opts);
log_debug("shadowsocks = %s", info->shadowsocks);
log_debug("shadowsocks_opts:"); // TODO: combine as one line output
char **option = info->shadowsocks_opts;
while(*option != NULL) {
printf(" '%s'\n", *option);
++option;
}
char *opts_str = string_list_join(info->shadowsocks_opts + 1);
log_debug("shadowsocks_opts: %s", opts_str); // combine output
free(opts_str);
}
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;
}
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);
if (json == NULL) {
log_fatal("JSON format error");
@ -217,12 +214,19 @@ int add_field(char *field, char **target, char ***arg, char ***arg_limit) {
return 1;
}
boot_info* load_info(int argc, char **argv) { // load info from input parameters
boot_info *info = (boot_info*)malloc(sizeof(boot_info));
bootstrap* load_info(int argc, char **argv) { // load info from input parameters
bootstrap *info = (bootstrap*)malloc(sizeof(bootstrap));
char **arg_limit = argv + argc;
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);
char **arg_limit = argv + argc;
for (char **arg = argv + 1; arg < arg_limit; ++arg) {
if (!strcmp(*arg, "--debug")) { // skip debug flag
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("--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);
}
}

6
src/local.c

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

5
src/server.c

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

98
src/sip003.c

@ -1,60 +1,66 @@
#include <stdlib.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
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;
void dump_sip003(sip003 *service);
void add_sip003_arg(bootstrap *info, char *key, char *value);
void dump_sip003(sip003 *service) { // show shadowsocks and plugin params
char *cmd_str = string_list_join(service->shadowsocks_cmd);
log_info("Shadowsocks -> %s", cmd_str);
free(cmd_str);
if (service->plugin_file == NULL) {
log_info("Plugin no need");
} 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
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);
void add_sip003_arg(bootstrap *info, char *key, char *value) {
if (value != NULL) {
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, key);
info->shadowsocks_opts = string_list_append(info->shadowsocks_opts, value);
}
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);
sip003* load_sip003(char *ss_default, bootstrap *info) { // load shadowsocks and plugin params
sip003 *service = (sip003*)malloc(sizeof(sip003));
if (info->shadowsocks == NULL) {
info->shadowsocks = ss_default; // load default shadowsocks name
}
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);
info->shadowsocks_opts[0] = info->shadowsocks; // fill with file name
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) {
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);
}
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)
show_params();

Loading…
Cancel
Save