Support TFO

This commit is contained in:
ruti 2024-03-20 02:23:56 +03:00
parent 542a04bffb
commit ca03b1d311
5 changed files with 57 additions and 15 deletions

View File

@ -251,22 +251,24 @@ int send_disorder(int sfd,
char *buffer, long pos, int fa)
{
int bttl = 1;
int status = 0;
if (setttl(sfd, bttl, fa) < 0) {
return -1;
}
if (send(sfd, buffer, pos, 0) < 0) {
uniperror("send");
return -1;
status = -1;
}
if (setttl(sfd, params.def_ttl, fa) < 0) {
return -1;
}
return 0;
return status;
}
int desync(int sfd, char *buffer, size_t bfsize,
ssize_t n, struct sockaddr *dst, int dp_c)
ssize_t n, ssize_t offset, struct sockaddr *dst, int dp_c)
{
struct desync_params dp = params.dp[dp_c];
@ -324,7 +326,7 @@ int desync(int sfd, char *buffer, size_t bfsize,
return -1;
}
}
long lp = 0;
long lp = offset;
if (!type && params.de_known) {
}
@ -347,7 +349,11 @@ int desync(int sfd, char *buffer, size_t bfsize,
else if (pos < 0) {
pos += n;
}
if (pos <= 0 || pos >= n || pos <= lp) {
// after EAGAIN
if (pos <= offset) {
continue;
}
else if (pos <= 0 || pos >= n || pos <= lp) {
LOG(LOG_E, "split cancel: pos=%ld-%ld, n=%ld\n", lp, pos, n);
break;
}
@ -373,12 +379,13 @@ int desync(int sfd, char *buffer, size_t bfsize,
case DESYNC_SPLIT:
default:
if (send(sfd, buffer + lp, pos - lp, 0) < 0) {
uniperror("send");
return -1;
}
s = send(sfd, buffer + lp, pos - lp, 0);
}
if (s) {
if (s < 0) {
if (part.m != DESYNC_FAKE
&& get_e() == EAGAIN) {
return lp;
}
return -1;
}
lp = pos;
@ -386,9 +393,12 @@ int desync(int sfd, char *buffer, size_t bfsize,
if (lp < n) {
LOG((lp ? LOG_S : LOG_L), "send: pos=%ld-%ld\n", lp, n);
if (send(sfd, buffer + lp, n - lp, 0) < 0) {
if (get_e() == EAGAIN) {
return lp;
}
uniperror("send");
return -1;
}
}
return 0;
return n;
}

View File

@ -1 +1 @@
int desync(int sfd, char *buffer, size_t bfsize, ssize_t n, struct sockaddr *dst, int dp_c);
int desync(int sfd, char *buffer, size_t bfsize, ssize_t n, ssize_t offset, struct sockaddr *dst, int dp_c);

12
main.c
View File

@ -16,6 +16,7 @@
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#else
#include <ws2tcpip.h>
#define close(fd) closesocket(fd)
@ -42,6 +43,7 @@ struct params params = {
.custom_ttl = 0,
.de_known = 0,
.tfo = 0,
.timeout = 0,
.cache_ttl = 100800,
.ipv6 = 1,
@ -66,6 +68,9 @@ const char help_text[] = {
" -g, --def-ttl <num> TTL for all outgoing connections\n"
// desync options
" -K, --desync-known Desync only HTTP and TLS with SNI\n"
#ifdef TCP_FASTOPEN_CONNECT
" -F, --tfo Enable TCP Fast Open\n"
#endif
" -A, --auto Try desync params after this option\n"
" -u, --cache-ttl <sec> Lifetime of cached desync params for IP\n"
#ifdef TIMEOUT_SUPPORT
@ -103,6 +108,9 @@ const struct option options[] = {
{"debug", 1, 0, 'x'},
{"desync-known ", 0, 0, 'K'},
#ifdef TCP_FASTOPEN_CONNECT
{"tfo ", 0, 0, 'F'},
#endif
{"auto", 0, 0, 'A'},
{"cache-ttl", 1, 0, 'u'},
#ifdef TIMEOUT_SUPPORT
@ -391,6 +399,10 @@ int main(int argc, char **argv)
params.de_known = 1;
break;
case 'F':
params.tfo = 1;
break;
case 'A':
dp = add((void *)&params.dp, &params.dp_count,
sizeof(struct desync_params));

View File

@ -46,6 +46,7 @@ struct params {
int def_ttl;
char custom_ttl;
char tfo;
unsigned int timeout;
long cache_ttl;
int spos_n;

25
proxy.c
View File

@ -333,6 +333,15 @@ int create_conn(struct poolhd *pool,
return -1;
}
#endif
#ifdef TCP_FASTOPEN_CONNECT
int yes = 1;
if (params.tfo && setsockopt(sfd, IPPROTO_TCP,
TCP_FASTOPEN_CONNECT, (char *)&yes, sizeof(yes))) {
uniperror("setsockopt TCP_FASTOPEN_CONNECT");
close(sfd);
return -1;
}
#endif
int one = 1;
if (setsockopt(sfd, IPPROTO_TCP,
TCP_NODELAY, (char *)&one, sizeof(one))) {
@ -633,7 +642,7 @@ int on_tunnel_check(struct poolhd *pool, struct eval *val,
ssize_t n = recv(val->fd, buffer, bfsize, 0);
if (n < 1) {
uniperror("recv");
switch (unie(get_e())) {
switch (get_e()) {
case ECONNRESET:
case ETIMEDOUT:
break;
@ -719,10 +728,20 @@ int on_desync(struct poolhd *pool, struct eval *val,
set_timeout(val->pair->fd, params.timeout)) {
return -1;
}
if (desync(val->pair->fd, buffer, bfsize, n,
(struct sockaddr *)&val->pair->in6, m)) {
ssize_t sn = desync(val->pair->fd, buffer, bfsize,
n, val->buff.offset, (struct sockaddr *)&val->pair->in6, m);
if (sn < 0) {
return -1;
}
if (sn != n) {
val->buff.offset = sn;
if (mod_etype(pool, val->pair, POLLOUT, 1)) {
uniperror("mod_etype");
return -1;
}
val->pair->type = EV_DESYNC;
return 0;
}
val->type = EV_TUNNEL;
val->pair->type = EV_PRE_TUNNEL;
return 0;