mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-21 22:43:48 +00:00
add multi-config support
This commit is contained in:
+24
-2
@@ -52,6 +52,27 @@ static int to_int(const json_value *value)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void parse_addr(const char *str, remote_addr_t *addr) {
|
||||
int ret = -1;
|
||||
char *pch;
|
||||
pch = strchr(str, ':');
|
||||
while (pch != NULL)
|
||||
{
|
||||
ret = pch - str;
|
||||
pch = strchr(pch + 1, ':');
|
||||
}
|
||||
if (ret == -1)
|
||||
{
|
||||
addr->host = str;
|
||||
addr->port = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
addr->host = ss_strndup(str, ret);
|
||||
addr->port = str + ret + 1;
|
||||
}
|
||||
}
|
||||
|
||||
jconf_t *read_jconf(const char* file)
|
||||
{
|
||||
|
||||
@@ -101,13 +122,14 @@ jconf_t *read_jconf(const char* file)
|
||||
{
|
||||
if (j >= MAX_REMOTE_NUM) break;
|
||||
json_value *v = value->u.array.values[j];
|
||||
conf.remote_host[j] = to_string(v);
|
||||
parse_addr(to_string(v), conf.remote_addr + j);
|
||||
conf.remote_num = j + 1;
|
||||
}
|
||||
}
|
||||
else if (value->type == json_string)
|
||||
{
|
||||
conf.remote_host[0] = to_string(value);
|
||||
conf.remote_addr[0].host = to_string(value);
|
||||
conf.remote_addr[0].port = NULL;
|
||||
conf.remote_num = 1;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -6,10 +6,16 @@
|
||||
#define DNS_THREAD_NUM 4
|
||||
#define MAX_UDP_CONN_NUM 4096
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *host;
|
||||
char *port;
|
||||
} remote_addr_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int remote_num;
|
||||
char *remote_host[MAX_REMOTE_NUM];
|
||||
remote_addr_t remote_addr[MAX_REMOTE_NUM];
|
||||
char *remote_port;
|
||||
char *local_port;
|
||||
char *password;
|
||||
|
||||
+13
-8
@@ -751,7 +751,11 @@ static void accept_cb (EV_P_ ev_io *w, int revents)
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
int index = rand() % listener->remote_num;
|
||||
int err = getaddrinfo(listener->remote_host[index], listener->remote_port, &hints, &res);
|
||||
if (verbose)
|
||||
{
|
||||
LOGD("connect to %s:%s", listener->remote_addr[index].host, listener->remote_addr[index].port);
|
||||
}
|
||||
int err = getaddrinfo(listener->remote_addr[index].host, listener->remote_addr[index].port, &hints, &res);
|
||||
if (err)
|
||||
{
|
||||
ERROR("getaddrinfo");
|
||||
@@ -804,7 +808,7 @@ int main (int argc, char **argv)
|
||||
char *iface = NULL;
|
||||
|
||||
int remote_num = 0;
|
||||
char *remote_host[MAX_REMOTE_NUM];
|
||||
remote_addr_t remote_addr[MAX_REMOTE_NUM];
|
||||
char *remote_port = NULL;
|
||||
|
||||
opterr = 0;
|
||||
@@ -814,7 +818,8 @@ int main (int argc, char **argv)
|
||||
switch (c)
|
||||
{
|
||||
case 's':
|
||||
remote_host[remote_num++] = optarg;
|
||||
remote_addr[remote_num].host = optarg;
|
||||
remote_addr[remote_num++].port = NULL;
|
||||
break;
|
||||
case 'p':
|
||||
remote_port = optarg;
|
||||
@@ -867,7 +872,7 @@ int main (int argc, char **argv)
|
||||
remote_num = conf->remote_num;
|
||||
for (i = 0; i < remote_num; i++)
|
||||
{
|
||||
remote_host[i] = conf->remote_host[i];
|
||||
remote_addr[i] = conf->remote_addr[i];
|
||||
}
|
||||
}
|
||||
if (remote_port == NULL) remote_port = conf->remote_port;
|
||||
@@ -922,13 +927,13 @@ int main (int argc, char **argv)
|
||||
// Setup proxy context
|
||||
struct listen_ctx listen_ctx;
|
||||
listen_ctx.remote_num = remote_num;
|
||||
listen_ctx.remote_host = malloc(sizeof(char *) * remote_num);
|
||||
listen_ctx.remote_addr = malloc(sizeof(remote_addr_t) * remote_num);
|
||||
while (remote_num > 0)
|
||||
{
|
||||
int index = --remote_num;
|
||||
listen_ctx.remote_host[index] = remote_host[index];
|
||||
if (remote_addr[index].port == NULL) remote_addr[index].port = remote_port;
|
||||
listen_ctx.remote_addr[index] = remote_addr[index];
|
||||
}
|
||||
listen_ctx.remote_port = remote_port;
|
||||
listen_ctx.timeout = atoi(timeout);
|
||||
listen_ctx.fd = listenfd;
|
||||
listen_ctx.iface = iface;
|
||||
@@ -946,7 +951,7 @@ int main (int argc, char **argv)
|
||||
if (udprelay)
|
||||
{
|
||||
LOGD("udprelay enabled.");
|
||||
udprelay_init(local_addr, local_port, remote_host[0], remote_port, m, iface);
|
||||
udprelay_init(local_addr, local_port, remote_addr[0].host, remote_addr[0].port, m, iface);
|
||||
}
|
||||
|
||||
ev_run (loop, 0);
|
||||
|
||||
+1
-2
@@ -8,8 +8,7 @@
|
||||
struct listen_ctx
|
||||
{
|
||||
ev_io io;
|
||||
char **remote_host;
|
||||
char *remote_port;
|
||||
remote_addr_t *remote_addr;
|
||||
char *iface;
|
||||
int remote_num;
|
||||
int method;
|
||||
|
||||
+13
-8
@@ -619,7 +619,11 @@ static void accept_cb (EV_P_ ev_io *w, int revents)
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
int index = rand() % listener->remote_num;
|
||||
err = getaddrinfo(listener->remote_host[index], listener->remote_port, &hints, &res);
|
||||
if (verbose)
|
||||
{
|
||||
LOGD("connect to %s:%s", listener->remote_addr[index].host, listener->remote_addr[index].port);
|
||||
}
|
||||
err = getaddrinfo(listener->remote_addr[index].host, listener->remote_addr[index].port, &hints, &res);
|
||||
if (err)
|
||||
{
|
||||
ERROR("getaddrinfo");
|
||||
@@ -670,7 +674,7 @@ int main (int argc, char **argv)
|
||||
char *conf_path = NULL;
|
||||
|
||||
int remote_num = 0;
|
||||
char *remote_host[MAX_REMOTE_NUM];
|
||||
remote_addr_t remote_addr[MAX_REMOTE_NUM];
|
||||
char *remote_port = NULL;
|
||||
|
||||
opterr = 0;
|
||||
@@ -680,7 +684,8 @@ int main (int argc, char **argv)
|
||||
switch (c)
|
||||
{
|
||||
case 's':
|
||||
remote_host[remote_num++] = optarg;
|
||||
remote_addr[remote_num].host = optarg;
|
||||
remote_addr[remote_num++].port = NULL;
|
||||
break;
|
||||
case 'p':
|
||||
remote_port = optarg;
|
||||
@@ -724,7 +729,7 @@ int main (int argc, char **argv)
|
||||
remote_num = conf->remote_num;
|
||||
for (i = 0; i < remote_num; i++)
|
||||
{
|
||||
remote_host[i] = conf->remote_host[i];
|
||||
remote_addr[i] = conf->remote_addr[i];
|
||||
}
|
||||
}
|
||||
if (remote_port == NULL) remote_port = conf->remote_port;
|
||||
@@ -755,7 +760,7 @@ int main (int argc, char **argv)
|
||||
signal(SIGABRT, SIG_IGN);
|
||||
|
||||
// Setup keys
|
||||
LOGD("calculating ciphers...");
|
||||
LOGD("initialize ciphers... %s", method);
|
||||
int m = enc_init(password, method);
|
||||
|
||||
// Setup socket
|
||||
@@ -775,13 +780,13 @@ int main (int argc, char **argv)
|
||||
// Setup proxy context
|
||||
struct listen_ctx listen_ctx;
|
||||
listen_ctx.remote_num = remote_num;
|
||||
listen_ctx.remote_host = malloc(sizeof(char *) * remote_num);
|
||||
listen_ctx.remote_addr = malloc(sizeof(remote_addr_t) * remote_num);
|
||||
while (remote_num > 0)
|
||||
{
|
||||
int index = --remote_num;
|
||||
listen_ctx.remote_host[index] = remote_host[index];
|
||||
if (remote_addr[index].port == NULL) remote_addr[index].port = remote_port;
|
||||
listen_ctx.remote_addr[index] = remote_addr[index];
|
||||
}
|
||||
listen_ctx.remote_port = remote_port;
|
||||
listen_ctx.timeout = atoi(timeout);
|
||||
listen_ctx.fd = listenfd;
|
||||
listen_ctx.method = m;
|
||||
|
||||
+1
-2
@@ -8,8 +8,7 @@
|
||||
struct listen_ctx
|
||||
{
|
||||
ev_io io;
|
||||
char **remote_host;
|
||||
char *remote_port;
|
||||
remote_addr_t *remote_host;
|
||||
int remote_num;
|
||||
int timeout;
|
||||
int fd;
|
||||
|
||||
+1
-1
@@ -969,7 +969,7 @@ int main (int argc, char **argv)
|
||||
server_num = conf->remote_num;
|
||||
for (i = 0; i < server_num; i++)
|
||||
{
|
||||
server_host[i] = conf->remote_host[i];
|
||||
server_host[i] = conf->remote_addr[i].host;
|
||||
}
|
||||
}
|
||||
if (server_port == NULL) server_port = conf->remote_port;
|
||||
|
||||
Reference in New Issue
Block a user