diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..397b4a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6255f5a --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = -g -Wall -Werror + +all: mproxy + +mproxy: mproxy.o + $(CC) $(CFLAGS) mproxy.o -o mproxy + +mproxy.o: + $(CC) $(CFLAGS) -c mproxy.c + +clean: + rm -rf *.o + rm mproxy diff --git a/README.md b/README.md index 045df21..2015dd0 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ mproxy可以运行在unix-like 的操作系统下面,程序很小没有第三 #### step3 : 配置浏览器http代理使用mporxy本地代理 设置你的浏览器http代理指向你的本地代理,使用chrome浏览器的同学强烈推荐使用 -[switchSharp](https://code.google.com/p/switchysharp/),安装完毕swichSharp以后只需要为switchSharp增加一个情景模式就好,如下图: +[switchSharp](https://github.com/feliscatus/switchyomega),安装完毕swichSharp以后只需要为switchSharp增加一个情景模式就好,如下图: ![设置switchSharp](./switchSharp_config.png) diff --git a/mproxy.c b/mproxy.c index f40cb90..4c358c1 100644 --- a/mproxy.c +++ b/mproxy.c @@ -15,7 +15,7 @@ #include -#define BUF_SIZE 1024 +#define BUF_SIZE 8192 #define READ 0 #define WRITE 1 @@ -43,7 +43,7 @@ #define LOG(fmt...) __android_log_print(ANDROID_LOG_DEBUG,__FILE__,##fmt) #else -#define LOG(fmt...) fprintf(stderr, ##fmt); +#define LOG(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0) #endif @@ -451,8 +451,7 @@ int send_data(int socket,char * buffer,int len) int i; for(i = 0; i < len ; i++) { - char c = buffer[i] ; - buffer[i] = c+ 1; + buffer[i] ^= 1; } } @@ -468,8 +467,7 @@ int receive_data(int socket, char * buffer, int len) int i; for(i = 0; i< n; i++ ) { - char c = buffer[i]; - buffer[i] = c -1; + buffer[i] ^= 1; // printf("%d => %d\n",c,buffer[i]); } } @@ -608,7 +606,6 @@ void server_loop() { void stop_server() { - int status; kill(m_pid, SIGKILL); } @@ -617,13 +614,13 @@ void usage(void) printf("Usage:\n"); printf(" -l specifyed local listen port \n"); printf(" -h specifyed next hop server name\n"); - printf(" -d run as deamon\n"); + printf(" -d run as daemon\n"); printf("-E encode data when forwarding data\n"); printf ("-D decode data when receiving data\n"); exit (8); } -void start_server(int deamon) +void start_server(int daemon) { //初始化全局变量 header_buffer = (char *) malloc(MAX_HEADER_SIZE); @@ -636,7 +633,7 @@ void start_server(int deamon) exit(server_sock); } - if(deamon) + if(daemon) { pid_t pid; if((pid = fork()) == 0) @@ -669,75 +666,54 @@ int _main(int argc, char *argv[]) { local_port = DEFAULT_LOCAL_PORT; io_flag = FLG_NONE; - int deamon = 0; + int daemon = 0; char info_buf[2048]; - - int i; - for( i = 1 ; i < argc ; i++) - { - if(argv[i][0] == '-' ) - { - - if(argv[i][1] == 'h') /* 指定远程服务器地址,及端口号 */ - { - char * s = argv[++i]; - if(s && s[0] != '-') - { - char * p = strchr(s,':'); - if(p) - { - strncpy(remote_host,s,p - s); - remote_port = atoi(++p); - } else - { - strncpy(remote_host,s,128); - } - - } - else - { - usage(); - } - - } - else if(argv[i][1] == 'l') /* 指定当前服务监听端口号 */ - { - char * s = argv[++i]; - if(s) - { - local_port = atoi(s); - } - else - { - usage(); - } - - } else if(argv[i][1] == 'd') - { - deamon = 1; - } else if(argv[i][1] == 'D') - { - io_flag = R_C_DEC; - - } else if(argv[i][1] == 'E') - { - io_flag = W_S_ENC; - } - else { - usage(); - } - } - else - { - usage(); - } - + + int opt; + char optstrs[] = ":l:h:dED"; + char *p = NULL; + while(-1 != (opt = getopt(argc, argv, optstrs))) + { + switch(opt) + { + case 'l': + local_port = atoi(optarg); + break; + case 'h': + p = strchr(optarg, ':'); + if(p) + { + strncpy(remote_host, optarg, p - optarg); + remote_port = atoi(p+1); + } + else + { + strncpy(remote_host, optarg, strlen(remote_host)); + } + break; + case 'd': + daemon = 1; + break; + case 'E': + io_flag = W_S_ENC; + break; + case 'D': + io_flag = R_C_DEC; + break; + case ':': + printf("\nMissing argument after: -%c\n", optopt); + usage(); + case '?': + printf("\nInvalid argument: %c\n", optopt); + default: + usage(); + } } get_info(info_buf); LOG("%s\n",info_buf); - start_server(deamon); + start_server(daemon); return 0; } diff --git a/mproxy.pac b/mproxy.pac new file mode 100644 index 0000000..693877b --- /dev/null +++ b/mproxy.pac @@ -0,0 +1,11 @@ +function FindProxyForURL(url, host) { + if (shExpMatch(url,"*.google.com/*")) { + return "PROXY localhost:8081"; + } + + if (shExpMatch(url,"*.google.com.hk/*")) { + return "PROXY localhost:8081"; + } + + return "DIRECT; PROXY localhost:8081"; +} \ No newline at end of file