forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDebugConnection.cpp
More file actions
executable file
·315 lines (256 loc) · 7.78 KB
/
DebugConnection.cpp
File metadata and controls
executable file
·315 lines (256 loc) · 7.78 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
#include "DebugConnection.h"
#include "ServerError.hpp"
#include "ConnectionError.hpp"
#include <WS2tcpip.h>
#define odslog(msg) { std::stringstream ss; ss << msg << std::endl; OutputDebugStringA(ss.str().c_str()); }
char* bin2hex(const unsigned char* bin, size_t length)
{
if (bin == NULL || length == 0)
{
return NULL;
}
char* hex = (char*)malloc(length * 2 + 1);
for (size_t i = 0; i < length; i++)
{
hex[i * 2] = "0123456789ABCDEF"[bin[i] >> 4];
hex[i * 2 + 1] = "0123456789ABCDEF"[bin[i] & 0x0F];
}
hex[length * 2] = '\0';
return hex;
}
DebugConnection::DebugConnection(std::shared_ptr<Device> device) : _device(device), _client(nullptr)
{
}
DebugConnection::~DebugConnection()
{
this->Disconnect();
}
pplx::task<void> DebugConnection::Connect()
{
return pplx::create_task([this]() {
/* Find Device */
idevice_t device = NULL;
auto cleanUp = [&]() {
if (device != NULL)
{
idevice_free(device);
}
};
try
{
if (idevice_new_with_options(&device, this->device()->identifier().c_str(), (enum idevice_options)((int)IDEVICE_LOOKUP_NETWORK | (int)IDEVICE_LOOKUP_USBMUX))
!= IDEVICE_E_SUCCESS)
{
throw ServerError(ServerErrorCode::DeviceNotFound);
}
/* Connect to debugserver */
debugserver_error_t error = debugserver_client_start_service(device, &_client, "AltServer");
if (error != DEBUGSERVER_E_SUCCESS)
{
auto err = ConnectionError::errorForDebugServerError(error, this->device());
if (err.has_value())
{
throw* err;
}
}
cleanUp();
}
catch (std::exception& e)
{
cleanUp();
throw;
}
});
}
void DebugConnection::Disconnect()
{
if (_client == nullptr)
{
return;
}
debugserver_client_free(_client);
_client = nullptr;
}
pplx::task<void> DebugConnection::EnableUnsignedCodeExecution(std::string processName)
{
return this->EnableUnsignedCodeExecution(processName, 0);
}
pplx::task<void> DebugConnection::EnableUnsignedCodeExecution(int pid)
{
return this->EnableUnsignedCodeExecution(std::nullopt, pid);
}
pplx::task<void> DebugConnection::EnableUnsignedCodeExecution(std::optional<std::string> processName, int pid)
{
return pplx::create_task([=] {
std::string name = processName.has_value() ? *processName : "this app";
std::string localizedFailure = "JIT could not be enabled for " + name + ".";
try
{
std::string attachCommand;
if (processName.has_value())
{
std::string encodedName(bin2hex((const unsigned char*)processName->c_str(), processName->length()));
attachCommand = "vAttachOrWait;" + encodedName;
}
else
{
// Convert to Big-endian
int rawPID = htonl((int32_t)pid);
std::string encodedName(bin2hex((const unsigned char*)&rawPID, 4));
attachCommand = "vAttach;" + encodedName;
}
this->SendCommand(attachCommand, {});
std::string detachCommand = "D";
this->SendCommand(detachCommand, {});
}
catch (ConnectionError& connectionError)
{
auto userInfo = connectionError.userInfo();
userInfo[AppNameErrorKey] = name;
userInfo[DeviceNameErrorKey] = this->device()->name();
userInfo[NSLocalizedFailureErrorKey] = localizedFailure;
auto error = ConnectionError((ConnectionErrorCode)connectionError.code(), userInfo);
throw error;
}
catch (ServerError& serverError)
{
auto userInfo = serverError.userInfo();
userInfo[AppNameErrorKey] = name;
userInfo[DeviceNameErrorKey] = this->device()->name();
userInfo[NSLocalizedFailureErrorKey] = localizedFailure;
auto error = ServerError((ServerErrorCode)serverError.code(), userInfo);
throw error;
}
});
}
void DebugConnection::SendCommand(std::string command, std::vector<std::string> arguments)
{
int argc = (int)arguments.size();
char** argv = new char* [argc + 1];
for (int i = 0; i < argc; i++)
{
std::string argument = arguments[i];
argv[i] = (char*)argument.c_str();
}
argv[argc] = NULL;
debugserver_command_t debugCommand = NULL;
debugserver_command_new(command.c_str(), argc, argv, &debugCommand);
delete[] argv;
char* response = NULL;
size_t responseSize = 0;
debugserver_error_t debugServerError = debugserver_client_send_command(this->client(), debugCommand, &response, &responseSize);
debugserver_command_free(debugCommand);
if (debugServerError != DEBUGSERVER_E_SUCCESS)
{
auto error = ConnectionError::errorForDebugServerError(debugServerError, this->device());
if (error.has_value())
{
throw* error;
}
}
std::optional<std::string> rawResponse = (response != NULL) ? std::make_optional(response) : std::nullopt;
this->ProcessResponse(rawResponse);
}
void DebugConnection::ProcessResponse(std::optional<std::string> rawResponse)
{
if (!rawResponse.has_value())
{
throw ServerError(ServerErrorCode::RequestedAppNotRunning);
}
if (rawResponse->length() == 0 || rawResponse->compare("OK") == 0)
{
return;
}
char type = (*rawResponse)[0];
std::string response(rawResponse->begin() + 1, rawResponse->end());
switch (type)
{
case 'O':
{
// stdout/stderr
char* decodedResponse = NULL;
debugserver_decode_string(response.c_str(), response.size(), &decodedResponse);
odslog("Response: " << decodedResponse);
if (decodedResponse)
{
free(decodedResponse);
}
break;
}
case 'T':
{
// Thread Information
odslog("Thread stopped. Details: " << response);
std::istringstream stream(response.c_str());
std::string mainThread;
for (int i = 0; i < 2; i++)
{
if (!std::getline(stream, mainThread, ';'))
{
std::map<std::string, std::any> userInfo = SOURCE_USERINFO;
userInfo[NSLocalizedFailureReasonErrorKey] = response;
throw ConnectionError(ConnectionErrorCode::Unknown, userInfo);
}
}
// Parse thread state to determine if app is running.
std::istringstream threadStream(mainThread.c_str());
std::string threadState;
if (!std::getline(threadStream, threadState, ':'))
{
std::map<std::string, std::any> userInfo = SOURCE_USERINFO;
userInfo[NSLocalizedFailureReasonErrorKey] = response;
throw ConnectionError(ConnectionErrorCode::Unknown, userInfo);
}
if (!std::getline(threadStream, threadState, ';'))
{
std::map<std::string, std::any> userInfo = SOURCE_USERINFO;
userInfo[NSLocalizedFailureReasonErrorKey] = response;
throw ConnectionError(ConnectionErrorCode::Unknown, userInfo);
}
// If main thread state == 0, app is not running.
// Must be unsigned long long to avoid potential overflow.
if (std::stoull(threadState, NULL, 16) == 0)
{
throw ServerError(ServerErrorCode::RequestedAppNotRunning);
}
break;
}
case 'E':
{
// Error
std::istringstream stream(response.c_str());
std::string errorCode;
if (!std::getline(stream, errorCode, ';'))
{
std::map<std::string, std::any> userInfo = SOURCE_USERINFO;
userInfo[NSLocalizedFailureReasonErrorKey] = response;
throw ConnectionError(ConnectionErrorCode::Unknown, userInfo);
}
switch (std::stoi(errorCode))
{
case 96: throw ServerError(ServerErrorCode::RequestedAppNotRunning);
default:
{
std::map<std::string, std::any> userInfo = SOURCE_USERINFO;
userInfo[NSLocalizedFailureReasonErrorKey] = response;
throw ConnectionError(ConnectionErrorCode::Unknown, userInfo);
}
};
break;
}
case 'W':
{
// Warning
odslog("WARNING: " << response);
break;
}
}
}
std::shared_ptr<Device> DebugConnection::device() const
{
return _device;
}
debugserver_client_t DebugConnection::client() const
{
return _client;
}