build passed

This commit is contained in:
Max Lv
2013-05-23 11:16:45 +08:00
parent 2b6e884354
commit 2d4940d3a2
28 changed files with 7974 additions and 3936 deletions
+3 -3
View File
@@ -1,9 +1,9 @@
bin_PROGRAMS = ss-local ss-server
ss_local_SOURCES = utils.c jconf.c json.c \
rc4.c md5.c encrypt.c \
encrypt.c \
local.c
ss_server_SOURCES = utils.c jconf.c json.c \
rc4.c md5.c encrypt.c \
encrypt.c \
server.c
ss_local_LDADD = $(top_builddir)/libev/libev.la
ss_server_LDADD = $(top_builddir)/libev/libev.la
@@ -12,7 +12,7 @@ ss_server_LDADD += $(top_builddir)/libasyncns/libasyncns.la
if BUILD_REDIRECTOR
bin_PROGRAMS += ss-redir
ss_redir_SOURCES = utils.c jconf.c json.c \
rc4.c md5.c encrypt.c \
encrypt.c \
redir.c
ss_redir_LDADD = $(top_builddir)/libev/libev.la
endif
+50 -22
View File
@@ -1,9 +1,9 @@
# Makefile.in generated by automake 1.11.1 from Makefile.am.
# Makefile.in generated by automake 1.11.6 from Makefile.am.
# @configure_input@
# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation,
# Inc.
# 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software
# Foundation, Inc.
# This Makefile.in is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
@@ -16,6 +16,23 @@
@SET_MAKE@
VPATH = @srcdir@
am__make_dryrun = \
{ \
am__dry=no; \
case $$MAKEFLAGS in \
*\\[\ \ ]*) \
echo 'am--echo: ; @echo "AM" OK' | $(MAKE) -f - 2>/dev/null \
| grep '^AM OK$$' >/dev/null || am__dry=yes;; \
*) \
for am__flg in $$MAKEFLAGS; do \
case $$am__flg in \
*=*|--*) ;; \
*n*) am__dry=yes; break;; \
esac; \
done;; \
esac; \
test $$am__dry = yes; \
}
pkgdatadir = $(datadir)/@PACKAGE@
pkgincludedir = $(includedir)/@PACKAGE@
pkglibdir = $(libdir)/@PACKAGE@
@@ -54,20 +71,18 @@ CONFIG_CLEAN_VPATH_FILES =
am__installdirs = "$(DESTDIR)$(bindir)"
PROGRAMS = $(bin_PROGRAMS)
am_ss_local_OBJECTS = utils.$(OBJEXT) jconf.$(OBJEXT) json.$(OBJEXT) \
rc4.$(OBJEXT) md5.$(OBJEXT) encrypt.$(OBJEXT) local.$(OBJEXT)
encrypt.$(OBJEXT) local.$(OBJEXT)
ss_local_OBJECTS = $(am_ss_local_OBJECTS)
ss_local_DEPENDENCIES = $(top_builddir)/libev/libev.la
am__ss_redir_SOURCES_DIST = utils.c jconf.c json.c rc4.c md5.c \
encrypt.c redir.c
am__ss_redir_SOURCES_DIST = utils.c jconf.c json.c encrypt.c redir.c
@BUILD_REDIRECTOR_TRUE@am_ss_redir_OBJECTS = utils.$(OBJEXT) \
@BUILD_REDIRECTOR_TRUE@ jconf.$(OBJEXT) json.$(OBJEXT) \
@BUILD_REDIRECTOR_TRUE@ rc4.$(OBJEXT) md5.$(OBJEXT) \
@BUILD_REDIRECTOR_TRUE@ encrypt.$(OBJEXT) redir.$(OBJEXT)
ss_redir_OBJECTS = $(am_ss_redir_OBJECTS)
@BUILD_REDIRECTOR_TRUE@ss_redir_DEPENDENCIES = \
@BUILD_REDIRECTOR_TRUE@ $(top_builddir)/libev/libev.la
am_ss_server_OBJECTS = utils.$(OBJEXT) jconf.$(OBJEXT) json.$(OBJEXT) \
rc4.$(OBJEXT) md5.$(OBJEXT) encrypt.$(OBJEXT) server.$(OBJEXT)
encrypt.$(OBJEXT) server.$(OBJEXT)
ss_server_OBJECTS = $(am_ss_server_OBJECTS)
ss_server_DEPENDENCIES = $(top_builddir)/libev/libev.la \
$(top_builddir)/libasyncns/libasyncns.la
@@ -87,6 +102,11 @@ LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
SOURCES = $(ss_local_SOURCES) $(ss_redir_SOURCES) $(ss_server_SOURCES)
DIST_SOURCES = $(ss_local_SOURCES) $(am__ss_redir_SOURCES_DIST) \
$(ss_server_SOURCES)
am__can_run_installinfo = \
case $$AM_UPDATE_INFO_DIR in \
n|no|NO) false;; \
*) (install-info --version) >/dev/null 2>&1;; \
esac
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@@ -105,6 +125,7 @@ CPPFLAGS = @CPPFLAGS@
CYGPATH_W = @CYGPATH_W@
DEFS = @DEFS@
DEPDIR = @DEPDIR@
DLLTOOL = @DLLTOOL@
DSYMUTIL = @DSYMUTIL@
DUMPBIN = @DUMPBIN@
ECHO_C = @ECHO_C@
@@ -128,6 +149,7 @@ LIPO = @LIPO@
LN_S = @LN_S@
LTLIBOBJS = @LTLIBOBJS@
MAKEINFO = @MAKEINFO@
MANIFEST_TOOL = @MANIFEST_TOOL@
MKDIR_P = @MKDIR_P@
NM = @NM@
NMEDIT = @NMEDIT@
@@ -156,6 +178,7 @@ abs_builddir = @abs_builddir@
abs_srcdir = @abs_srcdir@
abs_top_builddir = @abs_top_builddir@
abs_top_srcdir = @abs_top_srcdir@
ac_ct_AR = @ac_ct_AR@
ac_ct_CC = @ac_ct_CC@
ac_ct_DUMPBIN = @ac_ct_DUMPBIN@
acx_pthread_config = @acx_pthread_config@
@@ -189,7 +212,6 @@ libdir = @libdir@
libexecdir = @libexecdir@
localedir = @localedir@
localstatedir = @localstatedir@
lt_ECHO = @lt_ECHO@
mandir = @mandir@
mkdir_p = @mkdir_p@
oldincludedir = @oldincludedir@
@@ -206,18 +228,18 @@ top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
ss_local_SOURCES = utils.c jconf.c json.c \
rc4.c md5.c encrypt.c \
encrypt.c \
local.c
ss_server_SOURCES = utils.c jconf.c json.c \
rc4.c md5.c encrypt.c \
encrypt.c \
server.c
ss_local_LDADD = $(top_builddir)/libev/libev.la
ss_server_LDADD = $(top_builddir)/libev/libev.la \
$(top_builddir)/libasyncns/libasyncns.la
@BUILD_REDIRECTOR_TRUE@ss_redir_SOURCES = utils.c jconf.c json.c \
@BUILD_REDIRECTOR_TRUE@ rc4.c md5.c encrypt.c \
@BUILD_REDIRECTOR_TRUE@ encrypt.c \
@BUILD_REDIRECTOR_TRUE@ redir.c
@BUILD_REDIRECTOR_TRUE@ss_redir_LDADD = $(top_builddir)/libev/libev.la
@@ -260,8 +282,11 @@ $(ACLOCAL_M4): $(am__aclocal_m4_deps)
$(am__aclocal_m4_deps):
install-binPROGRAMS: $(bin_PROGRAMS)
@$(NORMAL_INSTALL)
test -z "$(bindir)" || $(MKDIR_P) "$(DESTDIR)$(bindir)"
@list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \
if test -n "$$list"; then \
echo " $(MKDIR_P) '$(DESTDIR)$(bindir)'"; \
$(MKDIR_P) "$(DESTDIR)$(bindir)" || exit 1; \
fi; \
for p in $$list; do echo "$$p $$p"; done | \
sed 's/$(EXEEXT)$$//' | \
while read p p1; do if test -f $$p || test -f $$p1; \
@@ -301,13 +326,13 @@ clean-binPROGRAMS:
list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \
echo " rm -f" $$list; \
rm -f $$list
ss-local$(EXEEXT): $(ss_local_OBJECTS) $(ss_local_DEPENDENCIES)
ss-local$(EXEEXT): $(ss_local_OBJECTS) $(ss_local_DEPENDENCIES) $(EXTRA_ss_local_DEPENDENCIES)
@rm -f ss-local$(EXEEXT)
$(LINK) $(ss_local_OBJECTS) $(ss_local_LDADD) $(LIBS)
ss-redir$(EXEEXT): $(ss_redir_OBJECTS) $(ss_redir_DEPENDENCIES)
ss-redir$(EXEEXT): $(ss_redir_OBJECTS) $(ss_redir_DEPENDENCIES) $(EXTRA_ss_redir_DEPENDENCIES)
@rm -f ss-redir$(EXEEXT)
$(LINK) $(ss_redir_OBJECTS) $(ss_redir_LDADD) $(LIBS)
ss-server$(EXEEXT): $(ss_server_OBJECTS) $(ss_server_DEPENDENCIES)
ss-server$(EXEEXT): $(ss_server_OBJECTS) $(ss_server_DEPENDENCIES) $(EXTRA_ss_server_DEPENDENCIES)
@rm -f ss-server$(EXEEXT)
$(LINK) $(ss_server_OBJECTS) $(ss_server_LDADD) $(LIBS)
@@ -321,8 +346,6 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/jconf.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/json.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/local.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/md5.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rc4.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/redir.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/server.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/utils.Po@am__quote@
@@ -453,10 +476,15 @@ install-am: all-am
installcheck: installcheck-am
install-strip:
$(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
`test -z '$(STRIP)' || \
echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install
if test -z '$(STRIP)'; then \
$(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
install; \
else \
$(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
"INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \
fi
mostlyclean-generic:
clean-generic:
+20 -27
View File
@@ -3,30 +3,27 @@
#endif
#include <stdint.h>
#include <openssl/md5.h>
#include "encrypt.h"
#include "utils.h"
#define OFFSET_ROL(p, o) ((uint64_t)(*(p + o)) << (8 * o))
static char *enc_table;
static char *dec_table;
static void* supported_ciphers = {
NULL,
EVP_rc4,
EVP_aes_128_cfb,
EVP_aes_192_cfb,
EVP_aes_256_cfb,
EVP_bf_cfb,
EVP_cast5_cvb,
EVP_des_cfb
};
static char *pass;
static void md5(const uint8_t *text, uint8_t *digest) {
md5_state_t state;
md5_init(&state);
md5_append(&state, text, strlen((char*)text));
md5_finish(&state, digest);
}
static char* supported_ciphers[8] = {
"table",
"rc4",
"aes-128-cfb",
"aes-192-cfb",
"aes-256-cfb",
"bf-cfb",
"cast5-cfb",
"des-cfb"
};
static int random_compare(const void *_x, const void *_y, uint32_t i, uint64_t a) {
uint8_t x = *((uint8_t *) _x);
@@ -104,12 +101,12 @@ void enc_table_init(const char *pass) {
uint32_t i;
uint32_t salt;
uint64_t key = 0;
uint8_t digest[16];
uint8_t *digest;
enc_table = malloc(256);
dec_table = malloc(256);
md5((const uint8_t*)pass, digest);
digest = MD5((const uint8_t *)key, strlen((const uint8_t *)key), NULL);
for (i = 0; i < 8; i++) {
key += OFFSET_ROL(digest, i);
@@ -130,9 +127,7 @@ void enc_table_init(const char *pass) {
char* encrypt(char *plaintext, int *len, struct enc_ctx *ctx) {
if (ctx != NULL) {
/* max ciphertext len for a n bytes of plaintext is n + AES_BLOCK_SIZE
* -1 bytes */
int c_len = *len + AES_BLOCK_SIZE;
int c_len = *len + BLOCK_SIZE;
uint8_t *ciphertext;
int iv_len = 0;
@@ -161,9 +156,7 @@ char* encrypt(char *plaintext, int *len, struct enc_ctx *ctx) {
char* decrypt(char *ciphertext, int *len, struct enc_ctx *ctx) {
if (ctx != NULL) {
/* max ciphertext len for a n bytes of plaintext is n + AES_BLOCK_SIZE
* -1 bytes */
int p_len = *len + AES_BLOCK_SIZE;
int p_len = *len + BLOCK_SIZE;
uint8_t *plaintext = malloc(p_len);
int iv_len = 0;
@@ -191,9 +184,9 @@ char* decrypt(char *ciphertext, int *len, struct enc_ctx *ctx) {
void enc_ctx_init(int method, struct enc_ctx *ctx, int enc) {
uint8_t key[EVP_MAX_KEY_LENGTH] = {0};
uint8_t iv[EVP_MAX_IV_LENGTH] = {0};
int key_len;
int key_len, i;
EVP_CIPHER *cipher = (*supported_ciphers[method])();
EVP_CIPHER *cipher = EVP_get_cipherbyname(supported_ciphers[method]);
key_len = EVP_BytesToKey(cipher, EVP_md5(), NULL, (uint8_t *)pass,
strlen(pass), 1, key, iv);
@@ -207,7 +200,6 @@ void enc_ctx_init(int method, struct enc_ctx *ctx, int enc) {
EVP_CIPHER_CTX_set_padding(ctx->evp, 1);
if (enc) {
int i;
EVP_CipherInit_ex(ctx->evp, NULL, NULL, key, iv, enc);
}
@@ -219,6 +211,7 @@ void enc_ctx_init(int method, struct enc_ctx *ctx, int enc) {
}
int enc_init(const char *pass, const char *method) {
pass = pass;
if (method == NULL || strcmp(method, "table") == 0) {
enc_table_init(pass);
return TABLE;
+3 -4
View File
@@ -16,9 +16,8 @@
#include <inttypes.h>
#endif
#include "md5.h"
#define BUF_SIZE 4096
#define BLOCK_SIZE 32
#define NONE -1
#define TABLE 0
@@ -36,8 +35,8 @@ struct enc_ctx {
EVP_CIPHER_CTX *evp;
};
char* encrypt(char *buf, int len, EVP_CIPHER_CTX *ctx);
char* decrypt(char *buf, int len, EVP_CIPHER_CTX *ctx);
char* encrypt(char *plaintext, int *len, struct enc_ctx *ctx);
char* decrypt(char *ciphertext, int *len, struct enc_ctx *ctx);
void enc_ctx_init(int method, struct enc_ctx *ctx, int enc);
int enc_init(const char *pass, const char *method);
+8 -8
View File
@@ -140,7 +140,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
// local socks5 server
if (server->stage == 5) {
remote->buf = encrypt(remote->buf, r, server->e_ctx);
remote->buf = encrypt(remote->buf, &r, server->e_ctx);
int s = send(remote->fd, remote->buf, r, 0);
if(s == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
@@ -188,7 +188,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
return;
}
char *addr_to_send = malloc(256);
char *addr_to_send = malloc(BUF_SIZE);
uint8_t addr_len = 0;
addr_to_send[addr_len++] = request->atyp;
@@ -223,7 +223,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
return;
}
addr_to_send = encrypt(addr_to_send, addr_len, server->e_ctx);
addr_to_send = encrypt(addr_to_send, &addr_len, server->e_ctx);
int s = send(remote->fd, addr_to_send, addr_len, 0);
free(addr_to_send);
@@ -354,7 +354,7 @@ static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
}
}
server->buf = decrypt(server->buf, r, server->d_ctx);
server->buf = decrypt(server->buf, &r, server->d_ctx);
int s = send(server->fd, server->buf, r, 0);
if (s == -1) {
@@ -447,7 +447,7 @@ static void remote_send_cb (EV_P_ ev_io *w, int revents) {
struct remote* new_remote(int fd, int timeout) {
struct remote *remote;
remote = malloc(sizeof(struct remote));
remote->buf = malloc(256);
remote->buf = malloc(BUF_SIZE);
remote->recv_ctx = malloc(sizeof(struct remote_ctx));
remote->send_ctx = malloc(sizeof(struct remote_ctx));
remote->fd = fd;
@@ -488,7 +488,7 @@ void close_and_free_remote(EV_P_ struct remote *remote) {
struct server* new_server(int fd, int method) {
struct server *server;
server = malloc(sizeof(struct server));
server->buf = malloc(256);
server->buf = malloc(BUF_SIZE);
server->recv_ctx = malloc(sizeof(struct server_ctx));
server->send_ctx = malloc(sizeof(struct server_ctx));
server->fd = fd;
@@ -519,11 +519,11 @@ void free_server(struct server *server) {
server->remote->server = NULL;
}
if (server->e_ctx != NULL) {
EVP_CIPHER_CTX_cleanup(server->e_ctx);
EVP_CIPHER_CTX_cleanup(server->e_ctx->evp);
free(server->e_ctx);
}
if (server->d_ctx != NULL) {
EVP_CIPHER_CTX_cleanup(server->d_ctx);
EVP_CIPHER_CTX_cleanup(server->d_ctx->evp);
free(server->d_ctx);
}
free(server->buf);
+4 -4
View File
@@ -8,9 +8,9 @@
struct listen_ctx {
ev_io io;
char **remote_host;
int remote_num;
char *remote_port;
char *iface;
int remote_num;
int method;
int timeout;
int fd;
@@ -25,10 +25,10 @@ struct server_ctx {
struct server {
int fd;
char *buf; // server send from, remote recv into
char stage;
int buf_len;
int buf_idx;
char *buf; // server send from, remote recv into
char stage;
struct enc_ctx *e_ctx;
struct enc_ctx *d_ctx;
struct server_ctx *recv_ctx;
@@ -45,9 +45,9 @@ struct remote_ctx {
struct remote {
int fd;
char buf[BUF_SIZE]; // remote send from, server recv into
int buf_len;
int buf_idx;
char *buf; // remote send from, server recv into
struct remote_ctx *recv_ctx;
struct remote_ctx *send_ctx;
struct server *server;
+40 -26
View File
@@ -72,10 +72,11 @@ int create_and_bind(const char *port) {
continue;
int opt = 1;
int err = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (err) {
ERROR("setsocket");
}
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
setsockopt(listen_sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
#ifdef SO_NOSIGPIPE
setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
#endif
s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen);
if (s == 0) {
@@ -132,7 +133,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
}
}
encrypt_ctx(remote->buf, r, server->e_ctx);
remote->buf = encrypt(remote->buf, &r, server->e_ctx);
int s = send(remote->fd, remote->buf, r, 0);
if(s == -1) {
@@ -252,7 +253,7 @@ static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
}
}
decrypt_ctx(server->buf, r, server->d_ctx);
server->buf = decrypt(server->buf, &r, server->d_ctx);
int s = send(server->fd, server->buf, r, 0);
if (s == -1) {
@@ -294,7 +295,7 @@ static void remote_send_cb (EV_P_ ev_io *w, int revents) {
ev_timer_stop(EV_A_ &remote_send_ctx->watcher);
// send destaddr
char addr_to_send[256];
char *addr_to_send = malloc(BUF_SIZE);
uint8_t addr_len = 0;
addr_to_send[addr_len++] = 1;
@@ -304,9 +305,11 @@ static void remote_send_cb (EV_P_ ev_io *w, int revents) {
addr_len += in_addr_len;
memcpy(addr_to_send + addr_len, &server->destaddr.sin_port, 2);
addr_len += 2;
encrypt_ctx(addr_to_send, addr_len, server->e_ctx);
addr_to_send = encrypt(addr_to_send, &addr_len, server->e_ctx);
int s = send(remote->fd, addr_to_send, addr_len, 0);
free(addr_to_send);
if (s < addr_len) {
LOGE("failed to send remote addr.");
close_and_free_remote(EV_A_ remote);
@@ -368,6 +371,7 @@ static void remote_send_cb (EV_P_ ev_io *w, int revents) {
struct remote* new_remote(int fd, int timeout) {
struct remote *remote;
remote = malloc(sizeof(struct remote));
remote->buf = malloc(BUF_SIZE);
remote->recv_ctx = malloc(sizeof(struct remote_ctx));
remote->send_ctx = malloc(sizeof(struct remote_ctx));
remote->fd = fd;
@@ -388,6 +392,7 @@ void free_remote(struct remote *remote) {
if (remote->server != NULL) {
remote->server->remote = NULL;
}
free(remote->buf);
free(remote->recv_ctx);
free(remote->send_ctx);
free(remote);
@@ -404,9 +409,10 @@ void close_and_free_remote(EV_P_ struct remote *remote) {
}
}
struct server* new_server(int fd) {
struct server* new_server(int fd, int method) {
struct server *server;
server = malloc(sizeof(struct server));
server->buf = malloc(BUF_SIZE);
server->recv_ctx = malloc(sizeof(struct server_ctx));
server->send_ctx = malloc(sizeof(struct server_ctx));
server->fd = fd;
@@ -416,11 +422,11 @@ struct server* new_server(int fd) {
server->recv_ctx->connected = 0;
server->send_ctx->server = server;
server->send_ctx->connected = 0;
if (enc_conf.method == RC4) {
server->e_ctx = malloc(sizeof(struct rc4_state));
server->d_ctx = malloc(sizeof(struct rc4_state));
enc_ctx_init(server->e_ctx, 1);
enc_ctx_init(server->d_ctx, 0);
if (method) {
server->e_ctx = malloc(sizeof(struct enc_ctx));
server->d_ctx = malloc(sizeof(struct enc_ctx));
enc_ctx_init(method, server->e_ctx, 1);
enc_ctx_init(method, server->d_ctx, 0);
} else {
server->e_ctx = NULL;
server->d_ctx = NULL;
@@ -435,10 +441,15 @@ void free_server(struct server *server) {
if (server->remote != NULL) {
server->remote->server = NULL;
}
if (enc_conf.method == RC4) {
if (server->e_ctx != NULL) {
EVP_CIPHER_CTX_cleanup(server->e_ctx->evp);
free(server->e_ctx);
}
if (server->d_ctx != NULL) {
EVP_CIPHER_CTX_cleanup(server->d_ctx->evp);
free(server->d_ctx);
}
free(server->buf);
free(server->recv_ctx);
free(server->send_ctx);
free(server);
@@ -472,6 +483,11 @@ static void accept_cb (EV_P_ ev_io *w, int revents) {
}
setnonblocking(clientfd);
int opt = 1;
setsockopt(clientfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
#ifdef SO_NOSIGPIPE
setsockopt(clientfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
#endif
struct addrinfo hints, *res;
int sockfd;
@@ -493,18 +509,15 @@ static void accept_cb (EV_P_ ev_io *w, int revents) {
return;
}
struct timeval timeout;
timeout.tv_sec = listener->timeout;
timeout.tv_usec = 0;
err = setsockopt(sockfd, SOL_SOCKET,
SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
if (err) ERROR("setsockopt");
err = setsockopt(sockfd, SOL_SOCKET,
SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
if (err) ERROR("setsockopt");
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
#ifdef SO_NOSIGPIPE
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
#endif
// Setup
setnonblocking(sockfd);
struct server *server = new_server(clientfd);
struct server *server = new_server(clientfd, listener->method);
struct remote *remote = new_remote(sockfd, listener->timeout);
server->remote = remote;
remote->server = server;
@@ -601,7 +614,7 @@ int main (int argc, char **argv) {
// Setup keys
LOGD("calculating ciphers...");
enc_conf_init(password, method);
int m = enc_init(password, method);
// Setup socket
int listenfd;
@@ -626,6 +639,7 @@ int main (int argc, char **argv) {
listen_ctx.remote_port = remote_port;
listen_ctx.timeout = atoi(timeout);
listen_ctx.fd = listenfd;
listen_ctx.method = m;
struct ev_loop *loop = ev_default_loop(0);
if (!loop) {
+7 -6
View File
@@ -8,10 +8,11 @@
struct listen_ctx {
ev_io io;
char **remote_host;
int remote_num;
char *remote_port;
int remote_num;
int timeout;
int fd;
int method;
struct sockaddr sock;
};
@@ -23,12 +24,12 @@ struct server_ctx {
struct server {
int fd;
char buf[BUF_SIZE]; // server send from, remote recv into
int buf_len;
int buf_idx;
char *buf; // server send from, remote recv into
struct sockaddr_in destaddr;
struct rc4_state *e_ctx;
struct rc4_state *d_ctx;
struct enc_ctx *e_ctx;
struct enc_ctx *d_ctx;
struct server_ctx *recv_ctx;
struct server_ctx *send_ctx;
struct remote *remote;
@@ -43,9 +44,9 @@ struct remote_ctx {
struct remote {
int fd;
char buf[BUF_SIZE]; // remote send from, server recv into
int buf_len;
int buf_idx;
char *buf; // remote send from, server recv into
struct remote_ctx *recv_ctx;
struct remote_ctx *send_ctx;
struct server *server;
@@ -60,7 +61,7 @@ static void remote_send_cb (EV_P_ ev_io *w, int revents);
struct remote* new_remote(int fd, int timeout);
void free_remote(struct remote *remote);
void close_and_free_remote(EV_P_ struct remote *remote);
struct server* new_server(int fd);
struct server* new_server(int fd, int method);
void free_server(struct server *server);
void close_and_free_server(EV_P_ struct server *server);
+18 -9
View File
@@ -174,7 +174,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
}
}
decrypt_ctx(buf, r, server->d_ctx);
buf = decrypt(buf, &r, server->d_ctx);
// handshake and transmit data
if (server->stage == 5) {
@@ -482,7 +482,7 @@ static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
}
}
encrypt_ctx(server->buf, r, server->e_ctx);
server->buf = encrypt(server->buf, &r, server->e_ctx);
int s = send(server->fd, server->buf, r, 0);
if (s == -1) {
@@ -596,6 +596,7 @@ struct remote* new_remote(int fd) {
remote_conn++;
struct remote *remote;
remote = malloc(sizeof(struct remote));
remote->buf = malloc(BUF_SIZE);
remote->recv_ctx = malloc(sizeof(struct remote_ctx));
remote->send_ctx = malloc(sizeof(struct remote_ctx));
remote->fd = fd;
@@ -617,6 +618,7 @@ void free_remote(struct remote *remote) {
if (remote->server != NULL) {
remote->server->remote = NULL;
}
free(remote->buf);
free(remote->recv_ctx);
free(remote->send_ctx);
free(remote);
@@ -639,6 +641,7 @@ struct server* new_server(int fd, struct listen_ctx *listener) {
server_conn++;
struct server *server;
server = malloc(sizeof(struct server));
server->buf = malloc(BUF_SIZE);
server->recv_ctx = malloc(sizeof(struct server_ctx));
server->send_ctx = malloc(sizeof(struct server_ctx));
server->fd = fd;
@@ -653,11 +656,11 @@ struct server* new_server(int fd, struct listen_ctx *listener) {
server->stage = 0;
server->query = NULL;
server->listen_ctx = listener;
if (enc_conf.method == RC4) {
server->e_ctx = malloc(sizeof(struct rc4_state));
server->d_ctx = malloc(sizeof(struct rc4_state));
enc_ctx_init(server->e_ctx, 1);
enc_ctx_init(server->d_ctx, 0);
if (listener->method) {
server->e_ctx = malloc(sizeof(struct enc_ctx));
server->d_ctx = malloc(sizeof(struct enc_ctx));
enc_ctx_init(listener->method, server->e_ctx, 1);
enc_ctx_init(listener->method, server->d_ctx, 0);
} else {
server->e_ctx = NULL;
server->d_ctx = NULL;
@@ -674,10 +677,15 @@ void free_server(struct server *server) {
if (server->remote != NULL) {
server->remote->server = NULL;
}
if (enc_conf.method == RC4) {
if (server->e_ctx != NULL) {
EVP_CIPHER_CTX_cleanup(server->e_ctx->evp);
free(server->e_ctx);
}
if (server->d_ctx != NULL) {
EVP_CIPHER_CTX_cleanup(server->d_ctx->evp);
free(server->d_ctx);
}
free(server->buf);
free(server->recv_ctx);
free(server->send_ctx);
free(server);
@@ -813,7 +821,7 @@ int main (int argc, char **argv) {
// Setup keys
LOGD("calculating ciphers...");
enc_conf_init(password, method);
int m = enc_init(password, method);
// Inilitialize ev loop
struct ev_loop *loop = EV_DEFAULT;
@@ -840,6 +848,7 @@ int main (int argc, char **argv) {
listen_ctx.timeout = atoi(timeout);
listen_ctx.asyncns = asyncns;
listen_ctx.fd = listenfd;
listen_ctx.method = m;
listen_ctx.iface = iface;
ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ);
+6 -5
View File
@@ -12,6 +12,7 @@ struct listen_ctx {
ev_io io;
int fd;
int timeout;
int method;
char *iface;
asyncns_t *asyncns;
struct sockaddr sock;
@@ -26,12 +27,12 @@ struct server_ctx {
struct server {
int fd;
char buf[BUF_SIZE]; // server send from, remote recv into
char stage;
int buf_len;
int buf_idx;
struct rc4_state *e_ctx;
struct rc4_state *d_ctx;
char *buf; // server send from, remote recv into
char stage;
struct enc_ctx *e_ctx;
struct enc_ctx *d_ctx;
struct server_ctx *recv_ctx;
struct server_ctx *send_ctx;
struct listen_ctx *listen_ctx;
@@ -47,9 +48,9 @@ struct remote_ctx {
struct remote {
int fd;
char buf[BUF_SIZE]; // remote send from, server recv into
int buf_len;
int buf_idx;
char *buf; // remote send from, server recv into
struct remote_ctx *recv_ctx;
struct remote_ctx *send_ctx;
struct server *server;