@sparkelf/dsh-mobile-bridge-server 0.2.6 → 0.2.8
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/package.json +2 -2
- package/src/index.ts +0 -0
- package/src/phone.ts +9 -5
- package/src/server.ts +42 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sparkelf/dsh-mobile-bridge-server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Multi-user bridge server connecting mobile browsers to local DeepSeek Harness instances through outbound tunnels.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,4 +29,4 @@
|
|
|
29
29
|
"ws": "^8.18.0",
|
|
30
30
|
"zxing-wasm": "3.1.3"
|
|
31
31
|
}
|
|
32
|
-
}
|
|
32
|
+
}
|
package/src/index.ts
CHANGED
|
File without changes
|
package/src/phone.ts
CHANGED
|
@@ -58,7 +58,7 @@ self.addEventListener('message', event => {
|
|
|
58
58
|
const replacedError = new Error('pair replaced')
|
|
59
59
|
for (const entry of pending.values()) {
|
|
60
60
|
if (entry.websocketPort) { entry.websocketPort.postMessage({ kind: 'websocket-error', message: replacedError.message }); entry.websocketPort.close() }
|
|
61
|
-
else if (entry.controller)
|
|
61
|
+
else if (entry.controller) entry.controller.error(replacedError)
|
|
62
62
|
else entry.reject(replacedError)
|
|
63
63
|
}
|
|
64
64
|
}
|
|
@@ -115,7 +115,7 @@ function wsReady() {
|
|
|
115
115
|
entry.websocketPort.postMessage({ kind: 'websocket-error', message: error.message })
|
|
116
116
|
entry.websocketPort.close()
|
|
117
117
|
} else if (entry.controller) {
|
|
118
|
-
|
|
118
|
+
entry.controller.error(error)
|
|
119
119
|
} else entry.reject(error)
|
|
120
120
|
}
|
|
121
121
|
pending.clear()
|
|
@@ -166,7 +166,7 @@ function wsReady() {
|
|
|
166
166
|
}
|
|
167
167
|
pending.delete(frame.id)
|
|
168
168
|
entry.resolve(new Response(body, { status: full.status, headers: full.headers }))
|
|
169
|
-
})().catch(error => { console.error('[dsh-mobile-bridge] response decrypt failed', error); entry.reject(error); pending.delete(frame.id) })
|
|
169
|
+
})().catch(error => { console.error('[dsh-mobile-bridge] response decrypt failed', error); if (entry.controller) entry.controller.error(error); else entry.reject(error); pending.delete(frame.id) })
|
|
170
170
|
}
|
|
171
171
|
})
|
|
172
172
|
return wsPromise
|
|
@@ -243,8 +243,12 @@ self.addEventListener('fetch', event => {
|
|
|
243
243
|
}
|
|
244
244
|
if ((request.headers.get('accept') ?? '').includes('text/event-stream')) {
|
|
245
245
|
return new Promise((resolve, reject) => {
|
|
246
|
-
|
|
247
|
-
|
|
246
|
+
let controller
|
|
247
|
+
const readable = new ReadableStream({
|
|
248
|
+
start(value) { controller = value },
|
|
249
|
+
cancel() { pending.delete(id) },
|
|
250
|
+
})
|
|
251
|
+
pending.set(id, { key, controller, resolve: () => resolve(new Response(readable, { headers: { 'content-type': 'text/event-stream' } })), reject, status: 200, headers: {} })
|
|
248
252
|
ws.send(JSON.stringify({ id, blob, streamHint: true }))
|
|
249
253
|
})
|
|
250
254
|
}
|
package/src/server.ts
CHANGED
|
@@ -174,6 +174,36 @@ function requestIp(req: IncomingMessage): string {
|
|
|
174
174
|
return value?.trim() || req.socket.remoteAddress || 'unknown'
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
+
/**
|
|
178
|
+
* Whether one request reached this server over TLS.
|
|
179
|
+
*
|
|
180
|
+
* The session cookie carries `Secure` only when it does, because a browser refuses to store or send
|
|
181
|
+
* a `Secure` cookie over plain HTTP. The phone service worker authenticates its relay socket from
|
|
182
|
+
* that cookie alone -- it opens `/ws/client` without a token -- so a deployment served over HTTP
|
|
183
|
+
* without this check lost the cookie, and the phone could not authenticate its socket at all.
|
|
184
|
+
* @param req - Request whose scheme to classify.
|
|
185
|
+
* @returns True when the client used HTTPS.
|
|
186
|
+
*/
|
|
187
|
+
function requestIsSecure(req: IncomingMessage): boolean {
|
|
188
|
+
const forwarded = req.headers['x-forwarded-proto']
|
|
189
|
+
const value = Array.isArray(forwarded) ? forwarded[0] : forwarded?.split(',')[0]
|
|
190
|
+
if (value !== undefined && value.trim() !== '') return value.trim() === 'https'
|
|
191
|
+
return (req.socket as { encrypted?: boolean }).encrypted === true
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Session cookie attributes, with `Secure` only where a browser can return the cookie.
|
|
196
|
+
* @param req - Request the cookie answers.
|
|
197
|
+
* @param token - Session token to store.
|
|
198
|
+
* @param days - Lifetime in days.
|
|
199
|
+
* @returns The `Set-Cookie` value.
|
|
200
|
+
*/
|
|
201
|
+
function sessionCookie(req: IncomingMessage, token: string, days: number): string {
|
|
202
|
+
return COOKIE + '=' + encodeURIComponent(token)
|
|
203
|
+
+ '; HttpOnly; SameSite=Lax; Path=/; Max-Age=' + String(days * 86400)
|
|
204
|
+
+ (requestIsSecure(req) ? '; Secure' : '')
|
|
205
|
+
}
|
|
206
|
+
|
|
177
207
|
function requestDeviceName(req: IncomingMessage): string {
|
|
178
208
|
const userAgent = req.headers['user-agent'] ?? ''
|
|
179
209
|
const platform = /iPad/u.test(userAgent)
|
|
@@ -319,10 +349,10 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
|
|
|
319
349
|
return { token, device }
|
|
320
350
|
}
|
|
321
351
|
|
|
322
|
-
const respondLogin = (res: ServerResponse, token: string, days: number): void => {
|
|
352
|
+
const respondLogin = (req: IncomingMessage, res: ServerResponse, token: string, days: number): void => {
|
|
323
353
|
res.writeHead(200, {
|
|
324
354
|
'content-type': 'application/json',
|
|
325
|
-
'set-cookie': [
|
|
355
|
+
'set-cookie': [sessionCookie(req, token, days)],
|
|
326
356
|
})
|
|
327
357
|
res.end(JSON.stringify({ token }))
|
|
328
358
|
}
|
|
@@ -409,7 +439,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
|
|
|
409
439
|
if (priorDevice === undefined) throw new Error('scan a desktop pairing QR first')
|
|
410
440
|
store.bindDevice(token, priorDevice.bridgeId, { name: requestDeviceName(req), ip: requestIp(req) }, priorDevice.id)
|
|
411
441
|
}
|
|
412
|
-
respondLogin(res, token, days)
|
|
442
|
+
respondLogin(req, res, token, days)
|
|
413
443
|
} catch (error) { fail(res, 401, 'email login failed', error) }
|
|
414
444
|
return
|
|
415
445
|
}
|
|
@@ -419,7 +449,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
|
|
|
419
449
|
const verify = externalAuth[provider]
|
|
420
450
|
if (verify === undefined) throw new Error('unknown provider')
|
|
421
451
|
const externalId = await verify(payload)
|
|
422
|
-
respondLogin(res, store.loginExternal(provider, externalId), DEFAULT_SESSION_DAYS)
|
|
452
|
+
respondLogin(req, res, store.loginExternal(provider, externalId), DEFAULT_SESSION_DAYS)
|
|
423
453
|
} catch (error) { fail(res, 401, 'external login failed', error) }
|
|
424
454
|
return
|
|
425
455
|
}
|
|
@@ -434,7 +464,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
|
|
|
434
464
|
return
|
|
435
465
|
}
|
|
436
466
|
const { token } = pairDevice(req, ticket, 'bridge', randomBytes(12).toString('hex'))
|
|
437
|
-
respondLogin(res, token, ticket.sessionDays)
|
|
467
|
+
respondLogin(req, res, token, ticket.sessionDays)
|
|
438
468
|
} catch (error) { fail(res, 401, 'pairing login failed', error) }
|
|
439
469
|
return
|
|
440
470
|
}
|
|
@@ -445,7 +475,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
|
|
|
445
475
|
if (ticket.email2fa === undefined) throw new Error('no pending challenge')
|
|
446
476
|
store.consumeEmailCode(ticket.email2fa, emailCode)
|
|
447
477
|
const { token } = pairDevice(req, ticket, 'bridge', randomBytes(12).toString('hex'))
|
|
448
|
-
respondLogin(res, token, ticket.sessionDays)
|
|
478
|
+
respondLogin(req, res, token, ticket.sessionDays)
|
|
449
479
|
} catch (error) { fail(res, 401, 'pairing verification failed', error) }
|
|
450
480
|
return
|
|
451
481
|
}
|
|
@@ -497,7 +527,7 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
|
|
|
497
527
|
if (socket?.readyState === WebSocket.OPEN) socket.send(JSON.stringify({ control: 'paired', devices: deviceViews(ticket.bridgeId) }))
|
|
498
528
|
}
|
|
499
529
|
res.writeHead(302, {
|
|
500
|
-
'set-cookie': [
|
|
530
|
+
'set-cookie': [sessionCookie(req, token, days)],
|
|
501
531
|
location: '/',
|
|
502
532
|
})
|
|
503
533
|
res.end()
|
|
@@ -621,7 +651,11 @@ export function createBridgeServer(store: UserStore, options: BridgeServerRuntim
|
|
|
621
651
|
const device = store.deviceFor(token)
|
|
622
652
|
const bridgeId = device?.bridgeId
|
|
623
653
|
if (device === undefined || bridgeId === undefined) {
|
|
624
|
-
|
|
654
|
+
// 4003 means the desktop revoked this device, and the phone treats it as terminal: it clears
|
|
655
|
+
// the pairing and stops reconnecting. A request that simply arrived without a usable session
|
|
656
|
+
// is not that, and reporting it as a revocation left a phone whose cookie was missing with a
|
|
657
|
+
// dead pairing it never got told to renew. 4004 keeps the two apart.
|
|
658
|
+
wss.handleUpgrade(req, socket, head, client => { client.close(4004, 'pairing required') })
|
|
625
659
|
return
|
|
626
660
|
}
|
|
627
661
|
if (bridges.get(bridgeId)?.readyState !== WebSocket.OPEN) {
|