mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-11-22 13:43:50 +00:00
refine local proxy for acl support
This commit is contained in:
@@ -2,6 +2,7 @@ AM_CFLAGS = -g -O2 -Wall -Werror -Wno-deprecated-declarations -fno-strict-aliasi
|
||||
AM_CFLAGS += -I$(top_builddir)/libev
|
||||
AM_CFLAGS += -I$(top_builddir)/libasyncns
|
||||
AM_CFLAGS += -I$(top_builddir)/libipset/include
|
||||
AM_CFLAGS += -I$(top_builddir)/libcork/include
|
||||
|
||||
AM_LDFLAGS = -static
|
||||
|
||||
|
||||
+2
-1
@@ -340,7 +340,8 @@ top_srcdir = @top_srcdir@
|
||||
AM_CFLAGS = -g -O2 -Wall -Werror -Wno-deprecated-declarations \
|
||||
-fno-strict-aliasing -std=gnu99 -D_GNU_SOURCE \
|
||||
-I$(top_builddir)/libev -I$(top_builddir)/libasyncns \
|
||||
-I$(top_builddir)/libipset/include
|
||||
-I$(top_builddir)/libipset/include \
|
||||
-I$(top_builddir)/libcork/include
|
||||
AM_LDFLAGS = -static
|
||||
SS_COMMON_LIBS = $(top_builddir)/libev/libev.la \
|
||||
$(top_builddir)/libcork/libcork.la \
|
||||
|
||||
@@ -1,7 +1,47 @@
|
||||
#include <ipset/ipset.h>
|
||||
#include <utils.h>
|
||||
|
||||
struct ip_set *init_acl(void)
|
||||
static struct ip_set set;
|
||||
|
||||
int init_acl(const char *path)
|
||||
{
|
||||
struct ip_set *ipset = ipset_new();
|
||||
return ipset;
|
||||
ipset_init_library();
|
||||
ipset_init(&set);
|
||||
|
||||
FILE *f = fopen(path, "r");
|
||||
if (f == NULL) FATAL("Invalid acl path.");
|
||||
|
||||
char line[256];
|
||||
while(!feof(f))
|
||||
{
|
||||
if (fgets(line, 256, f))
|
||||
{
|
||||
char host[256];
|
||||
int cidr;
|
||||
sscanf(line, "%s/%d", host, &cidr);
|
||||
struct cork_ipv4 addr;
|
||||
int err = cork_ipv4_init(&addr, host);
|
||||
if (err) continue;
|
||||
ipset_ipv4_add_network(&set, &addr, cidr);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void free_acl(void)
|
||||
{
|
||||
ipset_done(&set);
|
||||
}
|
||||
|
||||
int is_bypass(const char* host)
|
||||
{
|
||||
struct cork_ipv4 addr;
|
||||
int err = cork_ipv4_init(&addr, host);
|
||||
if (err) return 0;
|
||||
struct cork_ip ip;
|
||||
cork_ip_from_ipv4(&ip, &addr);
|
||||
return ipset_contains_ip(&set, &ip);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef _ACL_H
|
||||
#define _ACL_H
|
||||
|
||||
int init_acl(const char *path);
|
||||
void free_acl(void);
|
||||
int is_addr_bypass(const char* host);
|
||||
|
||||
#endif // _ACL_H
|
||||
+60
-45
@@ -50,7 +50,6 @@
|
||||
int verbose = 0;
|
||||
int udprelay = 0;
|
||||
static int fast_open = 0;
|
||||
static struct addrinfo *remote_res = NULL;
|
||||
|
||||
#ifndef __MINGW32__
|
||||
static int setnonblocking(int fd)
|
||||
@@ -196,7 +195,7 @@ static void server_recv_cb (EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
#ifdef TCP_FASTOPEN
|
||||
s = sendto(remote->fd, remote->buf, r, MSG_FASTOPEN,
|
||||
remote_res->ai_addr, remote_res->ai_addrlen);
|
||||
remote->addr_info->ai_addr, remote->addr_info->ai_addrlen);
|
||||
if (s == -1)
|
||||
{
|
||||
if (errno == EINPROGRESS)
|
||||
@@ -717,6 +716,7 @@ struct remote* new_remote(int fd, int timeout)
|
||||
remote->send_ctx->connected = 0;
|
||||
remote->buf_len = 0;
|
||||
remote->buf_idx = 0;
|
||||
remote->addr_info = NULL;
|
||||
return remote;
|
||||
}
|
||||
|
||||
@@ -732,6 +732,10 @@ static void free_remote(struct remote *remote)
|
||||
{
|
||||
free(remote->buf);
|
||||
}
|
||||
if (remote->addr_info)
|
||||
{
|
||||
freeaddrinfo(remote->addr_info);
|
||||
}
|
||||
free(remote->recv_ctx);
|
||||
free(remote->send_ctx);
|
||||
free(remote);
|
||||
@@ -823,6 +827,57 @@ static void close_and_free_server(EV_P_ struct server *server)
|
||||
}
|
||||
}
|
||||
|
||||
static struct remote* connect_to_remote(struct listen_ctx *listener)
|
||||
{
|
||||
int opt = 1;
|
||||
int sockfd;
|
||||
struct addrinfo *remote_res;
|
||||
|
||||
struct addrinfo hints;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
int index = rand() % listener->remote_num;
|
||||
if (verbose)
|
||||
{
|
||||
LOGD("connect to %s:%s", listener->remote_addr[index].host,
|
||||
listener->remote_addr[index].port);
|
||||
}
|
||||
int err = getaddrinfo(listener->remote_addr[index].host,
|
||||
listener->remote_addr[index].port,
|
||||
&hints, &remote_res);
|
||||
if (err)
|
||||
{
|
||||
ERROR("getaddrinfo");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sockfd = socket(remote_res->ai_family, remote_res->ai_socktype,
|
||||
remote_res->ai_protocol);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
ERROR("socket");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
|
||||
#ifdef SO_NOSIGPIPE
|
||||
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
|
||||
#endif
|
||||
|
||||
// Setup
|
||||
setnonblocking(sockfd);
|
||||
#ifdef SET_INTERFACE
|
||||
if (listener->iface) setinterface(sockfd, listener->iface);
|
||||
#endif
|
||||
|
||||
struct remote *remote = new_remote(sockfd, listener->timeout);
|
||||
remote->addr_info = remote_res;
|
||||
|
||||
return remote;
|
||||
}
|
||||
|
||||
|
||||
static void accept_cb (EV_P_ ev_io *w, int revents)
|
||||
{
|
||||
struct listen_ctx *listener = (struct listen_ctx *)w;
|
||||
@@ -839,56 +894,16 @@ static void accept_cb (EV_P_ ev_io *w, int revents)
|
||||
setsockopt(serverfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
|
||||
#endif
|
||||
|
||||
int sockfd;
|
||||
struct remote *remote = connect_to_remote(listener);
|
||||
|
||||
if (remote_res == NULL)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
int index = rand() % listener->remote_num;
|
||||
if (verbose)
|
||||
{
|
||||
LOGD("connect to %s:%s", listener->remote_addr[index].host,
|
||||
listener->remote_addr[index].port);
|
||||
}
|
||||
int err = getaddrinfo(listener->remote_addr[index].host,
|
||||
listener->remote_addr[index].port,
|
||||
&hints, &remote_res);
|
||||
if (err)
|
||||
{
|
||||
ERROR("getaddrinfo");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
sockfd = socket(remote_res->ai_family, remote_res->ai_socktype,
|
||||
remote_res->ai_protocol);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
ERROR("socket");
|
||||
return;
|
||||
}
|
||||
|
||||
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
|
||||
#ifdef SO_NOSIGPIPE
|
||||
setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
|
||||
#endif
|
||||
|
||||
// Setup
|
||||
setnonblocking(sockfd);
|
||||
#ifdef SET_INTERFACE
|
||||
if (listener->iface) setinterface(sockfd, listener->iface);
|
||||
#endif
|
||||
if (remote == NULL) return;
|
||||
|
||||
struct server *server = new_server(serverfd, listener->method);
|
||||
struct remote *remote = new_remote(sockfd, listener->timeout);
|
||||
server->remote = remote;
|
||||
remote->server = server;
|
||||
if (!fast_open)
|
||||
{
|
||||
connect(sockfd, remote_res->ai_addr, remote_res->ai_addrlen);
|
||||
connect(remote->fd, remote->addr_info->ai_addr, remote->addr_info->ai_addrlen);
|
||||
// listen to remote connected event
|
||||
ev_io_start(EV_A_ &remote->send_ctx->io);
|
||||
ev_timer_start(EV_A_ &remote->send_ctx->watcher);
|
||||
|
||||
@@ -59,6 +59,7 @@ struct remote
|
||||
struct remote_ctx *recv_ctx;
|
||||
struct remote_ctx *send_ctx;
|
||||
struct server *server;
|
||||
struct addrinfo *addr_info;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user