Browse Source

feat: add udp proxy switch

master
dnomd343 3 years ago
parent
commit
d5b2b44d7a
  1. 1
      include/common.h
  2. 2
      include/process.h
  3. 20
      src/common.c
  4. 15
      src/local.c
  5. 11
      src/process.c
  6. 17
      src/server.c

1
include/common.h

@ -4,6 +4,7 @@
#define RANDOM_PORT_START 41952 #define RANDOM_PORT_START 41952
#define RANDOM_PORT_END 65535 #define RANDOM_PORT_END 65535
extern int is_udp_proxy;
extern char *server_addr, *client_addr; extern char *server_addr, *client_addr;
extern char *server_port, *client_port; extern char *server_port, *client_port;
extern char *password; extern char *password;

2
include/process.h

@ -9,6 +9,6 @@ extern char *SS_LOCAL_HOST;
extern char *SS_LOCAL_PORT; extern char *SS_LOCAL_PORT;
extern char *SS_PLUGIN_OPTIONS; extern char *SS_PLUGIN_OPTIONS;
void start_bootstrap(char *ss_type); void start_bootstrap(char *ss_type, int is_udp_proxy);
#endif #endif

20
src/common.c

@ -6,6 +6,7 @@
#include "network.h" #include "network.h"
#include "process.h" #include "process.h"
int is_udp_proxy;
char *server_addr, *client_addr; char *server_addr, *client_addr;
char *server_port, *client_port; char *server_port, *client_port;
char *password; char *password;
@ -73,6 +74,7 @@ void params_load(char *ss_default) { // load shadowsocks and plugin params
} }
void args_init() { // init arguments void args_init() { // init arguments
is_udp_proxy = 1; // udp proxy in default
server_addr = client_addr = NULL; server_addr = client_addr = NULL;
server_port = client_port = NULL; server_port = client_port = NULL;
password = NULL; password = NULL;
@ -183,7 +185,16 @@ void json_decode(char *json_content) { // decode JSON content
} }
json = json->child; json = json->child;
while (json != NULL) { while (json != NULL) {
if (!strcmp(json->string, "server")) { // server => server_addr if (!strcmp(json->string, "no_udp")) { // no_udp => without udp proxy
if (!cJSON_IsBool(json)) {
error_exit("`no_udp` must be a bool.\n");
}
if (json->valueint) { // is_udp_proxy = ~(json->valueint)
is_udp_proxy = 0;
} else {
is_udp_proxy = 1;
}
} else if (!strcmp(json->string, "server")) { // server => server_addr
if (!cJSON_IsString(json)) { if (!cJSON_IsString(json)) {
error_exit("`server` must be a string.\n"); error_exit("`server` must be a string.\n");
} }
@ -301,6 +312,8 @@ void args_decode(int argc, char **argv) { // decode the input parameters
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "--debug")) { // --debug => dump args if (!strcmp(argv[i], "--debug")) { // --debug => dump args
debug_flag = 1; debug_flag = 1;
} else if (!strcmp(argv[i], "--no-udp")) { // --no-udp => without udp proxy
is_udp_proxy = 0;
} else if (!strcmp(argv[i], "-c")) { // -c => CONFIG_JSON } 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");
@ -408,6 +421,11 @@ void args_decode(int argc, char **argv) { // decode the input parameters
} }
void args_dump() { // show parameter's content void args_dump() { // show parameter's content
if (is_udp_proxy) {
printf("is_udp_proxy = true\n");
} else {
printf("is_udp_proxy = false\n");
}
printf("server_addr = %s\n", server_addr); printf("server_addr = %s\n", server_addr);
printf("client_addr = %s\n", client_addr); printf("client_addr = %s\n", client_addr);
printf("server_port = %s\n", server_port); printf("server_port = %s\n", server_port);

15
src/local.c

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "common.h" #include "common.h"
#include "process.h" #include "process.h"
@ -25,20 +26,28 @@ ss-bootstrap-local\n\
--plugin <name> Enable SIP003 plugin.\n\ --plugin <name> Enable SIP003 plugin.\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\
-h, --help Print this message.\n\ -h, --help Print this message.\n\
\n\ \n\
"; ";
void show_help() { // show help message
printf("%s", help_msg);
exit(0); // exit normally
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int i; int i;
if (argc <= 1) {
show_help();
}
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 show_help();
return 0;
} }
} }
args_decode(argc, argv); args_decode(argc, argv);
params_load(SHADOWSOCKS_DEFAULT); // default file name params_load(SHADOWSOCKS_DEFAULT); // default file name
start_bootstrap(SHADOWSOCKS_DEFAULT); // local or server mode start_bootstrap(SHADOWSOCKS_DEFAULT, is_udp_proxy); // local or server mode
return 0; return 0;
} }

11
src/process.c

@ -182,10 +182,10 @@ void show_params() { // show shadowsocks and plugin params
} }
printf("\n"); printf("\n");
if (plugin_file == NULL) { if (plugin_file == NULL) {
printf("[Shadowsocks Bootstrap] plugin no need.\n"); printf("[Shadowsocks Bootstrap] Plugin no need.\n");
return; return;
} }
printf("[Shadowsocks Bootstrap] plugin -> %s\n", plugin_file); printf("[Shadowsocks Bootstrap] Plugin -> %s\n", plugin_file);
printf("[Shadowsocks Bootstrap] SS_REMOTE_HOST -> %s\n", SS_REMOTE_HOST); 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_REMOTE_PORT -> %s\n", SS_REMOTE_PORT);
printf("[Shadowsocks Bootstrap] SS_LOCAL_HOST -> %s\n", SS_LOCAL_HOST); printf("[Shadowsocks Bootstrap] SS_LOCAL_HOST -> %s\n", SS_LOCAL_HOST);
@ -193,15 +193,14 @@ void show_params() { // show shadowsocks and plugin params
printf("[Shadowsocks Bootstrap] SS_PLUGIN_OPTIONS -> %s\n", SS_PLUGIN_OPTIONS); printf("[Shadowsocks Bootstrap] SS_PLUGIN_OPTIONS -> %s\n", SS_PLUGIN_OPTIONS);
} }
void start_bootstrap(char *ss_type) { // start shadowsocks and plugin (optional) void start_bootstrap(char *ss_type, int is_udp_proxy) { // start shadowsocks and plugin (optional)
show_params(); show_params();
main_loop = g_main_loop_new(NULL, FALSE); main_loop = g_main_loop_new(NULL, FALSE);
signal(SIGINT, exit_with_child); // catch Ctrl + C (2) signal(SIGINT, exit_with_child); // catch Ctrl + C (2)
signal(SIGTERM, exit_with_child); // catch exit signal (15) signal(SIGTERM, exit_with_child); // catch exit signal (15)
signal(SIGCHLD, get_sub_exit); // callback when child process die signal(SIGCHLD, get_sub_exit); // callback when child process die
process_exec(); // exec child process process_exec(); // exec child process
usleep(500 * 1000); // wait 500ms for plugin start if (plugin_file != NULL && is_udp_proxy) { // start udp proxy when using plugin
if (plugin_file != NULL) { // start udp proxy when using plugin
char *remote_ip; char *remote_ip;
if (is_ip_addr(SS_REMOTE_HOST)) { // remote_host -> ip address if (is_ip_addr(SS_REMOTE_HOST)) { // remote_host -> ip address
remote_ip = SS_REMOTE_HOST; remote_ip = SS_REMOTE_HOST;
@ -223,6 +222,8 @@ void start_bootstrap(char *ss_type) { // start shadowsocks and plugin (optional)
proxy(SS_LOCAL_HOST, atoi(SS_LOCAL_PORT), remote_ip, atoi(SS_REMOTE_PORT)); proxy(SS_LOCAL_HOST, atoi(SS_LOCAL_PORT), remote_ip, atoi(SS_REMOTE_PORT));
} }
} }
} else {
printf("[Shadowsocks Bootstrap] UDP Proxy no need.\n");
} }
g_main_loop_run(main_loop); // into main loop for wait g_main_loop_run(main_loop); // into main loop for wait
exit_with_child(); exit_with_child();

17
src/server.c

@ -1,10 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "common.h" #include "common.h"
#include "process.h" #include "process.h"
#include "network.h"
#define SHADOWSOCKS_DEFAULT "ssserver" #define SHADOWSOCKS_DEFAULT "ssserver"
char *help_msg = char *help_msg =
@ -27,20 +26,28 @@ ss-bootstrap-server\n\
--plugin <name> Enable SIP003 plugin.\n\ --plugin <name> Enable SIP003 plugin.\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\
-h, --help Print this message.\n\ -h, --help Print this message.\n\
\n\ \n\
"; ";
void show_help() { // show help message
printf("%s", help_msg);
exit(0); // exit normally
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int i; int i;
if (argc <= 1) {
show_help();
}
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 show_help();
return 0;
} }
} }
args_decode(argc, argv); args_decode(argc, argv);
params_load(SHADOWSOCKS_DEFAULT); // default file name params_load(SHADOWSOCKS_DEFAULT); // default file name
start_bootstrap(SHADOWSOCKS_DEFAULT); // local or server mode start_bootstrap(SHADOWSOCKS_DEFAULT, is_udp_proxy); // local or server mode
return 0; return 0;
} }

Loading…
Cancel
Save