forked from MrAntares/roBrowserLegacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
317 lines (267 loc) · 9.45 KB
/
service-worker.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
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
// Set up ping interval in service worker
const PING_INTERVAL = 10000; // 10 seconds
class ServiceWorkerManager {
constructor() {
this.connections = new Map();
this.connectionCounter = 0;
self.addEventListener('install', this.handleInstall.bind(this));
self.addEventListener('activate', this.handleActivate.bind(this));
self.addEventListener('message', this.handleMessage.bind(this));
}
handleInstall(event) {
self.skipWaiting();
}
handleActivate(event) {
event.waitUntil(clients.claim());
}
handleMessage(event) {
const data = event.data;
switch (data.type) {
case 'connect':
const connection = new Connection(this, data.channelName, data.config);
// Initialize encryption if this is a zone server connection
if (data.config.isZone) {
connection.isZone = true;
connection.keys = new Uint32Array(data.config.encryptionKeys)
}
this.connections.set(connection.id, connection);
break;
case 'send':
const conn = this.connections.get(data.clientId);
if (conn) {
conn.send(conn.maybeEncryptPacket(data.data));
}
break;
case 'close':
const connToClose = this.connections.get(data.clientId);
if (connToClose) {
connToClose.cleanup();
}
break;
case 'ping':
const connToPing = this.connections.get(data.clientId);
if (connToPing) {
connToPing.pingPacketType = data.packetType;
connToPing.packetHeader = data.packetHeader;
connToPing.startPingInterval();
}
break;
case 'skipWaiting':
self.skipWaiting();
break;
}
}
getNextConnectionId() {
return ++this.connectionCounter;
}
removeConnection(id) {
this.connections.delete(id);
}
}
class Connection {
constructor(manager, channelName, config) {
this.manager = manager;
this.id = manager.getNextConnectionId();
this.channel = new BroadcastChannel(channelName);
this.socket = null;
this.pingInterval = null;
this.startTick = null;
this.pingState = {
returned: true,
pingTime: 0
};
this.isZone = false;
this.keys = null;
this.init(config);
// Add listener for when channel is closed/disconnected
this.channel.onmessageerror = () => {
this.cleanup();
};
}
init(config) {
const url = this.buildUrl(config.host, config.port, config.proxy);
this.socket = new WebSocket(url);
this.socket.binaryType = 'arraybuffer';
this.socket.onopen = () => {
this.channel.postMessage({
type: 'connected',
clientId: this.id
});
};
this.socket.onerror = (error) => {
this.channel.postMessage({
type: 'connection_error'
});
};
this.socket.onmessage = (event) => {
try {
this.channel.postMessage({
type: 'message',
data: event.data
});
} catch (e) {
if (e instanceof InvalidStateError) {
try {
this.channel.close();
} catch (e) {
// May be already closed
}
} else {
throw e;
}
}
};
this.socket.onclose = () => {
this.cleanup();
};
}
buildUrl(host, port, proxy) {
let url = 'ws://' + host + ':' + port + '/';
if (proxy) {
url = proxy;
if (!url.match(/\/$/)) {
url += '/';
}
url += host + ':' + port;
}
return url;
}
send(data) {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.socket.send(data);
}
}
startPingInterval() {
// Clear any existing ping interval first
if (this.pingInterval) {
clearInterval(this.pingInterval);
this.pingInterval = null;
}
this.startTick = Date.now();
// Send initial ping immediately
this.sendPing();
this.pingInterval = setInterval(() => {
this.sendPing();
}, PING_INTERVAL);
}
sendPing() {
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
this.cleanup();
return;
}
const clientTime = Date.now() - this.startTick;
if (!this.pingState.returned && this.pingState.pingTime) {
// TODO: Handle as in MapEngine.js
//console.warn('[SendPing] Server did not answer previous ping');
}
this.pingState.pingTime = clientTime;
this.pingState.returned = false;
let buffer;
switch (this.pingPacketType) {
case 'PACKET_CA_CONNECT_INFO_CHANGED':
buffer = this.buildConnectInfoChangedPacket();
break;
case 'PACKET_CZ_HBT':
buffer = this.buildHBTPacket();
break;
case 'PACKET_CZ_REQUEST_TIME':
buffer = this.buildRequestTimePacket(clientTime);
break;
case 'PACKET_CZ_REQUEST_TIME2':
buffer = this.buildRequestTime2Packet(clientTime);
break;
case 'PACKET_CZ_PING':
buffer = this.buildPingPacket(clientTime);
break;
default:
return;
}
try {
// Health check to ensure channel is still open (page refreshed, etc)
this.channel.postMessage({ type: 'health_check' });
const encryptedPacket = this.maybeEncryptPacket(buffer);
this.socket.send(encryptedPacket);
} catch (error) {
this.cleanup();
}
}
buildConnectInfoChangedPacket() {
const pkt = new DataView(new ArrayBuffer(24));
pkt.setInt16(0, this.packetHeader, true); // PACKET.CA.CONNECT_INFO_CHANGED
pkt.setInt32(2, Date.now(), true); // TODO: Get login ID
return pkt.buffer;
}
buildHBTPacket() {
const pkt = new DataView(new ArrayBuffer(2));
pkt.setInt16(0, this.packetHeader, true); // PACKET.CZ.HBT
return pkt.buffer;
}
buildRequestTimePacket(clientTime) {
const pkt = new DataView(new ArrayBuffer(6));
pkt.setInt16(0, this.packetHeader, true); // PACKET.CZ.REQUEST_TIME
pkt.setInt32(2, clientTime, true);
return pkt.buffer;
}
buildRequestTime2Packet(clientTime) {
const pkt = new DataView(new ArrayBuffer(6));
pkt.setInt16(0, this.packetHeader, true); // PACKET.CZ.REQUEST_TIME2
pkt.setInt32(2, clientTime, true);
return pkt.buffer;
}
buildPingPacket(clientTime) {
const pkt = new DataView(new ArrayBuffer(6));
pkt.setInt16(0, this.packetHeader, true); // PACKET.CZ.PING
pkt.setInt32(2, clientTime, true); // TODO: Get AID
return pkt.buffer;
}
maybeEncryptPacket(buffer) {
if (!this.isZone) return buffer;
const view = new DataView(buffer);
const cmd = view.getInt16(0, true);
// console.log('[ServiceWorker] Encrypting packet for connection', this.id, {
// originalCmd: '0x' + cmd.toString(16),
// beforeKeys: {
// key0: '0x' + (this.keys[0] >>> 0).toString(16),
// key1: '0x' + (this.keys[1] >>> 0).toString(16),
// key2: '0x' + (this.keys[2] >>> 0).toString(16)
// }
// });
// Update encryption key using same algorithm as PacketCrypt
this.keys[0] = (Math.imul(this.keys[0], this.keys[1]) + this.keys[2]) >>> 0;
const encryptedCmd = cmd ^ ((this.keys[0] >>> 16) & 0x7FFF);
view.setInt16(0, encryptedCmd, true);
// console.log('[ServiceWorker] Packet encrypted for connection', this.id, {
// afterKeys: {
// key0: '0x' + (this.keys[0] >>> 0).toString(16),
// key1: '0x' + (this.keys[1] >>> 0).toString(16),
// key2: '0x' + (this.keys[2] >>> 0).toString(16)
// },
// encryptedCmd: '0x' + encryptedCmd.toString(16)
// });
return buffer;
}
cleanup() {
if (this.pingInterval) {
clearInterval(this.pingInterval);
this.pingInterval = null;
}
try {
this.channel.postMessage({
type: 'close'
});
} catch (e) {
// Channel might already be closed
}
try {
this.channel.close();
} catch (e) {
// Channel might already be closed
}
// Close the WebSocket if it's still open
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.socket.close();
}
this.manager.removeConnection(this.id);
}
}
const manager = new ServiceWorkerManager();