diff --git a/assets/pdnsd.conf b/assets/pdnsd.conf index 98b48792..d78ecc36 100644 --- a/assets/pdnsd.conf +++ b/assets/pdnsd.conf @@ -2,8 +2,9 @@ global { perm_cache = 2048; cache_dir = "/data/data/com.github.shadowsocks"; server_ip = 127.0.0.1; - server_port = 8053; + server_port = 8153; query_method = tcp_only; + run_ipv4 = on; min_ttl = 15m; max_ttl = 1w; timeout = 10; diff --git a/assets/shadowsocks b/assets/shadowsocks index 453daa2c..465409d3 100755 Binary files a/assets/shadowsocks and b/assets/shadowsocks differ diff --git a/jni/Application.mk b/jni/Application.mk index 51a0863e..05e88a76 100644 --- a/jni/Application.mk +++ b/jni/Application.mk @@ -1 +1 @@ -APP_PLATFORM = anrdoid-7 +APP_PLATFORM = anrdoid-8 diff --git a/jni/shadowsocks/android.h b/jni/shadowsocks/android.h new file mode 100644 index 00000000..89939d49 --- /dev/null +++ b/jni/shadowsocks/android.h @@ -0,0 +1,5 @@ +#include + +#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "shadowsocks", __VA_ARGS__)) +#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "shadowsocks", __VA_ARGS__)) + diff --git a/jni/shadowsocks/encrypt.c b/jni/shadowsocks/encrypt.c index 107e4a48..2d6f4205 100755 --- a/jni/shadowsocks/encrypt.c +++ b/jni/shadowsocks/encrypt.c @@ -1,4 +1,5 @@ #include "encrypt.h" +#include "android.h" #include @@ -33,37 +34,31 @@ int recv_decrypt(int sock, char *buf, int len, int flags) { return result; } -inline int random_compare(unsigned char x, unsigned char y, unsigned int i, unsigned long long a) { +static int random_compare(const void *_x, const void *_y) { + unsigned int i = _i; + unsigned long long a = _a; + unsigned char x = *((unsigned char*) _x); + unsigned char y = *((unsigned char*) _y); return (a % (x + i) - a % (y + i)); } -void get_table(const unsigned char* key) { +void get_table(const char* key) { unsigned char *table = encrypt_table; unsigned char *tmp_hash; - tmp_hash = MD5((const unsigned char*)key, strlen((const char*)key), NULL); - unsigned long long a; - a = *(unsigned long long *)tmp_hash; + tmp_hash = MD5((const unsigned char*)key, strlen(key), NULL); + _a = *(unsigned long long *)tmp_hash; unsigned int i; + LOGD("key hash: %lld", _a); + for(i = 0; i < 256; ++i) { table[i] = i; } for(i = 1; i < 1024; ++i) { // use bubble sort in order to keep the array stable as in Python - int k,j; unsigned char t; - for(k = 256 - 2; k >= 0; --k) - { - for(j = 0;j <= k; ++j) - { - if(random_compare(table[j], table[j + 1], i, a) > 0) - { - t=table[j]; - table[j]=table[j + 1]; - table[j + 1]=t; - } - } - } + _i = i; + qsort(table, 256, sizeof(unsigned char), random_compare); } for(i = 0; i < 256; ++i) { // gen decrypt table from encrypt table diff --git a/jni/shadowsocks/encrypt.h b/jni/shadowsocks/encrypt.h index a4cc835e..700398d1 100755 --- a/jni/shadowsocks/encrypt.h +++ b/jni/shadowsocks/encrypt.h @@ -7,9 +7,12 @@ unsigned char encrypt_table[256]; unsigned char decrypt_table[256]; -void get_table(const unsigned char* key); +void get_table(const char* key); void encrypt(char *buf, int len); void decrypt(char *buf, int len); int send_encrypt(int sock, char *buf, int len, int flags); int recv_decrypt(int sock, char *buf, int len, int flags); +unsigned int _i; +unsigned long long _a; + diff --git a/jni/shadowsocks/local.c b/jni/shadowsocks/local.c index cc89c84f..761f5766 100644 --- a/jni/shadowsocks/local.c +++ b/jni/shadowsocks/local.c @@ -17,13 +17,10 @@ #include #include #include -#include #include "local.h" #include "encrypt.h" - -#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "shadowsocks", __VA_ARGS__)) -#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "shadowsocks", __VA_ARGS__)) +#include "android.h" #define REPLY "HTTP/1.1 200 OK\n\nhello" @@ -60,7 +57,7 @@ int create_and_bind(const char *port) { s = getaddrinfo("0.0.0.0", port, &hints, &result); if (s != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + LOGD("getaddrinfo: %s", gai_strerror(s)); return -1; } @@ -83,7 +80,7 @@ int create_and_bind(const char *port) { } if (rp == NULL) { - fprintf(stderr, "Could not bind\n"); + LOGE("Could not bind"); return -1; } @@ -171,8 +168,8 @@ static void server_send_cb (EV_P_ ev_io *w, int revents) { return; } if (r < server->buf_len) { - // printf("r=%d\n", r); - // printf("server->buf_len=%d\n", server->buf_len); + LOGD("r=%d\n", r); + LOGD("server->buf_len=%d\n", server->buf_len); // partly sent, move memory, wait for the next time to send char *pt; for (pt = server->buf; pt < pt + min(r, BUF_SIZE); pt++) { @@ -204,7 +201,7 @@ static void remote_recv_cb (EV_P_ ev_io *w, int revents) { } while (1) { ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0); - // printf("after recv: r=%d\n", r); + /*LOGD("after recv: r=%d\n", r);*/ if (r == 0) { // connection closed server->buf_len = 0; @@ -227,7 +224,7 @@ static void remote_recv_cb (EV_P_ ev_io *w, int revents) { } decrypt(server->buf, r); int w = send(server->fd, server->buf, r, MSG_NOSIGNAL); - // printf("after send: w=%d\n", w); + /*LOGD("after send: w=%d\n", w);*/ if(w == -1) { perror("send"); if (errno == EAGAIN) { @@ -329,7 +326,6 @@ struct remote* new_remote(int fd) { remote->recv_ctx->connected = 0; remote->send_ctx->remote = remote; remote->send_ctx->connected = 0; - fprintf(stderr, "new remote\n"); return remote; } void free_remote(struct remote *remote) { @@ -340,7 +336,6 @@ void free_remote(struct remote *remote) { free(remote->recv_ctx); free(remote->send_ctx); free(remote); - fprintf(stderr, "free remote\n"); } } void close_and_free_remote(EV_P_ struct remote *remote) { @@ -363,7 +358,6 @@ struct server* new_server(int fd) { server->recv_ctx->connected = 0; server->send_ctx->server = server; server->send_ctx->connected = 0; - fprintf(stderr, "new server\n"); return server; } void free_server(struct server *server) { @@ -374,7 +368,6 @@ void free_server(struct server *server) { free(server->recv_ctx); free(server->send_ctx); free(server); - fprintf(stderr, "free server\n"); } } void close_and_free_server(EV_P_ struct server *server) { @@ -432,7 +425,7 @@ int main (int argc, char **argv) const char *server = argv[1]; const char *remote_port = argv[2]; const char *port = argv[3]; - const char *key = argv[4]; + const char *key = argv[4]; /* Our process ID and Session ID */ pid_t pid, sid; @@ -478,7 +471,7 @@ int main (int argc, char **argv) _server = strdup(server); _remote_port = strdup(remote_port); - fprintf(stderr, "calculating ciphers\n"); + LOGD("calculating ciphers"); get_table(key); int listenfd; diff --git a/src/com/github/shadowsocks/ShadowsocksService.java b/src/com/github/shadowsocks/ShadowsocksService.java index 0675e4bf..6b105964 100755 --- a/src/com/github/shadowsocks/ShadowsocksService.java +++ b/src/com/github/shadowsocks/ShadowsocksService.java @@ -95,7 +95,7 @@ public class ShadowsocksService extends Service { private static final int MSG_HOST_CHANGE = 4; private static final int MSG_STOP_SELF = 5; private static final String TAG = "ShadowsocksService"; - private final static int DNS_PORT = 8053; + private final static int DNS_PORT = 8153; private static final Class[] mStartForegroundSignature = new Class[]{ int.class, Notification.class}; private static final Class[] mStopForegroundSignature = new Class[]{boolean.class};