From 3da60eb357546d670dd8d87e2205c3bf66e74aff Mon Sep 17 00:00:00 2001 From: ruti <> Date: Mon, 18 Mar 2024 02:23:10 +0300 Subject: [PATCH] --fake on Windows --- Makefile | 2 +- desync.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- main.c | 6 ---- params.h | 5 +++ proxy.c | 2 +- 5 files changed, 103 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 28f0f9d..5783d2a 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ all: $(CC) $(CFLAGS) $(SOURCES) -I . -o $(TARGET) windows: - $(CC) $(CFLAGS) $(SOURCES) -I . -lws2_32 -o $(TARGET).exe + $(CC) $(CFLAGS) $(SOURCES) -I . -lws2_32 -lmswsock -o $(TARGET).exe clean: rm -f $(TARGET) *.o diff --git a/desync.c b/desync.c index a654278..79daf8c 100644 --- a/desync.c +++ b/desync.c @@ -24,6 +24,7 @@ #include #include #include + #include #endif #include @@ -124,6 +125,100 @@ int send_fake(int sfd, char *buffer, } #endif +#ifdef _WIN32 +int send_fake(int sfd, char *buffer, + int cnt, long pos, int fa, int ttl) +{ + struct packet pkt = cnt != IS_HTTP ? fake_tls : fake_http; + size_t psz = pkt.size; + + char path[MAX_PATH + 1]; + int ps = GetTempPath(sizeof(path), path); + if (!ps) { + uniperror("GetTempPath"); + return -1; + } + if (!GetTempFileName(path, "t", 0, path)) { + uniperror("GetTempFileName"); + return -1; + } + LOG(LOG_L, "temp file: %s\n", path); + + HANDLE hfile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (hfile == INVALID_HANDLE_VALUE) { + uniperror("CreateFileA"); + return -1; + } + + OVERLAPPED ov = {}; + int status = -1; + + while (status) { + ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + if (!ov.hEvent) { + uniperror("CreateEvent"); + break; + } + + 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; + } + if (setttl(sfd, ttl, fa) < 0) { + break; + } + 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; + } + } + delay(params.sfdelay); + + 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; + } + status = 0; + } + if (!CloseHandle(hfile)) { + uniperror("CloseHandle hfile"); + } + if (!CloseHandle(ov.hEvent)) { + uniperror("CloseHandle hEvent"); + } + if (!DeleteFile(path)) { + uniperror("DeleteFile"); + } + return status; +} +#endif + int send_oob(int sfd, char *buffer, ssize_t n, long pos) { @@ -263,7 +358,7 @@ int desync(int sfd, char *buffer, size_t bfsize, int s = 0; switch (part.m) { - #ifdef __linux__ + #ifdef FAKE_SUPPORT case DESYNC_FAKE: s = send_fake(sfd, buffer + lp, type, pos - lp, fa, dp.ttl ? dp.ttl : 8); diff --git a/main.c b/main.c index 3681028..65f5e11 100644 --- a/main.c +++ b/main.c @@ -16,15 +16,9 @@ #include #include #include - - #ifdef __linux__ - #define FAKE_SUPPORT 1 - #define TIMEOUT_SUPPORT 1 - #endif #else #include #define close(fd) closesocket(fd) - #define TIMEOUT_SUPPORT 1 #endif #define VERSION 6 diff --git a/params.h b/params.h index 3470f39..b5fcad4 100644 --- a/params.h +++ b/params.h @@ -7,6 +7,11 @@ #include #endif +#if defined(__linux__) || defined(_WIN32) +#define FAKE_SUPPORT 1 +#define TIMEOUT_SUPPORT 1 +#endif + #define OFFSET_SNI 1 #define OFFSET_HOST 2 diff --git a/proxy.c b/proxy.c index d723d9e..d057263 100644 --- a/proxy.c +++ b/proxy.c @@ -890,4 +890,4 @@ int run(struct sockaddr_ina *srv) } return event_loop(fd); } - \ No newline at end of file +