Commit 7f73b7b7 authored by Max Lv's avatar Max Lv

enable quick sort

parent 39c70f65
...@@ -2,8 +2,9 @@ global { ...@@ -2,8 +2,9 @@ global {
perm_cache = 2048; perm_cache = 2048;
cache_dir = "/data/data/com.github.shadowsocks"; cache_dir = "/data/data/com.github.shadowsocks";
server_ip = 127.0.0.1; server_ip = 127.0.0.1;
server_port = 8053; server_port = 8153;
query_method = tcp_only; query_method = tcp_only;
run_ipv4 = on;
min_ttl = 15m; min_ttl = 15m;
max_ttl = 1w; max_ttl = 1w;
timeout = 10; timeout = 10;
......
No preview for this file type
APP_PLATFORM = anrdoid-7 APP_PLATFORM = anrdoid-8
#include <android/log.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 "encrypt.h" #include "encrypt.h"
#include "android.h"
#include <openssl/md5.h> #include <openssl/md5.h>
...@@ -33,37 +34,31 @@ int recv_decrypt(int sock, char *buf, int len, int flags) { ...@@ -33,37 +34,31 @@ int recv_decrypt(int sock, char *buf, int len, int flags) {
return result; 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)); 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 *table = encrypt_table;
unsigned char *tmp_hash; unsigned char *tmp_hash;
tmp_hash = MD5((const unsigned char*)key, strlen((const char*)key), NULL); tmp_hash = MD5((const unsigned char*)key, strlen(key), NULL);
unsigned long long a; _a = *(unsigned long long *)tmp_hash;
a = *(unsigned long long *)tmp_hash;
unsigned int i; unsigned int i;
LOGD("key hash: %lld", _a);
for(i = 0; i < 256; ++i) { for(i = 0; i < 256; ++i) {
table[i] = i; table[i] = i;
} }
for(i = 1; i < 1024; ++i) { for(i = 1; i < 1024; ++i) {
// use bubble sort in order to keep the array stable as in Python // use bubble sort in order to keep the array stable as in Python
int k,j;
unsigned char t; unsigned char t;
for(k = 256 - 2; k >= 0; --k) _i = i;
{ qsort(table, 256, sizeof(unsigned char), random_compare);
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;
}
}
}
} }
for(i = 0; i < 256; ++i) { for(i = 0; i < 256; ++i) {
// gen decrypt table from encrypt table // gen decrypt table from encrypt table
......
...@@ -7,9 +7,12 @@ ...@@ -7,9 +7,12 @@
unsigned char encrypt_table[256]; unsigned char encrypt_table[256];
unsigned char decrypt_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 encrypt(char *buf, int len);
void decrypt(char *buf, int len); void decrypt(char *buf, int len);
int send_encrypt(int sock, char *buf, int len, int flags); int send_encrypt(int sock, char *buf, int len, int flags);
int recv_decrypt(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;
...@@ -17,13 +17,10 @@ ...@@ -17,13 +17,10 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <android/log.h>
#include "local.h" #include "local.h"
#include "encrypt.h" #include "encrypt.h"
#include "android.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__))
#define REPLY "HTTP/1.1 200 OK\n\nhello" #define REPLY "HTTP/1.1 200 OK\n\nhello"
...@@ -60,7 +57,7 @@ int create_and_bind(const char *port) { ...@@ -60,7 +57,7 @@ int create_and_bind(const char *port) {
s = getaddrinfo("0.0.0.0", port, &hints, &result); s = getaddrinfo("0.0.0.0", port, &hints, &result);
if (s != 0) { if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); LOGD("getaddrinfo: %s", gai_strerror(s));
return -1; return -1;
} }
...@@ -83,7 +80,7 @@ int create_and_bind(const char *port) { ...@@ -83,7 +80,7 @@ int create_and_bind(const char *port) {
} }
if (rp == NULL) { if (rp == NULL) {
fprintf(stderr, "Could not bind\n"); LOGE("Could not bind");
return -1; return -1;
} }
...@@ -171,8 +168,8 @@ static void server_send_cb (EV_P_ ev_io *w, int revents) { ...@@ -171,8 +168,8 @@ static void server_send_cb (EV_P_ ev_io *w, int revents) {
return; return;
} }
if (r < server->buf_len) { if (r < server->buf_len) {
// printf("r=%d\n", r); LOGD("r=%d\n", r);
// printf("server->buf_len=%d\n", server->buf_len); LOGD("server->buf_len=%d\n", server->buf_len);
// partly sent, move memory, wait for the next time to send // partly sent, move memory, wait for the next time to send
char *pt; char *pt;
for (pt = server->buf; pt < pt + min(r, BUF_SIZE); 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) { ...@@ -204,7 +201,7 @@ static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
} }
while (1) { while (1) {
ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0); 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) { if (r == 0) {
// connection closed // connection closed
server->buf_len = 0; server->buf_len = 0;
...@@ -227,7 +224,7 @@ static void remote_recv_cb (EV_P_ ev_io *w, int revents) { ...@@ -227,7 +224,7 @@ static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
} }
decrypt(server->buf, r); decrypt(server->buf, r);
int w = send(server->fd, server->buf, r, MSG_NOSIGNAL); 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) { if(w == -1) {
perror("send"); perror("send");
if (errno == EAGAIN) { if (errno == EAGAIN) {
...@@ -329,7 +326,6 @@ struct remote* new_remote(int fd) { ...@@ -329,7 +326,6 @@ struct remote* new_remote(int fd) {
remote->recv_ctx->connected = 0; remote->recv_ctx->connected = 0;
remote->send_ctx->remote = remote; remote->send_ctx->remote = remote;
remote->send_ctx->connected = 0; remote->send_ctx->connected = 0;
fprintf(stderr, "new remote\n");
return remote; return remote;
} }
void free_remote(struct remote *remote) { void free_remote(struct remote *remote) {
...@@ -340,7 +336,6 @@ void free_remote(struct remote *remote) { ...@@ -340,7 +336,6 @@ void free_remote(struct remote *remote) {
free(remote->recv_ctx); free(remote->recv_ctx);
free(remote->send_ctx); free(remote->send_ctx);
free(remote); free(remote);
fprintf(stderr, "free remote\n");
} }
} }
void close_and_free_remote(EV_P_ struct remote *remote) { void close_and_free_remote(EV_P_ struct remote *remote) {
...@@ -363,7 +358,6 @@ struct server* new_server(int fd) { ...@@ -363,7 +358,6 @@ struct server* new_server(int fd) {
server->recv_ctx->connected = 0; server->recv_ctx->connected = 0;
server->send_ctx->server = server; server->send_ctx->server = server;
server->send_ctx->connected = 0; server->send_ctx->connected = 0;
fprintf(stderr, "new server\n");
return server; return server;
} }
void free_server(struct server *server) { void free_server(struct server *server) {
...@@ -374,7 +368,6 @@ void free_server(struct server *server) { ...@@ -374,7 +368,6 @@ void free_server(struct server *server) {
free(server->recv_ctx); free(server->recv_ctx);
free(server->send_ctx); free(server->send_ctx);
free(server); free(server);
fprintf(stderr, "free server\n");
} }
} }
void close_and_free_server(EV_P_ struct server *server) { void close_and_free_server(EV_P_ struct server *server) {
...@@ -432,7 +425,7 @@ int main (int argc, char **argv) ...@@ -432,7 +425,7 @@ int main (int argc, char **argv)
const char *server = argv[1]; const char *server = argv[1];
const char *remote_port = argv[2]; const char *remote_port = argv[2];
const char *port = argv[3]; const char *port = argv[3];
const char *key = argv[4]; const char *key = argv[4];
/* Our process ID and Session ID */ /* Our process ID and Session ID */
pid_t pid, sid; pid_t pid, sid;
...@@ -478,7 +471,7 @@ int main (int argc, char **argv) ...@@ -478,7 +471,7 @@ int main (int argc, char **argv)
_server = strdup(server); _server = strdup(server);
_remote_port = strdup(remote_port); _remote_port = strdup(remote_port);
fprintf(stderr, "calculating ciphers\n"); LOGD("calculating ciphers");
get_table(key); get_table(key);
int listenfd; int listenfd;
......
...@@ -95,7 +95,7 @@ public class ShadowsocksService extends Service { ...@@ -95,7 +95,7 @@ public class ShadowsocksService extends Service {
private static final int MSG_HOST_CHANGE = 4; private static final int MSG_HOST_CHANGE = 4;
private static final int MSG_STOP_SELF = 5; private static final int MSG_STOP_SELF = 5;
private static final String TAG = "ShadowsocksService"; 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[]{ private static final Class<?>[] mStartForegroundSignature = new Class[]{
int.class, Notification.class}; int.class, Notification.class};
private static final Class<?>[] mStopForegroundSignature = new Class[]{boolean.class}; private static final Class<?>[] mStopForegroundSignature = new Class[]{boolean.class};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment