Everything sat in packages/: two programs next to the small libraries
they use, and the patch data loose at the root. The programs now live in
apps/ and the shared libraries in packages/:
apps/server the local server (was packages/local-service),
package @wto/server
packages/patcher builds the game copy (was packages/build),
package @wto/patcher
patches/exe exe patch definitions (was exe-patches)
patches/gui GUI script patches (was gui-patches)
Inside the server, the network layer folder becomes transport/ so it is
not server/server/, the entry module is server.ts, and startLocalService
and its types become startServer, ServerOptions and RunningServer. The
health answer names the app wto-server, and the TLS files in the data
folder are server.key and server.crt (made again on the next start).
Comments, logs and tool texts say "local server".
The patcher no longer reaches into the server's folder. It read the
default tuning/store.jsonc by a relative path to register bundle GUIDs
for the multi-vehicle packs, which also ignored the player's overrides
in <data>/tuning/. It now takes the pack ids from its caller; the
server exports multiVehiclePackIds(dataFolder), which reads them the way
the running server does. rebuildGuiPack takes the patch folder from its
caller as well.
The development build moves out of the patcher to
scripts/build-game-copy.ts, since it uses both the server and the
patcher. It needs --data (the folder the server will run with) and
defaults the patch folders and the hook library to this repository.
Checked with the root typecheck, check-patches, check-gui,
check-offsets, the native build, a run of build-game-copy.ts into a
scratch folder (12 exe patches, 35 GUI patches, 496 bundle GUIDs as
before) and a server start from apps/server answering wto-server.
128 lines
5.7 KiB
C
128 lines
5.7 KiB
C
/*
|
|
* Connections and datagrams: a destination that is the marker address
|
|
* or outside the loopback network goes to 127.0.0.1:<local server 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 server. 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);
|
|
}
|