@linxin666/dsh-remote-bridge 0.4.12 → 0.4.14
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/README.i18n.yaml +2 -2
- package/README.md +53 -11
- package/README.zh.md +53 -11
- package/lib/client.js +112 -90
- package/lib/client.js.map +1 -1
- package/lib/index.js +18800 -334
- package/lib/types/client/PairPanel.d.ts +7 -0
- package/lib/types/client/locales.d.ts +13 -10
- package/lib/types/core/connection.d.ts +50 -0
- package/lib/types/core/devices.d.ts +80 -6
- package/lib/types/core/liveactivity.d.ts +7 -0
- package/lib/types/core/model-efforts.d.ts +83 -0
- package/lib/types/core/protocol.d.ts +24 -0
- package/lib/types/core/push.d.ts +70 -3
- package/lib/types/host/firewall.d.ts +74 -0
- package/lib/types/host/host-bind-capability.d.ts +90 -0
- package/lib/types/host/ingress-gate.d.ts +59 -0
- package/lib/types/host/ingress-sync.d.ts +429 -0
- package/lib/types/host/lan-bind-plan.d.ts +96 -0
- package/lib/types/host/lan-bind.d.ts +106 -0
- package/lib/types/host/lan-forward.d.ts +112 -0
- package/lib/types/host/lan.d.ts +61 -0
- package/lib/types/host/pair-origin.d.ts +34 -19
- package/lib/types/host/relay-bridge-connector.d.ts +234 -0
- package/lib/types/host/relay-connector.d.ts +156 -0
- package/lib/types/host/relay-control.d.ts +195 -0
- package/lib/types/host/relay-registry.d.ts +123 -0
- package/lib/types/host/relay-secondary.d.ts +209 -0
- package/lib/types/host/relay.d.ts +170 -0
- package/lib/types/host/routes.d.ts +29 -1
- package/lib/types/host/tunnel-orphan.d.ts +145 -0
- package/lib/types/host/tunnel-plan.d.ts +41 -0
- package/lib/types/host/tunnel.d.ts +289 -0
- package/lib/types/index.d.ts +488 -1
- package/package.json +4 -2
- package/src/client/PairPanel.tsx +21 -0
- package/src/client/locales.ts +19 -15
- package/src/client/panel.module.css +48 -1
- package/src/core/connection.ts +316 -11
- package/src/core/devices.ts +165 -16
- package/src/core/hub.ts +17 -5
- package/src/core/liveactivity.ts +16 -2
- package/src/core/model-efforts.ts +223 -0
- package/src/core/protocol.ts +27 -0
- package/src/core/push.ts +90 -5
- package/src/host/firewall.ts +230 -0
- package/src/host/host-bind-capability.ts +249 -0
- package/src/host/ingress-gate.ts +203 -0
- package/src/host/ingress-sync.ts +1074 -0
- package/src/host/lan-bind-plan.ts +134 -0
- package/src/host/lan-bind.ts +250 -0
- package/src/host/lan-forward.ts +321 -0
- package/src/host/lan.ts +142 -0
- package/src/host/pair-origin.ts +44 -38
- package/src/host/relay-bridge-connector.ts +849 -0
- package/src/host/relay-connector.ts +324 -0
- package/src/host/relay-control.ts +315 -0
- package/src/host/relay-registry.ts +275 -0
- package/src/host/relay-secondary.ts +478 -0
- package/src/host/relay.ts +294 -0
- package/src/host/routes.ts +57 -3
- package/src/host/tunnel-orphan.ts +325 -0
- package/src/host/tunnel-plan.ts +58 -0
- package/src/host/tunnel.ts +688 -0
- package/src/index.ts +1516 -23
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The inbound ingress gate: one loopback-only HTTP listener that forwards
|
|
3
|
+
* exactly one upgrade path to the host's own protocol port.
|
|
4
|
+
*
|
|
5
|
+
* It exists so a tunnel or the relay bridge can be pointed at a listener whose
|
|
6
|
+
* whole surface is `WS_PATH`. Everything else is refused at this hop instead
|
|
7
|
+
* of being forwarded and judged further in: a request the gate does not serve
|
|
8
|
+
* never opens an upstream connection, so the host's routes, plugins and
|
|
9
|
+
* control plane are not reachable through it even by a misconfigured tunnel.
|
|
10
|
+
*
|
|
11
|
+
* The gate holds no state between two connections: it writes no log line,
|
|
12
|
+
* touches no file, and keeps no token. The only thing it owns is one socket
|
|
13
|
+
* per side, closed together.
|
|
14
|
+
*
|
|
15
|
+
* @module dsh-remote-bridge/host/ingress-gate
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import http from 'node:http'
|
|
19
|
+
import { connect } from 'node:net'
|
|
20
|
+
import type { AddressInfo, Socket } from 'node:net'
|
|
21
|
+
import { WS_PATH } from '../core/protocol.ts'
|
|
22
|
+
|
|
23
|
+
/** The body every refused request and upgrade is answered with. */
|
|
24
|
+
export const INGRESS_NOT_FOUND_BODY = '{"error":"not-found"}'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The one address the gate ever binds. It is a loopback listener by
|
|
28
|
+
* construction: nothing outside this machine may reach it directly, which is
|
|
29
|
+
* what keeps the forwarding decision the only way in.
|
|
30
|
+
*/
|
|
31
|
+
export const INGRESS_BIND_HOST = '127.0.0.1'
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether one inbound URL is the single path this gate forwards.
|
|
35
|
+
*
|
|
36
|
+
* The comparison is on the path, not the whole URL: the pairing link carries
|
|
37
|
+
* its one-shot ticket in the query (`WS_PATH?token=...`), so a strict
|
|
38
|
+
* whole-URL match would refuse the one client the host is waiting for. A path
|
|
39
|
+
* that merely starts the same way, another path, and an empty URL are refused.
|
|
40
|
+
* @param url - the request URL as node reports it.
|
|
41
|
+
* @returns true when the path is exactly `WS_PATH`.
|
|
42
|
+
*/
|
|
43
|
+
export function isIngressPath(url: string): boolean {
|
|
44
|
+
const query = url.indexOf('?')
|
|
45
|
+
return (query === -1 ? url : url.slice(0, query)) === WS_PATH
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** What the gate needs to know to dial the host's own listener. */
|
|
49
|
+
export interface IngressGateOptions {
|
|
50
|
+
/** The host the upstream protocol listener answers on. */
|
|
51
|
+
upstreamHost: string
|
|
52
|
+
/** The port the upstream protocol listener bound. */
|
|
53
|
+
upstreamPort: number
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The port the gate took. */
|
|
57
|
+
export interface IngressGateStarted {
|
|
58
|
+
port: number
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** One ingress gate: bind, report the port, close. */
|
|
62
|
+
export interface IngressGate {
|
|
63
|
+
/** Bind the loopback listener and report the port it took. */
|
|
64
|
+
start(): Promise<IngressGateStarted>
|
|
65
|
+
/** Close the listener and every socket it still holds (idempotent). */
|
|
66
|
+
stop(): void
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The response head of one refused upgrade, which has no `ServerResponse`. */
|
|
70
|
+
function notFoundHead(length: number): string {
|
|
71
|
+
return 'HTTP/1.1 404 Not Found\r\n'
|
|
72
|
+
+ 'content-type: application/json\r\n'
|
|
73
|
+
+ 'content-length: ' + String(length) + '\r\n'
|
|
74
|
+
+ 'connection: close\r\n'
|
|
75
|
+
+ '\r\n'
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Rebuild the upgrade request line and headers for the upstream hop: the
|
|
80
|
+
* `Host` the phone dialed is dropped and replaced with the loopback authority
|
|
81
|
+
* the host's own admission gate expects, and every other header travels
|
|
82
|
+
* untouched (the upgrade handshake cannot be reconstructed header by header
|
|
83
|
+
* without losing what the protocol put there).
|
|
84
|
+
* @param request - the inbound upgrade request.
|
|
85
|
+
* @param authority - the `host:port` the upstream listener answers on.
|
|
86
|
+
* @returns the request head, headers included, ending in a blank line.
|
|
87
|
+
*/
|
|
88
|
+
function upstreamHead(request: http.IncomingMessage, authority: string): string {
|
|
89
|
+
const lines = ['GET ' + (request.url ?? '/') + ' HTTP/1.1']
|
|
90
|
+
const raw = request.rawHeaders
|
|
91
|
+
for (let index = 0; index + 1 < raw.length; index += 2) {
|
|
92
|
+
if (raw[index].toLowerCase() === 'host') continue
|
|
93
|
+
lines.push(raw[index] + ': ' + raw[index + 1])
|
|
94
|
+
}
|
|
95
|
+
lines.push('Host: ' + authority)
|
|
96
|
+
return lines.join('\r\n') + '\r\n\r\n'
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Build one gate.
|
|
101
|
+
* @param options - the upstream listener the gate forwards to.
|
|
102
|
+
* @returns the gate itself; nothing is bound until `start()` runs.
|
|
103
|
+
*/
|
|
104
|
+
export function createIngressGate(options: IngressGateOptions): IngressGate {
|
|
105
|
+
const authority = options.upstreamHost + ':' + String(options.upstreamPort)
|
|
106
|
+
const sockets = new Set<Socket>()
|
|
107
|
+
|
|
108
|
+
const track = (socket: Socket): void => {
|
|
109
|
+
sockets.add(socket)
|
|
110
|
+
socket.once('close', () => { sockets.delete(socket) })
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Refuse one plain request: 404, no upstream dial. */
|
|
114
|
+
const refuse = (response: http.ServerResponse): void => {
|
|
115
|
+
const body = Buffer.from(INGRESS_NOT_FOUND_BODY)
|
|
116
|
+
response.writeHead(404, {
|
|
117
|
+
'content-type': 'application/json',
|
|
118
|
+
'content-length': String(body.byteLength),
|
|
119
|
+
connection: 'close',
|
|
120
|
+
})
|
|
121
|
+
response.end(body)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Refuse one upgrade: the same 404, then the socket is closed. */
|
|
125
|
+
const refuseUpgrade = (socket: Socket): void => {
|
|
126
|
+
const body = Buffer.from(INGRESS_NOT_FOUND_BODY)
|
|
127
|
+
socket.end(notFoundHead(body.byteLength) + INGRESS_NOT_FOUND_BODY)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Dial the upstream for one admitted upgrade and pipe both directions.
|
|
132
|
+
*
|
|
133
|
+
* The two sockets are piped blind: nothing here inspects the upstream's answer,
|
|
134
|
+
* so a handshake the upstream refuses is relayed as-is instead of becoming a
|
|
135
|
+
* 502. That is sound only because of what the upstream is — this gate always
|
|
136
|
+
* dials the same process's own Node http server, which stops parsing HTTP the
|
|
137
|
+
* moment it emits `upgrade` and never speaks first on that socket. A socket
|
|
138
|
+
* that answers something other than `101` therefore cannot occur today; if the
|
|
139
|
+
* upstream ever becomes a proxy, a TLS terminator or another process, this is
|
|
140
|
+
* the spot that must first read the response head and close both sides when it
|
|
141
|
+
* is not a 101.
|
|
142
|
+
* @param request - the admitted upgrade.
|
|
143
|
+
* @param socket - the client side of it.
|
|
144
|
+
* @param head - bytes the client already sent past the handshake.
|
|
145
|
+
*/
|
|
146
|
+
const forward = (request: http.IncomingMessage, socket: Socket, head: Buffer): void => {
|
|
147
|
+
const upstream = connect({ host: options.upstreamHost, port: options.upstreamPort })
|
|
148
|
+
track(upstream)
|
|
149
|
+
upstream.on('error', () => { socket.destroy() })
|
|
150
|
+
socket.on('error', () => { upstream.destroy() })
|
|
151
|
+
// A half-closed pair must not leave the other side holding a connection: a
|
|
152
|
+
// destroyed socket is not an ended one, so the pipe above never delivers it.
|
|
153
|
+
upstream.on('close', () => { socket.destroy() })
|
|
154
|
+
socket.on('close', () => { upstream.destroy() })
|
|
155
|
+
upstream.on('connect', () => {
|
|
156
|
+
upstream.write(upstreamHead(request, authority))
|
|
157
|
+
if (head.byteLength > 0) upstream.write(head)
|
|
158
|
+
upstream.pipe(socket)
|
|
159
|
+
socket.pipe(upstream)
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const server = http.createServer((_request, response) => {
|
|
164
|
+
// A plain request is never proxied: the gate serves one upgrade path and
|
|
165
|
+
// nothing else, so there is no branch in which this dials upstream.
|
|
166
|
+
refuse(response)
|
|
167
|
+
})
|
|
168
|
+
server.on('connection', track)
|
|
169
|
+
server.on('upgrade', (request, socket: Socket, head: Buffer) => {
|
|
170
|
+
track(socket)
|
|
171
|
+
if (!isIngressPath(request.url ?? '')) {
|
|
172
|
+
refuseUpgrade(socket)
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
forward(request, socket, head)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
start(): Promise<IngressGateStarted> {
|
|
180
|
+
if (server.listening) {
|
|
181
|
+
return Promise.resolve({ port: (server.address() as AddressInfo).port })
|
|
182
|
+
}
|
|
183
|
+
return new Promise<IngressGateStarted>((resolve, reject) => {
|
|
184
|
+
const onError = (error: Error): void => { reject(error) }
|
|
185
|
+
server.once('error', onError)
|
|
186
|
+
server.listen(0, INGRESS_BIND_HOST, () => {
|
|
187
|
+
server.off('error', onError)
|
|
188
|
+
resolve({ port: (server.address() as AddressInfo).port })
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
},
|
|
192
|
+
stop(): void {
|
|
193
|
+
if (!server.listening) {
|
|
194
|
+
for (const socket of sockets) socket.destroy()
|
|
195
|
+
sockets.clear()
|
|
196
|
+
return
|
|
197
|
+
}
|
|
198
|
+
server.close()
|
|
199
|
+
for (const socket of sockets) socket.destroy()
|
|
200
|
+
sockets.clear()
|
|
201
|
+
},
|
|
202
|
+
}
|
|
203
|
+
}
|