Get family by address, accept4

This commit is contained in:
ruti 2023-07-30 13:23:11 +02:00
parent 4b48ba353f
commit 8bba9ee3eb
4 changed files with 48 additions and 28 deletions

View File

@ -27,17 +27,32 @@
#include <packets.h>
static inline int get_family(struct sockaddr *dst)
{
if (dst->sa_family == AF_INET6) {
struct sockaddr_in6 *d6 = (struct sockaddr_in6 *)dst;
static char *pat = "\0\0\0\0\0\0\0\0\0\0\xff\xff";
if (!memcmp(&d6->sin6_addr, pat, 12)) {
return AF_INET;
}
}
return dst->sa_family;
}
int setttl(int fd, int ttl, int family) {
int _ttl = ttl;
if (family == AF_INET) {
if (setsockopt(fd, IPPROTO_IP, IP_TTL,
&_ttl, sizeof(_ttl)) < 0) {
if (setsockopt(fd, IPPROTO_IP,
IP_TTL, &_ttl, sizeof(_ttl)) < 0) {
perror("setsockopt IP_TTL");
return -1;
}
}
else if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
&_ttl, sizeof(_ttl)) < 0) {
else if (setsockopt(fd, IPPROTO_IPV6,
IPV6_UNICAST_HOPS, &_ttl, sizeof(_ttl)) < 0) {
perror("setsockopt IPV6_UNICAST_HOPS");
return -1;
}
@ -45,7 +60,8 @@ int setttl(int fd, int ttl, int family) {
}
int fake_attack(int sfd, char *buffer, ssize_t n, int cnt, int pos, int fa)
int fake_attack(int sfd, char *buffer,
size_t n, int cnt, int pos, int fa)
{
struct packet pkt = cnt != IS_HTTP ? fake_tls : fake_http;
size_t psz = pkt.size;
@ -98,7 +114,8 @@ int fake_attack(int sfd, char *buffer, ssize_t n, int cnt, int pos, int fa)
}
int disorder_attack(int sfd, char *buffer, ssize_t n, int pos, int fa)
int disorder_attack(int sfd, char *buffer,
ssize_t n, int pos, int fa)
{
int bttl = 1;
if (setttl(sfd, bttl, fa) < 0) {
@ -125,7 +142,7 @@ int desync(int sfd, char *buffer,
int pos = params.split;
char *host = 0;
int len = 0, type = 0;
int fa = dst->sa_family;
int fa = get_family(dst);
if ((len = parse_tls(buffer, n, &host))) {
type = IS_HTTPS;
@ -141,11 +158,12 @@ int desync(int sfd, char *buffer,
return -1;
}
}
if (host && params.split_host)
if (host && params.split_host) {
pos += (host - buffer);
else if (pos < 0)
}
else if (pos < 0) {
pos += n;
}
LOG(LOG_L, "split pos: %d, n: %ld\n", pos, n);
if (pos <= 0 || pos >= n ||
@ -180,31 +198,26 @@ int desync(int sfd, char *buffer,
int desync_udp(int fd, char *buffer,
ssize_t n, struct sockaddr_in6 *dst)
ssize_t n, struct sockaddr *dst)
{
char is_mv4 = 1;
int fa = get_family(dst);
socklen_t s = dst->sa_family == AF_INET6 ?
sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
if (dst->sin6_family == AF_INET6) {
static char *pat = "\0\0\0\0\0\0\0\0\0\0\xff\xff";
is_mv4 = !memcmp(&dst->sin6_addr, pat, 12);
}
if (params.desync_udp & DESYNC_UDP_FAKE) {
if (setttl(fd, params.ttl,
is_mv4 ? AF_INET : dst->sin6_family) < 0) {
if (setttl(fd, params.ttl, fa) < 0) {
return -1;
}
if (sendto(fd, fake_udp.data, fake_udp.size,
0, (struct sockaddr *)dst, sizeof(*dst)) < 0) {
if (sendto(fd, fake_udp.data,
fake_udp.size, 0, dst, s) < 0) {
perror("sendto");
return -1;
}
if (setttl(fd, params.def_ttl,
is_mv4 ? AF_INET : dst->sin6_family) < 0) {
if (setttl(fd, params.def_ttl, fa) < 0) {
return -1;
}
}
ssize_t ns = sendto(fd,
buffer, n, 0, (struct sockaddr *)dst, sizeof(*dst));
ssize_t ns = sendto(fd, buffer, n, 0, dst, s);
if (ns < 0) {
perror("sendto");
return -1;

View File

@ -1,2 +1,2 @@
int desync(int sfd, char *buffer, ssize_t n, struct sockaddr *dst);
int desync_udp(int fd, char *buffer, ssize_t n, struct sockaddr_in6 *dst);
int desync_udp(int fd, char *buffer, ssize_t n, struct sockaddr *dst);

2
main.c
View File

@ -13,7 +13,7 @@
#include <proxy.h>
#include <packets.h>
#define VERSION 1
#define VERSION 2
struct packet fake_tls = {
sizeof(tls_data), tls_data

View File

@ -461,18 +461,24 @@ static inline int on_accept(struct poolhd *pool, struct eval *val)
while (1) {
socklen_t len = sizeof(client);
#ifdef __linux__
int c = accept4(val->fd, &client.sa, &len, SOCK_NONBLOCK);
#else
int c = accept(val->fd, &client.sa, &len);
#endif
if (c < 0) {
if (errno == EAGAIN)
break;
perror("accept");
return -1;
}
#ifndef __linux__
if (fcntl(c, F_SETFL, O_NONBLOCK) < 0) {
perror("fcntl");
close(c);
continue;
}
#endif
int one = 1;
if (setsockopt(c, IPPROTO_TCP,
TCP_NODELAY, (char *)&one, sizeof(one))) {
@ -641,11 +647,12 @@ int on_udp_tunnel(struct eval *val, char *buffer, size_t bfsize)
buffer + skip + offs, n - offs, 0, &addr.sa, asz);
else {
ns = desync_udp(val->fd,
buffer + skip + offs, n - offs, &addr.in6);
buffer + skip + offs, n - offs, &addr.sa);
val->flag |= FLAG_CONN;
}
} else {
map_fix(&addr, 0);
offs = s_set_addr(buffer + skip, skip, &addr, 1);
if (offs < 0 || offs > skip) {
return -1;