Repository navigation
Expand file tree
/
Copy pathbridge.cpp
More file actions
403 lines (346 loc) · 14.3 KB
/
Copy pathbridge.cpp
File metadata and controls
403 lines (346 loc) · 14.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
402
403
#include "bridge.hpp"
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>
#include "./vr2bridge/bridge_protocol.hpp"
#include "logger.hpp"
#ifdef _WIN32
#include <conio.h>
#include <windows.h>
#else
#include <sys/select.h>
#include <termios.h>
#include <unistd.h>
static int _kbhit() {
struct timeval tv = {0L, 0L};
fd_set fds;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
return select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv) > 0;
}
static int _getch() {
unsigned char ch = 0;
if (read(STDIN_FILENO, &ch, 1) < 0) {
return 0;
}
return ch;
}
#endif
PSVR2Bridge::PSVR2Bridge(libusb_device_handle* dev_handle) : dev(dev_handle) {}
bool PSVR2Bridge::send_packet(uint8_t type, uint32_t session_id, const uint8_t* payload, uint16_t len) {
PacketHeader hdr = {PKT_MAGIC, type, len, session_id};
std::vector<uint8_t> tx_buf{};
tx_buf.reserve(sizeof(hdr) + len);
tx_buf.insert(tx_buf.end(), (uint8_t*)&hdr, (uint8_t*)&hdr + sizeof(hdr));
if (len > 0 && payload != nullptr) {
tx_buf.insert(tx_buf.end(), payload, payload + len);
}
int actual;
return (libusb_bulk_transfer(dev, 0x04, tx_buf.data(), static_cast<int>(tx_buf.size()), &actual, 1000) == 0);
}
void PSVR2Bridge::upload(const std::string& local_path, const std::string& remote_path) {
if (!dev) {
LOG_ERROR << "[-] USB device handle invalid." << std::endl;
return;
}
std::ifstream file(local_path, std::ios::binary);
if (!file) {
LOG_ERROR << "[-] Failed to open local file '" << local_path << "'" << std::endl;
return;
}
LOG_INFO << "[*] Uploading to " << remote_path << "..." << std::endl;
if (!send_packet(PKT_FILE_START, 0, (const uint8_t*)remote_path.c_str(), static_cast<uint16_t>(remote_path.length()))) {
LOG_ERROR << "[-] Failed to send file start command." << std::endl;
return;
}
uint8_t buffer[MAX_PAYLOAD_SIZE];
size_t total_bytes = 0;
while (file.read((char*)buffer, sizeof(buffer)) || file.gcount() > 0) {
if (!send_packet(PKT_FILE_DATA, 0, buffer, static_cast<uint16_t>(file.gcount()))) {
LOG_ERROR << "[-] Failed to transmit file chunk." << std::endl;
return;
}
total_bytes += file.gcount();
}
send_packet(PKT_FILE_END, 0, nullptr, 0);
LOG_INFO << "[+] Upload complete (" << std::dec << total_bytes << " bytes)." << std::endl;
}
void PSVR2Bridge::download(const std::string& remote_path, const std::string& local_path) {
if (!dev) {
LOG_ERROR << "[-] USB device handle invalid." << std::endl;
return;
}
std::ofstream file(local_path, std::ios::binary);
if (!file) {
LOG_ERROR << "[-] Failed to open local file '" << local_path << "' for writing." << std::endl;
return;
}
LOG_INFO << "[*] Requesting download of '" << remote_path << "'..." << std::endl;
if (!send_packet(PKT_DOWNLOAD_REQ, 0, (const uint8_t*)remote_path.c_str(), static_cast<uint16_t>(remote_path.length()))) {
LOG_ERROR << "[-] Failed to send download request." << std::endl;
return;
}
uint8_t rx_buf[0x1000];
int actual_length;
bool transmission_started = false;
bool transmission_ended = false;
size_t total_bytes = 0;
std::vector<uint8_t> stream_buf;
int timeouts = 0;
while (!transmission_ended) {
int r = libusb_bulk_transfer(dev, 0x84, rx_buf, sizeof(rx_buf), &actual_length, 2000);
if (r == 0) {
if (actual_length > 0) {
timeouts = 0; // Reset timeout counter on successful read
stream_buf.insert(stream_buf.end(), rx_buf, rx_buf + actual_length);
size_t offset = 0;
bool consumed_any = true;
while (consumed_any && offset < stream_buf.size()) {
consumed_any = false;
if (stream_buf[offset] != PKT_MAGIC) {
offset++;
consumed_any = true;
continue;
}
if (offset + sizeof(PacketHeader) <= stream_buf.size()) {
PacketHeader* hdr = (PacketHeader*)(stream_buf.data() + offset);
if (offset + sizeof(PacketHeader) + hdr->length <= stream_buf.size()) {
uint8_t* payload = stream_buf.data() + offset + sizeof(PacketHeader);
if (hdr->type == PKT_FILE_START) {
LOG_INFO << "[*] Download started." << std::endl;
transmission_started = true;
} else if (hdr->type == PKT_FILE_DATA) {
if (!transmission_started) {
LOG_WARN << "[!] Received file data before start packet." << std::endl;
transmission_started = true;
}
file.write((char*)payload, hdr->length);
total_bytes += hdr->length;
} else if (hdr->type == PKT_FILE_END) {
LOG_INFO << "[+] Download complete (" << std::dec << total_bytes << " bytes)." << std::endl;
transmission_ended = true;
} else if (hdr->type == PKT_FILE_ERR) {
std::string err_msg((char*)payload, hdr->length);
LOG_ERROR << "[-] Remote error: " << err_msg << std::endl;
transmission_ended = true;
file.close();
std::remove(local_path.c_str());
}
offset += sizeof(PacketHeader) + hdr->length;
consumed_any = true;
}
}
}
if (offset > 0) {
stream_buf.erase(stream_buf.begin(), stream_buf.begin() + offset);
}
} else {
// actual_length == 0 (ZLP), just continue reading
continue;
}
} else if (r == LIBUSB_ERROR_TIMEOUT) {
if (!transmission_started) {
LOG_ERROR << "[-] Timeout waiting for download response." << std::endl;
break;
}
timeouts++;
if (timeouts > 5) {
LOG_ERROR << "[-] Download timed out (no activity for 10 seconds)." << std::endl;
break;
}
} else {
LOG_ERROR << "[-] USB transfer error: " << r << std::endl;
break;
}
}
if (!transmission_ended) {
LOG_ERROR << "[-] Download failed or interrupted." << std::endl;
file.close();
std::remove(local_path.c_str());
}
}
struct RxThreadCtx {
libusb_device_handle* dev_handle;
std::atomic<bool>* keep_running;
uint32_t session_id;
};
static void rx_thread_mux(RxThreadCtx* ctx) {
uint8_t buf[0x1000];
int actual_length;
while (*ctx->keep_running) {
int r = libusb_bulk_transfer(ctx->dev_handle, 0x84, buf, sizeof(buf), &actual_length, 1000);
if (r == 0 && actual_length > 0) {
size_t offset = 0;
while (offset + sizeof(PacketHeader) <= (size_t)actual_length) {
PacketHeader* hdr = (PacketHeader*)(buf + offset);
if (hdr->magic != PKT_MAGIC) {
offset++;
continue;
}
if (offset + sizeof(PacketHeader) + hdr->length <= (size_t)actual_length) {
uint8_t* payload = buf + offset + sizeof(PacketHeader);
if (hdr->type == PKT_TTY_OUT && hdr->session_id == ctx->session_id) {
std::cout.write((char*)payload, hdr->length);
std::cout.flush();
} else if (hdr->type == PKT_TTY_STOP && hdr->session_id == ctx->session_id) {
*ctx->keep_running = false;
}
offset += sizeof(PacketHeader) + hdr->length;
} else {
break;
}
}
} else if (r != 0 && r != LIBUSB_ERROR_TIMEOUT) {
*ctx->keep_running = false;
break;
}
}
}
void PSVR2Bridge::shell(const std::string& shell_cmd, uint32_t attach_session_id) {
if (!dev) {
LOG_ERROR << "[-] USB device handle invalid." << std::endl;
return;
}
uint32_t current_session_id = attach_session_id;
if (current_session_id == 0) {
LOG_INFO << "[*] Starting remote shell: '" << shell_cmd << "'" << std::endl;
if (!send_packet(PKT_TTY_START, 0, (const uint8_t*)shell_cmd.c_str(), static_cast<uint16_t>(shell_cmd.length()))) {
LOG_ERROR << "[-] Failed to send TTY start command." << std::endl;
return;
}
uint8_t buf[0x1000];
int actual;
while (current_session_id == 0) {
int r = libusb_bulk_transfer(dev, 0x84, buf, sizeof(buf), &actual, 1000);
if (r == 0 && actual > 0) {
size_t offset = 0;
while (offset + sizeof(PacketHeader) <= (size_t)actual) {
PacketHeader* hdr = (PacketHeader*)(buf + offset);
if (hdr->magic == PKT_MAGIC && hdr->type == PKT_TTY_STARTED) {
current_session_id = hdr->session_id;
break;
}
offset += sizeof(PacketHeader) + hdr->length;
}
} else if (r != 0 && r != LIBUSB_ERROR_TIMEOUT) {
LOG_ERROR << "[-] Failed to get session ID." << std::endl;
return;
}
}
} else {
LOG_INFO << "[*] Attaching to session " << current_session_id << std::endl;
}
#ifdef _WIN32
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD original_mode;
GetConsoleMode(hIn, &original_mode);
SetConsoleMode(hIn, original_mode & ~ENABLE_PROCESSED_INPUT);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD original_out_mode;
GetConsoleMode(hOut, &original_out_mode);
SetConsoleMode(hOut, original_out_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#else
struct termios original_mode;
tcgetattr(STDIN_FILENO, &original_mode);
struct termios raw = original_mode;
raw.c_lflag &= ~(ICANON | ECHO | ISIG);
tcsetattr(STDIN_FILENO, TCSANOW, &raw);
#endif
LOG_INFO << "[*] Entered headset shell session " << current_session_id << ". Press Ctrl+] to detach." << std::endl;
std::atomic<bool> keep_running{true};
RxThreadCtx ctx = {dev, &keep_running, current_session_id};
std::thread t_rx(rx_thread_mux, &ctx);
while (keep_running) {
if (_kbhit()) {
int c = _getch();
if (c == 0x1D) { // Ctrl+]
LOG_INFO << "\n[*] Detaching from shell..." << std::endl;
send_packet(PKT_TTY_STOP, current_session_id, nullptr, 0);
keep_running = false;
break;
}
#ifdef _WIN32
if (c == 0x00 || c == 0xE0) {
int ext = _getch();
const char* seq = nullptr;
switch (ext) {
case 72:
seq = "\x1B[A";
break; // Up
case 80:
seq = "\x1B[B";
break; // Down
case 77:
seq = "\x1B[C";
break; // Right
case 75:
seq = "\x1B[D";
break; // Left
}
if (seq) {
if (!send_packet(PKT_TTY_IN, current_session_id, (uint8_t*)seq, 3)) {
keep_running = false;
break;
}
}
continue;
}
#endif
if (c == '\r') c = '\n';
if (c == '\b') c = 127;
uint8_t out_byte = (uint8_t)c;
if (!send_packet(PKT_TTY_IN, current_session_id, &out_byte, 1)) {
keep_running = false;
break;
}
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}
if (t_rx.joinable()) t_rx.join();
#ifdef _WIN32
SetConsoleMode(hIn, original_mode);
SetConsoleMode(hOut, original_out_mode);
#else
tcsetattr(STDIN_FILENO, TCSANOW, &original_mode);
#endif
}
void PSVR2Bridge::list_sessions() {
if (!dev) return;
if (!send_packet(PKT_TTY_LIST, 0, nullptr, 0)) return;
uint8_t buf[0x1000];
int actual;
while (true) {
int r = libusb_bulk_transfer(dev, 0x84, buf, sizeof(buf), &actual, 1000);
if (r == 0 && actual > 0) {
size_t offset = 0;
while (offset + sizeof(PacketHeader) <= (size_t)actual) {
PacketHeader* hdr = (PacketHeader*)(buf + offset);
if (hdr->magic == PKT_MAGIC) {
if (hdr->type == PKT_TTY_LIST_RESP) {
uint8_t* payload = buf + offset + sizeof(PacketHeader);
std::cout << "\nActive Sessions:\n";
std::cout.write((char*)payload, hdr->length);
std::cout.flush();
return;
}
offset += sizeof(PacketHeader) + hdr->length;
} else {
offset++;
}
}
} else if (r != 0 && r != LIBUSB_ERROR_TIMEOUT) {
break;
}
}
}
void PSVR2Bridge::kill_session(uint32_t session_id) {
if (!dev) return;
send_packet(PKT_TTY_STOP, session_id, nullptr, 0);
LOG_INFO << "[*] Sent kill signal to session " << session_id << "\n";
}