2024-08-07 14:25:26 +03:00
|
|
|
#include "desync.h"
|
|
|
|
|
2023-06-12 07:00:33 +03:00
|
|
|
#include <stdio.h>
|
2023-06-12 11:01:40 +03:00
|
|
|
#include <string.h>
|
2023-06-03 22:52:10 +03:00
|
|
|
|
2024-02-18 23:20:52 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
2024-08-19 15:35:13 +03:00
|
|
|
#include <sys/mman.h>
|
2024-02-18 23:20:52 +03:00
|
|
|
#include <arpa/inet.h>
|
2024-08-19 15:35:13 +03:00
|
|
|
#include <fcntl.h>
|
2024-08-19 15:56:18 +03:00
|
|
|
|
2024-08-19 15:35:13 +03:00
|
|
|
#ifndef __linux__
|
2024-07-25 18:27:52 +03:00
|
|
|
#include <netinet/tcp.h>
|
2024-08-09 22:49:44 +03:00
|
|
|
#else
|
2024-08-19 15:35:13 +03:00
|
|
|
#include <linux/tcp.h>
|
2024-08-09 22:49:44 +03:00
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <linux/filter.h>
|
2024-08-14 18:48:15 +03:00
|
|
|
#include <sys/syscall.h>
|
2024-08-19 15:35:13 +03:00
|
|
|
|
|
|
|
#define memfd_create(name, flags) syscall(__NR_memfd_create, name, flags)
|
2024-03-13 22:18:16 +03:00
|
|
|
#endif
|
2023-06-03 22:52:10 +03:00
|
|
|
#else
|
2024-02-18 23:20:52 +03:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2024-03-18 02:23:10 +03:00
|
|
|
#include <mswsock.h>
|
2023-06-03 22:52:10 +03:00
|
|
|
#endif
|
2024-03-21 21:44:17 +03:00
|
|
|
#define STR_MODE
|
2023-06-03 22:52:10 +03:00
|
|
|
|
2024-05-02 19:36:29 +03:00
|
|
|
#include "params.h"
|
|
|
|
#include "packets.h"
|
|
|
|
#include "error.h"
|
2023-06-03 22:52:10 +03:00
|
|
|
|
|
|
|
|
2024-08-14 15:20:35 +03:00
|
|
|
int get_family(struct sockaddr *dst)
|
2023-07-30 14:23:11 +03:00
|
|
|
{
|
2024-08-19 15:35:13 +03:00
|
|
|
#ifndef __NetBSD__
|
2023-07-30 14:23:11 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-08-19 15:35:13 +03:00
|
|
|
#endif
|
2023-07-30 14:23:11 +03:00
|
|
|
return dst->sa_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-03 20:59:39 +03:00
|
|
|
int setttl(int fd, int ttl, int family) {
|
|
|
|
int _ttl = ttl;
|
2023-07-30 14:23:11 +03:00
|
|
|
|
2023-07-03 20:59:39 +03:00
|
|
|
if (family == AF_INET) {
|
2023-07-30 14:23:11 +03:00
|
|
|
if (setsockopt(fd, IPPROTO_IP,
|
2024-02-18 23:20:52 +03:00
|
|
|
IP_TTL, (char *)&_ttl, sizeof(_ttl)) < 0) {
|
|
|
|
uniperror("setsockopt IP_TTL");
|
2023-07-03 20:59:39 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2023-07-30 14:23:11 +03:00
|
|
|
else if (setsockopt(fd, IPPROTO_IPV6,
|
2024-02-18 23:20:52 +03:00
|
|
|
IPV6_UNICAST_HOPS, (char *)&_ttl, sizeof(_ttl)) < 0) {
|
|
|
|
uniperror("setsockopt IPV6_UNICAST_HOPS");
|
2023-07-03 20:59:39 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:49:44 +03:00
|
|
|
#ifdef __linux__
|
|
|
|
int drop_sack(int fd)
|
|
|
|
{
|
|
|
|
struct sock_filter code[] = {
|
|
|
|
{ 0x30, 0, 0, 0x0000000c },
|
|
|
|
{ 0x74, 0, 0, 0x00000004 },
|
|
|
|
{ 0x35, 0, 3, 0x0000000b },
|
|
|
|
{ 0x30, 0, 0, 0x00000022 },
|
|
|
|
{ 0x15, 0, 1, 0x00000005 },
|
|
|
|
{ 0x6, 0, 0, 0x00000000 },
|
|
|
|
{ 0x6, 0, 0, 0x00040000 },
|
|
|
|
};
|
|
|
|
struct sock_fprog bpf = {
|
|
|
|
.len = sizeof(code)/sizeof(*code),
|
|
|
|
.filter = code
|
|
|
|
};
|
|
|
|
if (setsockopt(fd, SOL_SOCKET,
|
|
|
|
SO_ATTACH_FILTER, (char *)&bpf, sizeof(bpf)) == -1) {
|
|
|
|
uniperror("setsockopt SO_ATTACH_FILTER");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2024-03-01 18:00:25 +03:00
|
|
|
#ifndef _WIN32
|
2024-03-05 23:22:12 +03:00
|
|
|
static inline void delay(long ms)
|
2024-03-01 18:00:25 +03:00
|
|
|
{
|
|
|
|
struct timespec time = {
|
2024-03-12 02:53:57 +03:00
|
|
|
.tv_nsec = ms * 1e6
|
2024-03-01 18:00:25 +03:00
|
|
|
};
|
|
|
|
nanosleep(&time, 0);
|
|
|
|
}
|
|
|
|
#else
|
2024-03-05 23:22:12 +03:00
|
|
|
#define delay(ms) Sleep(ms)
|
2024-03-01 18:00:25 +03:00
|
|
|
#endif
|
|
|
|
|
2024-03-21 01:01:36 +03:00
|
|
|
#ifdef __linux__
|
|
|
|
void wait_send(int sfd)
|
|
|
|
{
|
2024-06-08 20:13:46 +03:00
|
|
|
for (int i = 0; params.wait_send && i < 500; i++) {
|
2024-08-19 15:56:18 +03:00
|
|
|
struct tcp_info tcpi = {};
|
2024-03-21 01:01:36 +03:00
|
|
|
socklen_t ts = sizeof(tcpi);
|
2024-03-21 21:44:17 +03:00
|
|
|
|
2024-03-21 01:01:36 +03:00
|
|
|
if (getsockopt(sfd, IPPROTO_TCP,
|
|
|
|
TCP_INFO, (char *)&tcpi, &ts) < 0) {
|
2024-08-19 15:56:18 +03:00
|
|
|
perror("getsockopt TCP_INFO");
|
2024-03-21 01:01:36 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-08-19 15:56:18 +03:00
|
|
|
if (tcpi.tcpi_state != 1) {
|
|
|
|
LOG(LOG_E, "state: %d\n", tcpi.tcpi_state);
|
2024-03-21 21:44:17 +03:00
|
|
|
return;
|
|
|
|
}
|
2024-08-19 15:56:18 +03:00
|
|
|
size_t s = (char *)&tcpi.tcpi_notsent_bytes - (char *)&tcpi.tcpi_state;
|
|
|
|
if (ts < s) {
|
2024-03-21 01:01:36 +03:00
|
|
|
LOG(LOG_E, "tcpi_notsent_bytes not provided\n");
|
|
|
|
params.wait_send = 0;
|
|
|
|
break;
|
|
|
|
}
|
2024-08-19 15:56:18 +03:00
|
|
|
if (tcpi.tcpi_notsent_bytes == 0) {
|
2024-03-21 01:01:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(LOG_S, "not sent after %d ms\n", i);
|
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
delay(params.sfdelay);
|
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
#define wait_send_if_support(sfd) \
|
|
|
|
if (params.wait_send) wait_send(sfd)
|
2024-03-21 01:01:36 +03:00
|
|
|
#else
|
|
|
|
#define wait_send(sfd) delay(params.sfdelay)
|
2024-03-21 21:44:17 +03:00
|
|
|
#define wait_send_if_support(sfd) // :(
|
2024-03-21 01:01:36 +03:00
|
|
|
#endif
|
|
|
|
|
2024-08-19 15:35:13 +03:00
|
|
|
#ifdef FAKE_SUPPORT
|
|
|
|
#ifndef _WIN32
|
2024-03-21 21:44:17 +03:00
|
|
|
ssize_t send_fake(int sfd, char *buffer,
|
2024-03-28 23:28:09 +03:00
|
|
|
int cnt, long pos, int fa, struct desync_params *opt)
|
2023-06-03 22:52:10 +03:00
|
|
|
{
|
2024-03-28 23:28:09 +03:00
|
|
|
struct sockaddr_in6 addr = {};
|
|
|
|
socklen_t addr_size = sizeof(addr);
|
2024-08-22 17:53:17 +03:00
|
|
|
#ifdef __linux__
|
2024-03-28 23:28:09 +03:00
|
|
|
if (opt->md5sig) {
|
|
|
|
if (getpeername(sfd,
|
|
|
|
(struct sockaddr *)&addr, &addr_size) < 0) {
|
|
|
|
uniperror("getpeername");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2024-08-22 17:53:17 +03:00
|
|
|
#endif
|
2024-04-23 08:47:27 +03:00
|
|
|
struct packet pkt;
|
|
|
|
if (opt->fake_data.data) {
|
|
|
|
pkt = opt->fake_data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pkt = cnt != IS_HTTP ? fake_tls : fake_http;
|
|
|
|
}
|
2024-08-06 19:11:01 +03:00
|
|
|
if (opt->fake_offset) {
|
|
|
|
if (pkt.size > opt->fake_offset) {
|
|
|
|
pkt.size -= opt->fake_offset;
|
|
|
|
pkt.data += opt->fake_offset;
|
|
|
|
}
|
|
|
|
else pkt.size = 0;
|
|
|
|
}
|
|
|
|
|
2024-08-14 18:48:15 +03:00
|
|
|
int ffd = memfd_create("name", 0);
|
2023-06-03 22:52:10 +03:00
|
|
|
if (ffd < 0) {
|
2024-02-24 20:44:54 +03:00
|
|
|
uniperror("memfd_create");
|
2023-06-03 22:52:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char *p = 0;
|
2024-03-21 21:44:17 +03:00
|
|
|
ssize_t len = -1;
|
2023-06-03 22:52:10 +03:00
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
while (1) {
|
2023-06-03 22:52:10 +03:00
|
|
|
if (ftruncate(ffd, pos) < 0) {
|
2024-02-24 20:44:54 +03:00
|
|
|
uniperror("ftruncate");
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = mmap(0, pos, PROT_WRITE, MAP_SHARED, ffd, 0);
|
|
|
|
if (p == MAP_FAILED) {
|
2024-02-24 20:44:54 +03:00
|
|
|
uniperror("mmap");
|
2023-08-20 16:30:03 +03:00
|
|
|
p = 0;
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-08-06 19:11:01 +03:00
|
|
|
memcpy(p, pkt.data, pkt.size < pos ? pkt.size : pos);
|
2023-06-03 22:52:10 +03:00
|
|
|
|
2024-03-21 01:01:36 +03:00
|
|
|
if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2024-08-19 15:35:13 +03:00
|
|
|
|
2024-08-22 17:53:17 +03:00
|
|
|
#ifdef __linux__
|
2024-03-26 17:15:34 +03:00
|
|
|
if (opt->md5sig) {
|
|
|
|
struct tcp_md5sig md5 = {
|
|
|
|
.tcpm_keylen = 5
|
|
|
|
};
|
2024-03-28 23:28:09 +03:00
|
|
|
memcpy(&md5.tcpm_addr, &addr, addr_size);
|
2024-03-26 17:15:34 +03:00
|
|
|
|
|
|
|
if (setsockopt(sfd, IPPROTO_TCP,
|
|
|
|
TCP_MD5SIG, (char *)&md5, sizeof(md5)) < 0) {
|
2024-07-22 20:20:26 +03:00
|
|
|
uniperror("setsockopt TCP_MD5SIG");
|
2024-03-26 17:15:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-08-22 17:53:17 +03:00
|
|
|
#endif
|
2024-03-26 17:15:34 +03:00
|
|
|
if (opt->ip_options && fa == AF_INET
|
2024-03-21 01:01:36 +03:00
|
|
|
&& setsockopt(sfd, IPPROTO_IP, IP_OPTIONS,
|
|
|
|
opt->ip_options, opt->ip_options_len) < 0) {
|
2024-07-22 20:20:26 +03:00
|
|
|
uniperror("setsockopt IP_OPTIONS");
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-08-22 17:53:17 +03:00
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
len = sendfile(sfd, ffd, 0, pos);
|
|
|
|
if (len < 0) {
|
2024-02-25 00:40:54 +03:00
|
|
|
uniperror("sendfile");
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-03-21 01:01:36 +03:00
|
|
|
wait_send(sfd);
|
2023-06-03 22:52:10 +03:00
|
|
|
memcpy(p, buffer, pos);
|
|
|
|
|
2023-07-03 20:59:39 +03:00
|
|
|
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
2023-06-03 22:52:10 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-03-26 17:15:34 +03:00
|
|
|
if (opt->ip_options && fa == AF_INET
|
2024-03-21 01:01:36 +03:00
|
|
|
&& setsockopt(sfd, IPPROTO_IP,
|
|
|
|
IP_OPTIONS, opt->ip_options, 0) < 0) {
|
2024-07-22 20:20:26 +03:00
|
|
|
uniperror("setsockopt IP_OPTIONS");
|
2024-03-21 01:01:36 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-08-22 17:53:17 +03:00
|
|
|
#ifdef __linux__
|
2024-03-26 17:15:34 +03:00
|
|
|
if (opt->md5sig) {
|
|
|
|
struct tcp_md5sig md5 = {
|
|
|
|
.tcpm_keylen = 0
|
|
|
|
};
|
2024-03-28 23:28:09 +03:00
|
|
|
memcpy(&md5.tcpm_addr, &addr, addr_size);
|
2024-03-26 17:15:34 +03:00
|
|
|
|
|
|
|
if (setsockopt(sfd, IPPROTO_TCP,
|
|
|
|
TCP_MD5SIG, (char *)&md5, sizeof(md5)) < 0) {
|
2024-07-22 20:20:26 +03:00
|
|
|
uniperror("setsockopt TCP_MD5SIG");
|
2024-03-26 17:15:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-08-22 17:53:17 +03:00
|
|
|
#endif
|
2024-03-21 21:44:17 +03:00
|
|
|
break;
|
2023-06-03 22:52:10 +03:00
|
|
|
}
|
|
|
|
if (p) munmap(p, pos);
|
|
|
|
close(ffd);
|
2024-03-21 21:44:17 +03:00
|
|
|
return len;
|
2023-06-03 22:52:10 +03:00
|
|
|
}
|
2024-08-19 15:35:13 +03:00
|
|
|
#else
|
2024-07-22 20:20:26 +03:00
|
|
|
OVERLAPPED ov = {};
|
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
ssize_t send_fake(int sfd, char *buffer,
|
2024-03-28 23:28:09 +03:00
|
|
|
int cnt, long pos, int fa, struct desync_params *opt)
|
2024-03-18 02:23:10 +03:00
|
|
|
{
|
2024-04-23 08:47:27 +03:00
|
|
|
struct packet pkt;
|
|
|
|
if (opt->fake_data.data) {
|
|
|
|
pkt = opt->fake_data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pkt = cnt != IS_HTTP ? fake_tls : fake_http;
|
|
|
|
}
|
2024-03-18 02:23:10 +03:00
|
|
|
size_t psz = pkt.size;
|
2024-08-06 19:11:01 +03:00
|
|
|
if (opt->fake_offset) {
|
|
|
|
if (psz > opt->fake_offset) {
|
|
|
|
psz -= opt->fake_offset;
|
|
|
|
pkt.data += opt->fake_offset;
|
|
|
|
}
|
|
|
|
else psz = 0;
|
|
|
|
}
|
2024-03-18 02:23:10 +03:00
|
|
|
|
2024-05-14 05:07:01 +03:00
|
|
|
char path[MAX_PATH], temp[MAX_PATH + 1];
|
2024-05-03 11:49:21 +03:00
|
|
|
int ps = GetTempPath(sizeof(temp), temp);
|
2024-03-18 02:23:10 +03:00
|
|
|
if (!ps) {
|
2024-03-18 04:42:40 +03:00
|
|
|
uniperror("GetTempPath");
|
|
|
|
return -1;
|
|
|
|
}
|
2024-05-03 11:49:21 +03:00
|
|
|
if (!GetTempFileName(temp, "t", 0, path)) {
|
2024-03-18 04:42:40 +03:00
|
|
|
uniperror("GetTempFileName");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LOG(LOG_L, "temp file: %s\n", path);
|
|
|
|
|
2024-03-18 02:23:10 +03:00
|
|
|
HANDLE hfile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE,
|
2024-05-14 05:07:01 +03:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
|
2024-03-18 02:23:10 +03:00
|
|
|
if (hfile == INVALID_HANDLE_VALUE) {
|
2024-03-18 04:42:40 +03:00
|
|
|
uniperror("CreateFileA");
|
2024-03-18 02:23:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
ssize_t len = -1;
|
2024-03-18 02:23:10 +03:00
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
while (1) {
|
2024-03-18 02:23:10 +03:00
|
|
|
ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!ov.hEvent) {
|
2024-03-18 04:42:40 +03:00
|
|
|
uniperror("CreateEvent");
|
|
|
|
break;
|
2024-03-18 02:23:10 +03:00
|
|
|
}
|
2024-03-18 04:42:40 +03:00
|
|
|
if (!WriteFile(hfile, pkt.data, psz < pos ? psz : pos, 0, 0)) {
|
|
|
|
uniperror("WriteFile");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (psz < pos) {
|
|
|
|
if (SetFilePointer(hfile, pos, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
|
|
|
|
uniperror("SetFilePointer");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!SetEndOfFile(hfile)) {
|
|
|
|
uniperror("SetFileEnd");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (SetFilePointer(hfile, 0, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
|
|
|
|
uniperror("SetFilePointer");
|
|
|
|
break;
|
|
|
|
}
|
2024-03-21 01:01:36 +03:00
|
|
|
if (setttl(sfd, opt->ttl ? opt->ttl : 8, fa) < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2024-03-18 04:42:40 +03:00
|
|
|
if (!TransmitFile(sfd, hfile, pos, pos, &ov,
|
|
|
|
NULL, TF_USE_KERNEL_APC | TF_WRITE_BEHIND)) {
|
|
|
|
if ((GetLastError() != ERROR_IO_PENDING)
|
|
|
|
&& (WSAGetLastError() != WSA_IO_PENDING)) {
|
|
|
|
uniperror("TransmitFile");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
wait_send(sfd);
|
2024-03-18 04:42:40 +03:00
|
|
|
|
|
|
|
if (SetFilePointer(hfile, 0, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
|
|
|
|
uniperror("SetFilePointer");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!WriteFile(hfile, buffer, pos, 0, 0)) {
|
|
|
|
uniperror("WriteFile");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
len = pos;
|
|
|
|
break;
|
2024-03-18 04:42:40 +03:00
|
|
|
}
|
2024-05-14 05:07:01 +03:00
|
|
|
if (!CloseHandle(hfile)
|
|
|
|
|| (ov.hEvent && !CloseHandle(ov.hEvent))) {
|
|
|
|
uniperror("CloseHandle");
|
|
|
|
return -1;
|
2024-03-18 04:42:40 +03:00
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
return len;
|
2024-03-18 02:23:10 +03:00
|
|
|
}
|
|
|
|
#endif
|
2024-08-19 15:35:13 +03:00
|
|
|
#endif
|
2024-03-18 02:23:10 +03:00
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
ssize_t send_oob(int sfd, char *buffer,
|
2024-08-16 19:08:22 +03:00
|
|
|
ssize_t n, long pos, char *c)
|
2023-06-03 22:52:10 +03:00
|
|
|
{
|
2024-03-06 20:37:59 +03:00
|
|
|
char rchar = buffer[pos];
|
2024-08-16 19:08:22 +03:00
|
|
|
buffer[pos] = c[1] ? c[0] : 'a';
|
2024-03-06 20:37:59 +03:00
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
ssize_t len = send(sfd, buffer, pos + 1, MSG_OOB);
|
|
|
|
buffer[pos] = rchar;
|
|
|
|
|
|
|
|
if (len < 0) {
|
2024-03-06 20:37:59 +03:00
|
|
|
uniperror("send");
|
|
|
|
return -1;
|
|
|
|
}
|
2024-08-16 19:08:22 +03:00
|
|
|
wait_send_if_support(sfd);
|
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
len--;
|
|
|
|
if (len != pos) {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return len;
|
2024-03-04 04:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
ssize_t send_disorder(int sfd,
|
2024-03-04 04:49:08 +03:00
|
|
|
char *buffer, long pos, int fa)
|
|
|
|
{
|
|
|
|
int bttl = 1;
|
2024-03-20 02:23:56 +03:00
|
|
|
|
2024-03-04 04:49:08 +03:00
|
|
|
if (setttl(sfd, bttl, fa) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
ssize_t len = send(sfd, buffer, pos, 0);
|
|
|
|
if (len < 0) {
|
2024-02-29 20:07:59 +03:00
|
|
|
uniperror("send");
|
|
|
|
}
|
2024-08-16 19:08:22 +03:00
|
|
|
else wait_send_if_support(sfd);
|
2024-03-21 21:44:17 +03:00
|
|
|
|
2024-03-04 04:49:08 +03:00
|
|
|
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
return len;
|
2024-02-29 20:07:59 +03:00
|
|
|
}
|
|
|
|
|
2024-03-04 04:49:08 +03:00
|
|
|
|
2024-08-06 19:11:01 +03:00
|
|
|
ssize_t send_late_oob(int sfd, char *buffer,
|
2024-08-16 19:08:22 +03:00
|
|
|
ssize_t n, long pos, int fa, char *c)
|
2024-08-06 19:11:01 +03:00
|
|
|
{
|
|
|
|
int bttl = 1;
|
|
|
|
|
|
|
|
if (setttl(sfd, bttl, fa) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2024-08-16 19:08:22 +03:00
|
|
|
ssize_t len = send_oob(sfd, buffer, n, pos, c);
|
2024-08-06 19:11:01 +03:00
|
|
|
if (len < 0) {
|
|
|
|
uniperror("send");
|
|
|
|
}
|
|
|
|
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-21 01:01:36 +03:00
|
|
|
ssize_t desync(int sfd, char *buffer, size_t bfsize,
|
2024-03-20 02:23:56 +03:00
|
|
|
ssize_t n, ssize_t offset, struct sockaddr *dst, int dp_c)
|
2023-06-03 22:52:10 +03:00
|
|
|
{
|
2024-03-08 03:37:02 +03:00
|
|
|
struct desync_params dp = params.dp[dp_c];
|
2024-03-12 02:53:57 +03:00
|
|
|
|
2023-06-03 22:52:10 +03:00
|
|
|
char *host = 0;
|
|
|
|
int len = 0, type = 0;
|
2023-07-30 14:23:11 +03:00
|
|
|
int fa = get_family(dst);
|
2024-03-21 21:44:17 +03:00
|
|
|
// parse packet
|
2023-06-03 22:52:10 +03:00
|
|
|
if ((len = parse_tls(buffer, n, &host))) {
|
|
|
|
type = IS_HTTPS;
|
|
|
|
}
|
|
|
|
else if ((len = parse_http(buffer, n, &host, 0))) {
|
|
|
|
type = IS_HTTP;
|
|
|
|
}
|
2023-08-20 16:30:03 +03:00
|
|
|
if (len && host) {
|
2024-08-07 14:44:12 +03:00
|
|
|
LOG(LOG_S, "host: %.*s (%zd)\n",
|
2024-03-06 20:37:59 +03:00
|
|
|
len, host, host - buffer);
|
2023-08-20 16:30:03 +03:00
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
// modify packet
|
2024-03-08 03:37:02 +03:00
|
|
|
if (type == IS_HTTP && dp.mod_http) {
|
2024-08-07 14:44:12 +03:00
|
|
|
LOG(LOG_S, "modify HTTP: n=%zd\n", n);
|
2024-03-08 03:37:02 +03:00
|
|
|
if (mod_http(buffer, n, dp.mod_http)) {
|
2024-02-25 00:40:54 +03:00
|
|
|
LOG(LOG_E, "mod http error\n");
|
2023-06-03 22:52:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 03:37:02 +03:00
|
|
|
else if (type == IS_HTTPS && dp.tlsrec_n) {
|
2024-03-04 15:30:23 +03:00
|
|
|
long lp = 0;
|
2024-03-08 03:37:02 +03:00
|
|
|
for (int i = 0; i < dp.tlsrec_n; i++) {
|
|
|
|
struct part part = dp.tlsrec[i];
|
2024-03-05 23:22:12 +03:00
|
|
|
|
|
|
|
long pos = part.pos + i * 5;
|
|
|
|
if (part.flag == OFFSET_SNI) {
|
2024-03-04 15:30:23 +03:00
|
|
|
pos += (host - buffer - 5);
|
|
|
|
}
|
|
|
|
else if (pos < 0) {
|
|
|
|
pos += n;
|
|
|
|
}
|
2024-03-05 23:22:12 +03:00
|
|
|
if (pos < lp) {
|
|
|
|
LOG(LOG_E, "tlsrec cancel: %ld < %ld\n", pos, lp);
|
|
|
|
break;
|
|
|
|
}
|
2024-03-04 15:30:23 +03:00
|
|
|
if (!part_tls(buffer + lp,
|
|
|
|
bfsize - lp, n - lp, pos - lp)) {
|
2024-08-07 14:44:12 +03:00
|
|
|
LOG(LOG_E, "tlsrec error: pos=%ld, n=%zd\n", pos, n);
|
2024-03-04 15:30:23 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-08-07 14:44:12 +03:00
|
|
|
LOG(LOG_S, "tlsrec: pos=%ld, n=%zd\n", pos, n);
|
2024-03-04 15:30:23 +03:00
|
|
|
n += 5;
|
|
|
|
lp = pos + 5;
|
2024-02-18 17:19:11 +03:00
|
|
|
}
|
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
// desync
|
2024-08-09 22:49:44 +03:00
|
|
|
#ifdef __linux__
|
|
|
|
if (dp.drop_sack && drop_sack(sfd)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2024-03-20 02:23:56 +03:00
|
|
|
long lp = offset;
|
2024-03-04 04:49:08 +03:00
|
|
|
|
2024-04-23 08:47:27 +03:00
|
|
|
for (int i = 0; i < dp.parts_n; i++) {
|
2024-03-08 03:37:02 +03:00
|
|
|
struct part part = dp.parts[i];
|
2024-03-05 23:22:12 +03:00
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
// change pos
|
2024-03-05 23:22:12 +03:00
|
|
|
long pos = part.pos;
|
|
|
|
if (part.flag == OFFSET_SNI) {
|
|
|
|
if (type != IS_HTTPS)
|
2024-03-06 20:37:59 +03:00
|
|
|
continue;
|
2024-03-05 23:22:12 +03:00
|
|
|
else
|
|
|
|
pos += (host - buffer);
|
|
|
|
}
|
|
|
|
else if (part.flag == OFFSET_HOST) {
|
|
|
|
if (type != IS_HTTP)
|
2024-03-06 20:37:59 +03:00
|
|
|
continue;
|
2024-03-05 23:22:12 +03:00
|
|
|
else
|
2024-03-04 04:49:08 +03:00
|
|
|
pos += (host - buffer);
|
|
|
|
}
|
2024-08-06 19:11:01 +03:00
|
|
|
else if (pos < 0 || part.flag == OFFSET_END) {
|
2024-03-04 04:49:08 +03:00
|
|
|
pos += n;
|
|
|
|
}
|
2024-03-20 02:23:56 +03:00
|
|
|
// after EAGAIN
|
2024-08-06 19:11:01 +03:00
|
|
|
if (offset && pos <= offset) {
|
2024-03-20 02:23:56 +03:00
|
|
|
continue;
|
|
|
|
}
|
2024-08-16 19:08:22 +03:00
|
|
|
else if (pos < 0 || pos > n || pos < lp) {
|
2024-08-07 14:44:12 +03:00
|
|
|
LOG(LOG_E, "split cancel: pos=%ld-%ld, n=%zd\n", lp, pos, n);
|
2024-03-04 04:49:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
// send part
|
|
|
|
ssize_t s = 0;
|
2024-03-05 23:22:12 +03:00
|
|
|
switch (part.m) {
|
2024-03-21 21:44:17 +03:00
|
|
|
#ifdef FAKE_SUPPORT
|
|
|
|
case DESYNC_FAKE:
|
2024-08-16 19:08:22 +03:00
|
|
|
if (pos != lp) s = send_fake(sfd,
|
2024-03-28 23:28:09 +03:00
|
|
|
buffer + lp, type, pos - lp, fa, &dp);
|
2024-03-21 21:44:17 +03:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case DESYNC_DISORDER:
|
|
|
|
s = send_disorder(sfd,
|
|
|
|
buffer + lp, pos - lp, fa);
|
|
|
|
break;
|
2024-02-29 20:07:59 +03:00
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
case DESYNC_OOB:
|
|
|
|
s = send_oob(sfd,
|
2024-08-16 19:08:22 +03:00
|
|
|
buffer + lp, n - lp, pos - lp, dp.oob_char);
|
2024-03-21 21:44:17 +03:00
|
|
|
break;
|
|
|
|
|
2024-08-16 19:08:22 +03:00
|
|
|
case DESYNC_DISOOB:
|
2024-08-06 19:11:01 +03:00
|
|
|
s = send_late_oob(sfd,
|
2024-08-16 19:08:22 +03:00
|
|
|
buffer + lp, n - lp, pos - lp, fa, dp.oob_char);
|
2024-08-06 19:11:01 +03:00
|
|
|
break;
|
|
|
|
|
2024-03-21 21:44:17 +03:00
|
|
|
case DESYNC_SPLIT:
|
2024-05-14 16:16:52 +03:00
|
|
|
case DESYNC_NONE:
|
2024-03-21 21:44:17 +03:00
|
|
|
s = send(sfd, buffer + lp, pos - lp, 0);
|
|
|
|
wait_send_if_support(sfd);
|
2024-05-14 16:23:17 +03:00
|
|
|
break;
|
2024-05-14 05:07:01 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
2024-03-04 04:49:08 +03:00
|
|
|
}
|
2024-08-07 14:44:12 +03:00
|
|
|
LOG(LOG_S, "split: pos=%ld-%ld (%zd), m: %s\n", lp, pos, s, demode_str[part.m]);
|
2024-05-14 05:07:01 +03:00
|
|
|
|
2024-03-20 02:23:56 +03:00
|
|
|
if (s < 0) {
|
2024-03-21 21:44:17 +03:00
|
|
|
if (get_e() == EAGAIN) {
|
2024-03-20 02:23:56 +03:00
|
|
|
return lp;
|
|
|
|
}
|
2024-03-04 04:49:08 +03:00
|
|
|
return -1;
|
2024-03-21 21:44:17 +03:00
|
|
|
}
|
|
|
|
else if (s != (pos - lp)) {
|
2024-08-07 14:44:12 +03:00
|
|
|
LOG(LOG_E, "%zd != %ld\n", s, pos - lp);
|
2024-03-21 21:44:17 +03:00
|
|
|
return lp + s;
|
2024-03-04 04:49:08 +03:00
|
|
|
}
|
|
|
|
lp = pos;
|
|
|
|
}
|
2024-03-21 21:44:17 +03:00
|
|
|
// send all/rest
|
2024-03-04 04:49:08 +03:00
|
|
|
if (lp < n) {
|
2024-08-07 14:44:12 +03:00
|
|
|
LOG((lp ? LOG_S : LOG_L), "send: pos=%ld-%zd\n", lp, n);
|
2024-03-04 04:49:08 +03:00
|
|
|
if (send(sfd, buffer + lp, n - lp, 0) < 0) {
|
2024-03-20 02:23:56 +03:00
|
|
|
if (get_e() == EAGAIN) {
|
|
|
|
return lp;
|
|
|
|
}
|
2024-03-04 04:49:08 +03:00
|
|
|
uniperror("send");
|
|
|
|
return -1;
|
|
|
|
}
|
2023-06-03 22:52:10 +03:00
|
|
|
}
|
2024-03-20 02:23:56 +03:00
|
|
|
return n;
|
2023-07-07 23:07:27 +03:00
|
|
|
}
|
2024-07-29 12:26:42 +03:00
|
|
|
|
|
|
|
|
2024-08-09 22:49:44 +03:00
|
|
|
int post_desync(int sfd, int dp_c)
|
|
|
|
{
|
|
|
|
struct desync_params *dp = ¶ms.dp[dp_c];
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
if (dp->drop_sack) {
|
|
|
|
if (setsockopt(sfd, SOL_SOCKET,
|
|
|
|
SO_DETACH_FILTER, &dp_c, sizeof(dp_c)) == -1) {
|
|
|
|
uniperror("setsockopt SO_DETACH_FILTER");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-29 12:26:42 +03:00
|
|
|
ssize_t desync_udp(int sfd, char *buffer, size_t bfsize,
|
|
|
|
ssize_t n, struct sockaddr *dst, int dp_c)
|
|
|
|
{
|
|
|
|
struct desync_params *dp = ¶ms.dp[dp_c];
|
|
|
|
int fa = get_family(dst);
|
|
|
|
|
|
|
|
if (dp->udp_fake_count != 0) {
|
|
|
|
struct packet pkt;
|
|
|
|
if (dp->fake_data.data) {
|
|
|
|
pkt = dp->fake_data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pkt = fake_udp;
|
|
|
|
}
|
2024-08-16 19:08:22 +03:00
|
|
|
if (dp->fake_offset) {
|
|
|
|
if (pkt.size > dp->fake_offset) {
|
|
|
|
pkt.size -= dp->fake_offset;
|
|
|
|
pkt.data += dp->fake_offset;
|
|
|
|
}
|
|
|
|
else pkt.size = 0;
|
|
|
|
}
|
2024-07-29 12:26:42 +03:00
|
|
|
int bttl = dp->ttl ? dp->ttl : 8;
|
|
|
|
if (setttl(sfd, bttl, fa) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < dp->udp_fake_count; i++) {
|
|
|
|
ssize_t len = sendto(sfd, pkt.data,
|
|
|
|
pkt.size, 0, dst, sizeof(struct sockaddr_in6));
|
|
|
|
if (len < 0) {
|
|
|
|
uniperror("send");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (setttl(sfd, params.def_ttl, fa) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sendto(sfd, buffer, n, 0,
|
|
|
|
dst, sizeof(struct sockaddr_in6));
|
|
|
|
}
|