Skip to content

Commit

Permalink
Simplify TFO support
Browse files Browse the repository at this point in the history
  • Loading branch information
semigodking committed Jul 13, 2018
1 parent d608c45 commit e7ff18b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
21 changes: 6 additions & 15 deletions shadowsocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ typedef struct ss_client_t {
struct enc_ctx d_ctx;
short e_ctx_init;
short d_ctx_init;
bool tfo;
size_t tfo_size;
char tfo_buff[512];
} ss_client;

typedef struct ss_instance_t {
Expand Down Expand Up @@ -278,7 +275,6 @@ static void ss_relay_readcb(struct bufferevent *buffev, void *_arg)
static void ss_relay_connected(struct bufferevent *buffev, void *_arg)
{
redsocks_client *client = _arg;
ss_client *sclient = (void*)(client + 1);

assert(buffev == client->relay);
assert(client->state == ss_new);
Expand Down Expand Up @@ -309,9 +305,6 @@ static void ss_relay_connected(struct bufferevent *buffev, void *_arg)
ss_relay_writecb,
redsocks_event_error,
client);
if(!sclient->tfo) {
bufferevent_write(client->relay, &sclient->tfo_buff[0], sclient->tfo_size);
}
// Write any data received from client side to relay.
if (evbuffer_get_length(bufferevent_get_input(client->client)))
ss_relay_writecb(client->relay, client);
Expand All @@ -328,6 +321,7 @@ static int ss_connect_relay(redsocks_client *client)
ss_header_ipv4 header;
struct timeval tv;
size_t len = 0;
char buff[64+sizeof(header)];

if (enc_ctx_init(&ss->info, &sclient->e_ctx, 1)) {
log_error(LOG_ERR, "Shadowsocks failed to initialize encryption context.");
Expand All @@ -348,28 +342,25 @@ static int ss_connect_relay(redsocks_client *client)
header.addr = client->destaddr.sin_addr.s_addr;
header.port = client->destaddr.sin_port;
len += sizeof(header);
size_t sz = sizeof(sclient->tfo_buff);
if (!ss_encrypt(&sclient->e_ctx, (char *)&header, len, &sclient->tfo_buff[0], &sz)) {
size_t sz = sizeof(buff);
if (!ss_encrypt(&sclient->e_ctx, (char *)&header, len, &buff[0], &sz)) {
log_error(LOG_ERR, "Encryption error.");
redsocks_drop_client(client);
return -1;
}
sclient->tfo_size = len = sz;
len = sz;

tv.tv_sec = client->instance->config.timeout;
tv.tv_usec = 0;
client->relay = red_connect_relay_tfo(interface, &client->instance->config.relayaddr,
NULL, ss_relay_connected, redsocks_event_error, client,
&tv, &sclient->tfo_buff[0], &sz);
&tv, &buff[0], &sz);

if (!client->relay) {
redsocks_drop_client(client);
return -1;
}
if (sz && sz == len) {
sclient->tfo = true;
}
else if (sz) {
else if (sz && sz != len) {
log_error(LOG_ERR, "Unexpected length of data sent.");
redsocks_drop_client(client);
return -1;
Expand Down
7 changes: 6 additions & 1 deletion utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,17 @@ struct bufferevent* red_connect_relay_tfo(const char *ifname,
fallback:
#endif

*len = 0; // Nothing sent, caller needs to write data again when connection is setup.
error = connect(relay_fd, (struct sockaddr*)addr, sizeof(*addr));
if (error && errno != EINPROGRESS) {
log_errno(LOG_NOTICE, "connect");
goto fail;
}
// write data to evbuffer so that data can be sent when connection is set up
if (bufferevent_write(retval, data, *len) != 0) {
log_errno(LOG_NOTICE, "bufferevent_write");
*len = 0; // Nothing sent, caller needs to write data again when connection is setup.
goto fail;
}
}
return retval;

Expand Down

0 comments on commit e7ff18b

Please sign in to comment.