From 87aaeb34cc69cf7ecb2425c149f82ae10edd9cb2 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Thu, 27 Dec 2012 18:38:46 +0800 Subject: [PATCH] almost works --- shadowsocks-iOS.xcodeproj/project.pbxproj | 8 +- shadowsocks-iOS/encrypt.c | 24 +++-- shadowsocks-iOS/local.h | 2 + shadowsocks-iOS/{local.c => local.m} | 105 ++++------------------ shadowsocks-iOS/main.m | 4 +- 5 files changed, 42 insertions(+), 101 deletions(-) rename shadowsocks-iOS/{local.c => local.m} (86%) diff --git a/shadowsocks-iOS.xcodeproj/project.pbxproj b/shadowsocks-iOS.xcodeproj/project.pbxproj index fc55c23..4ac8453 100644 --- a/shadowsocks-iOS.xcodeproj/project.pbxproj +++ b/shadowsocks-iOS.xcodeproj/project.pbxproj @@ -19,7 +19,7 @@ 62F3C304168C35C9006D77BC /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F3C303168C35C9006D77BC /* ViewController.m */; }; 62F3C307168C35C9006D77BC /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 62F3C305168C35C9006D77BC /* ViewController.xib */; }; 62F3C461168C411F006D77BC /* encrypt.c in Sources */ = {isa = PBXBuildFile; fileRef = 62F3C45C168C411F006D77BC /* encrypt.c */; }; - 62F3C462168C411F006D77BC /* local.c in Sources */ = {isa = PBXBuildFile; fileRef = 62F3C45E168C411F006D77BC /* local.c */; }; + 62F3C462168C411F006D77BC /* local.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F3C45E168C411F006D77BC /* local.m */; }; 62F3C48C168C488B006D77BC /* ev.c in Sources */ = {isa = PBXBuildFile; fileRef = 62F3C48A168C488B006D77BC /* ev.c */; }; /* End PBXBuildFile section */ @@ -42,7 +42,7 @@ 62F3C306168C35C9006D77BC /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController.xib; sourceTree = ""; }; 62F3C45C168C411F006D77BC /* encrypt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = encrypt.c; sourceTree = ""; }; 62F3C45D168C411F006D77BC /* encrypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = encrypt.h; sourceTree = ""; }; - 62F3C45E168C411F006D77BC /* local.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = local.c; sourceTree = ""; }; + 62F3C45E168C411F006D77BC /* local.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = local.m; sourceTree = ""; }; 62F3C45F168C411F006D77BC /* local.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = local.h; sourceTree = ""; }; 62F3C460168C411F006D77BC /* socks5.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = socks5.h; sourceTree = ""; }; 62F3C48A168C488B006D77BC /* ev.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ev.c; path = "shadowsocks-iOS/libev/ev.c"; sourceTree = ""; }; @@ -95,7 +95,7 @@ children = ( 62F3C45C168C411F006D77BC /* encrypt.c */, 62F3C45D168C411F006D77BC /* encrypt.h */, - 62F3C45E168C411F006D77BC /* local.c */, + 62F3C45E168C411F006D77BC /* local.m */, 62F3C45F168C411F006D77BC /* local.h */, 62F3C460168C411F006D77BC /* socks5.h */, 62F3C2F9168C35C9006D77BC /* AppDelegate.h */, @@ -200,7 +200,7 @@ 62F3C2FB168C35C9006D77BC /* AppDelegate.m in Sources */, 62F3C304168C35C9006D77BC /* ViewController.m in Sources */, 62F3C461168C411F006D77BC /* encrypt.c in Sources */, - 62F3C462168C411F006D77BC /* local.c in Sources */, + 62F3C462168C411F006D77BC /* local.m in Sources */, 62F3C48C168C488B006D77BC /* ev.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/shadowsocks-iOS/encrypt.c b/shadowsocks-iOS/encrypt.c index 8bc935b..8f890a4 100755 --- a/shadowsocks-iOS/encrypt.c +++ b/shadowsocks-iOS/encrypt.c @@ -33,14 +33,11 @@ int recv_decrypt(int sock, char *buf, int len, int flags) { return result; } -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); +int random_compare(unsigned char x, unsigned char y, unsigned int i, unsigned long long a) { return (a % (x + i) - a % (y + i)); } + void get_table(const char* key) { unsigned char *table = encrypt_table; unsigned char tmp_hash[16]; @@ -53,8 +50,21 @@ void get_table(const char* key) { table[i] = i; } for(i = 1; i < 1024; ++i) { - _i = i; - qsort(table, 256, sizeof(unsigned char), random_compare); + // 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; + } + } + } } for(i = 0; i < 256; ++i) { // gen decrypt table from encrypt table diff --git a/shadowsocks-iOS/local.h b/shadowsocks-iOS/local.h index 0a4493a..cfe4814 100644 --- a/shadowsocks-iOS/local.h +++ b/shadowsocks-iOS/local.h @@ -1,5 +1,6 @@ #pragma once +#include #include "libev/ev.h" #define BUF_SIZE 1500 @@ -50,4 +51,5 @@ void close_and_free_remote(EV_P_ struct remote *remote); struct server* new_server(int fd); void free_server(struct server *server); void close_and_free_server(EV_P_ struct server *server); +int local_main(); diff --git a/shadowsocks-iOS/local.c b/shadowsocks-iOS/local.m similarity index 86% rename from shadowsocks-iOS/local.c rename to shadowsocks-iOS/local.m index 39b8f57..0616964 100644 --- a/shadowsocks-iOS/local.c +++ b/shadowsocks-iOS/local.m @@ -17,6 +17,8 @@ #include #include +#import + #include "local.h" #include "socks5.h" #include "encrypt.h" @@ -55,7 +57,7 @@ int create_and_bind(const char *port) { s = getaddrinfo("0.0.0.0", port, &hints, &result); if (s != 0) { -// LOGD("getaddrinfo: %s", gai_strerror(s)); + NSLog(@"getaddrinfo: %s", gai_strerror(s)); return -1; } @@ -78,7 +80,7 @@ int create_and_bind(const char *port) { } if (rp == NULL) { -// LOGE("Could not bind"); + NSLog(@"Could not bind"); return -1; } @@ -168,7 +170,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) { struct socks5_request *request = (struct socks5_request *)server->buf; if (request->cmd != 1) { - LOGE("unsupported cmd: %d\n", request->cmd); + NSLog(@"unsupported cmd: %d\n", request->cmd); struct socks5_response response; response.ver = VERSION; response.rep = CMD_NOT_SUPPORTED; @@ -213,7 +215,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) { addr_to_send[addr_len++] = *(unsigned char *)(server->buf + 4 + 1 + name_len + 1); addr_to_send[addr_len] = 0; } else { - LOGE("unsupported addrtype: %d\n", request->atyp); + NSLog(@"unsupported addrtype: %d\n", request->atyp); close_and_free_server(EV_A_ server); close_and_free_remote(EV_A_ remote); return; @@ -239,7 +241,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) { int reply_size = 4 + sizeof(struct in_addr) + sizeof(unsigned short); int r = send(server->fd, server->buf, reply_size, 0); if (r < reply_size) { - LOGE("header not complete sent\n"); + NSLog(@"header not complete sent\n"); close_and_free_remote(EV_A_ remote); close_and_free_server(EV_A_ server); return; @@ -529,104 +531,29 @@ static void accept_cb (EV_P_ ev_io *w, int revents) } } -int local_main (int argc, char **argv) +int local_main () { - char *server = NULL; - char *remote_port = NULL; - char *port = NULL; - char *key = NULL; - int c; - int f_flags = 0; - opterr = 0; - while ((c = getopt (argc, argv, "fs:p:l:k:")) != -1) { - switch (c) { - case 's': - server = optarg; - break; - case 'p': - remote_port = optarg; - break; - case 'l': - port = optarg; - break; - case 'k': - key = optarg; - break; - case 'f': - f_flags = 1; - break; - } - } + _server = "127.0.0.1"; + _remote_port = "8387"; + char key[] = "foobar!"; - if (server == NULL || remote_port == NULL || - port == NULL || key == NULL) { - exit(EXIT_FAILURE); - } - - if (f_flags) { - - /* Our process ID and Session ID */ - pid_t pid, sid; - - /* Fork off the parent process */ - pid = fork(); - if (pid < 0) { - exit(EXIT_FAILURE); - } - /* If we got a good PID, then - we can exit the parent process. */ - if (pid > 0) { - FILE *file = fopen("/data/data/com.github.shadowsocks/shadowsocks.pid", "w"); - fprintf(file, "%d", pid); - fclose(file); - exit(EXIT_SUCCESS); - } - - /* Change the file mode mask */ - umask(0); - - /* Open any logs here */ - - /* Create a new SID for the child process */ - sid = setsid(); - if (sid < 0) { - /* Log the failure */ - exit(EXIT_FAILURE); - } - - - /* Change the current working directory */ - if ((chdir("/")) < 0) { - /* Log the failure */ - exit(EXIT_FAILURE); - } - - /* Close out the standard file descriptors */ - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - } - - _server = strdup(server); - _remote_port = strdup(remote_port); - - LOGD("calculating ciphers"); + NSLog(@"calculating ciphers"); get_table(key); int listenfd; - listenfd = create_and_bind(port); + listenfd = create_and_bind("1080"); if (listenfd < 0) { - LOGE("bind() error.."); + NSLog(@"bind() error.."); return 1; } if (listen(listenfd, SOMAXCONN) == -1) { - LOGE("listen() error."); + NSLog(@"listen() error."); return 1; } - LOGD("server listening at port %s\n", port); + NSLog(@"server listening at port %s\n", "1080"); setnonblocking(listenfd); struct listen_ctx listen_ctx; diff --git a/shadowsocks-iOS/main.m b/shadowsocks-iOS/main.m index 19a8ec1..b3051fa 100644 --- a/shadowsocks-iOS/main.m +++ b/shadowsocks-iOS/main.m @@ -9,10 +9,12 @@ #import #import "AppDelegate.h" +#import "local.h" int main(int argc, char *argv[]) { // @autoreleasepool { - return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); +// return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); // } + local_main(); }