diff --git a/error.h b/error.h index 10ce12b..0352f7a 100644 --- a/error.h +++ b/error.h @@ -64,4 +64,11 @@ static inline const int unie(int e) #define LOG(s, str, ...) \ if (params.debug >= s) \ fprintf(stderr, str, ##__VA_ARGS__) -#endif \ No newline at end of file +#endif + +#define INIT_ADDR_STR(dst) \ + char ADDR_STR[INET_ADDRSTRLEN + 1]; \ + if (dst.sa.sa_family == AF_INET) \ + inet_ntop(AF_INET, &dst.in.sin_addr, ADDR_STR, sizeof(ADDR_STR)); \ + else \ + inet_ntop(AF_INET6, &dst.in6.sin6_addr, ADDR_STR, sizeof(ADDR_STR)); diff --git a/extend.c b/extend.c index 892f1bc..2ab8f64 100644 --- a/extend.c +++ b/extend.c @@ -128,7 +128,19 @@ bool check_host(struct mphdr *hosts, struct eval *val) len = parse_http(val->buff.data, val->buff.size, &host, 0); } assert(len == 0 || host != 0); - return (len > 0) && mem_get(hosts, host, len) != 0; + if (len <= 0) { + return 0; + } + char *e = host + len; + for (; host < e; host++) { + if (mem_get(hosts, host, e - host)) { + return 1; + } + if (!(host = memchr(host, '.', e - host))) { + return 0; + } + } + return 0; } @@ -259,13 +271,15 @@ int on_tunnel_check(struct poolhd *pool, struct eval *val, if (!pair->cache) { return 0; } + struct sockaddr_ina *addr = (struct sockaddr_ina *)&val->in6; + if (m == 0) { LOG(LOG_S, "delete ip: m=%d\n", m); } else { - LOG(LOG_S, "save ip: m=%d\n", m); + INIT_ADDR_STR((*addr)); + LOG(LOG_S, "save ip: %s, m=%d\n", ADDR_STR, m); } - return mode_add_get( - (struct sockaddr_ina *)&val->in6, m); + return mode_add_get(addr, m); } diff --git a/proxy.c b/proxy.c index c4495bc..7200620 100644 --- a/proxy.c +++ b/proxy.c @@ -505,6 +505,7 @@ static inline int on_accept(struct poolhd *pool, struct eval *val) uniperror("accept"); return -1; } + LOG(LOG_L, "accept: fd=%d\n", c); #ifndef __linux__ #ifdef _WIN32 unsigned long mode = 1; @@ -753,8 +754,14 @@ static inline int on_request(struct poolhd *pool, struct eval *val, int en = get_e(); if (resp_error(val->fd, en ? en : error, val->flag) < 0) uniperror("send"); + LOG(LOG_S, "ss error: %d\n", en); return -1; } + if (params.debug) { + INIT_ADDR_STR(dst); + LOG(LOG_L, "new conn: fd=%d, addr=%s:%d\n", + val->pair->fd, ADDR_STR, ntohs(dst.in.sin_port)); + } return 0; } @@ -787,6 +794,13 @@ static inline int on_connect(struct poolhd *pool, struct eval *val, int e) } +void close_conn(struct poolhd *pool, struct eval *val) +{ + LOG(LOG_L, "close: fds=%d,%d\n", val->fd, val->pair ? val->pair->fd : -1); + del_event(pool, val); +} + + int event_loop(int srvfd) { size_t bfsize = params.bfsize; @@ -835,39 +849,39 @@ int event_loop(int srvfd) case EV_REQUEST: if ((etype & POLLHUP) || on_request(pool, val, buffer, bfsize)) - del_event(pool, val); + close_conn(pool, val); continue; case EV_PRE_TUNNEL: if (on_tunnel_check(pool, val, buffer, bfsize, etype & POLLOUT)) - del_event(pool, val); + close_conn(pool, val); continue; case EV_TUNNEL: if (on_tunnel(pool, val, buffer, bfsize, etype)) - del_event(pool, val); + close_conn(pool, val); continue; case EV_UDP_TUNNEL: if (on_udp_tunnel(val, buffer, bfsize)) - del_event(pool, val); + close_conn(pool, val); continue; case EV_CONNECT: if (on_connect(pool, val, etype & POLLERR)) - del_event(pool, val); + close_conn(pool, val); continue; case EV_DESYNC: if (on_desync(pool, val, buffer, bfsize, etype & POLLOUT)) - del_event(pool, val); + close_conn(pool, val); continue; case EV_IGNORE: if (etype & (POLLHUP | POLLERR | POLLRDHUP)) - del_event(pool, val); + close_conn(pool, val); continue; default: