forked from libimobiledevice-win32/ios-webkit-debug-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ios_webkit_debug_proxy_main.c
404 lines (373 loc) · 11.6 KB
/
ios_webkit_debug_proxy_main.c
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
404
// Google BSD license https://developers.google.com/google-bsd-license
// Copyright 2012 Google Inc. wrightt@google.com
//
// This "main" connects the debugger to our socket management backend.
//
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef HAVE_REGEX_H
#include <pcre.h>
#include <pcreposix.h>
#else
#include <regex.h>
#endif
#ifdef WIN32
#include <winsock2.h>
#endif
#include <openssl/opensslv.h>
#include "device_listener.h"
#include "hash_table.h"
#include "ios_webkit_debug_proxy.h"
#include "port_config.h"
#include "socket_manager.h"
#include "webinspector.h"
#include "websocket.h"
#ifdef _MSC_VER
#include "asprintf.h"
#endif
struct iwdpm_struct {
char *config;
char *frontend;
char *sim_wi_socket_addr;
bool is_debug;
pc_t pc;
sm_t sm;
iwdp_t iwdp;
};
typedef struct iwdpm_struct *iwdpm_t;
iwdpm_t iwdpm_new();
void iwdpm_free(iwdpm_t self);
int iwdpm_configure(iwdpm_t self, int argc, char **argv);
void iwdpm_create_bridge(iwdpm_t self);
static int quit_flag = 0;
static void on_signal(int sig) {
quit_flag++;
}
int main(int argc, char** argv) {
signal(SIGINT, on_signal);
signal(SIGTERM, on_signal);
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
#ifdef WIN32
WSADATA wsa_data;
int res = WSAStartup(MAKEWORD(2,2), &wsa_data);
if (res) {
fprintf(stderr, "WSAStartup failed with error: %d\n", res);
exit(1);
}
#endif
iwdpm_t self = iwdpm_new();
int ret = iwdpm_configure(self, argc, argv);
if (ret) {
exit(ret > 0 ? ret : 0);
return ret;
}
iwdpm_create_bridge(self);
iwdp_t iwdp = self->iwdp;
if (iwdp->start(iwdp)) {
return -1;// TODO cleanup
}
sm_t sm = self->sm;
while (!quit_flag) {
if (sm->select(sm, 2) < 0) {
ret = -1;
break;
}
}
sm->cleanup(sm);
iwdpm_free(self);
#ifdef WIN32
WSACleanup();
#endif
return ret;
}
//
// Connect ios_webkit_debug_proxy to socket_selector/etc:
//
int iwdpm_subscribe(iwdp_t iwdp) {
return dl_connect(-1);
}
int iwdpm_attach(iwdp_t iwdp, const char *device_id, char **to_device_id,
char **to_device_name, int *to_device_os_version, void **to_ssl_session) {
return wi_connect(device_id, to_device_id, to_device_name,
to_device_os_version, to_ssl_session, -1);
}
iwdp_status iwdpm_select_port(iwdp_t iwdp, const char *device_id,
int *to_port, int *to_min_port, int *to_max_port) {
iwdpm_t self = (iwdpm_t)iwdp->state;
int ret = 0;
// reparse every time, in case the file has changed
int is_file = 0;
if (!self->pc) {
self->pc = pc_new();
if (pc_add_line(self->pc, self->config, strlen(self->config))) {
pc_clear(self->pc);
pc_add_file(self->pc, self->config);
is_file = 1;
}
}
ret = pc_select_port(self->pc, device_id, to_port, to_min_port,to_max_port);
if (is_file) {
pc_free(self->pc);
self->pc = NULL;
}
return (ret ? IWDP_ERROR : IWDP_SUCCESS);
}
int iwdpm_listen(iwdp_t iwdp, int port) {
return sm_listen(port);
}
int iwdpm_connect(iwdp_t iwdp, const char *socket_addr) {
return sm_connect(socket_addr);
}
iwdp_status iwdpm_send(iwdp_t iwdp, int fd, const char *data, size_t length) {
sm_t sm = ((iwdpm_t)iwdp->state)->sm;
return sm->send(sm, fd, data, length, NULL);
}
iwdp_status iwdpm_add_fd(iwdp_t iwdp, int fd, void *ssl_session, void *value,
bool is_server) {
sm_t sm = ((iwdpm_t)iwdp->state)->sm;
return sm->add_fd(sm, fd, ssl_session, value, is_server);
}
iwdp_status iwdpm_remove_fd(iwdp_t iwdp, int fd) {
sm_t sm = ((iwdpm_t)iwdp->state)->sm;
return sm->remove_fd(sm, fd);
}
sm_status iwdpm_on_accept(sm_t sm, int s_fd, void *s_value,
int fd, void **to_value) {
iwdp_t iwdp = ((iwdpm_t)sm->state)->iwdp;
return iwdp->on_accept(iwdp, s_fd, s_value, fd, to_value);
}
sm_status iwdpm_on_sent(sm_t sm, int fd, void *value,
const char *buf, ssize_t length) {
return SM_SUCCESS;
}
sm_status iwdpm_on_recv(sm_t sm, int fd, void *value,
const char *buf, ssize_t length) {
iwdp_t iwdp = ((iwdpm_t)sm->state)->iwdp;
return iwdp->on_recv(iwdp, fd, value, buf, length);
}
sm_status iwdpm_on_close(sm_t sm, int fd, void *value, bool is_server) {
iwdp_t iwdp = ((iwdpm_t)sm->state)->iwdp;
return iwdp->on_close(iwdp, fd, value, is_server);
}
void iwdpm_create_bridge(iwdpm_t self) {
sm_t sm = sm_new(4096);
iwdp_t iwdp = iwdp_new(self->frontend, self->sim_wi_socket_addr);
if (!sm || !iwdp) {
sm_free(sm);
return;
}
self->sm = sm;
self->iwdp = iwdp;
iwdp->subscribe = iwdpm_subscribe;
iwdp->attach = iwdpm_attach;
iwdp->select_port = iwdpm_select_port;
iwdp->listen = iwdpm_listen;
iwdp->connect = iwdpm_connect;
iwdp->send = iwdpm_send;
iwdp->add_fd = iwdpm_add_fd;
iwdp->remove_fd = iwdpm_remove_fd;
iwdp->state = self;
iwdp->is_debug = &self->is_debug;
sm->on_accept = iwdpm_on_accept;
sm->on_sent = iwdpm_on_sent;
sm->on_recv = iwdpm_on_recv;
sm->on_close = iwdpm_on_close;
sm->state = self;
sm->is_debug = &self->is_debug;
}
void iwdpm_free(iwdpm_t self) {
if (self) {
pc_free(self->pc);
iwdp_free(self->iwdp);
sm_free(self->sm);
free(self->config);
free(self->frontend);
free(self->sim_wi_socket_addr);
memset(self, 0, sizeof(struct iwdpm_struct));
free(self);
}
}
iwdpm_t iwdpm_new() {
iwdpm_t self = malloc(sizeof(struct iwdpm_struct));
if (!self) {
return NULL;
}
memset(self, 0, sizeof(struct iwdpm_struct));
return self;
}
int iwdpm_configure(iwdpm_t self, int argc, char **argv) {
static struct option longopts[] = {
{"udid", 1, NULL, 'u'},
{"config", 1, NULL, 'c'},
{"frontend", 1, NULL, 'f'},
{"no-frontend", 0, NULL, 'F'},
{"simulator-webinspector", 1, NULL, 's'},
{"debug", 0, NULL, 'd'},
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'V'},
{NULL, 0, NULL, 0}
};
const char *DEFAULT_CONFIG = "null:9221,:9222-9322";
const char *DEFAULT_FRONTEND =
"http://chrome-devtools-frontend.appspot.com/static/27.0.1453.93/devtools.html";
// The port 27753 is from `locate com.apple.webinspectord.plist`
const char *DEFAULT_SIM_WI_SOCKET_ADDR = "localhost:27753";
self->config = strdup(DEFAULT_CONFIG);
self->frontend = strdup(DEFAULT_FRONTEND);
self->sim_wi_socket_addr = strdup(DEFAULT_SIM_WI_SOCKET_ADDR);
int ret = 0;
while (!ret) {
int c = getopt_long(argc, argv, "hVu:c:f:Fs:d", longopts, (int *)0);
if (c == -1) {
break;
}
switch (c) {
case 'h':
ret = -1;
break;
case 'V':
printf(
"%s\n"
"Built with libimobiledevice v%s, libplist v%s, libusbmuxd v%s, %s\n",
PACKAGE_STRING, LIBIMOBILEDEVICE_VERSION, LIBPLIST_VERSION,
LIBUSBMUXD_VERSION, OPENSSL_VERSION_TEXT);
ret = -2;
break;
case 'u':
{
regex_t *re = malloc(sizeof(regex_t));
regcomp(re, "^[a-fA-F0-9-]{25,}(:[0-9]+(-[0-9]+)?)?$", REG_EXTENDED);
size_t ngroups = re->re_nsub + 1;
regmatch_t *groups = calloc(ngroups, sizeof(regmatch_t));
bool is_match = !regexec(re, optarg, ngroups, groups, 0);
bool has_port = (is_match && groups[1].rm_so >= 0);
free(groups);
regfree(re);
free(self->config);
self->config = NULL;
if (!is_match) {
ret = 2;
} else if (!has_port) {
if (asprintf(&self->config, "%s%s", optarg, ":9222") < 0) {
ret = 2; // asprintf failed
}
} else {
self->config = strdup(optarg);
}
}
break;
case 'c':
free(self->config);
self->config = strdup(optarg);
break;
case 's':
free(self->sim_wi_socket_addr);
self->sim_wi_socket_addr = strdup(optarg);
break;
case 'f':
case 'F':
free(self->frontend);
self->frontend = (c == 'f' ? strdup(optarg) : NULL);
break;
case 'd':
self->is_debug = true;
break;
default:
ret = 2;
break;
}
}
if (!ret && ((argc - optind) > 0)) {
ret = 2;
}
if (ret && ret != -2) {
char *name = strrchr(argv[0], '/');
printf(
"Usage: %s [OPTIONS]\n"
"iOS WebKit Remote Debugging Protocol Proxy v%s.\n"
"\n"
"By default, the proxy will list all attached iOS devices on:\n"
" http://localhost:9221\n"
"and assign each device an incremented port number, e.g.:\n"
" http://localhost:9222\n"
"which lists the device's pages and provides inspector access.\n"
"\n"
"Your attached iOS device(s) must have the inspector enabled via:\n"
" Settings > Safari > Advanced > Web Inspector = ON\n"
"and have one or more open browser pages.\n"
"\n"
"To view the DevTools UI, either use the above links (which use the"
" \"frontend\"\nURL noted below) or use Chrome's built-in inspector,"
" e.g.:\n"
" chrome-devtools://devtools/bundled/inspector.html?ws=localhost:"
"9222/devtools/page/1"
"\n\n"
"OPTIONS:\n"
"\n"
" -u UDID[:minPort-[maxPort]]\tTarget a specific device by its"
" digital ID.\n"
" minPort defaults to 9222. maxPort defaults to minPort.\n"
" This is shorthand for the following \"-c\" option.\n"
"\n"
" -c, --config CSV\tUDID-to-port(s) configuration.\n"
" Defaults to:\n"
" %s\n"
" which lists devices (\"null:\") on port 9221 and assigns\n"
" all other devices (\":\") to the next unused port in the\n"
" 9222-9322 range, in the (somewhat random) order that the\n"
" devices are detected.\n"
" The value can be the path to a file in the above format.\n"
"\n"
" -f, --frontend URL\tDevTools frontend UI path or URL.\n"
" Defaults to:\n"
" %s\n"
" Examples:\n"
" * Use Chrome's built-in inspector:\n"
" chrome-devtools://devtools/bundled/inspector.html\n"
" * Use a local WebKit checkout:\n"
" /usr/local/WebCore/inspector/front-end/inspector.html\n"
" * Use an online copy of the inspector pages:\n"
" http://chrome-devtools-frontend.appspot.com/static/"
"33.0.1722.0"
"/devtools.html\n"
" where other online versions include:\n"
" 18.0.1025.74\n"
" 25.0.1364.169\n"
" 28.0.1501.0\n"
" 30.0.1599.92\n"
" 31.0.1651.0\n"
" 32.0.1689.3\n"
"\n"
" -F, --no-frontend\tDisable the DevTools frontend.\n"
"\n"
" -s, --simulator-webinspector\tSimulator web inspector socket\n"
" address. Provided value value needs to be in format\n"
" HOSTNAME:PORT or UNIX:PATH\n"
" Defaults to:\n"
" %s\n"
" Examples:\n"
" * TCP socket:\n"
" 192.168.0.20:27753\n"
" * Unix domain socket:\n"
" unix:/private/tmp/com.apple.launchd.2j5k1TMh6i/"
"com.apple.webinspectord_sim.socket\n"
"\n"
" -d, --debug\t\tEnable debug output.\n"
" -h, --help\t\tPrint this usage information.\n"
" -V, --version\t\tPrint version information and exit.\n"
"\n", (name ? name + 1 : argv[0]), PACKAGE_VERSION, DEFAULT_CONFIG,
DEFAULT_FRONTEND, DEFAULT_SIM_WI_SOCKET_ADDR);
}
return ret;
}