Skip to content

Commit

Permalink
Add delayed connect to ss-local as well
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed Mar 17, 2017
1 parent f2b585a commit fa82628
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ int send_traffic_stat(uint64_t tx, uint64_t rx);
#define STAGE_HANDSHAKE 1 /* Handshake with client */
#define STAGE_PARSE 2 /* Parse the header */
#define STAGE_RESOLVE 4 /* Resolve the hostname */
#define STAGE_STREAM 5 /* Stream between client and server */
#define STAGE_WAIT 5 /* Wait for more data */
#define STAGE_STREAM 6 /* Stream between client and server */

/* Vals for long options */
enum {
Expand Down
54 changes: 38 additions & 16 deletions src/local.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ free_connections(struct ev_loop *loop)
}
}

static void
delayed_connect_cb(EV_P_ ev_timer *watcher, int revents)
{
server_t *server = cork_container_of(watcher, server_t,
delayed_connect_watcher);

server->stage = STAGE_WAIT;
server_recv_cb(EV_A_ & server->recv_ctx->io, revents);
}

static void
server_recv_cb(EV_P_ ev_io *w, int revents)
{
Expand All @@ -262,29 +272,32 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
buf = remote->buf;
}

r = recv(server->fd, buf->data + buf->len, BUF_SIZE - buf->len, 0);
if (server->stage != STAGE_WAIT) {
r = recv(server->fd, buf->data + buf->len, BUF_SIZE - buf->len, 0);

if (r == 0) {
// connection closed
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
} else if (r == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// no data
// continue to wait for recv
return;
} else {
if (verbose)
ERROR("server_recv_cb_recv");
if (r == 0) {
// connection closed
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
} else if (r == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// no data
// continue to wait for recv
return;
} else {
if (verbose)
ERROR("server_recv_cb_recv");
close_and_free_remote(EV_A_ remote);
close_and_free_server(EV_A_ server);
return;
}
}
buf->len += r;
} else {
server->stage = STAGE_STREAM;
}

buf->len += r;

while (1) {
// local socks5 server
if (server->stage == STAGE_STREAM) {
Expand All @@ -294,6 +307,8 @@ server_recv_cb(EV_P_ ev_io *w, int revents)
return;
}

ev_timer_stop(EV_A_ & server->delayed_connect_watcher);

// insert shadowsocks header
if (!remote->direct) {
#ifdef ANDROID
Expand Down Expand Up @@ -743,6 +758,10 @@ server_recv_cb(EV_P_ ev_io *w, int revents)

server->remote = remote;
remote->server = server;

ev_timer_start(EV_A_ & server->delayed_connect_watcher);

return;
}
}
}
Expand Down Expand Up @@ -1048,6 +1067,9 @@ new_server(int fd)
ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ);
ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE);

ev_timer_init(&server->delayed_connect_watcher,
delayed_connect_cb, 0.05, 0);

cork_dllist_add(&connections, &server->entries);

return server;
Expand Down
2 changes: 2 additions & 0 deletions src/local.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ typedef struct server {
buffer_t *buf;
buffer_t *abuf;

ev_timer delayed_connect_watcher;

struct cork_dllist_item entries;
} server_t;

Expand Down

0 comments on commit fa82628

Please sign in to comment.