mirror of
https://github.com/zhigang1992/shadowsocks-iOS.git
synced 2026-04-23 12:36:52 +08:00
add logs; use merge sort
This commit is contained in:
@@ -2,6 +2,116 @@
|
||||
|
||||
#import <CommonCrypto/CommonDigest.h>
|
||||
|
||||
static int random_compare(const void *_x, const void *_y) {
|
||||
uint32_t i = _i;
|
||||
uint64_t a = _a;
|
||||
uint8_t x = *((uint8_t *) _x);
|
||||
uint8_t y = *((uint8_t*) _y);
|
||||
return (a % (x + i) - a % (y + i));
|
||||
}
|
||||
|
||||
|
||||
static void merge(uint8_t *left, int llength, uint8_t *right, int rlength)
|
||||
{
|
||||
/* Temporary memory locations for the 2 segments of the array to merge. */
|
||||
uint8_t *ltmp = (uint8_t *) malloc(llength * sizeof(uint8_t));
|
||||
uint8_t *rtmp = (uint8_t *) malloc(rlength * sizeof(uint8_t));
|
||||
|
||||
/*
|
||||
* Pointers to the elements being sorted in the temporary memory locations.
|
||||
*/
|
||||
uint8_t *ll = ltmp;
|
||||
uint8_t *rr = rtmp;
|
||||
|
||||
uint8_t *result = left;
|
||||
|
||||
/*
|
||||
* Copy the segment of the array to be merged into the temporary memory
|
||||
* locations.
|
||||
*/
|
||||
memcpy(ltmp, left, llength * sizeof(uint8_t));
|
||||
memcpy(rtmp, right, rlength * sizeof(uint8_t));
|
||||
|
||||
while (llength > 0 && rlength > 0) {
|
||||
if (random_compare(ll, rr) <= 0) {
|
||||
/*
|
||||
* Merge the first element from the left back into the main array
|
||||
* if it is smaller or equal to the one on the right.
|
||||
*/
|
||||
*result = *ll;
|
||||
++ll;
|
||||
--llength;
|
||||
} else {
|
||||
/*
|
||||
* Merge the first element from the right back into the main array
|
||||
* if it is smaller than the one on the left.
|
||||
*/
|
||||
*result = *rr;
|
||||
++rr;
|
||||
--rlength;
|
||||
}
|
||||
++result;
|
||||
}
|
||||
/*
|
||||
* All the elements from either the left or the right temporary array
|
||||
* segment have been merged back into the main array. Append the remaining
|
||||
* elements from the other temporary array back into the main array.
|
||||
*/
|
||||
if (llength > 0)
|
||||
while (llength > 0) {
|
||||
/* Appending the rest of the left temporary array. */
|
||||
*result = *ll;
|
||||
++result;
|
||||
++ll;
|
||||
--llength;
|
||||
}
|
||||
else
|
||||
while (rlength > 0) {
|
||||
/* Appending the rest of the right temporary array. */
|
||||
*result = *rr;
|
||||
++result;
|
||||
++rr;
|
||||
--rlength;
|
||||
}
|
||||
|
||||
/* Release the memory used for the temporary arrays. */
|
||||
free(ltmp);
|
||||
free(rtmp);
|
||||
}
|
||||
|
||||
static void merge_sort(uint8_t array[], int length)
|
||||
{
|
||||
/* This is the middle index and also the length of the right array. */
|
||||
uint8_t middle;
|
||||
|
||||
/*
|
||||
* Pointers to the beginning of the left and right segment of the array
|
||||
* to be merged.
|
||||
*/
|
||||
uint8_t *left, *right;
|
||||
|
||||
/* Length of the left segment of the array to be merged. */
|
||||
int llength;
|
||||
|
||||
if (length <= 1)
|
||||
return;
|
||||
|
||||
/* Let integer division truncate the value. */
|
||||
middle = length / 2;
|
||||
|
||||
llength = length - middle;
|
||||
|
||||
/*
|
||||
* Set the pointers to the appropriate segments of the array to be merged.
|
||||
*/
|
||||
left = array;
|
||||
right = array + llength;
|
||||
|
||||
merge_sort(left, llength);
|
||||
merge_sort(right, middle);
|
||||
merge(left, llength, right, middle);
|
||||
}
|
||||
|
||||
void encrypt(char *buf, int len) {
|
||||
char *end = buf + len;
|
||||
while (buf < end) {
|
||||
@@ -33,38 +143,21 @@ int recv_decrypt(int sock, char *buf, int len, int flags) {
|
||||
return result;
|
||||
}
|
||||
|
||||
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];
|
||||
// tmp_hash = MD5((const unsigned char*)key, strlen(key), NULL);
|
||||
CC_MD5(key, strlen(key), tmp_hash);
|
||||
_a = *(unsigned long long *)tmp_hash;
|
||||
unsigned int i;
|
||||
|
||||
_a = *(uint64_t *)tmp_hash;
|
||||
uint32_t i;
|
||||
|
||||
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;
|
||||
merge_sort(table, 256);
|
||||
}
|
||||
for(i = 0; i < 256; ++i) {
|
||||
// gen decrypt table from encrypt table
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
|
||||
#define min(a,b) (((a)<(b))?(a):(b))
|
||||
|
||||
#define ADDR_STR_LEN 256
|
||||
|
||||
// every watcher type has its own typedef'd struct
|
||||
// with the name ev_TYPE
|
||||
ev_io stdin_watcher;
|
||||
@@ -187,6 +189,8 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
|
||||
unsigned char addr_len = 0;
|
||||
addr_to_send[addr_len++] = request->atyp;
|
||||
|
||||
|
||||
char addr_str[ADDR_STR_LEN];
|
||||
// get remote addr and port
|
||||
if (request->atyp == 1) {
|
||||
|
||||
@@ -195,12 +199,17 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
|
||||
memcpy(addr_to_send + addr_len, server->buf + 4, in_addr_len + 2);
|
||||
addr_len += in_addr_len + 2;
|
||||
addr_to_send[addr_len] = 0;
|
||||
|
||||
// now get it back and print it
|
||||
inet_ntop(AF_INET, server->buf + 4, addr_str, ADDR_STR_LEN);
|
||||
|
||||
} else if (request->atyp == 3) {
|
||||
// Domain name
|
||||
unsigned char name_len = *(unsigned char *)(server->buf + 4);
|
||||
addr_to_send[addr_len++] = name_len;
|
||||
memcpy(addr_to_send + addr_len, server->buf + 4 + 1, name_len);
|
||||
memcpy(addr_str, server->buf + 4 + 1, name_len);
|
||||
addr_str[name_len] = '\0';
|
||||
addr_len += name_len;
|
||||
|
||||
// get port
|
||||
@@ -213,6 +222,8 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents) {
|
||||
close_and_free_remote(EV_A_ remote);
|
||||
return;
|
||||
}
|
||||
|
||||
NSLog(@"connecting %s", addr_str);
|
||||
|
||||
send_encrypt(remote->fd, addr_to_send, addr_len, 0);
|
||||
|
||||
@@ -531,7 +542,7 @@ int local_main ()
|
||||
|
||||
_server = "127.0.0.1";
|
||||
_remote_port = "8387";
|
||||
char key[] = "foobar!";
|
||||
char key[] = "onebox!";
|
||||
|
||||
NSLog(@"calculating ciphers");
|
||||
get_table(key);
|
||||
|
||||
Reference in New Issue
Block a user