@lockerpm/desktop-service 2.4.0 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/services/api.service.js +14 -13
- package/lib/cjs/services/socket.service.js +126 -87
- package/lib/cjs/types/services/api.service.d.ts +1 -1
- package/lib/cjs/types/services/api.service.d.ts.map +1 -1
- package/lib/cjs/types/services/socket.service.d.ts.map +1 -1
- package/lib/esm/services/api.service.js +14 -13
- package/lib/esm/services/socket.service.js +126 -87
- package/lib/esm/types/services/api.service.d.ts +1 -1
- package/lib/esm/types/services/api.service.d.ts.map +1 -1
- package/lib/esm/types/services/socket.service.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -34,12 +34,14 @@ class ApiService {
|
|
|
34
34
|
this.sessionToken = '';
|
|
35
35
|
}
|
|
36
36
|
// -------------------------------- NO AUTH --------------------------------
|
|
37
|
-
|
|
37
|
+
// TODO: if service is only built by CyStack, then this base API must be fixed
|
|
38
|
+
async getReleases(os) {
|
|
38
39
|
try {
|
|
39
|
-
const url = `${this.baseUrl}/cystack_platform/pm/
|
|
40
|
+
const url = `${this.baseUrl}/cystack_platform/pm/releases`;
|
|
40
41
|
const res = await axios_1.default.get(url, {
|
|
41
42
|
params: {
|
|
42
|
-
|
|
43
|
+
client_id: 'desktop',
|
|
44
|
+
os,
|
|
43
45
|
},
|
|
44
46
|
headers: this.#getHeaders(),
|
|
45
47
|
});
|
|
@@ -48,32 +50,31 @@ class ApiService {
|
|
|
48
50
|
return data;
|
|
49
51
|
}
|
|
50
52
|
catch (error) {
|
|
51
|
-
if (error.response?.status === 404) {
|
|
52
|
-
throw new errors_1.ServiceError('1002', error);
|
|
53
|
-
}
|
|
54
53
|
throw new errors_1.ServiceError('1000', error);
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
|
-
//
|
|
58
|
-
async
|
|
56
|
+
// -------------------------------- AUTH --------------------------------
|
|
57
|
+
async getPasswordlessCredentials(email) {
|
|
58
|
+
const headers = this.token ? this.#getAuthHeaders() : this.#getHeaders();
|
|
59
59
|
try {
|
|
60
|
-
const url = `${this.baseUrl}/cystack_platform/pm/
|
|
60
|
+
const url = `${this.baseUrl}/cystack_platform/pm/passwordless/credential`;
|
|
61
61
|
const res = await axios_1.default.get(url, {
|
|
62
62
|
params: {
|
|
63
|
-
|
|
64
|
-
os,
|
|
63
|
+
email,
|
|
65
64
|
},
|
|
66
|
-
headers
|
|
65
|
+
headers,
|
|
67
66
|
});
|
|
68
67
|
const data = res.data;
|
|
69
68
|
this.#logDebug({ url, method: 'GET', data });
|
|
70
69
|
return data;
|
|
71
70
|
}
|
|
72
71
|
catch (error) {
|
|
72
|
+
if (error.response?.status === 404) {
|
|
73
|
+
throw new errors_1.ServiceError('1002', error);
|
|
74
|
+
}
|
|
73
75
|
throw new errors_1.ServiceError('1000', error);
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
|
-
// -------------------------------- AUTH --------------------------------
|
|
77
78
|
async getPreloginInfo() {
|
|
78
79
|
const headers = this.#getAuthHeaders();
|
|
79
80
|
try {
|
|
@@ -10,13 +10,20 @@ const errors_1 = require("../abstractions/errors");
|
|
|
10
10
|
const https_1 = __importDefault(require("https"));
|
|
11
11
|
const http_1 = __importDefault(require("http"));
|
|
12
12
|
const config_1 = require("../misc/config");
|
|
13
|
+
// Only loopback addresses are served so other machines on the network cannot connect.
|
|
14
|
+
// Clients connect to `localhost`, which may resolve to either address first
|
|
15
|
+
const LOOPBACK_V4 = '127.0.0.1';
|
|
16
|
+
const LOOPBACK_V6 = '::1';
|
|
17
|
+
// The machine has no IPv6 loopback, so the IPv4 listener alone serves `localhost`
|
|
18
|
+
const NO_IPV6_ERRORS = ['EADDRNOTAVAIL', 'EAFNOSUPPORT'];
|
|
13
19
|
class SocketService {
|
|
14
20
|
#logger;
|
|
15
21
|
#pairingService;
|
|
16
22
|
#userService;
|
|
17
23
|
#eventService;
|
|
18
24
|
#sslConfig = undefined;
|
|
19
|
-
|
|
25
|
+
/** Shared by every WS and WSS listener, so `clients` covers all connected sockets */
|
|
26
|
+
#wss;
|
|
20
27
|
#clients = {};
|
|
21
28
|
#serviceAlias = '';
|
|
22
29
|
#allowedOrigins;
|
|
@@ -32,6 +39,18 @@ class SocketService {
|
|
|
32
39
|
if (serviceAlias) {
|
|
33
40
|
this.#serviceAlias = serviceAlias;
|
|
34
41
|
}
|
|
42
|
+
this.#wss = new ws_1.WebSocketServer({
|
|
43
|
+
noServer: true,
|
|
44
|
+
// Reject the upgrade (HTTP 401) when the browser-provided Origin is not allowed
|
|
45
|
+
verifyClient: (info) => {
|
|
46
|
+
const allowed = this.#isOriginAllowed(info.origin);
|
|
47
|
+
if (!allowed) {
|
|
48
|
+
this.#logger.debug(`Rejected WebSocket connection from origin: ${info.origin}`);
|
|
49
|
+
}
|
|
50
|
+
return allowed;
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
this.#wss.on('connection', (ws, req) => this.#handleConnection(ws, req));
|
|
35
54
|
if (ssl) {
|
|
36
55
|
this.#sslConfig = ssl;
|
|
37
56
|
this.initSslSocket();
|
|
@@ -69,7 +88,7 @@ class SocketService {
|
|
|
69
88
|
if (envelop.secure) {
|
|
70
89
|
throw new errors_1.ServiceError('4002');
|
|
71
90
|
}
|
|
72
|
-
this.#
|
|
91
|
+
this.#wss.clients.forEach((ws) => {
|
|
73
92
|
if (ws.readyState === ws_1.WebSocket.OPEN) {
|
|
74
93
|
this.#sendMessage(ws, envelop);
|
|
75
94
|
}
|
|
@@ -124,98 +143,118 @@ class SocketService {
|
|
|
124
143
|
}
|
|
125
144
|
return this.#allowedOrigins.some((allowed) => typeof allowed === 'string' ? allowed === origin : allowed.test(origin));
|
|
126
145
|
}
|
|
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
|
-
|
|
146
|
+
#handleHttpRequest(req, res) {
|
|
147
|
+
const origin = req.headers.origin;
|
|
148
|
+
const isAllowed = this.#isOriginAllowed(origin);
|
|
149
|
+
const headers = {
|
|
150
|
+
'Access-Control-Allow-Methods': 'OPTIONS, GET',
|
|
151
|
+
'Access-Control-Max-Age': '2592000', // 30 days
|
|
152
|
+
};
|
|
153
|
+
if (isAllowed && origin) {
|
|
154
|
+
headers['Access-Control-Allow-Origin'] = origin;
|
|
155
|
+
headers['Access-Control-Allow-Credentials'] = 'true';
|
|
156
|
+
}
|
|
157
|
+
if (req.method === 'OPTIONS') {
|
|
158
|
+
res.writeHead(204, headers);
|
|
159
|
+
res.end();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (req.method === 'GET' && req.url === '/ping-locker-desktop') {
|
|
163
|
+
res.writeHead(200, { ...headers, 'Content-Type': 'application/json' });
|
|
164
|
+
res.end(JSON.stringify({
|
|
165
|
+
message: 'pong',
|
|
166
|
+
alias: this.#serviceAlias,
|
|
167
|
+
port: this.currentPort,
|
|
168
|
+
sslPort: this.currentSslPort,
|
|
169
|
+
}));
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
res.writeHead(404, { ...headers, 'Content-Type': 'text/plain' });
|
|
173
|
+
res.end('Not Found');
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** Serves the port on both loopback addresses. Resolves false if the port is taken on either one */
|
|
177
|
+
async #initSocketOnPort(port, enableSsl) {
|
|
178
|
+
const debugName = `Socket${enableSsl ? ' with SSL' : ''}`;
|
|
179
|
+
let v4Server;
|
|
180
|
+
try {
|
|
181
|
+
v4Server = await this.#listenOnLoopback(port, LOOPBACK_V4, enableSsl);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
this.#logger.debug(`${debugName} cannot listen at ${LOOPBACK_V4}:${port}: ${error}`);
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
await this.#listenOnLoopback(port, LOOPBACK_V6, enableSsl);
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
const code = error.code;
|
|
192
|
+
if (!code || !NO_IPV6_ERRORS.includes(code)) {
|
|
193
|
+
// Another process holds [::1]:port and would receive the `localhost` connections
|
|
194
|
+
this.#logger.debug(`${debugName} cannot listen at [${LOOPBACK_V6}]:${port}: ${error}`);
|
|
195
|
+
v4Server.close();
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
this.#logger.debug(`${debugName}: no IPv6 loopback, serving ${LOOPBACK_V4} only`);
|
|
199
|
+
}
|
|
200
|
+
this.#logger.debug(`${debugName} listening at loopback :${port}`);
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
#listenOnLoopback(port, host, enableSsl) {
|
|
204
|
+
return new Promise((resolve, reject) => {
|
|
205
|
+
const handler = this.#handleHttpRequest.bind(this);
|
|
160
206
|
const server = enableSsl && this.#sslConfig
|
|
161
|
-
? https_1.default.createServer(this.#sslConfig,
|
|
162
|
-
: http_1.default.createServer(
|
|
163
|
-
|
|
164
|
-
// Init socket
|
|
165
|
-
const wss = new ws_1.WebSocketServer({
|
|
166
|
-
server,
|
|
167
|
-
// Reject the upgrade (HTTP 401) when the browser-provided Origin is not allowed
|
|
168
|
-
verifyClient: (info) => {
|
|
169
|
-
const allowed = this.#isOriginAllowed(info.origin);
|
|
170
|
-
if (!allowed) {
|
|
171
|
-
this.#logger.debug(`Rejected WebSocket connection from origin: ${info.origin}`);
|
|
172
|
-
}
|
|
173
|
-
return allowed;
|
|
174
|
-
},
|
|
175
|
-
});
|
|
176
|
-
const debugName = `Socket${enableSsl ? ' with SSL' : ''}`;
|
|
177
|
-
wss.on('listening', () => {
|
|
178
|
-
this.#logger.debug(`${debugName} listening at :${port}`);
|
|
179
|
-
resolve(true);
|
|
180
|
-
});
|
|
181
|
-
wss.on('error', (err) => {
|
|
207
|
+
? https_1.default.createServer(this.#sslConfig, handler)
|
|
208
|
+
: http_1.default.createServer(handler);
|
|
209
|
+
const onListenError = (err) => {
|
|
182
210
|
server.close();
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
211
|
+
reject(err);
|
|
212
|
+
};
|
|
213
|
+
server.once('error', onListenError);
|
|
214
|
+
server.listen(port, host, () => {
|
|
215
|
+
server.off('error', onListenError);
|
|
216
|
+
server.on('error', (err) => this.#logger.error(err));
|
|
217
|
+
server.on('upgrade', (req, socket, head) => {
|
|
218
|
+
this.#wss.handleUpgrade(req, socket, head, (ws) => {
|
|
219
|
+
this.#wss.emit('connection', ws, req);
|
|
220
|
+
});
|
|
190
221
|
});
|
|
191
|
-
|
|
192
|
-
|
|
222
|
+
resolve(server);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
#handleConnection(ws, req) {
|
|
227
|
+
const isSsl = !!req.socket.encrypted;
|
|
228
|
+
const debugName = `Socket${isSsl ? ' with SSL' : ''}`;
|
|
229
|
+
this.#logger.debug(`${debugName} connected at :${req.socket.localPort}`);
|
|
230
|
+
ws.on('error', (err) => {
|
|
231
|
+
this.#logger.error(err);
|
|
232
|
+
});
|
|
233
|
+
ws.on('message', async (data) => {
|
|
234
|
+
this.#logger.debug(`Received from ${debugName}: ${data}`);
|
|
235
|
+
try {
|
|
236
|
+
const envelop = JSON.parse(data.toString());
|
|
237
|
+
// Decrypt message if needed
|
|
238
|
+
if (envelop.secure) {
|
|
239
|
+
if (!this.#pairingService.isClientConfirmed(envelop.clientId)) {
|
|
240
|
+
this.#sendMessage(ws, (0, socket_service_1.buildOutgoingSocketMessage)('requestPairing', undefined));
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
193
243
|
try {
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
if (envelop.secure) {
|
|
197
|
-
if (!this.#pairingService.isClientConfirmed(envelop.clientId)) {
|
|
198
|
-
this.#sendMessage(ws, (0, socket_service_1.buildOutgoingSocketMessage)('requestPairing', undefined));
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
try {
|
|
202
|
-
const decData = await this.#pairingService.decryptDataFromClient(envelop.clientId, envelop.data);
|
|
203
|
-
envelop.data = JSON.parse(decData);
|
|
204
|
-
}
|
|
205
|
-
catch (error) {
|
|
206
|
-
this.#logger.debug(error);
|
|
207
|
-
this.#sendMessage(ws, (0, socket_service_1.buildOutgoingSocketMessage)('requestPairing', undefined));
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
this.#handleMessage(ws, envelop);
|
|
244
|
+
const decData = await this.#pairingService.decryptDataFromClient(envelop.clientId, envelop.data);
|
|
245
|
+
envelop.data = JSON.parse(decData);
|
|
212
246
|
}
|
|
213
247
|
catch (error) {
|
|
214
|
-
this.#logger.debug(
|
|
248
|
+
this.#logger.debug(error);
|
|
249
|
+
this.#sendMessage(ws, (0, socket_service_1.buildOutgoingSocketMessage)('requestPairing', undefined));
|
|
250
|
+
return;
|
|
215
251
|
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
252
|
+
}
|
|
253
|
+
this.#handleMessage(ws, envelop);
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
this.#logger.debug(`Invalid message: ${JSON.stringify(data)} - Error: ${error}`);
|
|
257
|
+
}
|
|
219
258
|
});
|
|
220
259
|
}
|
|
221
260
|
#sendMessage(ws, envelop) {
|
|
@@ -19,8 +19,8 @@ export declare class ApiService {
|
|
|
19
19
|
setToken(token: string): void;
|
|
20
20
|
setSessionToken(token: string): void;
|
|
21
21
|
clearSessionToken(): void;
|
|
22
|
-
getPasswordlessCredentials(email: string): Promise<GetPublicPwlCredentialResponse>;
|
|
23
22
|
getReleases(os: OS): Promise<GetReleasesResponse>;
|
|
23
|
+
getPasswordlessCredentials(email: string): Promise<GetPublicPwlCredentialResponse>;
|
|
24
24
|
getPreloginInfo(): Promise<GetPreloginResponse>;
|
|
25
25
|
setPasswordlessCredential(params: {
|
|
26
26
|
credentialId: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.service.d.ts","sourceRoot":"","sources":["../../../../src/services/api.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAE1C,OAAO,EAAE,EAAE,EAAE,MAAM,eAAe,CAAA;AAClC,OAAO,EACL,SAAS,EACT,8BAA8B,EAC9B,mBAAmB,EACnB,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,qBAAqB,EACtB,MAAM,6BAA6B,CAAA;AAIpC,qBAAa,UAAU;;IACrB,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,YAAY,EAAE,MAAM,CAAA;IACpB,OAAO,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAA;gBAItB,MAAM,EAAE;QAClB,OAAO,EAAE,MAAM,CAAA;QACf,MAAM,EAAE,UAAU,CAAA;QAClB,OAAO,CAAC,EAAE;YAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;SAAE,CAAA;KACpC;IASD,QAAQ,CAAC,KAAK,EAAE,MAAM;IAOtB,eAAe,CAAC,KAAK,EAAE,MAAM;IAI7B,iBAAiB;
|
|
1
|
+
{"version":3,"file":"api.service.d.ts","sourceRoot":"","sources":["../../../../src/services/api.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAE1C,OAAO,EAAE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Ae,CAAC,mBAAmB,CAAC;IAa9E,UAAU;IAeV,aAAa;CAuQpB"}
|
|
@@ -28,12 +28,14 @@ export class ApiService {
|
|
|
28
28
|
this.sessionToken = '';
|
|
29
29
|
}
|
|
30
30
|
// -------------------------------- NO AUTH --------------------------------
|
|
31
|
-
|
|
31
|
+
// TODO: if service is only built by CyStack, then this base API must be fixed
|
|
32
|
+
async getReleases(os) {
|
|
32
33
|
try {
|
|
33
|
-
const url = `${this.baseUrl}/cystack_platform/pm/
|
|
34
|
+
const url = `${this.baseUrl}/cystack_platform/pm/releases`;
|
|
34
35
|
const res = await axios.get(url, {
|
|
35
36
|
params: {
|
|
36
|
-
|
|
37
|
+
client_id: 'desktop',
|
|
38
|
+
os,
|
|
37
39
|
},
|
|
38
40
|
headers: this.#getHeaders(),
|
|
39
41
|
});
|
|
@@ -42,32 +44,31 @@ export class ApiService {
|
|
|
42
44
|
return data;
|
|
43
45
|
}
|
|
44
46
|
catch (error) {
|
|
45
|
-
if (error.response?.status === 404) {
|
|
46
|
-
throw new ServiceError('1002', error);
|
|
47
|
-
}
|
|
48
47
|
throw new ServiceError('1000', error);
|
|
49
48
|
}
|
|
50
49
|
}
|
|
51
|
-
//
|
|
52
|
-
async
|
|
50
|
+
// -------------------------------- AUTH --------------------------------
|
|
51
|
+
async getPasswordlessCredentials(email) {
|
|
52
|
+
const headers = this.token ? this.#getAuthHeaders() : this.#getHeaders();
|
|
53
53
|
try {
|
|
54
|
-
const url = `${this.baseUrl}/cystack_platform/pm/
|
|
54
|
+
const url = `${this.baseUrl}/cystack_platform/pm/passwordless/credential`;
|
|
55
55
|
const res = await axios.get(url, {
|
|
56
56
|
params: {
|
|
57
|
-
|
|
58
|
-
os,
|
|
57
|
+
email,
|
|
59
58
|
},
|
|
60
|
-
headers
|
|
59
|
+
headers,
|
|
61
60
|
});
|
|
62
61
|
const data = res.data;
|
|
63
62
|
this.#logDebug({ url, method: 'GET', data });
|
|
64
63
|
return data;
|
|
65
64
|
}
|
|
66
65
|
catch (error) {
|
|
66
|
+
if (error.response?.status === 404) {
|
|
67
|
+
throw new ServiceError('1002', error);
|
|
68
|
+
}
|
|
67
69
|
throw new ServiceError('1000', error);
|
|
68
70
|
}
|
|
69
71
|
}
|
|
70
|
-
// -------------------------------- AUTH --------------------------------
|
|
71
72
|
async getPreloginInfo() {
|
|
72
73
|
const headers = this.#getAuthHeaders();
|
|
73
74
|
try {
|
|
@@ -4,13 +4,20 @@ import { ServiceError } from '../abstractions/errors';
|
|
|
4
4
|
import https from 'https';
|
|
5
5
|
import http from 'http';
|
|
6
6
|
import { WEBSOCKET_PORTS, WEBSOCKET_SSL_PORTS } from '../misc/config';
|
|
7
|
+
// Only loopback addresses are served so other machines on the network cannot connect.
|
|
8
|
+
// Clients connect to `localhost`, which may resolve to either address first
|
|
9
|
+
const LOOPBACK_V4 = '127.0.0.1';
|
|
10
|
+
const LOOPBACK_V6 = '::1';
|
|
11
|
+
// The machine has no IPv6 loopback, so the IPv4 listener alone serves `localhost`
|
|
12
|
+
const NO_IPV6_ERRORS = ['EADDRNOTAVAIL', 'EAFNOSUPPORT'];
|
|
7
13
|
export class SocketService {
|
|
8
14
|
#logger;
|
|
9
15
|
#pairingService;
|
|
10
16
|
#userService;
|
|
11
17
|
#eventService;
|
|
12
18
|
#sslConfig = undefined;
|
|
13
|
-
|
|
19
|
+
/** Shared by every WS and WSS listener, so `clients` covers all connected sockets */
|
|
20
|
+
#wss;
|
|
14
21
|
#clients = {};
|
|
15
22
|
#serviceAlias = '';
|
|
16
23
|
#allowedOrigins;
|
|
@@ -26,6 +33,18 @@ export class SocketService {
|
|
|
26
33
|
if (serviceAlias) {
|
|
27
34
|
this.#serviceAlias = serviceAlias;
|
|
28
35
|
}
|
|
36
|
+
this.#wss = new WebSocketServer({
|
|
37
|
+
noServer: true,
|
|
38
|
+
// Reject the upgrade (HTTP 401) when the browser-provided Origin is not allowed
|
|
39
|
+
verifyClient: (info) => {
|
|
40
|
+
const allowed = this.#isOriginAllowed(info.origin);
|
|
41
|
+
if (!allowed) {
|
|
42
|
+
this.#logger.debug(`Rejected WebSocket connection from origin: ${info.origin}`);
|
|
43
|
+
}
|
|
44
|
+
return allowed;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
this.#wss.on('connection', (ws, req) => this.#handleConnection(ws, req));
|
|
29
48
|
if (ssl) {
|
|
30
49
|
this.#sslConfig = ssl;
|
|
31
50
|
this.initSslSocket();
|
|
@@ -63,7 +82,7 @@ export class SocketService {
|
|
|
63
82
|
if (envelop.secure) {
|
|
64
83
|
throw new ServiceError('4002');
|
|
65
84
|
}
|
|
66
|
-
this.#
|
|
85
|
+
this.#wss.clients.forEach((ws) => {
|
|
67
86
|
if (ws.readyState === WebSocket.OPEN) {
|
|
68
87
|
this.#sendMessage(ws, envelop);
|
|
69
88
|
}
|
|
@@ -118,98 +137,118 @@ export class SocketService {
|
|
|
118
137
|
}
|
|
119
138
|
return this.#allowedOrigins.some((allowed) => typeof allowed === 'string' ? allowed === origin : allowed.test(origin));
|
|
120
139
|
}
|
|
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
|
-
|
|
140
|
+
#handleHttpRequest(req, res) {
|
|
141
|
+
const origin = req.headers.origin;
|
|
142
|
+
const isAllowed = this.#isOriginAllowed(origin);
|
|
143
|
+
const headers = {
|
|
144
|
+
'Access-Control-Allow-Methods': 'OPTIONS, GET',
|
|
145
|
+
'Access-Control-Max-Age': '2592000', // 30 days
|
|
146
|
+
};
|
|
147
|
+
if (isAllowed && origin) {
|
|
148
|
+
headers['Access-Control-Allow-Origin'] = origin;
|
|
149
|
+
headers['Access-Control-Allow-Credentials'] = 'true';
|
|
150
|
+
}
|
|
151
|
+
if (req.method === 'OPTIONS') {
|
|
152
|
+
res.writeHead(204, headers);
|
|
153
|
+
res.end();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (req.method === 'GET' && req.url === '/ping-locker-desktop') {
|
|
157
|
+
res.writeHead(200, { ...headers, 'Content-Type': 'application/json' });
|
|
158
|
+
res.end(JSON.stringify({
|
|
159
|
+
message: 'pong',
|
|
160
|
+
alias: this.#serviceAlias,
|
|
161
|
+
port: this.currentPort,
|
|
162
|
+
sslPort: this.currentSslPort,
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
res.writeHead(404, { ...headers, 'Content-Type': 'text/plain' });
|
|
167
|
+
res.end('Not Found');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** Serves the port on both loopback addresses. Resolves false if the port is taken on either one */
|
|
171
|
+
async #initSocketOnPort(port, enableSsl) {
|
|
172
|
+
const debugName = `Socket${enableSsl ? ' with SSL' : ''}`;
|
|
173
|
+
let v4Server;
|
|
174
|
+
try {
|
|
175
|
+
v4Server = await this.#listenOnLoopback(port, LOOPBACK_V4, enableSsl);
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
this.#logger.debug(`${debugName} cannot listen at ${LOOPBACK_V4}:${port}: ${error}`);
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
await this.#listenOnLoopback(port, LOOPBACK_V6, enableSsl);
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
const code = error.code;
|
|
186
|
+
if (!code || !NO_IPV6_ERRORS.includes(code)) {
|
|
187
|
+
// Another process holds [::1]:port and would receive the `localhost` connections
|
|
188
|
+
this.#logger.debug(`${debugName} cannot listen at [${LOOPBACK_V6}]:${port}: ${error}`);
|
|
189
|
+
v4Server.close();
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
this.#logger.debug(`${debugName}: no IPv6 loopback, serving ${LOOPBACK_V4} only`);
|
|
193
|
+
}
|
|
194
|
+
this.#logger.debug(`${debugName} listening at loopback :${port}`);
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
#listenOnLoopback(port, host, enableSsl) {
|
|
198
|
+
return new Promise((resolve, reject) => {
|
|
199
|
+
const handler = this.#handleHttpRequest.bind(this);
|
|
154
200
|
const server = enableSsl && this.#sslConfig
|
|
155
|
-
? https.createServer(this.#sslConfig,
|
|
156
|
-
: http.createServer(
|
|
157
|
-
|
|
158
|
-
// Init socket
|
|
159
|
-
const wss = new WebSocketServer({
|
|
160
|
-
server,
|
|
161
|
-
// Reject the upgrade (HTTP 401) when the browser-provided Origin is not allowed
|
|
162
|
-
verifyClient: (info) => {
|
|
163
|
-
const allowed = this.#isOriginAllowed(info.origin);
|
|
164
|
-
if (!allowed) {
|
|
165
|
-
this.#logger.debug(`Rejected WebSocket connection from origin: ${info.origin}`);
|
|
166
|
-
}
|
|
167
|
-
return allowed;
|
|
168
|
-
},
|
|
169
|
-
});
|
|
170
|
-
const debugName = `Socket${enableSsl ? ' with SSL' : ''}`;
|
|
171
|
-
wss.on('listening', () => {
|
|
172
|
-
this.#logger.debug(`${debugName} listening at :${port}`);
|
|
173
|
-
resolve(true);
|
|
174
|
-
});
|
|
175
|
-
wss.on('error', (err) => {
|
|
201
|
+
? https.createServer(this.#sslConfig, handler)
|
|
202
|
+
: http.createServer(handler);
|
|
203
|
+
const onListenError = (err) => {
|
|
176
204
|
server.close();
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
205
|
+
reject(err);
|
|
206
|
+
};
|
|
207
|
+
server.once('error', onListenError);
|
|
208
|
+
server.listen(port, host, () => {
|
|
209
|
+
server.off('error', onListenError);
|
|
210
|
+
server.on('error', (err) => this.#logger.error(err));
|
|
211
|
+
server.on('upgrade', (req, socket, head) => {
|
|
212
|
+
this.#wss.handleUpgrade(req, socket, head, (ws) => {
|
|
213
|
+
this.#wss.emit('connection', ws, req);
|
|
214
|
+
});
|
|
184
215
|
});
|
|
185
|
-
|
|
186
|
-
|
|
216
|
+
resolve(server);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
#handleConnection(ws, req) {
|
|
221
|
+
const isSsl = !!req.socket.encrypted;
|
|
222
|
+
const debugName = `Socket${isSsl ? ' with SSL' : ''}`;
|
|
223
|
+
this.#logger.debug(`${debugName} connected at :${req.socket.localPort}`);
|
|
224
|
+
ws.on('error', (err) => {
|
|
225
|
+
this.#logger.error(err);
|
|
226
|
+
});
|
|
227
|
+
ws.on('message', async (data) => {
|
|
228
|
+
this.#logger.debug(`Received from ${debugName}: ${data}`);
|
|
229
|
+
try {
|
|
230
|
+
const envelop = JSON.parse(data.toString());
|
|
231
|
+
// Decrypt message if needed
|
|
232
|
+
if (envelop.secure) {
|
|
233
|
+
if (!this.#pairingService.isClientConfirmed(envelop.clientId)) {
|
|
234
|
+
this.#sendMessage(ws, buildOutgoingSocketMessage('requestPairing', undefined));
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
187
237
|
try {
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
if (envelop.secure) {
|
|
191
|
-
if (!this.#pairingService.isClientConfirmed(envelop.clientId)) {
|
|
192
|
-
this.#sendMessage(ws, buildOutgoingSocketMessage('requestPairing', undefined));
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
try {
|
|
196
|
-
const decData = await this.#pairingService.decryptDataFromClient(envelop.clientId, envelop.data);
|
|
197
|
-
envelop.data = JSON.parse(decData);
|
|
198
|
-
}
|
|
199
|
-
catch (error) {
|
|
200
|
-
this.#logger.debug(error);
|
|
201
|
-
this.#sendMessage(ws, buildOutgoingSocketMessage('requestPairing', undefined));
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
this.#handleMessage(ws, envelop);
|
|
238
|
+
const decData = await this.#pairingService.decryptDataFromClient(envelop.clientId, envelop.data);
|
|
239
|
+
envelop.data = JSON.parse(decData);
|
|
206
240
|
}
|
|
207
241
|
catch (error) {
|
|
208
|
-
this.#logger.debug(
|
|
242
|
+
this.#logger.debug(error);
|
|
243
|
+
this.#sendMessage(ws, buildOutgoingSocketMessage('requestPairing', undefined));
|
|
244
|
+
return;
|
|
209
245
|
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
246
|
+
}
|
|
247
|
+
this.#handleMessage(ws, envelop);
|
|
248
|
+
}
|
|
249
|
+
catch (error) {
|
|
250
|
+
this.#logger.debug(`Invalid message: ${JSON.stringify(data)} - Error: ${error}`);
|
|
251
|
+
}
|
|
213
252
|
});
|
|
214
253
|
}
|
|
215
254
|
#sendMessage(ws, envelop) {
|
|
@@ -19,8 +19,8 @@ export declare class ApiService {
|
|
|
19
19
|
setToken(token: string): void;
|
|
20
20
|
setSessionToken(token: string): void;
|
|
21
21
|
clearSessionToken(): void;
|
|
22
|
-
getPasswordlessCredentials(email: string): Promise<GetPublicPwlCredentialResponse>;
|
|
23
22
|
getReleases(os: OS): Promise<GetReleasesResponse>;
|
|
23
|
+
getPasswordlessCredentials(email: string): Promise<GetPublicPwlCredentialResponse>;
|
|
24
24
|
getPreloginInfo(): Promise<GetPreloginResponse>;
|
|
25
25
|
setPasswordlessCredential(params: {
|
|
26
26
|
credentialId: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.service.d.ts","sourceRoot":"","sources":["../../../../src/services/api.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAE1C,OAAO,EAAE,EAAE,EAAE,MAAM,eAAe,CAAA;AAClC,OAAO,EACL,SAAS,EACT,8BAA8B,EAC9B,mBAAmB,EACnB,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,qBAAqB,EACtB,MAAM,6BAA6B,CAAA;AAIpC,qBAAa,UAAU;;IACrB,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,YAAY,EAAE,MAAM,CAAA;IACpB,OAAO,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAA;gBAItB,MAAM,EAAE;QAClB,OAAO,EAAE,MAAM,CAAA;QACf,MAAM,EAAE,UAAU,CAAA;QAClB,OAAO,CAAC,EAAE;YAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;SAAE,CAAA;KACpC;IASD,QAAQ,CAAC,KAAK,EAAE,MAAM;IAOtB,eAAe,CAAC,KAAK,EAAE,MAAM;IAI7B,iBAAiB;
|
|
1
|
+
{"version":3,"file":"api.service.d.ts","sourceRoot":"","sources":["../../../../src/services/api.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAE1C,OAAO,EAAE,EAAE,EAAE,MAAM,eAAe,CAAA;AAClC,OAAO,EACL,SAAS,EACT,8BAA8B,EAC9B,mBAAmB,EACnB,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,qBAAqB,EACtB,MAAM,6BAA6B,CAAA;AAIpC,qBAAa,UAAU;;IACrB,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,YAAY,EAAE,MAAM,CAAA;IACpB,OAAO,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAA;gBAItB,MAAM,EAAE;QAClB,OAAO,EAAE,MAAM,CAAA;QACf,MAAM,EAAE,UAAU,CAAA;QAClB,OAAO,CAAC,EAAE;YAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;SAAE,CAAA;KACpC;IASD,QAAQ,CAAC,KAAK,EAAE,MAAM;IAOtB,eAAe,CAAC,KAAK,EAAE,MAAM;IAI7B,iBAAiB;IAOX,WAAW,CAAC,EAAE,EAAE,EAAE;IAoBlB,0BAA0B,CAAC,KAAK,EAAE,MAAM;IAqBxC,eAAe;IAiBf,yBAAyB,CAAC,MAAM,EAAE;QACtC,YAAY,EAAE,MAAM,CAAA;QACpB,IAAI,EAAE,MAAM,CAAA;QACZ,IAAI,EAAE,gBAAgB,CAAA;QACtB,MAAM,CAAC,EAAE,MAAM,CAAA;KAChB;gBAiByB,MAAM;;IAQ1B,4BAA4B;IAmB5B,iCAAiC;IAkBjC,+BAA+B,CAAC,OAAO,EAAE,kBAAkB;YAQpC,MAAM;;IAM7B,kCAAkC,CAAC,EAAE,EAAE,MAAM;IAY7C,kCAAkC,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,qBAAqB;CAuCpF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socket.service.d.ts","sourceRoot":"","sources":["../../../../src/services/socket.service.ts"],"names":[],"mappings":";;AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,EAIL,eAAe,EACf,mBAAmB,EAEpB,MAAM,gCAAgC,CAAA;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"socket.service.d.ts","sourceRoot":"","sources":["../../../../src/services/socket.service.ts"],"names":[],"mappings":";;AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,EAIL,eAAe,EACf,mBAAmB,EAEpB,MAAM,gCAAgC,CAAA;AACvC,OAAO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