From 9aa4d195475f2c389f080df8262a6549db4282af Mon Sep 17 00:00:00 2001 From: aixiao Date: Thu, 9 Dec 2021 19:31:51 +0800 Subject: [PATCH] The getaddrinfo () function is no longer used, the static compilation libc dependency is removed, and the configuration file server address is no longer supported to fill in the domain name. --- Android.mk | 0 Application.mk | 0 CProxy.conf | 5 ++- CProxy.conf.explain | 25 ++++++------- conf.h | 0 help.h | 0 http_proxy.c | 88 +++++++++++++++++++++++++++++++++++++++++++-- http_request.c | 16 +++++---- httpdns.c | 5 +-- main.c | 5 ++- 10 files changed, 116 insertions(+), 28 deletions(-) mode change 100644 => 100755 Android.mk mode change 100644 => 100755 Application.mk mode change 100644 => 100755 conf.h mode change 100644 => 100755 help.h diff --git a/Android.mk b/Android.mk old mode 100644 new mode 100755 diff --git a/Application.mk b/Application.mk old mode 100644 new mode 100755 diff --git a/CProxy.conf b/CProxy.conf index 6111db7..9f383c2 100755 --- a/CProxy.conf +++ b/CProxy.conf @@ -4,12 +4,11 @@ global { timeout=1; encode=128; tcp_listen=0124; - tcp6_listen=0125; dns_listen=0126; } http { - http_ip="47.240.75.93"; + http_ip="2408:8221:9916:15e0:3cf9:3d8d:64ac:b880"; http_port=127; http_del="Host"; http_first="[M] http://[host][U] [V]\r\nHost: [H]\r\n"; @@ -18,7 +17,7 @@ http { } https { - https_ip="174.137.54.215"; + https_ip="127.0.0.1"; https_port=127; https_del="Host,host,x-online-host"; https_first="[M] [U] [V]\r\nHost: [host]\r\n"; diff --git a/CProxy.conf.explain b/CProxy.conf.explain index a48315a..ec53af8 100755 --- a/CProxy.conf.explain +++ b/CProxy.conf.explain @@ -33,12 +33,12 @@ httpdns { global模块 -uid=3004; //进程UID -process=2; //进程数量(已经弃用) -timeout=60; //超时时间(秒) -sslencoding=128; //编码(非零生效) +uid=3004; //进程UID +process=2; //进程数量(已经弃用) +timeout=60; //超时时间(秒) +sslencoding=128; //编码(非零生效) tcp_listen=0124; //TCP监听端口 -tcp6_listen=0124; //TCP6监听端口 (TCP和TCP6可以使用同一个端口, IPV4的数据走内部IPV4程序结构, IPV6数据走内部IPV6程序结构.) +tcp6_listen=0124; //TCP6监听端口 (TCP和TCP6可以使用同一个端口, IPV4的数据走内部IPV4程序结构, IPV6数据走内部IPV6程序结构.)(已经弃用) dns_listen=0125; //UDP监听端口 @@ -52,18 +52,19 @@ http、https 模块关键字: [M], [method], [uri], [U], [V], [version], [H], [h [port] 原请求端口 [H] 原请求[host]:[port] -http_ip=cproxy.aixiao.me; //指定HTTP服务器IP -http_port=124; //指定HTTP服务器端口 -http_del="x-online-host,X-Online-Host,host,Host"; //删除HTTP头字段,逗号分开 -http_first="[M] [U] [V]\r\nHost: [H]\r\n"; //自定义HTTP头 +http_ip=47.240.75.93; //指定HTTP服务器IP +http_port=127; //指定HTTP服务器端口 +http_del="x-online-host,X-Online-Host,host,Host"; //删除HTTP头字段,逗号分开 +http_first="[M] [U] [V]\r\nHost: [H]\r\n"; //自定义HTTP请求头 关键字strrep替换字符串指令. -strrep = "Mi MIX 2->Linux"; 以"->"为分界符,"Mi MIX 2"字符串替换为"Linux"字符串. +strrep = "Mi MIX 2->Linux"; //以"->"为分界符,"Mi MIX 2"字符串替换为"Linux"字符串. 关键字regrep正则匹配替换字符串. -regrep = "Host*.+?->Host: iread.wo.cn:443"; 以"->"为分界符,匹配到的内容"Host*.+?"替换为"Host: iread.wo.cn:443"字符串. +regrep = "Host*.+?->Host: iread.wo.cn:443"; //以"->"为分界符,匹配到的内容"Host*.+?"替换为"Host: iread.wo.cn:443"字符串. httpdns模块 httpdns 模块关键字: [M], [D], [V], \r, \n, \v, \f, \b, \t, \a. 默认 [M] 为 GET 默认 [V] 为 HTTP/1.0 -addr=119.29.29.29:80; //HTTPDNS服务器IP +addr=119.29.29.29:80; //HTTPDNS服务器IP +http_req = "[M] [U] [V]\r\nHost: [H]\r\n\r\n"; //自定义HTTPDNS请求头 diff --git a/conf.h b/conf.h old mode 100644 new mode 100755 diff --git a/help.h b/help.h old mode 100644 new mode 100755 diff --git a/http_proxy.c b/http_proxy.c index 9a35c14..51db613 100755 --- a/http_proxy.c +++ b/http_proxy.c @@ -121,7 +121,13 @@ static int8_t request_type(char *data) strncmp(data, "PUT", 3) == 0 || strncmp(data, "OPTIONS", 7) == 0 || strncmp(data, "MOVE", 4) == 0 || - strncmp(data, "COPY", 4) == 0 || strncmp(data, "TRACE", 5) == 0 || strncmp(data, "DELETE", 6) == 0 || strncmp(data, "LINK", 4) == 0 || strncmp(data, "UNLINK", 6) == 0 || strncmp(data, "PATCH", 5) == 0 || strncmp(data, "WRAPPED", 7) == 0) + strncmp(data, "COPY", 4) == 0 || + strncmp(data, "TRACE", 5) == 0 || + strncmp(data, "DELETE", 6) == 0 || + strncmp(data, "LINK", 4) == 0 || + strncmp(data, "UNLINK", 6) == 0 || + strncmp(data, "PATCH", 5) == 0 || + strncmp(data, "WRAPPED", 7) == 0) return HTTP_TYPE; return OTHER_TYPE; } @@ -138,12 +144,14 @@ int check_ipversion(char *address) return AF_INET6; } } + return 0; } int create_connection6(char *remote_host, int remote_port) { - struct addrinfo hints, *res = NULL; + struct addrinfo hints; + //struct addrinfo *res = NULL; int sock; int validfamily = 0; char portstr[CACHE_SIZE]; @@ -161,13 +169,16 @@ int create_connection6(char *remote_host, int remote_port) hints.ai_family = validfamily; hints.ai_flags |= AI_NUMERICHOST; // remote_host是有效的数字ip,跳过解析 } + +/* + //getaddrinfo方法 // 检查指定的主机是否有效。 如果remote_host是主机名,尝试解析地址 if (getaddrinfo(remote_host, portstr, &hints, &res) != 0) { errno = EFAULT; perror("getaddrinfo"); return -1; } - + if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) { perror("socket"); return -1; @@ -178,9 +189,80 @@ int create_connection6(char *remote_host, int remote_port) return -1; } + if (res != NULL) freeaddrinfo(res); +*/ + //通用sockaddr_storage结构体 + struct sockaddr_storage remote_addr; + memset(&remote_addr, 0, sizeof(struct sockaddr_storage)); + if (validfamily == AF_INET) { + struct sockaddr_in *addr_v4 = (struct sockaddr_in *)&remote_addr; + addr_v4->sin_family = AF_INET; + addr_v4->sin_port = htons(remote_port); + inet_aton(remote_host, &(addr_v4->sin_addr)); + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("socket"); + return -1; + } + } else { + struct sockaddr_in6 *addr_v6 = (struct sockaddr_in6 *)&remote_addr; + addr_v6->sin6_family = AF_INET6; + addr_v6->sin6_port = htons(remote_port); + inet_pton(AF_INET6, remote_host, &(addr_v6->sin6_addr)); + if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) < 0) { + perror("socket"); + return -1; + } + } + + if (connect(sock, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr_storage)) < 0) { + perror("connect"); + return -1; + } + + +/* + //普通方法 + if (validfamily == AF_INET) { + struct sockaddr_in remote_addr; + memset(&remote_addr, 0, sizeof(remote_addr)); + remote_addr.sin_family = AF_INET; + remote_addr.sin_port = htons(remote_port); + remote_addr.sin_addr.s_addr = inet_addr(remote_host); + + if ((sock = socket(validfamily, SOCK_STREAM, 0)) < 0) { + perror("socket"); + return -1; + } + + if (connect(sock, (struct sockaddr *)&remote_addr, sizeof(remote_addr)) < 0) { + perror("connect"); + return -1; + } + + return sock; + } else { + struct sockaddr_in6 remote_addr; + memset(&remote_addr, 0, sizeof(remote_addr)); + remote_addr.sin6_family = AF_INET6; + remote_addr.sin6_port = htons(remote_port); + inet_pton(AF_INET6, remote_host, &(remote_addr.sin6_addr)); + + if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) < 0) { + perror("socket"); + return -1; + } + + if (connect(sock, (struct sockaddr *)&remote_addr, sizeof(remote_addr)) < 0) { + perror("connect"); + return -1; + } + + return sock; + } +*/ return sock; } diff --git a/http_request.c b/http_request.c index 484bc83..764a4c7 100755 --- a/http_request.c +++ b/http_request.c @@ -275,7 +275,7 @@ int extract_host(char *header, char *host, char *port) if (p2 && p2 < p1) { memcpy(port, p2 + 1, (int)(p1 - p2 - 1)); - memcpy(host, p + 5 + 1, (int)(p2 - p - 5 - 1 - 1)); + memcpy(host, p + 5 + 1, (int)(p2 - p - 5 - 1)); } else { if (0 < (int)(p1 - p - 5 - 1 - 1)) { memcpy(host, p + 5 + 1, (p1 - p - 5 - 1 - 1)); @@ -285,6 +285,8 @@ int extract_host(char *header, char *host, char *port) memcpy(port, "80", 2); } } + //printf("%s\n", host); + //printf("%s\n", port); return 0; } @@ -392,14 +394,14 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque } head_len = strlen(http_request_line) - strlen(p); - head = (char *)malloc(sizeof(char) * head_len * 2); + head = (char *)malloc(sizeof(char) * head_len + 1); if (head == NULL) free(head); - memset(head, 0, head_len * 2); + memset(head, 0, sizeof(char) * head_len + 1); memcpy(head, http_request_line, head_len); - http_request->method = (char *)malloc(sizeof(char) * 7); - http_request->U = (char *)malloc(sizeof(char) * HTTP_HEAD_CACHE_SIZE); + http_request->method = (char *)malloc(sizeof(char) * 8); + http_request->U = (char *)malloc(sizeof(char) * head_len); http_request->version = (char *)malloc(10); if (http_request->method == NULL) { perror("malloc"); @@ -410,8 +412,8 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque if (http_request->version == NULL) { perror("malloc"); } - memset(http_request->method, 0, 7); - memset(http_request->U, 0, HTTP_HEAD_CACHE_SIZE); + memset(http_request->method, 0, 8); + memset(http_request->U, 0, sizeof(char) * head_len); memset(http_request->version, 0, 10); m = strchr(head, ' '); diff --git a/httpdns.c b/httpdns.c index ab81a61..07afe68 100755 --- a/httpdns.c +++ b/httpdns.c @@ -49,7 +49,7 @@ int8_t read_cache_file() char *buff, *cache_ptr; struct dns_cache *cache_temp; long file_size; - int len; + int len, fl=0; cache_temp = NULL; cache_using = 0; @@ -69,7 +69,8 @@ int8_t read_cache_file() if ((buff = (char *)alloca(file_size)) == NULL) return 1; rewind(cfp); - fread(buff, file_size, 1, cfp); + if ((fl = fread(buff, file_size, 1, cfp)) != 1) + ; fclose(cfp); for (cache_ptr = buff; cache_ptr - buff < file_size; cache_ptr += len) { diff --git a/main.c b/main.c index 4dbd356..a055149 100755 --- a/main.c +++ b/main.c @@ -18,6 +18,7 @@ int epollfd, server_sock, server_sock6, local_port, process; conn_t cts[MAX_CONNECTION]; char local_host[CACHE_SIZE]; +/* int create_connection(char *remote_host, int remote_port) { struct sockaddr_in server_addr; @@ -39,6 +40,7 @@ int create_connection(char *remote_host, int remote_port) server_addr.sin_family = AF_INET; memmove(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length); server_addr.sin_port = htons(remote_port); + //server_addr.sin_addr.s_addr = inet_addr(remote_host); if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { perror("connect"); close(sock); @@ -48,6 +50,7 @@ int create_connection(char *remote_host, int remote_port) fcntl(sock, F_SETFL, O_NONBLOCK); return sock; } +*/ int create_server_socket(int port) { @@ -388,7 +391,7 @@ void initialize(conf * configure) copy_new_mem(configure->http_req, httpdns.http_req_len, &httpdns.http_req); server_sock = create_server_socket(configure->tcp_listen); // IPV4 - server_sock6 = create_server_socket6(configure->tcp6_listen); // IPV6 + server_sock6 = create_server_socket6(configure->tcp_listen); // IPV6 epollfd = epoll_create(MAX_CONNECTION); if (epollfd == -1) { perror("epoll_create");