From 407255604354a346d185397af06368d597473dfb Mon Sep 17 00:00:00 2001 From: aixiao Date: Tue, 19 Feb 2019 17:29:58 +0800 Subject: [PATCH] =?UTF-8?q?=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20co?= =?UTF-8?q?nf.c=20=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20conf.h=20?= =?UTF-8?q?=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20conf/cproxy.ini=20?= =?UTF-8?q?=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20conf/cproxy.ini.ex?= =?UTF-8?q?plain=20=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20cproxy.h?= =?UTF-8?q?=20=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20cproxy=5Fhelp.h?= =?UTF-8?q?=20=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20cproxy=5Freques?= =?UTF-8?q?t.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf.c | 158 +++++++++++++++++++++------ conf.h | 1 + conf/cproxy.ini | 5 + conf/cproxy.ini.explain | 8 +- cproxy.h | 21 ++-- cproxy_help.h | 1 - cproxy_request.c | 232 +++++++++++++++++++++++++++++++++++----- 7 files changed, 361 insertions(+), 65 deletions(-) diff --git a/conf.c b/conf.c index 57450f4..3a0c9d7 100644 --- a/conf.c +++ b/conf.c @@ -6,82 +6,164 @@ void read_conf(char *file, conf *p) // server module p->server_port = iniparser_getint(ini, "server:PORT", 0); - p->len_server_pid_file = - strlen(iniparser_getstring(ini, "server:PID_FILE", NULL)) + 1; + p->len_server_pid_file = strlen(iniparser_getstring(ini, "server:PID_FILE", NULL)) + 1; p->server_pid_file = (char *)malloc(p->len_server_pid_file); if (p->server_pid_file == NULL) { goto err; } memset(p->server_pid_file, 0, p->len_server_pid_file); - memcpy(p->server_pid_file, - iniparser_getstring(ini, "server:PID_FILE", NULL), - p->len_server_pid_file); + memcpy(p->server_pid_file, iniparser_getstring(ini, "server:PID_FILE", NULL), p->len_server_pid_file); - // http module + // http ip p->len_http_ip = strlen(iniparser_getstring(ini, "http:http_ip", NULL)) + 1; p->http_ip = (char *)malloc(p->len_http_ip); if (p->http_ip == NULL) { goto err; } memset(p->http_ip, 0, p->len_http_ip); - memcpy(p->http_ip, iniparser_getstring(ini, "http:http_ip", NULL), - p->len_http_ip); + memcpy(p->http_ip, iniparser_getstring(ini, "http:http_ip", NULL), p->len_http_ip); + // http port p->http_port = iniparser_getint(ini, "http:http_port", 0); - p->len_http_del = - strlen(iniparser_getstring(ini, "http:http_del", NULL)) + 1; + // http del + p->len_http_del = strlen(iniparser_getstring(ini, "http:http_del", NULL)) + 1; p->http_del = (char *)malloc(p->len_http_del); if (p->http_del == NULL) { goto err; } memset(p->http_del, 0, p->len_http_del); - memcpy(p->http_del, iniparser_getstring(ini, "http:http_del", NULL), - p->len_http_del); + memcpy(p->http_del, iniparser_getstring(ini, "http:http_del", NULL), p->len_http_del); - p->len_http_first = - strlen(iniparser_getstring(ini, "http:http_first", NULL)) + 1; + // http first + p->len_http_first = strlen(iniparser_getstring(ini, "http:http_first", NULL)) + 1; p->http_first = (char *)malloc(p->len_http_first); if (p->http_first == NULL) { goto err; } memset(p->http_first, 0, p->len_http_first); - memcpy(p->http_first, iniparser_getstring(ini, "http:http_first", NULL), - p->len_http_first); + memcpy(p->http_first, iniparser_getstring(ini, "http:http_first", NULL), p->len_http_first); - // https module - p->len_https_ip = - strlen(iniparser_getstring(ini, "https:https_ip", NULL)) + 1; + + // https ip + p->len_https_ip = strlen(iniparser_getstring(ini, "https:https_ip", NULL)) + 1; p->https_ip = (char *)malloc(p->len_https_ip); if (p->https_ip == NULL) { goto err; } memset(p->https_ip, 0, p->len_http_ip); - memcpy(p->https_ip, iniparser_getstring(ini, "https:https_ip", NULL), - p->len_https_ip); + memcpy(p->https_ip, iniparser_getstring(ini, "https:https_ip", NULL), p->len_https_ip); + //https port p->https_port = iniparser_getint(ini, "https:https_port", 0); - - p->len_https_del = - strlen(iniparser_getstring(ini, "https:https_del", NULL)) + 1; + + // https del + p->len_https_del = strlen(iniparser_getstring(ini, "https:https_del", NULL)) + 1; p->https_del = (char *)malloc(p->len_https_del); if (p->https_del == NULL) { goto err; } memset(p->https_del, 0, p->len_https_del); - memcpy(p->https_del, iniparser_getstring(ini, "https:https_del", NULL), - p->len_https_del); + memcpy(p->https_del, iniparser_getstring(ini, "https:https_del", NULL), p->len_https_del); - p->len_https_first = - strlen(iniparser_getstring(ini, "https:https_first", NULL)) + 1; + // https first + p->len_https_first = strlen(iniparser_getstring(ini, "https:https_first", NULL)) + 1; p->https_first = (char *)malloc(p->len_https_first); if (p->https_first == NULL) { goto err; } memset(p->https_first, 0, p->len_https_first); - memcpy(p->https_first, iniparser_getstring(ini, "https:https_first", NULL), - p->len_https_first); + memcpy(p->https_first, iniparser_getstring(ini, "https:https_first", NULL), p->len_https_first); + + // http strrep + if (iniparser_find_entry(ini, "http:strrep") == 1) { + p->len_http_strrep = strlen(iniparser_getstring(ini, "http:strrep", NULL)) + 1; + p->http_strrep = (char *)malloc(p->len_http_strrep); + if (p->http_strrep == NULL) { + free(p->http_strrep); + } + memset(p->http_strrep, 0, p->len_http_strrep); + memcpy(p->http_strrep, iniparser_getstring(ini, "http:strrep", NULL), p->len_http_strrep); + char *p1 = strstr(p->http_strrep, "->"); + p->http_strrep_aim = (char *)malloc(strlen(p->http_strrep) - strlen(p1+2) -2 + 1); + if (p->http_strrep_aim == NULL) { + free(p->http_strrep_aim); + } + strncpy_(p->http_strrep_aim, p->http_strrep, strlen(p->http_strrep) - strlen(p1+2) - 2); + p->http_strrep_obj = (char *)malloc(strlen(p1+2) + 1); + if (p->http_strrep_obj == NULL) { + free(p->http_strrep_obj); + } + strncpy_(p->http_strrep_obj, p1+2, strlen(p1+2)); + } + + // https strrep + if (iniparser_find_entry(ini, "https:strrep") == 1) { + p->len_https_strrep = strlen(iniparser_getstring(ini, "https:strrep", NULL)) + 1; + p->https_strrep = (char *)malloc(p->len_https_strrep); + if (p->https_strrep == NULL) { + free(p->https_strrep); + } + memset(p->https_strrep, 0, p->len_https_strrep); + memcpy(p->https_strrep, iniparser_getstring(ini, "https:strrep", NULL), p->len_https_strrep); + char *p2 = strstr(p->https_strrep, "->"); + p->https_strrep_aim = (char *)malloc(strlen(p->https_strrep) - strlen(p2+2) -2 + 1); + if (p->https_strrep_aim == NULL) { + free(p->https_strrep_aim); + } + strncpy_(p->https_strrep_aim, p->https_strrep, strlen(p->https_strrep) - strlen(p2+2) - 2); + p->https_strrep_obj = (char *)malloc(strlen(p2+2) + 1); + if (p->https_strrep_obj == NULL) { + free(p->https_strrep_obj); + } + strncpy_(p->https_strrep_obj, p2+2, strlen(p2+2)); + } + + // http regrep + if (iniparser_find_entry(ini, "http:regrep") == 1) { + p->len_http_regrep = strlen(iniparser_getstring(ini, "http:regrep", NULL)) + 1; + p->http_regrep = (char *)malloc(p->len_http_regrep); + if (p->http_regrep == NULL) { + free(p->http_regrep); + } + memset(p->http_regrep, 0, p->len_http_regrep); + memcpy(p->http_regrep, iniparser_getstring(ini, "http:regrep", NULL), p->len_http_regrep); + char *p3 = strstr(p->http_regrep, "->"); + p->http_regrep_aim = (char *)malloc(strlen(p->http_regrep) - strlen(p3+2) -2 + 1); + if (p->http_regrep_aim == NULL) { + free(p->http_regrep_aim); + } + strncpy_(p->http_regrep_aim, p->http_regrep, strlen(p->http_regrep) - strlen(p3+2) - 2); + p->http_regrep_obj = (char *)malloc(strlen(p3+2) + 1); + if (p->http_regrep_obj == NULL) { + free(p->http_regrep_obj); + } + strncpy_(p->http_regrep_obj, p3+2, strlen(p3+2)); + } + + // https regrep + if (iniparser_find_entry(ini, "https:regrep") == 1) { + p->len_https_regrep = strlen(iniparser_getstring(ini, "https:regrep", NULL)) + 1; + p->https_regrep = (char *)malloc(p->len_https_regrep); + if (p->https_regrep == NULL) { + free(p->https_regrep); + } + memset(p->https_regrep, 0, p->len_https_regrep); + memcpy(p->https_regrep, iniparser_getstring(ini, "https:regrep", NULL), p->len_https_regrep); + char *p4 = strstr(p->https_regrep, "->"); + p->https_regrep_aim = (char *)malloc(strlen(p->https_regrep) - strlen(p4+2) -2 + 1); + if (p->https_regrep_aim == NULL) { + free(p->https_regrep_aim); + } + strncpy_(p->https_regrep_aim, p->https_regrep, strlen(p->https_regrep) - strlen(p4+2) - 2); + p->https_regrep_obj = (char *)malloc(strlen(p4+2) + 1); + if (p->https_regrep_obj == NULL) { + free(p->https_regrep_obj); + } + strncpy_(p->https_regrep_obj, p4+2, strlen(p4+2)); + } + err: if (p->server_pid_file == NULL) free(p->server_pid_file); @@ -110,5 +192,19 @@ void free_conf(conf *p) free(p->https_ip); free(p->https_del); free(p->https_first); + + free(p->http_strrep); + free(p->http_strrep_aim); + free(p->http_strrep_obj); + free(p->https_strrep); + free(p->https_strrep_aim); + free(p->https_strrep_obj); + + free(p->http_regrep); + free(p->http_regrep_aim); + free(p->http_regrep_obj); + free(p->https_regrep); + free(p->https_regrep_aim); + free(p->https_regrep_obj); + } - diff --git a/conf.h b/conf.h index 132835d..27dcd01 100644 --- a/conf.h +++ b/conf.h @@ -3,6 +3,7 @@ #include "iniparser.h" #include "cproxy.h" +char *strncpy_(char *dest, const char *src, size_t n); void read_conf(char *file, conf *p); void free_conf(conf *p); diff --git a/conf/cproxy.ini b/conf/cproxy.ini index cce4c1d..b7d466a 100644 --- a/conf/cproxy.ini +++ b/conf/cproxy.ini @@ -7,9 +7,14 @@ http_ip=10.0.0.172; http_port=80; http_del="x-online-host,X-Online-Host,host,Host"; http_first="[M] [U] [V]\r\n.aixiao.me\rx-online-host: [host]\r\nhost: iread.wo.cn\r\n"; +strrep = "Mi MIX 2->Linux"; +;regrep = "host*.+?->host: iread.wo.cn"; [https] https_ip=10.0.0.172; https_port=80; https_del=",Host"; +;https_first="[M] [U] [V]\r\nHost: [host]:[port]\r\n"; https_first="[M] iread.wo.cn//https://[host]:[port]:iread.wo.cn [V]\r\nHost: iread.wo.cn\r\n"; +strrep = "Mi MIX 2->Linux"; +regrep = "Host*.+?->Host: iread.wo.cn:443"; \ No newline at end of file diff --git a/conf/cproxy.ini.explain b/conf/cproxy.ini.explain index 41a1b99..ddcab46 100644 --- a/conf/cproxy.ini.explain +++ b/conf/cproxy.ini.explain @@ -5,4 +5,10 @@ [U] 原请求url [V] 原请求协议版本 [host] 原请求host -[port] 原请求端口 \ No newline at end of file +[port] 原请求端口 + +关键字strrep替换字符串指令 +strrep = "Mi MIX 2->Linux"; 以"->"为分界符,"Mi MIX 2"字符串替换为"Linux"字符串. + +关键字regrep正则匹配替换字符串 +regrep = "Host*.+?->Host: iread.wo.cn:443"; 以"->"为分界符,匹配到的内容"Host*.+?"替换为"Host: iread.wo.cn:443"字符串. diff --git a/cproxy.h b/cproxy.h index 382239f..ebe7c47 100644 --- a/cproxy.h +++ b/cproxy.h @@ -14,9 +14,9 @@ #include #include #include -#include -#include -#include +#include +#include +#include #define BUF_SIZE 8192 #define BUF_SIZES 1024 @@ -58,6 +58,16 @@ typedef struct CONF { int len_server_pid_file; // length int len_http_ip, len_http_del, len_http_first; int len_https_ip, len_https_del, len_https_first; + + char *http_strrep, *http_regrep, *https_strrep, *https_regrep; + char *http_strrep_aim, *http_strrep_obj; + char *https_strrep_aim, *https_strrep_obj; + + char *http_regrep_aim, *http_regrep_obj; + char *https_regrep_aim, *https_regrep_obj; + + int len_http_strrep, len_http_regrep; + int len_https_strrep, len_https_regrep; } conf; // 请求类型 @@ -75,12 +85,11 @@ int receive_data(int socket, char *buffer, int len); void forward_data(int source_sock, int destination_sock); int create_connection(conf *configure, int SIGN); int create_server_socket(int port); -int init_daemon(int nochdir, int noclose, conf * configure); +int init_daemon(int nochdir, int noclose, conf *configure); void sigchld_handler(int signal); void server_loop(conf *configure); -void start_server(conf * configure); +void start_server(conf *configure); int _main(int argc, char *argv[]); - void read_conf(char *file, conf *p); void free_conf(conf *p); int extract_host(char *header); diff --git a/cproxy_help.h b/cproxy_help.h index 886e659..db3e7ca 100644 --- a/cproxy_help.h +++ b/cproxy_help.h @@ -3,7 +3,6 @@ #include #include "cproxy.h" - #define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0) char help_information(void); diff --git a/cproxy_request.c b/cproxy_request.c index 5a93b90..133e430 100644 --- a/cproxy_request.c +++ b/cproxy_request.c @@ -57,6 +57,87 @@ char *replace(char *replace_memory, int *replace_memory_len, const char *src, co return replace_memory; } +/* 正则表达式字符串替换,str为可用free释放的指针 */ +static char *regrep(char *str, int *str_len, const char *src, char *dest, int dest_len) +{ + if (!str || !src || !dest) + return NULL; + + regmatch_t pm[10]; + regex_t reg; + char child_num[2] = {'\\', '0'}, *p, *real_dest; + int match_len, real_dest_len, i; + + p = str; + regcomp(®, src, REG_NEWLINE|REG_ICASE|REG_EXTENDED); + while (regexec(®, p, 10, pm, 0) == 0) + { + real_dest = (char *)malloc(dest_len); + if (real_dest == NULL) + { + regfree(®); + free(str); + return NULL; + } + memcpy(real_dest, dest, dest_len); + real_dest_len = dest_len; + //不进行不必要的字符串操作 + if (pm[1].rm_so >= 0) + { + /* 替换目标字符串中的子表达式 */ + for (i = 1; i < 10 && pm[i].rm_so > -1; i++) + { + child_num[1] = i + 48; + real_dest = replace(real_dest, &real_dest_len, child_num, 2, p + pm[i].rm_so, pm[i].rm_eo - pm[i].rm_so); + if (real_dest == NULL) + { + regfree(®); + free(str); + return NULL; + } + } + } + + match_len = pm[0].rm_eo - pm[0].rm_so; + p += pm[0].rm_so; + //目标字符串不大于匹配字符串则不用分配新内存 + if (match_len >= real_dest_len) + { + memcpy(p, real_dest, real_dest_len); + if (match_len > real_dest_len) + //strcpy(p + real_dest_len, p + match_len); + memmove(p + real_dest_len, p + match_len, *str_len - (p + match_len - str)); + p += real_dest_len; + *str_len -= match_len - real_dest_len; + } + else + { + int diff; + char *before_end, *new_str; + + diff = real_dest_len - match_len; + *str_len += diff; + new_str = (char *)realloc(str, *str_len + 1); + if (new_str == NULL) + { + free(str); + free(real_dest); + regfree(®); + return NULL; + } + str = new_str; + before_end = str + pm[0].rm_so; + p = before_end + real_dest_len; + memmove(p, p - diff, *str_len - (p - str) + 1); + memcpy(before_end, real_dest, real_dest_len); + } + free(real_dest); + } + + regfree(®); + return str; +} + // 删除单个字符 void del_chr(char *s, char ch) { @@ -202,11 +283,14 @@ int numbin(int n) return sum; } -// 删除字符串header_buffer中第一位到character处,并拼接string,character必须存在.(string替换第一个字符到character处) +// 删除字符串header_buffer中第一位到character处,并拼接string,character为空返回原字符串.(string替换第一个字符到character处) char *splice_head(char *header_buffer, const char *character, char *string) { int len_first = strlen(string); char *p1 = strstr(header_buffer, character); + if (p1 == NULL) { + return header_buffer; + } p1 = p1 + 1; char new_string[len_first + strlen(p1) + 1]; strcpy(new_string, string); // 拼接 @@ -230,18 +314,43 @@ char *delete_header(char *header_buffer, const char *character, int string) int replacement_http_head(char *header_buffer, char *remote_host, int *remote_port, int *SIGN, conf *p) { char *http_firsts = (char *)malloc(strlen(p->http_first) + 1); - strcpy(http_firsts, p->http_first); // 拷贝http_first + if (http_firsts) { + strcpy(http_firsts, p->http_first); // 拷贝http_first + } else { + free(http_firsts); + return 0; + } char *https_firsts = (char *)malloc(strlen(p->https_first) + 1); - strcpy(https_firsts, p->https_first); // 拷贝https_first + if (https_firsts) { + strcpy(https_firsts, p->https_first); // 拷贝https_first + } else { + free(https_firsts); + return 0; + } char *header_buffer_backup = (char *)malloc(strlen(header_buffer) + 1); // 拷贝原字符串 - strcpy(header_buffer_backup, header_buffer); + if (header_buffer_backup) { + strcpy(header_buffer_backup, header_buffer); + } else { + free(header_buffer_backup); + return 0; + } char *new_http_del = malloc(strlen(p->http_del) + 1); // 拷贝http_del - strcpy(new_http_del, p->http_del); + if (new_http_del) { + strcpy(new_http_del, p->http_del); + } else { + free(new_http_del); + return 0; + } char *new_https_del = malloc(strlen(p->https_del) + 1); // 拷贝https_del - strcpy(new_https_del, p->https_del); + if (new_https_del) { + strcpy(new_https_del, p->https_del); + } else { + free(new_https_del); + return 0; + } if (*SIGN == HTTP) { char *result = NULL; @@ -254,23 +363,38 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po char *p2 = strstr(header_buffer_backup, "\n"); p2 = p2 + 1; int len_http_head = strlen(header_buffer_backup) - strlen(p2); - char *HTTP_HEAD = (char *)malloc(len_http_head + 1); //http头第一行 - strncpy_(HTTP_HEAD, header_buffer_backup, len_http_head); + char *HTTP_HEAD = (char *)malloc(len_http_head + 1); // http头第一行 + if (HTTP_HEAD) { + strncpy_(HTTP_HEAD, header_buffer_backup, len_http_head); + } else { + free(HTTP_HEAD); + return 0; + } // M char *p3 = strstr(HTTP_HEAD, " "); int l = strlen(HTTP_HEAD) - strlen(p3); char *M = malloc(l + 1); - strncpy_(M, HTTP_HEAD, l); - //printf("%s", M); + if (M) { + strncpy_(M, HTTP_HEAD, l); + //printf("%s", M); + } else { + free(M); + return 0; + } // U p3 = p3 + 1; char *p4 = strstr(p3, " "); l = strlen(p3) - strlen(p4); char *U = (char *)malloc(l + 1); - strncpy_(U, p3, l); - //printf("%s", U); + if (U) { + strncpy_(U, p3, l); + //printf("%s", U); + } else { + free(U); + return 0; + } // V p4 = p4 + 1; @@ -278,18 +402,28 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po del_chr(p4, '\n'); l = strlen(p4); char *V = (char *)malloc(l); - strcpy(V, p4); - //printf("%s", V); + if (V) { + strcpy(V, p4); + //printf("%s", V); + } else { + free(V); + return 0; + } char *new_header_buffer = (char *)malloc(strlen(splice_head(header_buffer_backup, "\n", http_firsts)) + 1); - strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", http_firsts)); + if (new_header_buffer) { + strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", http_firsts)); + } else { + free(new_header_buffer); + return 0; + } int len = strlen(new_header_buffer); int len_m = strlen(M); int len_u = strlen(U); int len_v = strlen(V); int len_remote_host = strlen(remote_host); - + new_header_buffer = replace(new_header_buffer, &len, "[M]", 3, M, len_m); new_header_buffer = replace(new_header_buffer, &len, "[U]", 3, U, len_u); new_header_buffer = replace(new_header_buffer, &len, "[V]", 3, V, len_v); @@ -309,6 +443,15 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po new_header_buffer = replace(new_header_buffer, &len, "\\t", 2, "\t", 1); new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1); + if (p->http_strrep) { + int regrep_aim_len = strlen(p->http_strrep_aim); + int regrep_obj_len = strlen(p->http_strrep_obj); + new_header_buffer = replace(new_header_buffer, &len, p->http_strrep_aim, regrep_aim_len, p->http_strrep_obj, regrep_obj_len); + } + if (p->http_regrep) { + len = strlen(new_header_buffer) + 1; + new_header_buffer = regrep(new_header_buffer, &len, p->http_regrep_aim, p->http_regrep_obj, strlen(p->http_regrep_obj)); + } //stpcpy(p->http_ip, remote_host); //p->http_port = *remote_port; @@ -333,39 +476,65 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po char *p2 = strstr(header_buffer_backup, "\n"); p2 = p2 + 1; int len_https_head = strlen(header_buffer_backup) - strlen(p2); - char *HTTPS_HEAD = (char *)malloc(len_https_head + 1); //https头第一行 - strncpy_(HTTPS_HEAD, header_buffer_backup, len_https_head); + char *HTTPS_HEAD = (char *)malloc(len_https_head + 1); // https头第一行 + if (HTTPS_HEAD) { + strncpy_(HTTPS_HEAD, header_buffer_backup, len_https_head); + } else { + free(HTTPS_HEAD); + return 0; + } // M char *p3 = strstr(HTTPS_HEAD, " "); int l = strlen(HTTPS_HEAD) - strlen(p3); char *M = malloc(l + 1); - strncpy_(M, HTTPS_HEAD, l); - //printf("%s", M); + if (M) { + strncpy_(M, HTTPS_HEAD, l); + //printf("%s", M); + } else { + free(M); + return 0; + } // U p3 = p3 + 1; char *p4 = strstr(p3, " "); l = strlen(p3) - strlen(p4); char *U = (char *)malloc(l + 1); - strncpy_(U, p3, l); - //printf("%s", U); + if (U) { + strncpy_(U, p3, l); + //printf("%s", U); + } else { + free(U); + return 0; + } // V p4 = p4 + 1; l = strlen(p4); char *V = (char *)malloc(l); - strncpy_(V, p4, 8); - //printf("%s", V); + if (V) { + strncpy_(V, p4, 8); + //printf("%s", V); + } else { + free(V); + return 0; + } char *new_header_buffer = (char *) malloc(strlen(splice_head(header_buffer_backup, "\n", https_firsts)) + 1); + if (new_header_buffer) { strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", https_firsts)); - + } else { + free(new_header_buffer); + return 0; + } + int len = strlen(new_header_buffer); int len_m = strlen(M); int len_u = strlen(U); int len_v = strlen(V); int len_remote_host = strlen(remote_host); + new_header_buffer = replace(new_header_buffer, &len, "[M]", 3, M, len_m); new_header_buffer = replace(new_header_buffer, &len, "[U]", 3, U, len_u); new_header_buffer = replace(new_header_buffer, &len, "[V]", 3, V, len_v); @@ -385,7 +554,18 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po new_header_buffer = replace(new_header_buffer, &len, "\\t", 2, "\t", 1); new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1); - + + if (p->https_strrep) { + int regrep_aim_len = strlen(p->https_strrep_aim); + int regrep_obj_len = strlen(p->https_strrep_obj); + new_header_buffer = replace(new_header_buffer, &len, p->https_strrep_aim, regrep_aim_len, p->https_strrep_obj, regrep_obj_len); + } + if (p->https_regrep) { + len = strlen(new_header_buffer) + 1; + new_header_buffer = regrep(new_header_buffer, &len, p->https_regrep_aim, p->https_regrep_obj, strlen(p->https_regrep_obj)); + + } + //stpcpy(p->https_ip, remote_host); // 走真实IP非代理 //p->https_port = *remote_port; memset(header_buffer, 0, strlen(header_buffer));