-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
59 lines (50 loc) · 1.58 KB
/
app.js
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
if (process.env.IDSPY_UPGRADE && process.getuid() !== 0) {
let args = process.env.DEBUG ? process.argv.concat(process.env.DEBUG): process.argv;
require('kexec')('pkexec', args);
}
if (process.argv[2]) {
process.env.DEBUG = process.argv[2];
}
const net = require('net');
const debug = require('debug')('idspy:app');
const readline = require('readline');
const UsbmuxdInterceptor = require('./interceptor.js');
let server = net.createServer(socket => {
debug('new socket');
let realSocket = new net.Socket();
realSocket.connect('/var/run/usbmuxx', () => debug('realSocket connected'));
let interceptor = new UsbmuxdInterceptor(data => socket.write(data), data => realSocket.write(data));
realSocket.on('data', data => {
debug('interceptToClient data len', data.length);
interceptor.interceptToClient(data);
});
realSocket.on('close', () => {
debug('realSocket closed');
socket.destroy()
});
socket.on('data', data => {
debug('interceptToDaemon data len', data.length);
interceptor.interceptToDaemon(data);
});
socket.on('close', () => {
debug('socket closed');
realSocket.destroy();
})
});
server.listen({
path: '/var/run/usbmuxd',
readableAll: true,
writableAll: true
}, () => debug('listening'));
function cleanup () {
server.close(() => {
debug('server closed');
process.exit();
});
}
process.on('SIGINT', cleanup);
readline.createInterface(process.stdin, process.stdout).on('line', line => {
if (line === 'stop') {
cleanup();
}
});