Set TTL before connect

This commit is contained in:
ruti 2024-08-14 15:20:35 +03:00
parent 00ae8524f1
commit e203aacc2c
5 changed files with 29 additions and 14 deletions

View File

@ -39,7 +39,7 @@
#include "error.h" #include "error.h"
static inline int get_family(struct sockaddr *dst) int get_family(struct sockaddr *dst)
{ {
if (dst->sa_family == AF_INET6) { if (dst->sa_family == AF_INET6) {
struct sockaddr_in6 *d6 = (struct sockaddr_in6 *)dst; 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; lp = pos + 5;
} }
} }
// set custom TTL
if (params.custom_ttl) {
if (setttl(sfd, params.def_ttl, fa) < 0) {
return -1;
}
}
// desync // desync
long lp = offset; long lp = offset;

View File

@ -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); 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 { struct tcpi {
uint8_t state; uint8_t state;
uint8_t r[3]; uint8_t r[3];

View File

@ -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) int reconnect(struct poolhd *pool, struct eval *val, int m)
{ {
struct eval *client = val->pair; struct eval *client = val->pair;

View File

@ -5,6 +5,8 @@
#include "proxy.h" #include "proxy.h"
int socket_mod(int fd, struct sockaddr *dst);
int connect_hook(struct poolhd *pool, struct eval *val, int connect_hook(struct poolhd *pool, struct eval *val,
struct sockaddr_ina *dst, int next); struct sockaddr_ina *dst, int next);

15
proxy.c
View File

@ -331,8 +331,7 @@ int create_conn(struct poolhd *pool,
uniperror("socket"); uniperror("socket");
return -1; return -1;
} }
if (params.protect_path if (socket_mod(sfd, &addr.sa) < 0) {
&& protect(sfd, params.protect_path) < 0) {
close(sfd); close(sfd);
return -1; return -1;
} }
@ -416,11 +415,6 @@ int udp_associate(struct poolhd *pool,
uniperror("socket"); uniperror("socket");
return -1; return -1;
} }
if (params.protect_path
&& protect(ufd, params.protect_path) < 0) {
close(ufd);
return -1;
}
if (params.baddr.sin6_family == AF_INET6) { if (params.baddr.sin6_family == AF_INET6) {
int no = 0; int no = 0;
if (setsockopt(ufd, IPPROTO_IPV6, if (setsockopt(ufd, IPPROTO_IPV6,
@ -448,6 +442,10 @@ int udp_associate(struct poolhd *pool,
del_event(pool, pair); del_event(pool, pair);
return -1; return -1;
} }
if (socket_mod(ufd, &addr.sa) < 0) {
del_event(pool, pair);
return -1;
}
pair->in6 = addr.in6; pair->in6 = addr.in6;
} }
// //
@ -695,6 +693,9 @@ int on_udp_tunnel(struct eval *val, char *buffer, size_t bfsize)
uniperror("connect"); uniperror("connect");
return -1; return -1;
} }
if (socket_mod(val->pair->fd, &addr.sa) < 0) {
return -1;
}
val->pair->in6 = addr.in6; val->pair->in6 = addr.in6;
} }
ns = udp_hook(val->pair, data + offs, bfsize - offs, n - offs, ns = udp_hook(val->pair, data + offs, bfsize - offs, n - offs,