Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
lparam committed Oct 6, 2020
2 parents 31d638d + db39641 commit d0ab4b7
Show file tree
Hide file tree
Showing 26 changed files with 146 additions and 102 deletions.
2 changes: 1 addition & 1 deletion 3rd/libsodium
Submodule libsodium updated 568 files
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v0.5.0 (2020-10-06)
-----------
* Fix: ipv6 sockaddr
* Fix: crypto overlap
* Change: chacha20 instead of salsa20


v0.4.6 (2020-8-17)
-----------
* Fix: don't use uv_write_t
Expand Down
22 changes: 15 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MAJOR = 0
MINOR = 4
PATCH = 5
MINOR = 5
PATCH = 0
NAME = xSocks

ifdef O
Expand All @@ -26,8 +26,18 @@ OBJTREE := $(if $(BUILD_DIR),$(BUILD_DIR),$(CURDIR))
SRCTREE := $(CURDIR)
export SRCTREE OBJTREE

TAG = $(shell git describe --always --tags --abbrev=0 | tr -d "[v\r\n]")
COMMIT = $(shell git rev-parse --short HEAD| tr -d "[ \r\n\']")
VERSION = v$(TAG)-$(COMMIT)

ifeq ($(strip $(COMMIT)),)
VERSION = v$(MAJOR).$(MINOR).$(PATCH)
endif

#########################################################################

CPPFLAGS = -DVERSION=\"$(VERSION)\" -DBUILD_TIME=\"$(shell date '+%Y-%m-%d')\ $(shell date '+%H:%M:%S')\"

ifdef HOST
CROSS_COMPILE = $(HOST)-
endif
Expand All @@ -43,7 +53,7 @@ OPENWRT = 1
endif

ifdef CROSS_COMPILE
CPPFLAGS = -DCROSS_COMPILE
CPPFLAGS += -DCROSS_COMPILE
endif

CFLAGS = \
Expand Down Expand Up @@ -87,10 +97,6 @@ LDFLAGS = -Wl,--gc-sections
ifdef ANDROID
LDFLAGS += -pie -fPIE
LIBS += -llog
else
ifndef MINGW32
LIBS += -lrt
endif
endif

LIBCORK = $(OBJTREE)/3rd/libcork/libcork.a
Expand Down Expand Up @@ -165,6 +171,8 @@ $(LIBCORK): \
$(OBJTREE)/3rd/libcork/src/ds/array.o \
$(OBJTREE)/3rd/libcork/src/ds/hash-table.o \
$(OBJTREE)/3rd/libcork/src/ds/buffer.o \
$(OBJTREE)/3rd/libcork/src/ds/managed-buffer.o \
$(OBJTREE)/3rd/libcork/src/ds/slice.o \
$(OBJTREE)/3rd/libcork/src/ds/dllist.o \
$(OBJTREE)/3rd/libcork/src/posix/process.o
$(BUILD_AR) rcu $@ $^
Expand Down
2 changes: 1 addition & 1 deletion openwrt/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=xSocks
PKG_VERSION:=0.4.5
PKG_VERSION:=0.5.0
PKG_RELEASE=

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
Expand Down
8 changes: 7 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
#include "uv.h"
#include "socks.h"

#define XSOCKS_VERSION "0.4.5"
#ifdef VERSION
#define xSocks_VER VERSION
#define xSocks_BUILD_TIME BUILD_TIME
#else
#define xSocks_VER "undefined"
#define xSocks_BUILD_TIME ""
#endif

struct server_context {
int index;
Expand Down
22 changes: 11 additions & 11 deletions src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@

#define COB crypto_onetimeauth_BYTES // 16U
#define COKB crypto_onetimeauth_KEYBYTES // 32U
#define CSSNB crypto_stream_salsa20_NONCEBYTES // 8U
#define CSSKB crypto_stream_salsa20_KEYBYTES //32U
#define CSSNB crypto_stream_chacha20_NONCEBYTES // 8U
#define CSSKB crypto_stream_chacha20_KEYBYTES //32U

static uint8_t secret_key[crypto_generichash_BYTES];


static int
salsa208poly1305_encrypt(uint8_t *c, const uint8_t *m, const uint32_t mlen,
chacha20poly1305_encrypt(uint8_t *c, const uint8_t *m, const uint32_t mlen,
const uint8_t *n, const uint8_t *k) {
uint8_t cok[COKB];

crypto_stream_salsa208(cok, COKB, n, k);
crypto_stream_salsa208_xor(c + COB, m, mlen, n, k);
crypto_stream_chacha20(cok, COKB, n, k);
crypto_stream_chacha20_xor(c + COB, m, mlen, n, k);
crypto_onetimeauth_poly1305(c, c + COB, mlen, cok);

return 0;
}

static int
salsa208poly1305_decrypt(uint8_t *m, const uint8_t *c, const uint32_t clen,
chacha20poly1305_decrypt(uint8_t *m, const uint8_t *c, const uint32_t clen,
const uint8_t *n, const uint8_t *k) {
uint8_t cok[COKB];

Expand All @@ -44,9 +44,9 @@ salsa208poly1305_decrypt(uint8_t *m, const uint8_t *c, const uint32_t clen,

int mlen = clen - COB;

crypto_stream_salsa208(cok, COKB, n, k);
crypto_stream_chacha20(cok, COKB, n, k);
if (crypto_onetimeauth_poly1305_verify(c, c + COB, mlen, cok) == 0) {
return crypto_stream_salsa208_xor(m, c + COB, mlen, n, k);
return crypto_stream_chacha20_xor(m, c + COB, mlen, n, k);
}

return -1;
Expand All @@ -58,7 +58,7 @@ crypto_init(const char *password) {
return 1;
}

randombytes_set_implementation(&randombytes_salsa20_implementation);
randombytes_set_implementation(&randombytes_internal_implementation);
randombytes_stir();

return crypto_generichash(secret_key, sizeof secret_key, (uint8_t*)password, strlen(password), NULL, 0);
Expand All @@ -74,7 +74,7 @@ crypto_encrypt(uint8_t *c, const uint8_t *m, const uint32_t mlen) {
uint8_t nonce[CSSNB];
randombytes_buf(nonce, CSSNB);
memcpy(c, nonce, CSSNB);
return salsa208poly1305_encrypt(c + CSSNB, m, mlen, nonce, secret_key);
return chacha20poly1305_encrypt(c + CSSNB, m, mlen, nonce, secret_key);
}

int
Expand All @@ -84,5 +84,5 @@ crypto_decrypt(uint8_t *m, const uint8_t *c, const uint32_t clen) {
return -1;
}
memcpy(nonce, c, CSSNB);
return salsa208poly1305_decrypt(m, c + CSSNB, clen - CSSNB, nonce, secret_key);
return chacha20poly1305_decrypt(m, c + CSSNB, clen - CSSNB, nonce, secret_key);
}
6 changes: 3 additions & 3 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ print_buffer(const void *data, uint32_t count, uint32_t width, uint32_t linelen)
}

int
resolve_addr(const char *buf, struct sockaddr *addr) {
resolve_addr(const char *buf, struct sockaddr_storage *addr) {
char *p;
char *tmp = strdup(buf);
int rc = 0;
Expand Down Expand Up @@ -148,12 +148,12 @@ resolve_addr(const char *buf, struct sockaddr *addr) {
goto err;

} else {
*addr = *(struct sockaddr*)&addr4;
memcpy(addr, &addr4, sizeof(struct sockaddr_in));
}

} else {
uv_ip6_addr(tmp, port, &addr6);
*addr = *(struct sockaddr*)&addr6;
memcpy(addr, &addr6, sizeof(struct sockaddr_in6));
}

err:
Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#define container_of(ptr, type, member) ((type*)(((char*)(ptr)) - offsetof(type, member)))

int resolve_addr(const char *buf, struct sockaddr *addr);
int resolve_addr(const char *buf, struct sockaddr_storage *addr);
int read_size(uint8_t *buffer);
void write_size(uint8_t *buffer, int len);
int ip_name(const struct sockaddr *ip, char *name, size_t size);
Expand Down
18 changes: 9 additions & 9 deletions src/xForwarder.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static const struct option _lopts[] = {

static void
print_usage(const char *prog) {
printf("xForwarder Version: %s Maintained by lparam\n", XFORWARDER_VER);
printf("xForwarder Version: %s Maintained by lparam\n", xForwarder_VER);
printf("Usage: %s [-l local] <-s server> <-d dest> <-k password> [-p pidfile] [-c concurrency] [-s signal] [-nhvV]\n\n", prog);
printf("Options:\n");
puts(" -s <server address>\t : server address:port\n"
Expand Down Expand Up @@ -71,7 +71,7 @@ parse_opts(int argc, char *argv[]) {
while ((opt = getopt_long(argc, argv, _optString, _lopts, &longindex)) != -1) {
switch (opt) {
case 'v':
printf("xForwarder version: %s \n", XFORWARDER_VER);
printf("%s %s\n", xForwarder_VER, xForwarder_BUILD_TIME);
exit(0);
break;
case 'h':
Expand Down Expand Up @@ -272,12 +272,12 @@ main(int argc, char *argv[]) {
struct server_context ctx;
ctx.udprelay = 1;
ctx.udp_fd = create_socket(SOCK_DGRAM, 0);
ctx.dest_addr = &dest_addr;
ctx.local_addr = &bind_addr;
ctx.server_addr = &server_addr;
ctx.dest_addr = (struct sockaddr *)&dest_addr;
ctx.local_addr = (struct sockaddr *)&bind_addr;
ctx.server_addr = (struct sockaddr *)&server_addr;

uv_tcp_init(loop, &ctx.tcp);
rc = uv_tcp_bind(&ctx.tcp, &bind_addr, 0);
rc = uv_tcp_bind(&ctx.tcp, (struct sockaddr *)&bind_addr, 0);
if (rc) {
logger_stderr("bind error: %s", uv_strerror(rc));
return 1;
Expand Down Expand Up @@ -310,9 +310,9 @@ main(int argc, char *argv[]) {
ctx->udp_fd = create_socket(SOCK_DGRAM, 1);
ctx->udprelay = 1;
ctx->accept_cb = client_accept_cb;
ctx->dest_addr = &dest_addr;
ctx->local_addr = &bind_addr;
ctx->server_addr = &server_addr;
ctx->dest_addr = (struct sockaddr *)&dest_addr;
ctx->local_addr = (struct sockaddr *)&bind_addr;
ctx->server_addr = (struct sockaddr *)&server_addr;
rc = uv_sem_init(&ctx->semaphore, 0);
rc = uv_thread_create(&ctx->thread_id, consumer_start, ctx);
}
Expand Down
15 changes: 10 additions & 5 deletions src/xForwarder.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
#include "packet.h"


#define XFORWARDER_VER "xForwarder/" XSOCKS_VERSION

#ifdef VERSION
#define xForwarder_VER VERSION
#define xForwarder_BUILD_TIME BUILD_TIME
#else
#define xForwarder_VER "undefined"
#define xForwarder_BUILD_TIME ""
#endif

struct client_context {
int stage;
Expand Down Expand Up @@ -58,8 +63,8 @@ void close_loop(uv_loop_t *loop);
int verbose;
uint16_t idle_timeout;
char *dest_addr_buf;
struct sockaddr bind_addr;
struct sockaddr dest_addr;
struct sockaddr server_addr;
struct sockaddr_storage bind_addr;
struct sockaddr_storage dest_addr;
struct sockaddr_storage server_addr;

#endif // for #ifndef XFORWARDER_H
10 changes: 5 additions & 5 deletions src/xForwarder_remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ remote_connect_cb(uv_connect_t *req, int status) {
} else {
if (status != UV_ECANCELED) {
char addrbuf[INET6_ADDRSTRLEN + 1];
ip_name(&server_addr, addrbuf, sizeof(addrbuf));
ip_name((struct sockaddr *)&server_addr, addrbuf, sizeof(addrbuf));
logger_log(LOG_ERR, "connect to %s failed: %s", addrbuf, uv_strerror(status));
close_client(client);
close_remote(remote);
Expand Down Expand Up @@ -144,7 +144,7 @@ request_to_server(struct remote_context *remote) {
char buf[260] = {0};
struct client_context *client = remote->client;

struct sockaddr *addr = &dest_addr;
struct sockaddr *addr = (struct sockaddr *)&dest_addr;

/*
*
Expand Down Expand Up @@ -183,10 +183,10 @@ void
connect_to_remote(struct remote_context *remote) {
remote->stage = XSTAGE_CONNECT;
remote->connect_req.data = remote;
int rc = uv_tcp_connect(&remote->connect_req, &remote->handle.tcp, &server_addr, remote_connect_cb);
int rc = uv_tcp_connect(&remote->connect_req, &remote->handle.tcp, (struct sockaddr *)&server_addr, remote_connect_cb);
if (rc) {
char addrbuf[INET6_ADDRSTRLEN + 1];
ip_name(&server_addr, addrbuf, sizeof(addrbuf));
ip_name((struct sockaddr *)&server_addr, addrbuf, sizeof(addrbuf));
logger_log(LOG_ERR, "connect to %s error: %s", addrbuf, uv_strerror(rc));
close_client(remote->client);
close_remote(remote);
Expand Down Expand Up @@ -222,7 +222,7 @@ remote_recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
if (rc == PACKET_COMPLETED) {
int clen = packet->size;
int mlen = packet->size - PRIMITIVE_BYTES;
uint8_t *c = packet->buf, *m = packet->buf;
uint8_t *c = packet->buf, *m = packet->buf + PRIMITIVE_BYTES;

assert(mlen > 0 && mlen <= MAX_PACKET_SIZE - PRIMITIVE_BYTES);

Expand Down
14 changes: 7 additions & 7 deletions src/xSocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static const struct option _lopts[] = {

static void
print_usage(const char *prog) {
printf("xSocks Version: %s Maintained by lparam\n", XSOCKS_VER);
printf("xSocks Version: %s Maintained by lparam\n", xSocks_VER);
#ifdef _WIN32
printf("Usage: %s [-l local] <-s server> <-k password> [-hvV]\n\n", prog);
#else
Expand Down Expand Up @@ -95,7 +95,7 @@ parse_opts(int argc, char *argv[]) {
while ((opt = getopt_long(argc, argv, _optString, _lopts, &longindex)) != -1) {
switch (opt) {
case 'v':
printf("xSocks version: %s \n", XSOCKS_VER);
printf("%s %s\n", xSocks_VER, xSocks_BUILD_TIME);
exit(0);
break;
case 'h':
Expand Down Expand Up @@ -302,11 +302,11 @@ main(int argc, char *argv[]) {
struct server_context ctx;
ctx.udprelay = 1;
ctx.udp_fd = create_socket(SOCK_DGRAM, 0);
ctx.local_addr = &bind_addr;
ctx.server_addr = &server_addr;
ctx.local_addr = (struct sockaddr *)&bind_addr;
ctx.server_addr = (struct sockaddr *)&server_addr;

uv_tcp_init(loop, &ctx.tcp);
rc = uv_tcp_bind(&ctx.tcp, &bind_addr, 0);
rc = uv_tcp_bind(&ctx.tcp, (struct sockaddr *)&bind_addr, 0);
if (rc) {
logger_stderr("bind error: %s", uv_strerror(rc));
return 1;
Expand Down Expand Up @@ -339,8 +339,8 @@ main(int argc, char *argv[]) {
ctx->udp_fd = create_socket(SOCK_DGRAM, 1);
ctx->udprelay = 1;
ctx->accept_cb = client_accept_cb;
ctx->local_addr = &bind_addr;
ctx->server_addr = &server_addr;
ctx->local_addr = (struct sockaddr *)&bind_addr;
ctx->server_addr = (struct sockaddr *)&server_addr;
rc = uv_sem_init(&ctx->semaphore, 0);
rc = uv_thread_create(&ctx->thread_id, consumer_start, ctx);
}
Expand Down
4 changes: 2 additions & 2 deletions src/xSocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int vpn;
int acl;
int verbose;
uint16_t idle_timeout;
struct sockaddr bind_addr;
struct sockaddr server_addr;
struct sockaddr_storage bind_addr;
struct sockaddr_storage server_addr;

#endif // for #ifndef XSOCKS_H
5 changes: 2 additions & 3 deletions src/xSocks_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ request_start(struct client_context *client, char *req_buf) {
}

int direct = 0;
struct sockaddr addr;
memset(&addr, 0, sizeof addr);
struct sockaddr_storage addr;

#if !defined(_WIN32)
if ((acl && (req->atyp == 1 || req->atyp == 4) && acl_contains_ip(host))) {
Expand All @@ -250,7 +249,7 @@ request_start(struct client_context *client, char *req_buf) {
client->buflen = buflen;
memcpy(req_buf, buf, buflen);

struct remote_context *remote = new_remote(idle_timeout, direct ? &addr : NULL);
struct remote_context *remote = new_remote(idle_timeout, direct ? (struct sockaddr *)&addr : NULL);
remote->direct = direct;
remote->client = client;
client->remote = remote;
Expand Down
Loading

0 comments on commit d0ab4b7

Please sign in to comment.