-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptor.js
161 lines (125 loc) · 4.24 KB
/
interceptor.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
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
const EventEmitter = require('events');
const plist = require('plist');
const USBMUXD_HEADER_LENGTH = 16;
const STATE_WAITING_FOR_HEADER = 0;
const STATE_WAITING_FOR_MESSAGE = 1;
const STATE_STREAMING = 2;
class DynamicBuffer {
constructor () {
this.buffers = [];
this.available = 0;
}
push (buffer) {
this.buffers.push(buffer);
this.available += buffer.length;
}
readBytes (length) {
if (this.available < length) {
return null;
}
let parts = [];
let currentLen = 0;
while(currentLen < length) {
let buffer = this.buffers[0];
let lengthNeeded = length - currentLen;
if (buffer.length > lengthNeeded) {
parts.push(buffer.slice(0, lengthNeeded));
this.buffers[0] = buffer.slice(lengthNeeded);
currentLen += lengthNeeded;
} else {
parts.push(buffer);
currentLen += buffer.length;
this.buffers.shift();
}
}
this.available -= length;
return Buffer.concat(parts, length);
}
}
class StreamInterceptor extends EventEmitter {
constructor (send) {
super();
this.send = send;
this.stream = new DynamicBuffer();
this.state = STATE_WAITING_FOR_HEADER;
}
push (data) {
this.stream.push(data);
this.process();
}
process () {
switch (this.state) {
case STATE_WAITING_FOR_HEADER:
let headerBytes = this.stream.readBytes(USBMUXD_HEADER_LENGTH);
if (headerBytes) {
this.send(headerBytes);
this.header = this.parseHeader(headerBytes);
this.state = STATE_WAITING_FOR_MESSAGE;
this.emit('header', this.header);
this.process();
}
break;
case STATE_WAITING_FOR_MESSAGE:
let messageBytes = this.stream.readBytes(this.header.length - USBMUXD_HEADER_LENGTH);
if (messageBytes) {
this.send(messageBytes);
let message = this.parseMessage(messageBytes);
if (message.MessageType === 'Connect') {
this.state = STATE_STREAMING;
console.log('client started streaming');
this.emit('startStreaming');
} else {
this.state = STATE_WAITING_FOR_HEADER;
}
this.emit('message', message);
this.process();
}
break;
case STATE_STREAMING:
this.send(this.stream.readBytes(this.stream.available));
break;
}
}
parseHeader (buf) {
return {
length: buf.readUInt32LE(0),
reserved: buf.readUInt32LE(4),
type: buf.readUInt32LE(8),
tag: buf.readUInt32LE(12)
}
}
parseMessage (buf) {
return plist.parse(buf.toString());
}
}
class UsbmuxdInterceptor {
constructor (sendToClient, sendToDaemon) {
this.toClientStream = new StreamInterceptor(sendToClient);
this.toDaemonStream = new StreamInterceptor(sendToDaemon);
this.toDaemonStream.on('startStreaming', () => {
this.toClientStream.once('message', () => {
this.toClientStream.state = STATE_STREAMING;
console.log('toClient started streaming');
});
});
this.toClientStream.on('header', header => {
console.log('toClient header', header);
});
this.toClientStream.on('message', message => {
console.log('toClient message', message);
});
this.toDaemonStream.on('header', header => {
console.log('toDaemon header', header);
});
this.toDaemonStream.on('message', message => {
console.log('toDaemon message', message);
})
}
interceptToClient (data) {
this.toClientStream.push(data);
}
interceptToDaemon (data) {
this.toDaemonStream.push(data);
}
}
module.exports = UsbmuxdInterceptor;