Skip to content

Commit 192d9e9

Browse files
committed
HevSocks5Tunnel: Add support for TCP fastopen.
1 parent 2782db6 commit 192d9e9

10 files changed

Lines changed: 59 additions & 48 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ socks5:
117117
# password: 'password'
118118
# Socket mark
119119
# mark: 0
120+
# TCP fastopen
121+
# tcp-fastopen: false
120122

121123
#mapdns:
122124
# Mapped DNS address

conf/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ socks5:
3333
# password: 'password'
3434
# Socket mark
3535
# mark: 0
36+
# TCP fastopen
37+
# tcp-fastopen: false
3638

3739
#mapdns:
3840
# Mapped DNS address

src/hev-config.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ hev_config_parse_socks5 (yaml_document_t *doc, yaml_node_t *base)
181181
const char *pass = NULL;
182182
const char *mark = NULL;
183183
const char *pipe = NULL;
184+
const char *tfso = NULL;
184185

185186
if (!base || YAML_MAPPING_NODE != base->type)
186187
return -1;
@@ -219,6 +220,8 @@ hev_config_parse_socks5 (yaml_document_t *doc, yaml_node_t *base)
219220
pass = value;
220221
else if (0 == strcmp (key, "mark"))
221222
mark = value;
223+
else if (0 == strcmp (key, "tcp-fastopen"))
224+
tfso = value;
222225
}
223226

224227
if (!port) {
@@ -258,6 +261,9 @@ hev_config_parse_socks5 (yaml_document_t *doc, yaml_node_t *base)
258261
if (mark)
259262
srv.mark = strtoul (mark, NULL, 0);
260263

264+
if (tfso)
265+
srv.fastopen = (0 == strcasecmp (tfso, "true")) ? 1 : 0;
266+
261267
return 0;
262268
}
263269

src/hev-config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct _HevConfigServer
2020
short udp_in_udp;
2121
unsigned short port;
2222
unsigned char pipeline;
23+
unsigned char fastopen;
2324
char udp_addr[256];
2425
char addr[256];
2526
};

src/hev-socks5-session-tcp.c

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,6 @@ hev_socks5_session_tcp_new (struct tcp_pcb *pcb, HevTaskMutex *mutex)
192192
return self;
193193
}
194194

195-
static int
196-
hev_socks5_session_tcp_bind (HevSocks5 *self, int fd,
197-
const struct sockaddr *dest)
198-
{
199-
HevConfigServer *srv;
200-
unsigned int mark;
201-
202-
LOG_D ("%p socks5 session tcp bind", self);
203-
204-
srv = hev_config_get_socks5_server ();
205-
mark = srv->mark;
206-
207-
if (mark) {
208-
int res;
209-
210-
res = set_sock_mark (fd, mark);
211-
if (res < 0)
212-
return -1;
213-
}
214-
215-
return 0;
216-
}
217-
218195
static void
219196
hev_socks5_session_tcp_splice (HevSocks5Session *base)
220197
{
@@ -369,7 +346,7 @@ hev_socks5_session_tcp_class (void)
369346
okptr->iface = hev_socks5_session_tcp_iface;
370347

371348
skptr = HEV_SOCKS5_CLASS (kptr);
372-
skptr->binder = hev_socks5_session_tcp_bind;
349+
skptr->binder = hev_socks5_session_bind;
373350

374351
siptr = &kptr->session;
375352
siptr->splicer = hev_socks5_session_tcp_splice;

src/hev-socks5-session-udp.c

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -224,29 +224,6 @@ hev_socks5_session_udp_new (struct udp_pcb *pcb, HevTaskMutex *mutex)
224224
return self;
225225
}
226226

227-
static int
228-
hev_socks5_session_udp_bind (HevSocks5 *self, int fd,
229-
const struct sockaddr *dest)
230-
{
231-
HevConfigServer *srv;
232-
unsigned int mark;
233-
234-
LOG_D ("%p socks5 session udp bind", self);
235-
236-
srv = hev_config_get_socks5_server ();
237-
mark = srv->mark;
238-
239-
if (mark) {
240-
int res;
241-
242-
res = set_sock_mark (fd, mark);
243-
if (res < 0)
244-
return -1;
245-
}
246-
247-
return 0;
248-
}
249-
250227
static uint16_t
251228
hev_socks5_addr_get_port (const HevSocks5Addr *addr)
252229
{
@@ -432,7 +409,7 @@ hev_socks5_session_udp_class (void)
432409
okptr->iface = hev_socks5_session_udp_iface;
433410

434411
skptr = HEV_SOCKS5_CLASS (kptr);
435-
skptr->binder = hev_socks5_session_udp_bind;
412+
skptr->binder = hev_socks5_session_bind;
436413

437414
ckptr = HEV_SOCKS5_CLIENT_CLASS (kptr);
438415
ckptr->set_upstream_addr = hev_socks5_session_udp_set_upstream_addr;

src/hev-socks5-session.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <string.h>
1111

12+
#include "hev-utils.h"
1213
#include "hev-logger.h"
1314
#include "hev-config.h"
1415
#include "hev-socks5-client.h"
@@ -79,6 +80,30 @@ hev_socks5_session_get_node (HevSocks5Session *self)
7980
return iface->get_node (self);
8081
}
8182

83+
int
84+
hev_socks5_session_bind (HevSocks5 *self, int fd, const struct sockaddr *dest)
85+
{
86+
HevConfigServer *srv;
87+
unsigned int mark;
88+
89+
LOG_D ("%p socks5 session bind", self);
90+
91+
srv = hev_config_get_socks5_server ();
92+
mark = srv->mark;
93+
94+
if (mark) {
95+
int res;
96+
97+
res = set_sock_mark (fd, mark);
98+
if (res < 0)
99+
return -1;
100+
}
101+
102+
set_sock_tcp_fastopen (fd, srv->fastopen);
103+
104+
return 0;
105+
}
106+
82107
void *
83108
hev_socks5_session_iface (void)
84109
{

src/hev-socks5-session.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ void hev_socks5_session_terminate (HevSocks5Session *self);
4545
void hev_socks5_session_set_task (HevSocks5Session *self, HevTask *task);
4646
HevListNode *hev_socks5_session_get_node (HevSocks5Session *self);
4747

48+
int hev_socks5_session_bind (HevSocks5 *self, int fd,
49+
const struct sockaddr *dest);
50+
4851
#endif /* __HEV_SOCKS5_SESSION_H__ */

src/misc/hev-utils.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
#include "hev-utils.h"
2828

29+
#ifndef TCP_FASTOPEN_CONNECT
30+
#define TCP_FASTOPEN_CONNECT 30
31+
#endif
32+
2933
void
3034
run_as_daemon (const char *pid_file)
3135
{
@@ -81,6 +85,19 @@ set_sock_mark (int fd, unsigned int mark)
8185
return 0;
8286
}
8387

88+
void
89+
set_sock_tcp_fastopen (int fd, int enable)
90+
{
91+
#ifdef __linux__
92+
int one = 1;
93+
94+
if (!enable)
95+
return;
96+
97+
setsockopt (fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &one, sizeof (one));
98+
#endif
99+
}
100+
84101
int
85102
hev_socks5_addr_from_lwip (HevSocks5Addr *addr, const ip_addr_t *ip, u16_t port)
86103
{

src/misc/hev-utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
void run_as_daemon (const char *pid_file);
1717
int set_limit_nofile (int limit_nofile);
1818
int set_sock_mark (int fd, unsigned int mark);
19+
void set_sock_tcp_fastopen (int fd, int enable);
1920

2021
int hev_socks5_addr_from_lwip (HevSocks5Addr *addr, const ip_addr_t *ip,
2122
u16_t port);

0 commit comments

Comments
 (0)