From e203aacc2cd6a303d4cb2022915a26a476666ce4 Mon Sep 17 00:00:00 2001 From: ruti <> Date: Wed, 14 Aug 2024 15:20:35 +0300 Subject: [PATCH] Set TTL before connect --- desync.c | 8 +------- desync.h | 4 ++++ extend.c | 14 ++++++++++++++ extend.h | 2 ++ proxy.c | 15 ++++++++------- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/desync.c b/desync.c index a87496a..8d81f1b 100644 --- a/desync.c +++ b/desync.c @@ -39,7 +39,7 @@ #include "error.h" -static inline int get_family(struct sockaddr *dst) +int get_family(struct sockaddr *dst) { if (dst->sa_family == AF_INET6) { struct sockaddr_in6 *d6 = (struct sockaddr_in6 *)dst; @@ -429,12 +429,6 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize, lp = pos + 5; } } - // set custom TTL - if (params.custom_ttl) { - if (setttl(sfd, params.def_ttl, fa) < 0) { - return -1; - } - } // desync long lp = offset; diff --git a/desync.h b/desync.h index a1a67db..2e456df 100644 --- a/desync.h +++ b/desync.h @@ -14,6 +14,10 @@ ssize_t desync(int sfd, char *buffer, size_t bfsize, ssize_t n, ssize_t offset, ssize_t desync_udp(int sfd, char *buffer, size_t bfsize, ssize_t n, struct sockaddr *dst, int dp_c); +int get_family(struct sockaddr *dst); + +int setttl(int fd, int ttl, int family); + struct tcpi { uint8_t state; uint8_t r[3]; diff --git a/extend.c b/extend.c index ac90b02..a37acc6 100644 --- a/extend.c +++ b/extend.c @@ -110,6 +110,20 @@ int connect_hook(struct poolhd *pool, struct eval *val, } +int socket_mod(int fd, struct sockaddr *dst) +{ + if (params.custom_ttl) { + if (setttl(fd, params.def_ttl, get_family(dst)) < 0) { + return -1; + } + } + if (params.protect_path) { + return protect(fd, params.protect_path); + } + return 0; +} + + int reconnect(struct poolhd *pool, struct eval *val, int m) { struct eval *client = val->pair; diff --git a/extend.h b/extend.h index 22d8ed8..36bce5b 100644 --- a/extend.h +++ b/extend.h @@ -5,6 +5,8 @@ #include "proxy.h" +int socket_mod(int fd, struct sockaddr *dst); + int connect_hook(struct poolhd *pool, struct eval *val, struct sockaddr_ina *dst, int next); diff --git a/proxy.c b/proxy.c index 88539e4..bad27c7 100644 --- a/proxy.c +++ b/proxy.c @@ -331,8 +331,7 @@ int create_conn(struct poolhd *pool, uniperror("socket"); return -1; } - if (params.protect_path - && protect(sfd, params.protect_path) < 0) { + if (socket_mod(sfd, &addr.sa) < 0) { close(sfd); return -1; } @@ -416,11 +415,6 @@ int udp_associate(struct poolhd *pool, uniperror("socket"); return -1; } - if (params.protect_path - && protect(ufd, params.protect_path) < 0) { - close(ufd); - return -1; - } if (params.baddr.sin6_family == AF_INET6) { int no = 0; if (setsockopt(ufd, IPPROTO_IPV6, @@ -448,6 +442,10 @@ int udp_associate(struct poolhd *pool, del_event(pool, pair); return -1; } + if (socket_mod(ufd, &addr.sa) < 0) { + del_event(pool, pair); + return -1; + } pair->in6 = addr.in6; } // @@ -695,6 +693,9 @@ int on_udp_tunnel(struct eval *val, char *buffer, size_t bfsize) uniperror("connect"); return -1; } + if (socket_mod(val->pair->fd, &addr.sa) < 0) { + return -1; + } val->pair->in6 = addr.in6; } ns = udp_hook(val->pair, data + offs, bfsize - offs, n - offs,