Browse Source

Refine ip initialization

pull/2135/merge
Max Lv 6 years ago
parent
commit
9a56155513
1 changed files with 21 additions and 17 deletions
  1. 38
      src/local.c

38
src/local.c

@ -358,6 +358,7 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
} }
char host[257], ip[INET6_ADDRSTRLEN], port[16]; char host[257], ip[INET6_ADDRSTRLEN], port[16];
int resolved = 0;
buffer_t *abuf = server->abuf; buffer_t *abuf = server->abuf;
abuf->idx = 0; abuf->idx = 0;
@ -381,6 +382,7 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
inet_ntop(AF_INET, (const void *)(buf->data + request_len), inet_ntop(AF_INET, (const void *)(buf->data + request_len),
ip, INET_ADDRSTRLEN); ip, INET_ADDRSTRLEN);
sprintf(port, "%d", p); sprintf(port, "%d", p);
resolved = 1;
} }
} else if (atyp == 3) { } else if (atyp == 3) {
// Domain name // Domain name
@ -413,6 +415,7 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
inet_ntop(AF_INET6, (const void *)(buf->data + request_len), inet_ntop(AF_INET6, (const void *)(buf->data + request_len),
ip, INET6_ADDRSTRLEN); ip, INET6_ADDRSTRLEN);
sprintf(port, "%d", p); sprintf(port, "%d", p);
resolved = 1;
} }
} else { } else {
LOGE("unsupported addrtype: %d", request->atyp); LOGE("unsupported addrtype: %d", request->atyp);
@ -471,9 +474,6 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
#endif #endif
) { ) {
int bypass = 0; int bypass = 0;
#ifndef __ANDROID__
int resolved = 0;
#endif
struct sockaddr_storage storage; struct sockaddr_storage storage;
memset(&storage, 0, sizeof(struct sockaddr_storage)); memset(&storage, 0, sizeof(struct sockaddr_storage));
int err; int err;
@ -491,18 +491,19 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
if (atyp == 3) { // resolve domain so we can bypass domain with geoip if (atyp == 3) { // resolve domain so we can bypass domain with geoip
err = get_sockaddr(host, port, &storage, 0, ipv6first); err = get_sockaddr(host, port, &storage, 0, ipv6first);
if (err != -1) { if (err != -1) {
resolved = 1;
switch (((struct sockaddr *)&storage)->sa_family) { switch (((struct sockaddr *)&storage)->sa_family) {
case AF_INET: case AF_INET:
{ {
struct sockaddr_in *addr_in = (struct sockaddr_in *)&storage; struct sockaddr_in *addr_in = (struct sockaddr_in *)&storage;
inet_ntop(AF_INET, &(addr_in->sin_addr), ip, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(addr_in->sin_addr), ip, INET_ADDRSTRLEN);
resolved = 1;
break; break;
} }
case AF_INET6: case AF_INET6:
{ {
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&storage; struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&storage;
inet_ntop(AF_INET6, &(addr_in6->sin6_addr), ip, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &(addr_in6->sin6_addr), ip, INET6_ADDRSTRLEN);
resolved = 1;
break; break;
} }
default: default:
@ -511,17 +512,19 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
} }
} }
#endif #endif
int ip_match = acl_match_host(ip);
switch (get_acl_mode()) {
case BLACK_LIST:
if (ip_match > 0)
bypass = 1; // bypass IPs in black list
break;
case WHITE_LIST:
bypass = 1;
if (ip_match < 0)
bypass = 0; // proxy IPs in white list
break;
if (resolved) {
int ip_match = acl_match_host(ip);
switch (get_acl_mode()) {
case BLACK_LIST:
if (ip_match > 0)
bypass = 1; // bypass IPs in black list
break;
case WHITE_LIST:
bypass = 1;
if (ip_match < 0)
bypass = 0; // proxy IPs in white list
break;
}
} }
} }
@ -535,11 +538,12 @@ server_handshake(EV_P_ ev_io *w, buffer_t *buf)
LOGI("bypass [%s]:%s", ip, port); LOGI("bypass [%s]:%s", ip, port);
} }
#ifndef __ANDROID__ #ifndef __ANDROID__
if (atyp == 3 && resolved != 1)
if (atyp == 3 && !resolved)
err = get_sockaddr(host, port, &storage, 0, ipv6first); err = get_sockaddr(host, port, &storage, 0, ipv6first);
else else
#endif #endif
err = get_sockaddr(ip, port, &storage, 0, ipv6first);
if (resolved)
err = get_sockaddr(ip, port, &storage, 0, ipv6first);
if (err != -1) { if (err != -1) {
remote = create_remote(server->listener, (struct sockaddr *)&storage); remote = create_remote(server->listener, (struct sockaddr *)&storage);
if (remote != NULL) if (remote != NULL)

Loading…
Cancel
Save