From b1b28b11e4c461ea954f03d00269e17e8b0f5c9e Mon Sep 17 00:00:00 2001 From: aixiao Date: Mon, 7 Feb 2022 19:25:28 +0800 Subject: [PATCH] Optimize the extract_host() function. --- README.md | 2 +- ais.c | 58 ++++++++++++++++++++++++++++++------------------------- ais.conf | 5 ++--- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 663a2c3..9a18c8f 100755 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ // 本地端口 local_port="127"; - // IO标志 R_C_DEC、W_S_ENC. W_S_ENC在转发数据时对数据进行编码, R_C_DEC 接收数据时解码数据 + // IO标志 R_C_DEC、W_S_ENC、FLG_NONE. W_S_ENC 在转发数据时对数据进行编码, R_C_DEC 接收数据时解码数据, FLG_NONE 正常数据流不进行编解码 io_flag="R_C_DEC"; // 编码 diff --git a/ais.c b/ais.c index 93308dd..dc51ea8 100755 --- a/ais.c +++ b/ais.c @@ -48,7 +48,7 @@ #define LOG(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0) #endif -char remote_host[128]; +char remote_host[270]; int remote_port; int local_port; @@ -192,15 +192,16 @@ int isChar(char *string) // string字符串里有字符时返回1 int extract_host(const char *header) { + char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号 if (_p) { if (strchr(header, '[') || strchr(header, ']')) { char *_p1 = strchr(header, '['); char *_p2 = strchr(_p1 + 1, ']'); - strncpy(remote_host, _p1 + 1, (int)(_p2 - _p1) - 1); - + strncpy(remote_host, _p1 + 1, _p2 - (_p1 + 1)); + char *_p3 = strchr(_p2 + 1, ' '); - char s_port[270]; + char s_port[(_p3 - _p2)]; strncpy(s_port, _p2 + 2, (int)(_p3 - _p2) - 1); remote_port = atoi(s_port); @@ -210,24 +211,29 @@ int extract_host(const char *header) char *_p1 = strchr(_p, ' '); char *_p2 = strchr(_p1 + 1, ':'); char *_p3 = strchr(_p1 + 1, ' '); - if (_p2) { - char s_port[10]; - bzero(s_port, 10); - - strncpy(remote_host, _p1 + 1, (int)(_p2 - _p1) - 1); - strncpy(s_port, _p2 + 1, (int)(_p3 - _p2) - 1); - remote_port = atoi(s_port); - + if (_p2) { + char s_port[270]; + bzero(s_port, 270); + + if ((_p3-_p2) < 0) { // 不带端口, 指向Host的':' + strncpy(remote_host, _p1 + 1, _p3 - (_p1+1)); + remote_port = 80; + } else { // 带端口正常情况 + strncpy(remote_host, _p1 + 1, _p2 - (_p1+1)); + strncpy(s_port, _p2 + 1, (int)(_p3 - _p2) - 1); + remote_port = atoi(s_port); + } } else { - strncpy(remote_host, _p1 + 1, (int)(_p3 - _p1) - 1); - remote_port = 80; + return -1; } return 0; } else { // 在非 CONNECT 方法中解析主机名称及端口号 char *p = strstr(header, "Host:"); - char *p0 = strstr(header, "host:"); - if (!p && !p0) { + if (p == NULL) { + p = strstr(header, "host:"); + } + if (p == NULL) { return -1; } @@ -259,21 +265,22 @@ int extract_host(const char *header) strncpy(url, p5 + 1, p6 - p5 - 1); url[p6 - p5 - 1] = '\0'; if (strstr(url, "http") != NULL) { - memcpy(url, url + 7, strlen(url) - 7); // 去除 'http://' - url[strlen(url) - 7] = '\0'; + memcpy(url, url + 7, p6 - p5 - 1 - 7); // 去除 'http://' + url[(p6 - p5 - 1) - 7] = '\0'; char *p7 = strchr(url, '/'); if (p7) { // 去除 uri url[p7 - url] = '\0'; } printf("url: %s\n", url); char *p8 = strchr(url, ']'); + char *p9 = strchr(url, '\0'); if (p8) { remote_port = atoi(p8 + 2); - strncpy(remote_host, url + 1, strlen(url) - strlen(p8) - 1); + strncpy(remote_host, url + 1, p8 - (url+1)); - if (strlen(p8) < 3) { // 如果p8为 ']' 时, 长度为1 + if ((p9-p8) == 1) { // 如果p8为 ']' 时, 长度为1 remote_port = 80; - strncpy(remote_host, url + 1, strlen(url) - strlen(p8) - 1); + strncpy(remote_host, url + 1, p8 - (url+1)); } } else { // 不包含'['、']'时 remote_port = 80; @@ -289,7 +296,7 @@ int extract_host(const char *header) remote_port = atoi(_p2+2); if (strlen(_p2) < 3) { remote_port = 80; - strncpy(remote_host, s_host+1, strlen(s_host)-strlen(_p2)-1); + strncpy(remote_host, s_host+1, _p2 - (s_host+1)); } } @@ -307,11 +314,11 @@ int extract_host(const char *header) s_port[p_len] = '\0'; remote_port = atoi(s_port); - int h_len = (int)(p2 - p - 5 - 1); + int h_len = (int)(p2 - (p + 6)); strncpy(remote_host, p + 5 + 1, h_len); remote_host[h_len] = '\0'; } else { // http请求头不带端口 - int h_len = (int)(p1 - p - 5 - 1); + int h_len = (int)((p1-1) - (p + 6)); strncpy(remote_host, p + 5 + 1, h_len); remote_host[h_len] = '\0'; remote_port = 80; @@ -434,7 +441,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr) } if (extract_host(header_buffer) < 0) { - LOG(RED "Cannot extract host field,bad http protrotol" NONE); + LOG(RED "Cannot extract host field, bad http protrotol" NONE); return; } LOG(RED "Host:%s port: %d io_flag:%d\n" NONE, remote_host, remote_port, io_flag); @@ -835,7 +842,6 @@ int _main(int argc, char *argv[]) char *p = NULL; char *conffile = "./ais.conf"; - //conf *configure = (struct CONF *)malloc(sizeof(struct CONF)); configure = (struct CONF *)malloc(sizeof(struct CONF)); read_conf(conffile, configure); diff --git a/ais.conf b/ais.conf index 14797ab..68127db 100755 --- a/ais.conf +++ b/ais.conf @@ -1,8 +1,7 @@ global { - local_port="127"; + local_port="129"; io_flag="R_C_DEC"; encode=128; IP_RESTRICTION = 0; - IP_SEGMENT= 127.0.0.1; + IP_SEGMENT= 127.0.0.1 2408:8221:9913:d040:4804:5d12:4c4a:c3a; } -