Files
WT-Offline/native/wto-net/src/hook_config.c
T
N0VAN0M4LY d95a5d8f0c Complete local HTTPS handling and allow shared log reads
Use the loopback service certificate for the offline HTTPS path. Retain
original host names for host-based routing and allow the network log
to be read while the client is running.
2026-11-25 06:05:20 +08:00

100 lines
3.2 KiB
C

/*
* 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 <share.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)
g_log = _wfsopen(path, L"w", _SH_DENYWR);
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);
}