almost works

This commit is contained in:
clowwindy
2012-12-27 18:38:46 +08:00
parent e0b06e6a46
commit 87aaeb34cc
5 changed files with 42 additions and 101 deletions

View File

@@ -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 = "<group>"; };
62F3C45C168C411F006D77BC /* encrypt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = encrypt.c; sourceTree = "<group>"; };
62F3C45D168C411F006D77BC /* encrypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = encrypt.h; sourceTree = "<group>"; };
62F3C45E168C411F006D77BC /* local.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = local.c; sourceTree = "<group>"; };
62F3C45E168C411F006D77BC /* local.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = local.m; sourceTree = "<group>"; };
62F3C45F168C411F006D77BC /* local.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = local.h; sourceTree = "<group>"; };
62F3C460168C411F006D77BC /* socks5.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = socks5.h; sourceTree = "<group>"; };
62F3C48A168C488B006D77BC /* ev.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ev.c; path = "shadowsocks-iOS/libev/ev.c"; sourceTree = "<group>"; };
@@ -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;

View File

@@ -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

View File

@@ -1,5 +1,6 @@
#pragma once
#include <sys/socket.h>
#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();

View File

@@ -17,6 +17,8 @@
#include <time.h>
#include <unistd.h>
#import <Foundation/Foundation.h>
#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;

View File

@@ -9,10 +9,12 @@
#import <UIKit/UIKit.h>
#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();
}