Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
401 lines (341 loc) · 11.3 KB
/
Copy pathmain.cpp
File metadata and controls
401 lines (341 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// wine-discord-ipc-bridge -- C++20 rewrite.
//
// Bridges Discord IPC between a Windows named pipe (the side a program running
// under Wine connects to) and the host's Linux Unix-domain socket.
//
// This is a C++20 rewrite of the original C by Matthew Tran (0e4ef622):
// https://github.com/0e4ef622/wine-discord-ipc-bridge
// Behaviour is intentionally identical to the original. Only the language
// surface was modernised -- C++ headers, C++ casts, `constexpr` constants,
// `nullptr`, and the standard `int main()` entry point. The raw `int 0x80`
// syscall stubs are byte-for-byte the original assembly.
#include <windows.h> // kept bare on purpose: pulls in <winsock.h> for `struct sockaddr`
#include <cstdint>
#include <cstdio>
#include <cstring>
constexpr int BUFSIZE = 4096;
constexpr int DUMP_LIMIT = 256;
static HANDLE hPipe = INVALID_HANDLE_VALUE;
static int sock_fd = -1;
// ------------------ Wine/Linux syscall wrappers ------------------
__declspec(naked) int l_close(int fd) {
__asm__(
"push ebx\n\t"
"mov eax, 0x06\n\t"
"mov ebx, [esp + 8]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
__declspec(naked) int l_read(unsigned int fd, char* buf, unsigned int count) {
__asm__(
"push ebx\n\t"
"mov eax, 0x03\n\t"
"mov ebx, [esp + 8]\n\t"
"mov ecx, [esp + 12]\n\t"
"mov edx, [esp + 16]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
__declspec(naked) int l_write(unsigned int fd, const char* buf, unsigned int count) {
__asm__(
"push ebx\n\t"
"mov eax, 0x04\n\t"
"mov ebx, [esp + 8]\n\t"
"mov ecx, [esp + 12]\n\t"
"mov edx, [esp + 16]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
__declspec(naked) int l_open(const char* path, int flags, int mode) {
__asm__(
"push ebx\n\t"
"mov eax, 0x05\n\t"
"mov ebx, [esp + 8]\n\t"
"mov ecx, [esp + 12]\n\t"
"mov edx, [esp + 16]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
// ------------------ Socket wrappers ------------------
struct sockaddr_un {
unsigned short sun_family;
char sun_path[108];
};
#define AF_UNIX 1
#define SOCK_STREAM 1
__declspec(naked) int l_socketcall(int call, void* args) {
__asm__(
"push ebx\n\t"
"mov eax, 0x66\n\t"
"mov ebx, [esp + 8]\n\t"
"mov ecx, [esp + 12]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
int l_socket(int domain, int type, int protocol) {
void* args[3];
args[0] = reinterpret_cast<void*>(static_cast<std::intptr_t>(domain));
args[1] = reinterpret_cast<void*>(static_cast<std::intptr_t>(type));
args[2] = reinterpret_cast<void*>(static_cast<std::intptr_t>(protocol));
return l_socketcall(1, args);
}
int l_connect(int sockfd, const sockaddr* addr, unsigned int addrlen) {
void* args[3];
args[0] = reinterpret_cast<void*>(static_cast<std::intptr_t>(sockfd));
args[1] = const_cast<void*>(reinterpret_cast<const void*>(addr));
args[2] = reinterpret_cast<void*>(static_cast<std::intptr_t>(addrlen));
return l_socketcall(3, args);
}
// ------------------ Environment ------------------
char* getenv_(const char* name) {
static char buf[1024 * 1024];
static char* end = nullptr;
if (!end) {
int fd = l_open("/proc/self/environ", 0, 0);
if (fd < 0) return nullptr;
int n = l_read(fd, buf, sizeof(buf));
l_close(fd);
if (n < 0) return nullptr;
end = buf + n;
}
unsigned int len = static_cast<unsigned int>(std::strlen(name));
char* p = buf;
while (p < end) {
if (!std::strncmp(p, name, len) && p[len] == '=') {
return p + len + 1;
}
while (*p && p < end) p++;
p++;
}
return nullptr;
}
static const char* get_temp_path() {
const char* temp = getenv_("XDG_RUNTIME_DIR");
if (!temp) temp = getenv_("TMPDIR");
if (!temp) temp = getenv_("TMP");
if (!temp) temp = getenv_("TEMP");
if (!temp) temp = "/tmp";
return temp;
}
// ------------------ Helpers ------------------
static uint32_t read_le32(const unsigned char* p) {
return (static_cast<uint32_t>(p[0])) |
(static_cast<uint32_t>(p[1]) << 8) |
(static_cast<uint32_t>(p[2]) << 16) |
(static_cast<uint32_t>(p[3]) << 24);
}
static void print_json_snippet(const char* data, uint32_t len) {
uint32_t n = len;
if (n > DUMP_LIMIT) n = DUMP_LIMIT;
std::putchar('"');
for (uint32_t i = 0; i < n; ++i) {
unsigned char c = static_cast<unsigned char>(data[i]);
if (c == '\n') {
std::fputs("\\n", stdout);
} else if (c == '\r') {
std::fputs("\\r", stdout);
} else if (c == '\t') {
std::fputs("\\t", stdout);
} else if (c == '"') {
std::fputs("\\\"", stdout);
} else if (c == '\\') {
std::fputs("\\\\", stdout);
} else if (c >= 32 && c < 127) {
std::putchar(static_cast<char>(c));
} else {
std::printf("\\x%02X", c);
}
}
if (len > n) {
std::fputs("...<truncated>", stdout);
}
std::putchar('"');
}
static void dump_frame(const char* tag, const char* buf, int bytes) {
if (bytes < 8) {
std::printf("%s: short frame (%d bytes)\n", tag, bytes);
std::fflush(stdout);
return;
}
{
uint32_t opcode = read_le32(reinterpret_cast<const unsigned char*>(buf));
uint32_t length = read_le32(reinterpret_cast<const unsigned char*>(buf) + 4);
int payload_bytes = bytes - 8;
std::printf("%s: opcode=%u length=%u payload_bytes_now=%d json=",
tag, opcode, length, payload_bytes);
if (payload_bytes > 0) {
print_json_snippet(buf + 8, static_cast<uint32_t>(payload_bytes));
} else {
std::fputs("\"\"", stdout);
}
std::putchar('\n');
std::fflush(stdout);
}
}
// ------------------ Thread Socket -> Pipe ------------------
DWORD WINAPI sock_to_pipe(LPVOID lpvParam) {
(void)lpvParam;
for (;;) {
char header[8];
int n = l_read(sock_fd, header, 8);
if (n <= 0) {
std::printf("sock->pipe: socket read failed/EOF (%d)\n", n);
std::fflush(stdout);
break;
}
if (n != 8) {
std::printf("sock->pipe: partial header read (%d)\n", n);
std::fflush(stdout);
break;
}
{
uint32_t length = read_le32(reinterpret_cast<const unsigned char*>(header) + 4);
if (length > BUFSIZE - 8) length = BUFSIZE - 8;
char buf[BUFSIZE];
std::memcpy(buf, header, 8);
int payload_read = 0;
while (payload_read < static_cast<int>(length)) {
int r = l_read(sock_fd, buf + 8 + payload_read, length - payload_read);
if (r <= 0) {
std::printf("sock->pipe: payload read failed/EOF (%d)\n", r);
std::fflush(stdout);
break;
}
payload_read += r;
}
dump_frame("sock->pipe", buf, payload_read + 8);
{
DWORD written = 0;
if (!WriteFile(hPipe, buf, static_cast<DWORD>(payload_read + 8), &written, nullptr)) {
std::printf("sock->pipe: pipe write failed (%lu)\n", GetLastError());
std::fflush(stdout);
break;
}
}
}
}
return 0;
}
// ------------------ Thread Pipe -> Socket ------------------
DWORD WINAPI pipe_to_sock(LPVOID lpvParam) {
(void)lpvParam;
for (;;) {
char buf[BUFSIZE];
DWORD bytes_read = 0;
if (!ReadFile(hPipe, buf, BUFSIZE, &bytes_read, nullptr)) {
std::printf("pipe->sock: ReadFile failed (%lu)\n", GetLastError());
std::fflush(stdout);
break;
}
if (bytes_read == 0) {
std::printf("pipe->sock: EOF\n");
std::fflush(stdout);
break;
}
dump_frame("pipe->sock", buf, static_cast<int>(bytes_read));
{
int total_written = 0;
while (total_written < static_cast<int>(bytes_read)) {
int w = l_write(sock_fd, buf + total_written, static_cast<unsigned int>(bytes_read - total_written));
if (w <= 0) {
std::printf("pipe->sock: socket write failed (%d)\n", w);
std::fflush(stdout);
break;
}
total_written += w;
}
}
}
return 0;
}
// ------------------ Main ------------------
int main() {
const char* lpszPipename = "\\\\.\\pipe\\discord-ipc-0";
std::printf("Pipe server: awaiting client connection on %s\n", lpszPipename);
hPipe = CreateNamedPipeA(
lpszPipename,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1,
BUFSIZE,
BUFSIZE,
0,
nullptr
);
if (hPipe == INVALID_HANDLE_VALUE) {
std::printf("CreateNamedPipe failed (%lu)\n", GetLastError());
std::fflush(stdout);
return -1;
}
{
BOOL fConnected = ConnectNamedPipe(hPipe, nullptr) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if (!fConnected) {
std::printf("ConnectNamedPipe failed (%lu)\n", GetLastError());
std::fflush(stdout);
return 0;
}
}
std::printf("Client connected\n");
std::fflush(stdout);
sock_fd = l_socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_fd < 0) {
std::printf("socket() failed\n");
std::fflush(stdout);
return 1;
}
{
sockaddr_un addr;
const char* temp_path = get_temp_path();
const char* paths[] = {
"%s/discord-ipc-%d",
"%s/app/com.discordapp.Discord/discord-ipc-%d"
};
int connected = 0;
addr.sun_family = AF_UNIX;
for (int p = 0; p < 2; ++p) {
for (int n = 0; n < 10; ++n) {
std::snprintf(addr.sun_path, sizeof(addr.sun_path), paths[p], temp_path, n);
std::printf("Trying %s\n", addr.sun_path);
std::fflush(stdout);
if (l_connect(sock_fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) >= 0) {
connected = 1;
goto breakout;
}
}
}
breakout:
if (!connected) {
std::printf("Failed to connect to Discord socket\n");
std::fflush(stdout);
return 1;
}
}
std::printf("Socket connected\n");
std::fflush(stdout);
// IMPORTANT:
// NO send_rpc()/AUTHENTICATE FROM THE BRIDGE.
// The game/SDK must send the handshake themselves.
{
DWORD tid1 = 0, tid2 = 0;
HANDLE th1 = CreateThread(nullptr, 0, sock_to_pipe, nullptr, 0, &tid1);
HANDLE th2 = CreateThread(nullptr, 0, pipe_to_sock, nullptr, 0, &tid2);
if (!th1 || !th2) {
std::printf("CreateThread failed (%lu)\n", GetLastError());
std::fflush(stdout);
return 1;
}
WaitForSingleObject(th1, INFINITE);
WaitForSingleObject(th2, INFINITE);
}
return 0;
}