forked from jkcoxson/idevice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_proxy.cpp
More file actions
128 lines (103 loc) · 3.81 KB
/
Copy pathdebug_proxy.cpp
File metadata and controls
128 lines (103 loc) · 3.81 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
// Jackson Coxson
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <idevice++/core_device_proxy.hpp>
#include <idevice++/debug_proxy.hpp>
#include <idevice++/ffi.hpp>
#include <idevice++/option.hpp>
#include <idevice++/provider.hpp>
#include <idevice++/rsd.hpp>
#include <idevice++/usbmuxd.hpp>
using namespace IdeviceFFI;
[[noreturn]]
static void die(const char* msg, const IdeviceFFI::FfiError& e) {
std::cerr << msg << ": " << e.message << "\n";
std::exit(1);
}
static std::vector<std::string> split_args(const std::string& line) {
std::istringstream iss(line);
std::vector<std::string> toks;
std::string tok;
while (iss >> tok) {
toks.push_back(tok);
}
return toks;
}
int main() {
IdeviceFFI::FfiError err;
// 1) Connect to usbmuxd and pick first device
auto mux = UsbmuxdConnection::default_new(/*tag*/ 0).expect("failed to connect to usbmuxd");
auto devices = mux.get_devices().expect("failed to list devices");
if (devices.empty()) {
std::cerr << "no devices connected\n";
return 1;
}
auto& dev = (devices)[0];
auto udid = dev.get_udid();
if (udid.is_none()) {
std::cerr << "device has no UDID\n";
return 1;
}
auto mux_id = dev.get_id();
if (mux_id.is_none()) {
std::cerr << "device has no mux id\n";
return 1;
}
// 2) Provider via default usbmuxd addr
auto addr = UsbmuxdAddr::default_new();
const uint32_t tag = 0;
const std::string label = "app_service-jkcoxson";
auto provider =
Provider::usbmuxd_new(std::move(addr), tag, udid.unwrap(), mux_id.unwrap(), label)
.expect("failed to create provider");
// 3) CoreDeviceProxy
auto cdp = CoreDeviceProxy::connect(provider).unwrap_or_else(
[](FfiError e) -> CoreDeviceProxy { die("failed to connect CoreDeviceProxy", e); });
auto rsd_port = cdp.get_server_rsd_port().unwrap_or_else(
[](FfiError err) -> uint16_t { die("failed to get server RSD port", err); });
// 4) Create software tunnel adapter (consumes proxy)
auto adapter =
std::move(cdp).create_tcp_adapter().expect("failed to create software tunnel adapter");
// 5) Connect adapter to RSD → ReadWrite stream
auto stream = adapter.connect(rsd_port).expect("failed to connect RSD stream");
// 6) RSD handshake (consumes stream)
auto rsd = RsdHandshake::from_socket(std::move(stream)).expect("failed RSD handshake");
// 6) DebugProxy over RSD
auto dbg =
IdeviceFFI::DebugProxy::connect_rsd(adapter, rsd).expect("failed to connect DebugProxy");
std::cout << "Shell connected! Type 'exit' to quit.\n";
for (;;) {
std::cout << "> " << std::flush;
std::string line;
if (!std::getline(std::cin, line)) {
break;
}
// trim
auto first = line.find_first_not_of(" \t\r\n");
if (first == std::string::npos) {
continue;
}
auto last = line.find_last_not_of(" \t\r\n");
line = line.substr(first, last - first + 1);
if (line == "exit") {
break;
}
// Interpret: first token = command name, rest = argv
auto toks = split_args(line);
if (toks.empty()) {
continue;
}
std::string name = toks.front();
std::vector<std::string> argv(toks.begin() + 1, toks.end());
auto res = dbg.send_command(name, argv);
match_result(
res,
ok_value,
{ if_let_some(ok_value, some_value, { std::cout << some_value << "\n"; }); },
err_value,
{ std::cerr << "send_command failed: " << err_value.message << "\n"; });
}
return 0;
}