Optimize the extract_host() function.

This commit is contained in:
aixiao 2022-02-07 19:25:28 +08:00
parent 6403ab9004
commit b1b28b11e4
3 changed files with 35 additions and 30 deletions

View File

@ -20,7 +20,7 @@
// 本地端口 // 本地端口
local_port="127"; 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"; io_flag="R_C_DEC";
// 编码 // 编码

58
ais.c
View File

@ -48,7 +48,7 @@
#define LOG(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0) #define LOG(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
#endif #endif
char remote_host[128]; char remote_host[270];
int remote_port; int remote_port;
int local_port; int local_port;
@ -192,15 +192,16 @@ int isChar(char *string) // string字符串里有字符时返回1
int extract_host(const char *header) int extract_host(const char *header)
{ {
char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号 char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号
if (_p) { if (_p) {
if (strchr(header, '[') || strchr(header, ']')) { if (strchr(header, '[') || strchr(header, ']')) {
char *_p1 = strchr(header, '['); char *_p1 = strchr(header, '[');
char *_p2 = strchr(_p1 + 1, ']'); 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 *_p3 = strchr(_p2 + 1, ' ');
char s_port[270]; char s_port[(_p3 - _p2)];
strncpy(s_port, _p2 + 2, (int)(_p3 - _p2) - 1); strncpy(s_port, _p2 + 2, (int)(_p3 - _p2) - 1);
remote_port = atoi(s_port); remote_port = atoi(s_port);
@ -210,24 +211,29 @@ int extract_host(const char *header)
char *_p1 = strchr(_p, ' '); char *_p1 = strchr(_p, ' ');
char *_p2 = strchr(_p1 + 1, ':'); char *_p2 = strchr(_p1 + 1, ':');
char *_p3 = strchr(_p1 + 1, ' '); char *_p3 = strchr(_p1 + 1, ' ');
if (_p2) { if (_p2) {
char s_port[10]; char s_port[270];
bzero(s_port, 10); bzero(s_port, 270);
strncpy(remote_host, _p1 + 1, (int)(_p2 - _p1) - 1); if ((_p3-_p2) < 0) { // 不带端口, 指向Host的':'
strncpy(s_port, _p2 + 1, (int)(_p3 - _p2) - 1); strncpy(remote_host, _p1 + 1, _p3 - (_p1+1));
remote_port = atoi(s_port); 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 { } else {
strncpy(remote_host, _p1 + 1, (int)(_p3 - _p1) - 1); return -1;
remote_port = 80;
} }
return 0; return 0;
} else { // 在非 CONNECT 方法中解析主机名称及端口号 } else { // 在非 CONNECT 方法中解析主机名称及端口号
char *p = strstr(header, "Host:"); char *p = strstr(header, "Host:");
char *p0 = strstr(header, "host:"); if (p == NULL) {
if (!p && !p0) { p = strstr(header, "host:");
}
if (p == NULL) {
return -1; return -1;
} }
@ -259,21 +265,22 @@ int extract_host(const char *header)
strncpy(url, p5 + 1, p6 - p5 - 1); strncpy(url, p5 + 1, p6 - p5 - 1);
url[p6 - p5 - 1] = '\0'; url[p6 - p5 - 1] = '\0';
if (strstr(url, "http") != NULL) { if (strstr(url, "http") != NULL) {
memcpy(url, url + 7, strlen(url) - 7); // 去除 'http://' memcpy(url, url + 7, p6 - p5 - 1 - 7); // 去除 'http://'
url[strlen(url) - 7] = '\0'; url[(p6 - p5 - 1) - 7] = '\0';
char *p7 = strchr(url, '/'); char *p7 = strchr(url, '/');
if (p7) { // 去除 uri if (p7) { // 去除 uri
url[p7 - url] = '\0'; url[p7 - url] = '\0';
} }
printf("url: %s\n", url); printf("url: %s\n", url);
char *p8 = strchr(url, ']'); char *p8 = strchr(url, ']');
char *p9 = strchr(url, '\0');
if (p8) { if (p8) {
remote_port = atoi(p8 + 2); 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; remote_port = 80;
strncpy(remote_host, url + 1, strlen(url) - strlen(p8) - 1); strncpy(remote_host, url + 1, p8 - (url+1));
} }
} else { // 不包含'['、']'时 } else { // 不包含'['、']'时
remote_port = 80; remote_port = 80;
@ -289,7 +296,7 @@ int extract_host(const char *header)
remote_port = atoi(_p2+2); remote_port = atoi(_p2+2);
if (strlen(_p2) < 3) { if (strlen(_p2) < 3) {
remote_port = 80; 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'; s_port[p_len] = '\0';
remote_port = atoi(s_port); 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); strncpy(remote_host, p + 5 + 1, h_len);
remote_host[h_len] = '\0'; remote_host[h_len] = '\0';
} else { // http请求头不带端口 } 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); strncpy(remote_host, p + 5 + 1, h_len);
remote_host[h_len] = '\0'; remote_host[h_len] = '\0';
remote_port = 80; remote_port = 80;
@ -434,7 +441,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr)
} }
if (extract_host(header_buffer) < 0) { 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; return;
} }
LOG(RED "Host:%s port: %d io_flag:%d\n" NONE, remote_host, remote_port, io_flag); 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 *p = NULL;
char *conffile = "./ais.conf"; char *conffile = "./ais.conf";
//conf *configure = (struct CONF *)malloc(sizeof(struct CONF));
configure = (struct CONF *)malloc(sizeof(struct CONF)); configure = (struct CONF *)malloc(sizeof(struct CONF));
read_conf(conffile, configure); read_conf(conffile, configure);

View File

@ -1,8 +1,7 @@
global { global {
local_port="127"; local_port="129";
io_flag="R_C_DEC"; io_flag="R_C_DEC";
encode=128; encode=128;
IP_RESTRICTION = 0; IP_RESTRICTION = 0;
IP_SEGMENT= 127.0.0.1; IP_SEGMENT= 127.0.0.1 2408:8221:9913:d040:4804:5d12:4c4a:c3a;
} }