Route client traffic to the loopback service

Build the socket library and client-local port configuration for redirecting
traffic to the offline service. Accept HTTPS and the existing matching and
HTTP protocols on the same port. Route requests by the original host.
This commit is contained in:
N0VAN0M4LY committed 2026-11-25 05:02:34 +08:00
1 parent ae6958f291
commit afb22c4f08
35 files changed
+3897 -23

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
/*
* Connections and datagrams: a destination that is the marker address
* or outside the loopback network goes to 127.0.0.1:<local service port> instead; when that
* is impossible (no port configured, IPv6 destination) the call fails as unreachable.
*
* ConnectEx is not an export: programs fetch it with WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER),
* so that call hands out a wrapper around the real one.
*/
#include "hooks.h"
#include "hook_config.h"
#include "redirect_rules.h"
#include <mswsock.h>
typedef int (WSAAPI *connect_fn)(SOCKET, const struct sockaddr *, int);
typedef int (WSAAPI *WSAConnect_fn)(SOCKET, const struct sockaddr *, int, LPWSABUF, LPWSABUF, LPQOS, LPQOS);
typedef int (WSAAPI *sendto_fn)(SOCKET, const char *, int, int, const struct sockaddr *, int);
typedef int (WSAAPI *WSASendTo_fn)(SOCKET, LPWSABUF, DWORD, LPDWORD, DWORD, const struct sockaddr *, int,
LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE);
typedef int (WSAAPI *WSAIoctl_fn)(SOCKET, DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPWSAOVERLAPPED,
LPWSAOVERLAPPED_COMPLETION_ROUTINE);
static connect_fn real_connect;
static WSAConnect_fn real_WSAConnect;
static sendto_fn real_sendto;
static WSASendTo_fn real_WSASendTo;
static WSAIoctl_fn real_WSAIoctl;
static LPFN_CONNECTEX real_ConnectEx;
/*
* The destination to use: the original one, or the local service. Returns 0 when the call
* must fail (WSAENETUNREACH is set). Connections are logged; datagrams only when refused or
* the first time a destination is seen would be too noisy to track, so only refusals.
*/
static int choose_destination(const struct sockaddr **name, int *length, struct sockaddr_storage *buffer,
const char *what, int log_change)
{
char original[64];
char replaced[64];
int new_length = 0;
enum redirect_result result = redirect_destination(*name, *length, buffer, &new_length);
if (result == REDIRECT_KEEP)
return 1;
describe_address(*name, *length, original, sizeof(original));
if (result == REDIRECT_REFUSE) {
log_line("%s %s refused", what, original);
WSASetLastError(WSAENETUNREACH);
return 0;
}
if (log_change) {
describe_address((const struct sockaddr *)buffer, new_length, replaced, sizeof(replaced));
log_line("%s %s -> %s", what, original, replaced);
}
*name = (const struct sockaddr *)buffer;
*length = new_length;
return 1;
}
static int WSAAPI hooked_connect(SOCKET s, const struct sockaddr *name, int length)
{
struct sockaddr_storage buffer;
if (!choose_destination(&name, &length, &buffer, "connect", 1))
return SOCKET_ERROR;
return real_connect(s, name, length);
}
static int WSAAPI hooked_WSAConnect(SOCKET s, const struct sockaddr *name, int length, LPWSABUF caller_data,
LPWSABUF callee_data, LPQOS sqos, LPQOS gqos)
{
struct sockaddr_storage buffer;
if (!choose_destination(&name, &length, &buffer, "WSAConnect", 1))
return SOCKET_ERROR;
return real_WSAConnect(s, name, length, caller_data, callee_data, sqos, gqos);
}
static BOOL PASCAL hooked_ConnectEx(SOCKET s, const struct sockaddr *name, int length, PVOID send_buffer,
DWORD send_length, LPDWORD sent, LPOVERLAPPED overlapped)
{
struct sockaddr_storage buffer;
if (!choose_destination(&name, &length, &buffer, "ConnectEx", 1))
return FALSE;
return real_ConnectEx(s, name, length, send_buffer, send_length, sent, overlapped);
}
static int WSAAPI hooked_sendto(SOCKET s, const char *data, int size, int flags, const struct sockaddr *to, int length)
{
struct sockaddr_storage buffer;
if (!choose_destination(&to, &length, &buffer, "sendto", 0))
return SOCKET_ERROR;
return real_sendto(s, data, size, flags, to, length);
}
static int WSAAPI hooked_WSASendTo(SOCKET s, LPWSABUF buffers, DWORD count, LPDWORD sent, DWORD flags,
const struct sockaddr *to, int length, LPWSAOVERLAPPED overlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE completion)
{
struct sockaddr_storage buffer;
if (!choose_destination(&to, &length, &buffer, "WSASendTo", 0))
return SOCKET_ERROR;
return real_WSASendTo(s, buffers, count, sent, flags, to, length, overlapped, completion);
}
static int WSAAPI hooked_WSAIoctl(SOCKET s, DWORD code, LPVOID in, DWORD in_size, LPVOID out, DWORD out_size,
LPDWORD returned, LPWSAOVERLAPPED overlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE completion)
{
static const GUID connect_ex_id = WSAID_CONNECTEX;
int result = real_WSAIoctl(s, code, in, in_size, out, out_size, returned, overlapped, completion);
if (result == 0 && code == SIO_GET_EXTENSION_FUNCTION_POINTER && in && in_size >= sizeof(GUID)
&& memcmp(in, &connect_ex_id, sizeof(GUID)) == 0 && out && out_size >= sizeof(LPFN_CONNECTEX)) {
LPFN_CONNECTEX handed_out = *(LPFN_CONNECTEX *)out;
if (handed_out != hooked_ConnectEx) {
real_ConnectEx = handed_out;
*(LPFN_CONNECTEX *)out = hooked_ConnectEx;
}
}
return result;
}
int install_connection_hooks(void)
{
return hook_export("connect", (void *)hooked_connect, (void **)&real_connect)
&& hook_export("WSAConnect", (void *)hooked_WSAConnect, (void **)&real_WSAConnect)
&& hook_export("sendto", (void *)hooked_sendto, (void **)&real_sendto)
&& hook_export("WSASendTo", (void *)hooked_WSASendTo, (void **)&real_WSASendTo)
&& hook_export("WSAIoctl", (void *)hooked_WSAIoctl, (void **)&real_WSAIoctl);
}
+57
View File
@@ -0,0 +1,57 @@
/*
* wto_ws.dll: the network hook library of WTOffline.
*
* aces.exe imports its network functions from this DLL instead of WS2_32.dll
* (exe-patches/online/network-hook-library). Every export is forwarded to the system's
* ws2_32.dll (wto_ws.def); on load the DLL hooks the resolution, connection and datagram
* functions of the system's ws2_32.dll itself with MinHook, so the calls of every module in
* the process pass through the hooks, not only the executable's.
*
* If the hooks cannot be installed the DLL fails to load and the game does not start: running
* unhooked would let the game reach Gaijin's servers.
*/
#include "hooks.h"
#include "hook_config.h"
#include "MinHook.h"
static HMODULE g_ws2;
int hook_export(const char *name, void *replacement, void **original)
{
void *target = (void *)GetProcAddress(g_ws2, name);
if (target == NULL) {
log_line("hook %s: not exported by ws2_32.dll", name);
return 0;
}
if (MH_CreateHook(target, replacement, original) != MH_OK || MH_EnableHook(target) != MH_OK) {
log_line("hook %s: failed", name);
return 0;
}
return 1;
}
static int install(void)
{
/* ws2_32.dll is already loaded: this DLL links against it. */
g_ws2 = GetModuleHandleW(L"ws2_32.dll");
if (g_ws2 == NULL || MH_Initialize() != MH_OK)
return 0;
return install_resolver_hooks() && install_connection_hooks();
}
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
{
(void)reserved;
if (reason != DLL_PROCESS_ATTACH)
return TRUE;
DisableThreadLibraryCalls(instance);
config_load(instance);
if (!install()) {
/* Failing DllMain makes the loader refuse to start the game. */
log_line("wto_ws: hooks not installed, refusing to load so the game cannot go online");
return FALSE;
}
log_line("wto_ws: hooks installed");
return TRUE;
}
+98
View File
@@ -0,0 +1,98 @@
/*
* Settings and log next to the DLL.
*
* wto_ws.cfg, written by the build into the game copy: one line "port=<local service port>".
* Without a valid port the hooks still answer every host name locally but refuse every
* connection that would leave the machine.
*
* wto_ws.log: which host names were answered locally and which connections were redirected or
* refused, so a run shows every address the game tried (the audit handover 6.2 asks for).
*/
#include "hook_config.h"
#include "redirect_rules.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static wchar_t g_folder[MAX_PATH];
static CRITICAL_SECTION g_log_lock;
static FILE *g_log;
static void find_folder(HMODULE self)
{
DWORD length = GetModuleFileNameW(self, g_folder, MAX_PATH);
if (length == 0 || length >= MAX_PATH) {
g_folder[0] = L'\0';
return;
}
wchar_t *slash = wcsrchr(g_folder, L'\\');
if (slash)
slash[1] = L'\0';
}
static void read_port(void)
{
wchar_t path[MAX_PATH];
char line[128];
FILE *file;
if (_snwprintf_s(path, MAX_PATH, _TRUNCATE, L"%swto_ws.cfg", g_folder) < 0)
return;
if (_wfopen_s(&file, path, L"r") != 0 || file == NULL)
return;
while (fgets(line, sizeof(line), file)) {
unsigned long port;
char *end;
if (strncmp(line, "port=", 5) != 0)
continue;
port = strtoul(line + 5, &end, 10);
if (end != line + 5 && port > 0 && port < 65536)
g_service_port = (unsigned short)port;
}
fclose(file);
}
void config_load(HMODULE self)
{
wchar_t path[MAX_PATH];
InitializeCriticalSection(&g_log_lock);
find_folder(self);
read_port();
if (_snwprintf_s(path, MAX_PATH, _TRUNCATE, L"%swto_ws.log", g_folder) >= 0)
_wfopen_s(&g_log, path, L"w");
log_line("wto_ws: local service port %u%s", g_service_port, g_service_port ? "" : " (none: outside connections are refused)");
}
void log_line(const char *format, ...)
{
va_list args;
SYSTEMTIME now;
if (g_log == NULL)
return;
GetLocalTime(&now);
EnterCriticalSection(&g_log_lock);
fprintf(g_log, "%02u:%02u:%02u.%03u ", now.wHour, now.wMinute, now.wSecond, now.wMilliseconds);
va_start(args, format);
vfprintf(g_log, format, args);
va_end(args);
fputc('\n', g_log);
fflush(g_log);
LeaveCriticalSection(&g_log_lock);
}
void describe_address(const struct sockaddr *name, int length, char *text, size_t size)
{
char host[INET6_ADDRSTRLEN] = "?";
unsigned short port = 0;
if (name && name->sa_family == AF_INET && length >= (int)sizeof(struct sockaddr_in)) {
const struct sockaddr_in *v4 = (const struct sockaddr_in *)name;
inet_ntop(AF_INET, &v4->sin_addr, host, sizeof(host));
port = ntohs(v4->sin_port);
} else if (name && name->sa_family == AF_INET6 && length >= (int)sizeof(struct sockaddr_in6)) {
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)name;
inet_ntop(AF_INET6, &v6->sin6_addr, host, sizeof(host));
port = ntohs(v6->sin6_port);
}
_snprintf_s(text, size, _TRUNCATE, "%s:%u", host, port);
}
+9
View File
@@ -0,0 +1,9 @@
/* Settings (wto_ws.cfg) and the run log (wto_ws.log) next to the DLL. */
#pragma once
#include <winsock2.h>
#include <ws2tcpip.h>
void config_load(HMODULE self);
void log_line(const char *format, ...);
void describe_address(const struct sockaddr *name, int length, char *text, size_t size);
+11
View File
@@ -0,0 +1,11 @@
/* Hooks on the system's ws2_32.dll exports. */
#pragma once
#include <winsock2.h>
#include <ws2tcpip.h>
/* Hook one export of the system's ws2_32.dll; *original receives the trampoline. Returns 1 on success. */
int hook_export(const char *name, void *replacement, void **original);
int install_resolver_hooks(void);
int install_connection_hooks(void);
+88
View File
@@ -0,0 +1,88 @@
/* Where a connection may go (redirect_rules.h, implementation notes). */
#include "redirect_rules.h"
#include <string.h>
unsigned short g_service_port = 0;
static int is_marker_v4(const struct in_addr *address)
{
return address->S_un.S_un_b.s_b1 == 127 && address->S_un.S_un_b.s_b2 == 0
&& address->S_un.S_un_b.s_b3 == 0 && address->S_un.S_un_b.s_b4 == 2;
}
static int is_loopback_v4(const struct in_addr *address)
{
return address->S_un.S_un_b.s_b1 == 127;
}
static int is_unspecified_v4(const struct in_addr *address)
{
return address->S_un.S_addr == 0;
}
static enum redirect_result to_service(struct sockaddr_storage *out, int *out_length)
{
struct sockaddr_in *local = (struct sockaddr_in *)out;
if (g_service_port == 0)
return REDIRECT_REFUSE;
memset(out, 0, sizeof(*out));
local->sin_family = AF_INET;
local->sin_port = htons(g_service_port);
local->sin_addr.S_un.S_addr = htonl(INADDR_LOOPBACK);
*out_length = (int)sizeof(struct sockaddr_in);
return REDIRECT_CHANGED;
}
enum redirect_result redirect_destination(const struct sockaddr *name, int length,
struct sockaddr_storage *out, int *out_length)
{
if (name == NULL)
return REDIRECT_KEEP;
if (name->sa_family == AF_INET && length >= (int)sizeof(struct sockaddr_in)) {
const struct sockaddr_in *v4 = (const struct sockaddr_in *)name;
if (is_marker_v4(&v4->sin_addr) || (!is_loopback_v4(&v4->sin_addr) && !is_unspecified_v4(&v4->sin_addr)))
return to_service(out, out_length);
return REDIRECT_KEEP;
}
if (name->sa_family == AF_INET6 && length >= (int)sizeof(struct sockaddr_in6)) {
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)name;
if (IN6_IS_ADDR_LOOPBACK(&v6->sin6_addr) || IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
return REDIRECT_KEEP;
if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) {
struct in_addr v4;
memcpy(&v4, &v6->sin6_addr.u.Byte[12], sizeof(v4));
if (!is_marker_v4(&v4) && is_loopback_v4(&v4))
return REDIRECT_KEEP;
}
/* The local service listens on IPv4 only, which an IPv6 socket cannot always reach. */
return REDIRECT_REFUSE;
}
return REDIRECT_KEEP;
}
static int is_numeric_host(const char *host)
{
struct in_addr v4;
struct in6_addr v6;
return inet_pton(AF_INET, host, &v4) == 1 || inet_pton(AF_INET6, host, &v6) == 1;
}
int is_redirected_host(const char *host)
{
if (host == NULL || host[0] == '\0')
return 0;
if (_stricmp(host, "localhost") == 0)
return 0;
return !is_numeric_host(host);
}
int is_redirected_host_w(const wchar_t *host)
{
char narrow[256];
if (host == NULL || host[0] == L'\0')
return 0;
if (WideCharToMultiByte(CP_UTF8, 0, host, -1, narrow, (int)sizeof(narrow), NULL, NULL) == 0)
return 1; /* too long to be an address or localhost */
return is_redirected_host(narrow);
}
+32
View File
@@ -0,0 +1,32 @@
/*
* Where a connection may go.
*
* Every host name resolves to MARKER_ADDRESS (127.0.0.2). A destination that is the marker,
* or any address outside the loopback network, is replaced by 127.0.0.1:<local service port>;
* other loopback addresses (127.0.0.1 and the rest of 127/8, ::1) are left alone, because the
* game reaches the local service and its own helpers there.
*/
#pragma once
#include <winsock2.h>
#include <ws2tcpip.h>
#define MARKER_ADDRESS "127.0.0.2"
#define MARKER_ADDRESS_W L"127.0.0.2"
/* The local service port read from wto_ws.cfg; 0 when there is none, and then nothing is let out. */
extern unsigned short g_service_port;
enum redirect_result {
REDIRECT_KEEP, /* leave the destination as it is */
REDIRECT_CHANGED, /* *out holds the local service address */
REDIRECT_REFUSE /* no way to keep it local: fail the call */
};
/* Decide for one destination; out must hold at least sizeof(struct sockaddr_in6). */
enum redirect_result redirect_destination(const struct sockaddr *name, int length,
struct sockaddr_storage *out, int *out_length);
/* Whether a host name must be answered with the marker address (not a numeric address, not localhost). */
int is_redirected_host(const char *host);
int is_redirected_host_w(const wchar_t *host);
+75
View File
@@ -0,0 +1,75 @@
/*
* Host name resolution: every host name that is not a numeric address
* or localhost is resolved as the marker address 127.0.0.2, so no lookup leaves the machine
* and the connection hooks know the destination stood for a host name.
*/
#include "hooks.h"
#include "hook_config.h"
#include "redirect_rules.h"
typedef int (WSAAPI *getaddrinfo_fn)(PCSTR, PCSTR, const ADDRINFOA *, PADDRINFOA *);
typedef int (WSAAPI *GetAddrInfoW_fn)(PCWSTR, PCWSTR, const ADDRINFOW *, PADDRINFOW *);
typedef struct hostent *(WSAAPI *gethostbyname_fn)(const char *);
static getaddrinfo_fn real_getaddrinfo;
static GetAddrInfoW_fn real_GetAddrInfoW;
static gethostbyname_fn real_gethostbyname;
/* Only IPv4 answers: the marker is an IPv4 address. */
static int wants_only_ipv6(int family)
{
return family == AF_INET6;
}
static int WSAAPI hooked_getaddrinfo(PCSTR node, PCSTR service, const ADDRINFOA *hints, PADDRINFOA *result)
{
ADDRINFOA local_hints;
if (!is_redirected_host(node))
return real_getaddrinfo(node, service, hints, result);
log_line("resolve %s -> " MARKER_ADDRESS, node);
if (hints && wants_only_ipv6(hints->ai_family))
return WSAHOST_NOT_FOUND;
if (hints)
local_hints = *hints;
else {
ZeroMemory(&local_hints, sizeof(local_hints));
}
local_hints.ai_family = AF_INET;
local_hints.ai_flags |= AI_NUMERICHOST;
local_hints.ai_flags &= ~AI_CANONNAME;
return real_getaddrinfo(MARKER_ADDRESS, service, &local_hints, result);
}
static int WSAAPI hooked_GetAddrInfoW(PCWSTR node, PCWSTR service, const ADDRINFOW *hints, PADDRINFOW *result)
{
ADDRINFOW local_hints;
if (!is_redirected_host_w(node))
return real_GetAddrInfoW(node, service, hints, result);
log_line("resolve %ls -> " MARKER_ADDRESS, node);
if (hints && wants_only_ipv6(hints->ai_family))
return WSAHOST_NOT_FOUND;
if (hints)
local_hints = *hints;
else {
ZeroMemory(&local_hints, sizeof(local_hints));
}
local_hints.ai_family = AF_INET;
local_hints.ai_flags |= AI_NUMERICHOST;
local_hints.ai_flags &= ~AI_CANONNAME;
return real_GetAddrInfoW(MARKER_ADDRESS_W, service, &local_hints, result);
}
static struct hostent *WSAAPI hooked_gethostbyname(const char *name)
{
if (!is_redirected_host(name))
return real_gethostbyname(name);
log_line("resolve %s -> " MARKER_ADDRESS " (gethostbyname)", name);
return real_gethostbyname(MARKER_ADDRESS);
}
int install_resolver_hooks(void)
{
return hook_export("getaddrinfo", (void *)hooked_getaddrinfo, (void **)&real_getaddrinfo)
&& hook_export("GetAddrInfoW", (void *)hooked_GetAddrInfoW, (void **)&real_GetAddrInfoW)
&& hook_export("gethostbyname", (void *)hooked_gethostbyname, (void **)&real_gethostbyname);
}
+54
View File
@@ -0,0 +1,54 @@
; wto_ws.dll exports: every function aces.exe 2.61.0.37 imports from WS2_32.dll, forwarded to the
; system ws2_32.dll (whose resolution and connection functions the DLL hooks, dll_main.c).
; Regenerate from the executable's import table when the game version changes.
LIBRARY wto_ws
EXPORTS
WSACleanup=ws2_32.WSACleanup
WSACloseEvent=ws2_32.WSACloseEvent
WSACreateEvent=ws2_32.WSACreateEvent
WSAEnumNetworkEvents=ws2_32.WSAEnumNetworkEvents
WSAEventSelect=ws2_32.WSAEventSelect
WSAGetLastError=ws2_32.WSAGetLastError
WSAIoctl=ws2_32.WSAIoctl
WSARecv=ws2_32.WSARecv
WSARecvFrom=ws2_32.WSARecvFrom
WSAResetEvent=ws2_32.WSAResetEvent
WSASend=ws2_32.WSASend
WSASendTo=ws2_32.WSASendTo
WSASetEvent=ws2_32.WSASetEvent
WSASetLastError=ws2_32.WSASetLastError
WSASocketA=ws2_32.WSASocketA
WSAStartup=ws2_32.WSAStartup
WSAWaitForMultipleEvents=ws2_32.WSAWaitForMultipleEvents
__WSAFDIsSet=ws2_32.__WSAFDIsSet
accept=ws2_32.accept
bind=ws2_32.bind
closesocket=ws2_32.closesocket
connect=ws2_32.connect
freeaddrinfo=ws2_32.freeaddrinfo
getaddrinfo=ws2_32.getaddrinfo
gethostbyname=ws2_32.gethostbyname
gethostname=ws2_32.gethostname
getnameinfo=ws2_32.getnameinfo
getpeername=ws2_32.getpeername
getservbyname=ws2_32.getservbyname
getsockname=ws2_32.getsockname
getsockopt=ws2_32.getsockopt
htonl=ws2_32.htonl
htons=ws2_32.htons
inet_addr=ws2_32.inet_addr
inet_ntoa=ws2_32.inet_ntoa
inet_ntop=ws2_32.inet_ntop
inet_pton=ws2_32.inet_pton
ioctlsocket=ws2_32.ioctlsocket
listen=ws2_32.listen
ntohl=ws2_32.ntohl
ntohs=ws2_32.ntohs
recv=ws2_32.recv
recvfrom=ws2_32.recvfrom
select=ws2_32.select
send=ws2_32.send
sendto=ws2_32.sendto
setsockopt=ws2_32.setsockopt
shutdown=ws2_32.shutdown
socket=ws2_32.socket