Browse Source

Clean up

pull/2463/head
Max Lv 5 years ago
parent
commit
64b97cd9a3
16 changed files with 91 additions and 93 deletions
  1. 3
      src/acl.c
  2. 17
      src/aead.c
  3. 3
      src/android.c
  4. 2
      src/http.c
  5. 5
      src/jconf.c
  6. 28
      src/local.c
  7. 18
      src/manager.c
  8. 3
      src/netutils.c
  9. 8
      src/plugin.c
  10. 8
      src/redir.c
  11. 14
      src/server.c
  12. 2
      src/tls.c
  13. 8
      src/tunnel.c
  14. 44
      src/udprelay.c
  15. 5
      src/utils.c
  16. 16
      src/utils.h

3
src/acl.c

@ -100,8 +100,7 @@ trimwhitespace(char *str)
int
init_acl(const char *path)
{
if (path == NULL)
{
if (path == NULL) {
return -1;
}

17
src/aead.c

@ -168,13 +168,13 @@ aead_cipher_encrypt(cipher_ctx_t *cipher_ctx,
switch (cipher_ctx->cipher->method) {
case AES256GCM: // Only AES-256-GCM is supported by libsodium.
if (cipher_ctx->aes256gcm_ctx != NULL) { // Use it if availble
err = crypto_aead_aes256gcm_encrypt_afternm(c, &long_clen, m, mlen,
ad, adlen, NULL, n,
(const aes256gcm_ctx *)cipher_ctx->aes256gcm_ctx);
err = crypto_aead_aes256gcm_encrypt_afternm(c, &long_clen, m, mlen,
ad, adlen, NULL, n,
(const aes256gcm_ctx *)cipher_ctx->aes256gcm_ctx);
*clen = (size_t)long_clen; // it's safe to cast 64bit to 32bit length here
break;
}
// Otherwise, just use the mbedTLS one with crappy AES-NI.
// Otherwise, just use the mbedTLS one with crappy AES-NI.
case AES192GCM:
case AES128GCM:
@ -218,12 +218,12 @@ aead_cipher_decrypt(cipher_ctx_t *cipher_ctx,
case AES256GCM: // Only AES-256-GCM is supported by libsodium.
if (cipher_ctx->aes256gcm_ctx != NULL) { // Use it if availble
err = crypto_aead_aes256gcm_decrypt_afternm(p, &long_plen, NULL, m, mlen,
ad, adlen, n,
(const aes256gcm_ctx *)cipher_ctx->aes256gcm_ctx);
ad, adlen, n,
(const aes256gcm_ctx *)cipher_ctx->aes256gcm_ctx);
*plen = (size_t)long_plen; // it's safe to cast 64bit to 32bit length here
break;
}
// Otherwise, just use the mbedTLS one with crappy AES-NI.
// Otherwise, just use the mbedTLS one with crappy AES-NI.
case AES192GCM:
case AES128GCM:
err = mbedtls_cipher_auth_decrypt(cipher_ctx->evp, n, nlen, ad, adlen,
@ -335,7 +335,7 @@ aead_cipher_ctx_init(cipher_ctx_t *cipher_ctx, int method, int enc)
memset(cipher_ctx->aes256gcm_ctx, 0, sizeof(aes256gcm_ctx));
} else {
cipher_ctx->aes256gcm_ctx = NULL;
cipher_ctx->evp = ss_malloc(sizeof(cipher_evp_t));
cipher_ctx->evp = ss_malloc(sizeof(cipher_evp_t));
memset(cipher_ctx->evp, 0, sizeof(cipher_evp_t));
cipher_evp_t *evp = cipher_ctx->evp;
mbedtls_cipher_init(evp);
@ -349,7 +349,6 @@ aead_cipher_ctx_init(cipher_ctx_t *cipher_ctx, int method, int enc)
FATAL("Cannot initialize mbed TLS cipher");
}
#ifdef SS_DEBUG
dump("KEY", (char *)cipher_ctx->cipher->key, cipher_ctx->cipher->key_len);
#endif

3
src/android.c

@ -97,7 +97,8 @@ extern char *stat_path;
int
send_traffic_stat(uint64_t tx, uint64_t rx)
{
if (!stat_path) return 0;
if (!stat_path)
return 0;
int sock;
struct sockaddr_un addr;

2
src/http.c

@ -42,7 +42,7 @@ static int get_header(const char *, const char *, int, char **);
static int next_header(const char **, int *);
static const protocol_t http_protocol_st = {
.default_port = 80,
.default_port = 80,
.parse_packet = &parse_http_header,
};
const protocol_t *const http_protocol = &http_protocol_st;

5
src/jconf.c

@ -107,8 +107,7 @@ parse_addr(const char *str_in, ss_addr_t *addr)
} else {
addr->host = ss_strndup(str, ret);
}
if (ret < len - 1)
{
if (ret < len - 1) {
addr->port = strdup(str + ret + 1);
} else {
addr->port = NULL;
@ -220,7 +219,7 @@ read_jconf(const char *file)
}
} else if (value->type == json_string) {
parse_addr(to_string(value), conf.remote_addr);
conf.remote_num = 1;
conf.remote_num = 1;
}
} else if (strcmp(name, "port_password") == 0) {
if (value->type == json_object) {

28
src/local.c

@ -89,7 +89,7 @@ uint64_t rx = 0;
ev_tstamp last = 0;
int is_remote_dns = 1; // resolve hostname remotely
char *stat_path = NULL;
char *stat_path = NULL;
#endif
static crypto_t *crypto;
@ -97,7 +97,7 @@ static crypto_t *crypto;
static int acl = 0;
static int mode = TCP_ONLY;
static int ipv6first = 0;
int fast_open = 0;
int fast_open = 0;
static int no_delay = 0;
static int udp_fd = 0;
static int ret_val = 0;
@ -362,7 +362,7 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
return -1;
}
char host[MAX_HOSTNAME_LEN+1], ip[INET6_ADDRSTRLEN], port[16];
char host[MAX_HOSTNAME_LEN + 1], ip[INET6_ADDRSTRLEN], port[16];
buffer_t *abuf = server->abuf;
abuf->idx = 0;
@ -441,10 +441,10 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
if (atyp == SOCKS5_ATYP_IPV4 || atyp == SOCKS5_ATYP_IPV6) {
if (dst_port == http_protocol->default_port)
hostname_len = http_protocol->parse_packet(buf->data + 3 + abuf->len,
buf->len - 3 - abuf->len, &hostname);
buf->len - 3 - abuf->len, &hostname);
else if (dst_port == tls_protocol->default_port)
hostname_len = tls_protocol->parse_packet(buf->data + 3 + abuf->len,
buf->len - 3 - abuf->len, &hostname);
buf->len - 3 - abuf->len, &hostname);
if (hostname_len == -1 && buf->len < SOCKET_BUF_SIZE && server->stage != STAGE_SNI) {
if (server_handshake_reply(EV_A_ w, 0, &response) < 0)
return -1;
@ -529,7 +529,7 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
}
int ip_match = (resolved || atyp == SOCKS5_ATYP_IPV4
|| atyp == SOCKS5_ATYP_IPV6) ? acl_match_host(ip) : 0;
|| atyp == SOCKS5_ATYP_IPV6) ? acl_match_host(ip) : 0;
switch (get_acl_mode()) {
case BLACK_LIST:
@ -875,10 +875,10 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
// all processed
return;
} else if (server->stage == STAGE_INIT) {
if(verbose) {
if (verbose) {
struct sockaddr_in peer_addr;
socklen_t peer_addr_len = sizeof peer_addr;
if(getpeername(server->fd, (struct sockaddr *)&peer_addr, &peer_addr_len) == 0) {
if (getpeername(server->fd, (struct sockaddr *)&peer_addr, &peer_addr_len) == 0) {
LOGI("connection from %s:%hu", inet_ntoa(peer_addr.sin_addr), ntohs(peer_addr.sin_port));
}
}
@ -1365,7 +1365,7 @@ create_remote(listen_ctx_t *listener,
memcpy(&(remote->addr), remote_addr, remote->addr_len);
remote->direct = direct;
if(verbose) {
if (verbose) {
struct sockaddr_in *sockaddr = (struct sockaddr_in *)&remote->addr;
LOGI("remote: %s:%hu", inet_ntoa(sockaddr->sin_addr), ntohs(sockaddr->sin_port));
}
@ -1491,7 +1491,7 @@ main(int argc, char **argv)
{ "password", required_argument, NULL, GETOPT_VAL_PASSWORD },
{ "key", required_argument, NULL, GETOPT_VAL_KEY },
{ "help", no_argument, NULL, GETOPT_VAL_HELP },
{ NULL, 0, NULL, 0 }
{ NULL, 0, NULL, 0 }
};
opterr = 0;
@ -1696,21 +1696,21 @@ main(int argc, char **argv)
}
}
if(remote_num == 0) {
if (remote_num == 0) {
fprintf(stderr, "remote_num is 0\n");
exit(EXIT_FAILURE);
}
if(!remote_port) {
if (!remote_port) {
fprintf(stderr, "remote_port is NULL\n");
exit(EXIT_FAILURE);
}
#ifndef HAVE_LAUNCHD
if(!local_port) {
if (!local_port) {
fprintf(stderr, "local_port is NULL\n");
exit(EXIT_FAILURE);
}
#endif
if(!password && !key) {
if (!password && !key) {
fprintf(stderr, "both password and key are NULL\n");
exit(EXIT_FAILURE);
}

18
src/manager.c

@ -212,8 +212,7 @@ construct_command_line(struct manager_ctx *manager, struct server *server)
int len = strlen(cmd);
snprintf(cmd + len, BUF_SIZE - len, " -d \"%s\"", manager->nameservers);
}
if (manager->workdir)
{
if (manager->workdir) {
int len = strlen(cmd);
snprintf(cmd + len, BUF_SIZE - len, " -D \"%s\"", manager->workdir);
}
@ -705,12 +704,12 @@ manager_recv_cb(EV_P_ ev_io *w, int revents)
if (parse_traffic(buf, r, port, &traffic) == -1) {
LOGE("invalid command: %s:%s", buf, get_data(buf, r));
goto ERROR_MSG;
goto ERROR_MSG;
}
update_stat(port, traffic);
char msg[3] = "ok";
char msg[3] = "ok";
if (sendto(manager->fd, msg, 2, 0, (struct sockaddr *)&claddr, len) != 2) {
ERROR("stat_sendto");
}
@ -901,7 +900,7 @@ main(int argc, char **argv)
{ "password", required_argument, NULL, GETOPT_VAL_PASSWORD },
{ "workdir", required_argument, NULL, GETOPT_VAL_WORKDIR },
{ "help", no_argument, NULL, GETOPT_VAL_HELP },
{ NULL, 0, NULL, 0 }
{ NULL, 0, NULL, 0 }
};
opterr = 0;
@ -1055,8 +1054,7 @@ main(int argc, char **argv)
if (ipv6first == 0) {
ipv6first = conf->ipv6_first;
}
if (workdir == NULL)
{
if (workdir == NULL) {
workdir = conf->workdir;
}
if (acl == NULL) {
@ -1161,7 +1159,7 @@ main(int argc, char **argv)
}
#endif
struct passwd *pw = getpwuid(getuid());
struct passwd *pw = getpwuid(getuid());
if (workdir == NULL || strlen(workdir) == 0) {
workdir = pw->pw_dir;
@ -1171,11 +1169,11 @@ main(int argc, char **argv)
}
working_dir_size = strlen(workdir) + 15;
working_dir = ss_malloc(working_dir_size);
working_dir = ss_malloc(working_dir_size);
snprintf(working_dir, working_dir_size, "%s/.shadowsocks", workdir);
} else {
working_dir_size = strlen(workdir) + 2;
working_dir = ss_malloc(working_dir_size);
working_dir = ss_malloc(working_dir_size);
snprintf(working_dir, working_dir_size, "%s", workdir);
}
LOGI("working directory points to %s", working_dir);

3
src/netutils.c

@ -301,8 +301,7 @@ int
is_ipv6only(ss_addr_t *servers, size_t server_num, int ipv6first)
{
int i;
for (i = 0; i < server_num; i++)
{
for (i = 0; i < server_num; i++) {
struct sockaddr_storage storage;
memset(&storage, 0, sizeof(struct sockaddr_storage));
if (get_sockaddr(servers[i].host, servers[i].port, &storage, 1, ipv6first) == -1) {

8
src/plugin.c

@ -112,10 +112,12 @@ start_ss_plugin(const char *plugin,
exec = cork_exec_new(plugin);
cork_exec_add_param(exec, plugin); // argv[0]
extern int fast_open;
if (fast_open) cork_exec_add_param(exec, "--fast-open");
if (fast_open)
cork_exec_add_param(exec, "--fast-open");
#ifdef __ANDROID__
extern int vpn;
if (vpn) cork_exec_add_param(exec, "-V");
if (vpn)
cork_exec_add_param(exec, "-V");
#endif
cork_exec_set_env(exec, env);
@ -166,7 +168,7 @@ start_obfsproxy(const char *plugin,
{
char *pch;
char *opts_dump = NULL;
char *buf = NULL;
char *buf = NULL;
int ret, buf_size = 0;
if (plugin_opts != NULL) {

8
src/redir.c

@ -89,9 +89,9 @@ static int mode = TCP_ONLY;
#ifdef HAVE_SETRLIMIT
static int nofile = 0;
#endif
int fast_open = 0;
static int no_delay = 0;
static int ret_val = 0;
int fast_open = 0;
static int no_delay = 0;
static int ret_val = 0;
static struct ev_signal sigint_watcher;
static struct ev_signal sigterm_watcher;
@ -870,7 +870,7 @@ main(int argc, char **argv)
{ "password", required_argument, NULL, GETOPT_VAL_PASSWORD },
{ "key", required_argument, NULL, GETOPT_VAL_KEY },
{ "help", no_argument, NULL, GETOPT_VAL_HELP },
{ NULL, 0, NULL, 0 }
{ NULL, 0, NULL, 0 }
};
opterr = 0;

14
src/server.c

@ -106,8 +106,8 @@ static void close_and_free_server(EV_P_ server_t *server);
static void resolv_cb(struct sockaddr *addr, void *data);
static void resolv_free_cb(void *data);
int verbose = 0;
int reuse_port = 0;
int verbose = 0;
int reuse_port = 0;
int is_bind_local_addr = 0;
struct sockaddr_storage local_addr_v4;
@ -118,7 +118,7 @@ static crypto_t *crypto;
static int acl = 0;
static int mode = TCP_ONLY;
static int ipv6first = 0;
int fast_open = 0;
int fast_open = 0;
static int no_delay = 0;
static int ret_val = 0;
@ -475,8 +475,7 @@ connect_to_remote(EV_P_ struct addrinfo *res,
if (setnonblocking(sockfd) == -1)
ERROR("setnonblocking");
if (is_bind_local_addr)
{
if (is_bind_local_addr) {
struct sockaddr_storage *local_addr =
res->ai_family == AF_INET ? &local_addr_v4 : &local_addr_v6;
if (bind_to_addr(local_addr, sockfd) == -1) {
@ -1584,7 +1583,7 @@ main(int argc, char **argv)
#ifdef __linux__
{ "mptcp", no_argument, NULL, GETOPT_VAL_MPTCP },
#endif
{ NULL, 0, NULL, 0 }
{ NULL, 0, NULL, 0 }
};
opterr = 0;
@ -2034,7 +2033,8 @@ main(int argc, char **argv)
LOGI("udp server listening at %s:%s", host ? host : "0.0.0.0", port);
// Setup UDP
int err = init_udprelay(host, port, mtu, crypto, atoi(timeout), iface);
if (err == -1) continue;
if (err == -1)
continue;
num_listen_ctx++;
}

2
src/tls.c

@ -59,7 +59,7 @@ static int parse_extensions(const char *, size_t, char **);
static int parse_server_name_extension(const char *, size_t, char **);
static const protocol_t tls_protocol_st = {
.default_port = 443,
.default_port = 443,
.parse_packet = &parse_tls_header,
};
const protocol_t *const tls_protocol = &tls_protocol_st;

8
src/tunnel.c

@ -90,9 +90,9 @@ static int mode = TCP_ONLY;
#ifdef HAVE_SETRLIMIT
static int nofile = 0;
#endif
static int no_delay = 0;
int fast_open = 0;
static int ret_val = 0;
static int no_delay = 0;
int fast_open = 0;
static int ret_val = 0;
static struct ev_signal sigint_watcher;
static struct ev_signal sigterm_watcher;
@ -901,7 +901,7 @@ main(int argc, char **argv)
{ "password", required_argument, NULL, GETOPT_VAL_PASSWORD },
{ "key", required_argument, NULL, GETOPT_VAL_KEY },
{ "help", no_argument, NULL, GETOPT_VAL_HELP },
{ NULL, 0, NULL, 0 }
{ NULL, 0, NULL, 0 }
};
opterr = 0;

44
src/udprelay.c

@ -109,7 +109,7 @@ static int buf_size = DEFAULT_PACKET_SIZE * 2;
static int server_num = 0;
static server_ctx_t *server_ctx_list[MAX_REMOTE_NUM] = { NULL };
const char* s_port = NULL;
const char *s_port = NULL;
#ifndef __MINGW32__
static int
@ -492,8 +492,8 @@ create_server_socket(const char *host, const char *port)
}
#ifdef IP_TOS
// Set QoS flag
int tos = 46;
int proto = rp->ai_family == AF_INET6 ? IPPROTO_IP: IPPROTO_IPV6;
int tos = 46;
int proto = rp->ai_family == AF_INET6 ? IPPROTO_IP : IPPROTO_IPV6;
setsockopt(server_sock, proto, IP_TOS, &tos, sizeof(tos));
#endif
@ -654,8 +654,8 @@ resolv_cb(struct sockaddr *addr, void *data)
#endif
#ifdef IP_TOS
// Set QoS flag
int tos = 46;
int proto = addr->sa_family == AF_INET6 ? IPPROTO_IP: IPPROTO_IPV6;
int tos = 46;
int proto = addr->sa_family == AF_INET6 ? IPPROTO_IP : IPPROTO_IPV6;
setsockopt(remotefd, proto, IP_TOS, &tos, sizeof(tos));
#endif
#ifdef SET_INTERFACE
@ -664,9 +664,9 @@ resolv_cb(struct sockaddr *addr, void *data)
ERROR("setinterface");
}
#endif
remote_ctx = new_remote(remotefd, query_ctx->server_ctx);
remote_ctx->src_addr = query_ctx->src_addr;
remote_ctx->server_ctx = query_ctx->server_ctx;
remote_ctx = new_remote(remotefd, query_ctx->server_ctx);
remote_ctx->src_addr = query_ctx->src_addr;
remote_ctx->server_ctx = query_ctx->server_ctx;
} else {
ERROR("[udp] bind() error");
}
@ -842,8 +842,8 @@ remote_recv_cb(EV_P_ ev_io *w, int revents)
}
#ifdef IP_TOS
// Set QoS flag
int tos = 46;
int proto = remote_ctx->src_addr.ss_family == AF_INET6 ? IPPROTO_IP: IPPROTO_IPV6;
int tos = 46;
int proto = remote_ctx->src_addr.ss_family == AF_INET6 ? IPPROTO_IP : IPPROTO_IPV6;
setsockopt(src_fd, proto, IP_TOS, &tos, sizeof(tos));
#endif
if (bind(src_fd, (struct sockaddr *)&dst_addr, remote_dst_addr_len) != 0) {
@ -1014,7 +1014,7 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
#ifdef MODULE_REDIR
char addr_header[MAX_ADDR_HEADER_SIZE] = { 0 };
int addr_header_len = construct_udprelay_header(&dst_addr, addr_header);
int addr_header_len = construct_udprelay_header(&dst_addr, addr_header);
if (addr_header_len == 0) {
LOGE("[udp] failed to parse tproxy addr");
@ -1030,11 +1030,11 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
#elif MODULE_TUNNEL
char addr_header[MAX_ADDR_HEADER_SIZE] = { 0 };
char *host = server_ctx->tunnel_addr.host;
char *port = server_ctx->tunnel_addr.port;
uint16_t port_num = (uint16_t)atoi(port);
uint16_t port_net_num = htons(port_num);
int addr_header_len = 0;
char *host = server_ctx->tunnel_addr.host;
char *port = server_ctx->tunnel_addr.port;
uint16_t port_num = (uint16_t)atoi(port);
uint16_t port_net_num = htons(port_num);
int addr_header_len = 0;
struct cork_ip ip;
if (cork_ip_init(&ip, host) != -1) {
@ -1086,7 +1086,7 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
#else
char host[MAX_HOSTNAME_LEN] = { 0 };
char port[MAX_PORT_STR_LEN] = { 0 };
char port[MAX_PORT_STR_LEN] = { 0 };
struct sockaddr_storage dst_addr;
memset(&dst_addr, 0, sizeof(struct sockaddr_storage));
@ -1264,8 +1264,8 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
#endif
#ifdef IP_TOS
// Set QoS flag
int tos = 46;
int proto = dst_addr.ss_family == AF_INET6 ? IPPROTO_IP: IPPROTO_IPV6;
int tos = 46;
int proto = dst_addr.ss_family == AF_INET6 ? IPPROTO_IP : IPPROTO_IPV6;
setsockopt(remotefd, proto, IP_TOS, &tos, sizeof(tos));
#endif
#ifdef SET_INTERFACE
@ -1274,9 +1274,9 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
ERROR("setinterface");
}
#endif
remote_ctx = new_remote(remotefd, server_ctx);
remote_ctx->src_addr = src_addr;
remote_ctx->server_ctx = server_ctx;
remote_ctx = new_remote(remotefd, server_ctx);
remote_ctx->src_addr = src_addr;
remote_ctx->server_ctx = server_ctx;
memcpy(&remote_ctx->dst_addr, &dst_addr, sizeof(struct sockaddr_storage));
} else {
ERROR("[udp] bind() error");

5
src/utils.c

@ -552,9 +552,10 @@ get_default_conf(void)
#endif
}
uint16_t load16_be(const void *s)
uint16_t
load16_be(const void *s)
{
const uint8_t *in = (const uint8_t *)s;
return ((uint16_t)in[0] << 8)
| ((uint16_t)in[1]);
| ((uint16_t)in[1]);
}

16
src/utils.h

@ -232,17 +232,17 @@ void *ss_aligned_malloc(size_t size);
void *ss_realloc(void *ptr, size_t new_size);
#define ss_free(ptr) \
{ \
free(ptr); \
ptr = NULL; \
}
{ \
free(ptr); \
ptr = NULL; \
}
#ifdef __MINGW32__
#define ss_aligned_free(ptr) \
{ \
_aligned_free(ptr); \
ptr = NULL; \
}
{ \
_aligned_free(ptr); \
ptr = NULL; \
}
#else
#define ss_aligned_free(ptr) ss_free(ptr)
#endif

Loading…
Cancel
Save