-
Notifications
You must be signed in to change notification settings - Fork 149
/
SocketService.js
272 lines (199 loc) · 8.12 KB
/
SocketService.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
import StorageService from './StorageService'
import getRandomValues from 'get-random-values';
import createHash from 'create-hash';
import WebSocket from 'isomorphic-ws';
import device from "../util/Device";
const suffix = '/socket.io/?EIO=3&transport=websocket';
const sha256 = data => createHash('sha256').update(data).digest('hex');
const random = () => {
const array = new Uint8Array(24);
getRandomValues(array);
return array.join('');
};
export default class SocketService {
constructor(_plugin, _timeout){
this.plugin = _plugin;
this.timeout = _timeout;
this.uuid = null;
this.socket = null;
this.connected = false;
this.paired = false;
this.openRequests = [];
this.pairingPromise = null;
this.eventHandlers = {};
this.appkey = StorageService.getAppKey();
if(!this.appkey) this.appkey = 'appkey:'+random();
}
addEventHandler(handler, key){
if(!key) key = 'app';
this.eventHandlers[key] = handler;
}
removeEventHandler(key){
if(!key) key = 'app';
delete this.eventHandlers[key];
}
link(allowHttp = true, _uuid = null, socketHost = null){
this.uuid = _uuid;
return new Promise(async resolve => {
const setupSocket = () => {
this.socket.onmessage = msg => {
// Handshaking/Upgrading
if(msg.data.indexOf('42/scatter') === -1) return false;
// Real message
const [type, data] = JSON.parse(msg.data.replace('42/scatter,', ''));
if(type === 'pong') return;
if(type === 'ping') return this.socket.send(`42/scatter,["pong"]`);
switch(type){
case 'paired': return msg_paired(data);
case 'rekey': return msg_rekey();
case 'api': return msg_api(data);
case 'event': return event_api(data);
}
};
const msg_paired = result => {
this.paired = result;
if(this.paired) {
const savedKey = StorageService.getAppKey();
const hashed = this.appkey.indexOf('appkey:') > -1 ? sha256(this.appkey) : this.appkey;
if (!savedKey || savedKey !== hashed) {
StorageService.setAppKey(hashed);
this.appkey = StorageService.getAppKey();
}
}
this.pairingPromise.resolve(result);
};
const msg_rekey = () => {
this.appkey = 'appkey:'+random();
this.send('rekeyed', {data:{ appkey:this.appkey, origin:this.getOrigin() }, plugin:this.plugin});
};
const msg_api = response => {
// If not already parsed into JSON
try { response = JSON.parse(response); } catch(e){}
const openRequest = this.openRequests.find(x => x.id === response.id);
if(!openRequest) return;
this.openRequests = this.openRequests.filter(x => x.id !== response.id);
const isErrorResponse = typeof response.result === 'object'
&& response.result !== null
&& response.result.hasOwnProperty('isError');
if(isErrorResponse) openRequest.reject(response.result);
else openRequest.resolve(response.result);
};
const event_api = ({event, payload}) => {
if(Object.keys(this.eventHandlers).length) Object.keys(this.eventHandlers).map(key => {
this.eventHandlers[key](event, payload);
});
};
};
const getHostname = (port, ssl) => {
if(socketHost) return socketHost;
return ssl ? `local.get-scatter.com:${port}` : `127.0.0.1:${port}`;
}
const ports = await new Promise(async (portResolver) => {
if(socketHost) return portResolver([50006]);
const checkPort = (host, cb) => fetch(host).then(r => r.text()).then(r => cb(r === 'scatter')).catch(() => cb(false));
let startingPort = 50005;
let availablePorts = [];
const preparePorts = () => (!availablePorts.length ? /* BACKWARDS COMPAT */ [50006, 50005] : availablePorts).filter(x => {
if(allowHttp) return true;
return !(x % 2);
}).sort((a,b) => {
// Always try to use SSL first.
return !(b % 2) ? 1 : !(a % 2) ? -1 : 0;
});
let returned = false;
const resolveAndPushPort = (port = null) => {
if(returned) return;
returned = true;
if(port !== null) availablePorts.push(port);
portResolver(preparePorts());
}
await Promise.all([...new Array(5).keys()].map(async i => {
if(returned) return;
const _port = startingPort + i * 1500;
await checkPort(`https://` + getHostname(_port + 1, true), x => x ? resolveAndPushPort(_port + 1) : null);
if(allowHttp) await checkPort(`http://` + getHostname(_port, false), x => x ? resolveAndPushPort(_port) : null);
return true;
}));
resolveAndPushPort();
});
const trySocket = port => new Promise(socketResolver => {
const ssl = !(port % 2);
const hostname = getHostname(port, ssl);
const protocol = ssl ? 'wss://' : 'ws://';
const s = new WebSocket(`${protocol}${hostname}${suffix}`);
s.onerror = () => socketResolver(false);
s.onopen = () => socketResolver(s);
});
let connected = false;
for(let i = 0; i < ports.length; i++){
if(connected) continue;
const s = await trySocket(ports[i]);
if(s){
connected = true;
this.socket = s;
this.send();
this.connected = true;
setupSocket();
this.pairingPromise = null;
this.pair(true).then(() => resolve(true));
break;
}
}
})
}
isConnected(){
return this.connected;
}
isPaired(){
return this.paired;
}
disconnect(){
if(this.socket) this.socket.close();
return true;
}
sendApiRequest(request){
return new Promise((resolve, reject) => {
if(request.type === 'identityFromPermissions' && !this.paired) return resolve(false);
this.pair().then(() => {
if(!this.paired) return reject({code:'not_paired', message:'The user did not allow this app to connect to their Scatter'});
// Request ID used for resolving promises
request.id = random();
// Set Application Key
request.appkey = this.appkey;
// Nonce used to authenticate this request
request.nonce = StorageService.getNonce() || 0;
// Next nonce used to authenticate the next request
const nextNonce = random();
request.nextNonce = sha256(nextNonce);
StorageService.setNonce(nextNonce);
if(request.hasOwnProperty('payload') && !request.payload.hasOwnProperty('origin'))
request.payload.origin = this.getOrigin();
this.openRequests.push(Object.assign(request, {resolve, reject}));
this.send('api', {data:request, plugin:this.plugin})
})
});
}
pair(passthrough = false){
return new Promise((resolve, reject) => {
this.pairingPromise = {resolve, reject};
this.send('pair', {data:{ appkey:this.appkey, origin:this.getOrigin(), passthrough }, plugin:this.plugin})
})
}
send(type = null, data = null){
if(type === null && data === null) this.socket.send('40/scatter');
else this.socket.send('42/scatter,' + JSON.stringify([type, Object.assign(data, {device, uuid:this.uuid})]));
}
getOrigin(){
return SocketService.getOriginOrPlugin(this.plugin);
}
static getOriginOrPlugin(plugin){
let origin;
if(typeof location !== 'undefined')
if(location.hasOwnProperty('hostname') && location.hostname.length && location.hostname !== 'localhost')
origin = location.hostname;
else origin = plugin;
else origin = plugin;
if(origin.substr(0, 4) === 'www.') origin = origin.replace('www.','');
return origin;
}
}