@linxin666/dsh-remote-bridge 0.4.12 → 0.4.13
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 +45 -8
- package/README.zh.md +45 -8
- package/lib/client.js +1 -1
- package/lib/client.js.map +1 -1
- package/lib/index.js +18031 -373
- package/lib/types/core/connection.d.ts +9 -0
- package/lib/types/core/devices.d.ts +27 -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 +2 -0
- package/lib/types/core/push.d.ts +51 -2
- 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 +215 -0
- package/lib/types/host/relay-connector.d.ts +152 -0
- package/lib/types/host/relay-control.d.ts +124 -0
- package/lib/types/host/relay-registry.d.ts +114 -0
- package/lib/types/host/relay.d.ts +170 -0
- package/lib/types/host/routes.d.ts +24 -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 +426 -1
- package/package.json +4 -2
- package/src/client/panel.module.css +16 -1
- package/src/core/connection.ts +206 -11
- package/src/core/devices.ts +31 -6
- 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 +3 -0
- package/src/core/push.ts +70 -4
- 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 +783 -0
- package/src/host/relay-connector.ts +317 -0
- package/src/host/relay-control.ts +224 -0
- package/src/host/relay-registry.ts +263 -0
- package/src/host/relay.ts +294 -0
- package/src/host/routes.ts +47 -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 +1280 -22
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure decision helpers shared by the lan-bind sync path (index.ts) and the
|
|
3
|
+
* status path (lanBindStatus). Extracted so the LAN-edge plan and the firewall
|
|
4
|
+
* re-apply gate have one implementation and direct unit coverage.
|
|
5
|
+
*
|
|
6
|
+
* Since issue #1917 the plugin owns no bind: the host stays on loopback and LAN
|
|
7
|
+
* reachability comes from the forwarder in lan-forward.ts, so the only decision
|
|
8
|
+
* left here is which LAN address (if any) that forwarder may listen on.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { isLoopbackBind } from './host-bind-capability.ts'
|
|
12
|
+
|
|
13
|
+
/** webStartup facts the CLI hands the process (absent unless a flag was passed). */
|
|
14
|
+
export interface StartupFacts {
|
|
15
|
+
host?: string
|
|
16
|
+
port?: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* What the running host accepts, and which concrete addresses it could pin.
|
|
21
|
+
* Sampled once per boot: the webserver package cannot change under a running
|
|
22
|
+
* process, and the interface list is frozen for the same reason the QR bases
|
|
23
|
+
* are (see src/lan.ts).
|
|
24
|
+
*/
|
|
25
|
+
export interface LanBindCapability {
|
|
26
|
+
/** Whether the installed webserver schema accepts the wildcard bind (reported, never written). */
|
|
27
|
+
wildcardSupported: boolean
|
|
28
|
+
/** Local non-loopback IPv4 literals, in preference order (`lanIPv4Addresses()`). */
|
|
29
|
+
candidates: string[]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Why the LAN forwarder is not listening, when it is not. */
|
|
33
|
+
export type ForwardStopReason =
|
|
34
|
+
/** The toggle is off (or was never flipped). */
|
|
35
|
+
| 'off'
|
|
36
|
+
/** The host itself binds a non-loopback address (`--host`): the forwarder yields the port. */
|
|
37
|
+
| 'host-bind'
|
|
38
|
+
/** The toggle is on but this machine has no LAN IPv4 address to listen on. */
|
|
39
|
+
| 'no-address'
|
|
40
|
+
|
|
41
|
+
/** The LAN edge one sync should run: an address to listen on, or why not. */
|
|
42
|
+
export interface ForwardPlan {
|
|
43
|
+
/** The LAN address the forwarder listens on; undefined when it must stay off. */
|
|
44
|
+
host: string | undefined
|
|
45
|
+
/** Why it stays off; undefined when a host is planned. */
|
|
46
|
+
reason: ForwardStopReason | undefined
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The LAN address the forwarder may listen on, or the reason it must not.
|
|
51
|
+
*
|
|
52
|
+
* The order is: the toggle off keeps LAN off; a non-loopback `--host` means the
|
|
53
|
+
* operator asked the HOST to bind the network itself, so the forwarder yields
|
|
54
|
+
* the port instead of fighting it for the same address:port (issue #1917);
|
|
55
|
+
* otherwise this machine's first LAN candidate. A host with no LAN IPv4 address
|
|
56
|
+
* has nothing to listen on, which the caller reports instead of failing.
|
|
57
|
+
*
|
|
58
|
+
* Only a concrete interface address is ever returned: the wildcard would
|
|
59
|
+
* collide with the loopback listener on the same port, and a forwarder bound to
|
|
60
|
+
* `0.0.0.0` would additionally answer the loopback port a second time.
|
|
61
|
+
* @param lanBind - the toggle value.
|
|
62
|
+
* @param startupHost - the CLI-provided bind host, when given.
|
|
63
|
+
* @param capability - the sampled interface candidates.
|
|
64
|
+
* @returns the address to listen on, or the reason there is none.
|
|
65
|
+
*/
|
|
66
|
+
export function forwardPlanOf(
|
|
67
|
+
lanBind: boolean | undefined,
|
|
68
|
+
startupHost: string | undefined,
|
|
69
|
+
capability: LanBindCapability,
|
|
70
|
+
): ForwardPlan {
|
|
71
|
+
if (lanBind !== true) return { host: undefined, reason: 'off' }
|
|
72
|
+
if (typeof startupHost === 'string' && startupHost !== '' && isLanReachableBind(startupHost)) {
|
|
73
|
+
return { host: undefined, reason: 'host-bind' }
|
|
74
|
+
}
|
|
75
|
+
const host = capability.candidates[0]
|
|
76
|
+
return host === undefined ? { host: undefined, reason: 'no-address' } : { host, reason: undefined }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The reachability decision behind the QR bases, the status `lanUrls`, the
|
|
81
|
+
* firewall summary and the boot's LAN warning: a bind that is not loopback is
|
|
82
|
+
* reachable from another device, whatever literal it names. Equality with the
|
|
83
|
+
* wildcard is deliberately not the test - on a host that refuses wildcard
|
|
84
|
+
* binds the only LAN bind is a concrete interface address.
|
|
85
|
+
* @param host - the block host or the live bind (absent before it settles).
|
|
86
|
+
* @returns true when another device could dial the listener.
|
|
87
|
+
*/
|
|
88
|
+
export function isLanReachableBind(host: string | undefined): boolean {
|
|
89
|
+
return !isLoopbackBind(host)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The last firewall state this process applied. */
|
|
93
|
+
export interface AppliedFirewallState {
|
|
94
|
+
enabled: boolean
|
|
95
|
+
port: number
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The managed profile when neither the config nor the runtime names one. */
|
|
99
|
+
export const DEFAULT_MANAGED_PROFILE = 'web'
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The profile whose `cordis.patch.yml` the LAN bind toggle manages. An
|
|
103
|
+
* explicit `profile` config wins; otherwise the launched profile the Host
|
|
104
|
+
* publishes is authoritative, because the DSH Desktop client boots the
|
|
105
|
+
* `desktop` profile without exporting `DSH_PROFILE` — an environment-only
|
|
106
|
+
* fallback edited `profiles/web` there, so the toggle looked applied while
|
|
107
|
+
* the running host never followed the block.
|
|
108
|
+
* @param configured - the explicit `profile` plugin config, when set.
|
|
109
|
+
* @param launched - the launched profile name the Host published, when available.
|
|
110
|
+
* @param env - the DSH_PROFILE environment value, when set.
|
|
111
|
+
*/
|
|
112
|
+
export function resolveManagedProfile(
|
|
113
|
+
configured: string | undefined,
|
|
114
|
+
launched: string | undefined,
|
|
115
|
+
env: string | undefined,
|
|
116
|
+
): string {
|
|
117
|
+
return configured ?? launched ?? env ?? DEFAULT_MANAGED_PROFILE
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Whether the firewall rule must be (re)applied: only when the toggle or the
|
|
122
|
+
* bound port moved since the last application. Keeps unrelated settings
|
|
123
|
+
* saves from spawning netsh/firewall-cmd delete+add churn.
|
|
124
|
+
* @param previous - the state applied earlier by this process, if any.
|
|
125
|
+
* @param next - the state the settings now ask for, if derivable.
|
|
126
|
+
*/
|
|
127
|
+
export function firewallActionNeeded(
|
|
128
|
+
previous: AppliedFirewallState | undefined,
|
|
129
|
+
next: AppliedFirewallState | undefined,
|
|
130
|
+
): boolean {
|
|
131
|
+
if (next === undefined) return false
|
|
132
|
+
if (previous === undefined) return true
|
|
133
|
+
return previous.enabled !== next.enabled || previous.port !== next.port
|
|
134
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lan-bind block this plugin used to write, and its removal (issue #1917).
|
|
3
|
+
*
|
|
4
|
+
* Up to 0.4.8 the toggle pinned the LAN address into the profile's
|
|
5
|
+
* `cordis.patch.yml` as a managed `- id: webserver` row. That approach is
|
|
6
|
+
* retired for two measured reasons:
|
|
7
|
+
*
|
|
8
|
+
* 1. A same-id patch row REPLACES the whole row config, so the block silently
|
|
9
|
+
* swallowed the official row's own bind expression; the CLI `--host` /
|
|
10
|
+
* `--port` never reached the running row, and the machine's port came from
|
|
11
|
+
* a literal this plugin guessed.
|
|
12
|
+
* 2. The profile patch is exactly what the Host's HMR config watcher refreshes
|
|
13
|
+
* from, so writing it recomposed the running process and rebound the live
|
|
14
|
+
* listener - the "takes effect on the next start" premise was false, and a
|
|
15
|
+
* write made mid-session moved the bind under the running host.
|
|
16
|
+
*
|
|
17
|
+
* LAN reachability now comes from a runtime forwarder instead (lan-forward.ts),
|
|
18
|
+
* so the host always listens on loopback and this module's only remaining job
|
|
19
|
+
* is to remove the block older versions left behind. The removal is
|
|
20
|
+
* strip-only: no write appends anything, so the official webserver row (and
|
|
21
|
+
* every other row) stays byte-identical. The block discipline (markers, atomic
|
|
22
|
+
* write) follows the dsh-LAN reference implementation (MIT).
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs'
|
|
26
|
+
import { dirname, join, resolve, sep } from 'node:path'
|
|
27
|
+
import { dshHome } from './dsh-home.ts'
|
|
28
|
+
|
|
29
|
+
/** Profile names safe to interpolate into the patch path: one path segment, no traversal. */
|
|
30
|
+
const PROFILE_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/
|
|
31
|
+
|
|
32
|
+
export const LAN_BIND_BLOCK_BEGIN = '# --- remote-web-ui lan-bind block (managed - do not edit) ---'
|
|
33
|
+
export const LAN_BIND_BLOCK_END = '# --- end remote-web-ui lan-bind block ---'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A bind host the managed block may pin. `'0.0.0.0'` only on a host whose
|
|
37
|
+
* webserver schema still accepts the wildcard, `'127.0.0.1'` for the toggle
|
|
38
|
+
* off, and otherwise one concrete interface address of this machine (see
|
|
39
|
+
* lan-bind-plan.ts / host-bind-capability.ts, issue #1893).
|
|
40
|
+
*/
|
|
41
|
+
export type LanBindHost = string
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The absolute path of the profile patch file this toggle manages. The value
|
|
45
|
+
* is config-controlled (and the runtime/environment fallbacks bypass schema
|
|
46
|
+
* validation entirely), so the path is guarded twice: the profile must be a
|
|
47
|
+
* single safe path segment, and the resolved file must stay under the
|
|
48
|
+
* profiles directory.
|
|
49
|
+
*/
|
|
50
|
+
export function profilePatchFile(profile: string, home: string = dshHome()): string {
|
|
51
|
+
if (!PROFILE_PATTERN.test(profile)) {
|
|
52
|
+
throw new Error(`remote-bridge: unsafe lan-bind profile ${JSON.stringify(profile)}`)
|
|
53
|
+
}
|
|
54
|
+
const file = resolve(join(home, 'profiles', profile, 'cordis.patch.yml'))
|
|
55
|
+
const root = resolve(join(home, 'profiles')) + sep
|
|
56
|
+
if (!file.startsWith(root)) {
|
|
57
|
+
throw new Error(`remote-bridge: lan-bind profile ${JSON.stringify(profile)} escapes the profiles directory`)
|
|
58
|
+
}
|
|
59
|
+
return file
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function readPatchContent(file: string): string {
|
|
63
|
+
if (!existsSync(file)) return ''
|
|
64
|
+
return readFileSync(file, 'utf8')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** One top-level sequence item inside a managed-block region. */
|
|
68
|
+
interface BlockItem {
|
|
69
|
+
/** Offset of the item's first character inside the region. */
|
|
70
|
+
start: number
|
|
71
|
+
/** Offset just past the item's last character inside the region. */
|
|
72
|
+
end: number
|
|
73
|
+
/** The item's source text, line terminator included. */
|
|
74
|
+
text: string
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Split a block-sequence region into its top-level items. Only a column-0 `-`
|
|
79
|
+
* starts an item: that is the shape every writer of this file produces - the
|
|
80
|
+
* managed block itself and the YAML document API's block-style re-emission.
|
|
81
|
+
* @param region - text between two line boundaries.
|
|
82
|
+
* @returns the items in source order; text outside them is not returned.
|
|
83
|
+
*/
|
|
84
|
+
function blockItems(region: string): BlockItem[] {
|
|
85
|
+
const items: BlockItem[] = []
|
|
86
|
+
let start = -1
|
|
87
|
+
let offset = 0
|
|
88
|
+
for (const line of region.split('\n')) {
|
|
89
|
+
if (line.startsWith('- ') || line === '-') {
|
|
90
|
+
if (start !== -1) items.push({ start, end: offset, text: region.slice(start, offset) })
|
|
91
|
+
start = offset
|
|
92
|
+
}
|
|
93
|
+
offset += line.length + 1
|
|
94
|
+
}
|
|
95
|
+
if (start !== -1) items.push({ start, end: region.length, text: region.slice(start) })
|
|
96
|
+
return items
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Whether one top-level item is the webserver row this plugin's block owns.
|
|
101
|
+
* The row id is the whole signal: the markers declare everything inside them
|
|
102
|
+
* managed, our block always writes `- id: webserver`, and a same-id row later
|
|
103
|
+
* in the patch replaces this one wholesale anyway - so an id-webserver row
|
|
104
|
+
* found inside our markers is this plugin's row (a hand-truncated block may
|
|
105
|
+
* have lost its `name` line).
|
|
106
|
+
* @param item - one top-level item's text.
|
|
107
|
+
* @returns true when the item is the managed webserver row.
|
|
108
|
+
*/
|
|
109
|
+
function isManagedWebserverItem(item: string): boolean {
|
|
110
|
+
return /^- id:\s*['"]?webserver['"]?[ \t]*(?:\r?\n|$)/.test(item)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The text between a block's BEGIN and END markers: from the character after
|
|
115
|
+
* the BEGIN line's terminator to the start of the END marker's line (or to
|
|
116
|
+
* the end of the content without an END marker).
|
|
117
|
+
* @param content - the patch file text.
|
|
118
|
+
* @param begin - index of the BEGIN marker.
|
|
119
|
+
* @param end - index of the END marker, or -1 when the block is unterminated.
|
|
120
|
+
* @returns the region's text.
|
|
121
|
+
*/
|
|
122
|
+
function managedRegion(content: string, begin: number, end: number): string {
|
|
123
|
+
const newlineAfterBegin = content.indexOf('\n', begin)
|
|
124
|
+
const innerStart = newlineAfterBegin === -1 ? content.length : newlineAfterBegin + 1
|
|
125
|
+
const endLineStart = end === -1 ? content.length : content.lastIndexOf('\n', end - 1) + 1
|
|
126
|
+
return content.slice(innerStart, Math.max(innerStart, endLineStart))
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Remove the managed block (its markers and the webserver row this plugin
|
|
131
|
+
* wrote) from patch content, keeping every other row.
|
|
132
|
+
*
|
|
133
|
+
* The markers travel with the file through writers that append rows through
|
|
134
|
+
* the YAML document API: the appended rows land at the end of the sequence,
|
|
135
|
+
* which is BEFORE a trailing END comment, so the region between the markers
|
|
136
|
+
* can hold rows this plugin never wrote. Deleting the whole region silently
|
|
137
|
+
* removed the user's model, plugin and remote-control rows on the next rewrite
|
|
138
|
+
* - the remote-control row carries the LAN toggle itself, so the feature reset
|
|
139
|
+
* to `undefined` and the phone was locked out again after a restart (DSH
|
|
140
|
+
* Desktop report, 2026-09-30). Only the managed item is removed here; every
|
|
141
|
+
* foreign row is kept in place and the fresh block is appended after them (the
|
|
142
|
+
* same-id row last wins).
|
|
143
|
+
* @param content - the patch file text.
|
|
144
|
+
* @returns the text with the markers and the managed row removed.
|
|
145
|
+
*/
|
|
146
|
+
export function stripManagedBlock(content: string): string {
|
|
147
|
+
let text = content
|
|
148
|
+
for (;;) {
|
|
149
|
+
const begin = text.indexOf(LAN_BIND_BLOCK_BEGIN)
|
|
150
|
+
if (begin === -1) return text
|
|
151
|
+
const end = text.indexOf(LAN_BIND_BLOCK_END, begin)
|
|
152
|
+
const blockStart = text.lastIndexOf('\n', begin - 1) + 1
|
|
153
|
+
const newlineAfterEnd = end === -1 ? -1 : text.indexOf('\n', end)
|
|
154
|
+
const resumeAt = end === -1 ? text.length : (newlineAfterEnd === -1 ? text.length : newlineAfterEnd + 1)
|
|
155
|
+
const region = managedRegion(text, begin, end)
|
|
156
|
+
let kept = ''
|
|
157
|
+
let cursor = 0
|
|
158
|
+
for (const item of blockItems(region)) {
|
|
159
|
+
if (!isManagedWebserverItem(item.text)) continue
|
|
160
|
+
kept += region.slice(cursor, item.start)
|
|
161
|
+
cursor = item.end
|
|
162
|
+
}
|
|
163
|
+
kept += region.slice(cursor)
|
|
164
|
+
text = text.slice(0, blockStart) + kept + text.slice(resumeAt)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The bind state a managed block pinned (undefined when there is no block). */
|
|
169
|
+
export interface ManagedBindState {
|
|
170
|
+
host: LanBindHost | (string & {})
|
|
171
|
+
port: number | undefined
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Parse the block's pinned bind out of patch content. A block whose values
|
|
176
|
+
* were hand-edited reports the literals as-is so the card can surface them
|
|
177
|
+
* instead of silently claiming one of the two known states.
|
|
178
|
+
*/
|
|
179
|
+
export function managedBindOf(content: string): ManagedBindState | undefined {
|
|
180
|
+
const begin = content.indexOf(LAN_BIND_BLOCK_BEGIN)
|
|
181
|
+
if (begin === -1) return undefined
|
|
182
|
+
const end = content.indexOf(LAN_BIND_BLOCK_END, begin)
|
|
183
|
+
const region = managedRegion(content, begin, end)
|
|
184
|
+
// Read the bind out of the managed row itself. A region a YAML writer
|
|
185
|
+
// displaced holds foreign rows too, and their `host`/`port` keys are not
|
|
186
|
+
// this block's bind - reading them made the boot re-assert
|
|
187
|
+
// believe the block was already applied. A region without a recognizable
|
|
188
|
+
// managed row (hand-edited literals) keeps the historical whole-region read.
|
|
189
|
+
const items = blockItems(region).filter(item => isManagedWebserverItem(item.text))
|
|
190
|
+
const scope = items.length > 0 ? items.map(item => item.text).join('') : region
|
|
191
|
+
const hostMatch = /host:\s*'([^']+)'/.exec(scope)
|
|
192
|
+
const portMatch = /port:\s*(\d+)/.exec(scope)
|
|
193
|
+
return {
|
|
194
|
+
host: hostMatch?.[1] ?? '',
|
|
195
|
+
port: portMatch !== null ? Number(portMatch[1]) : undefined,
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** File-level state of a patch file a previous version wrote to. */
|
|
200
|
+
export function lanBindState(profile: string, home: string = dshHome()): { blockPresent: boolean; host?: string; port?: number } {
|
|
201
|
+
const state = managedBindOf(readPatchContent(profilePatchFile(profile, home)))
|
|
202
|
+
if (state === undefined) return { blockPresent: false }
|
|
203
|
+
return { blockPresent: true, host: state.host, port: state.port }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** What one legacy-block cleanup did, for the settings card and the log. */
|
|
207
|
+
export interface LegacyBlockCleanup {
|
|
208
|
+
/** Whether the file actually carried the managed block. */
|
|
209
|
+
removed: boolean
|
|
210
|
+
/** The bind the removed block pinned, when it could be read. */
|
|
211
|
+
host: string | undefined
|
|
212
|
+
/** The port the removed block pinned, when it could be read. */
|
|
213
|
+
port: number | undefined
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Remove the managed block a previous version wrote, and nothing else.
|
|
218
|
+
*
|
|
219
|
+
* The file is rewritten atomically (unique temp file + rename, so a concurrent
|
|
220
|
+
* reader never sees a half-written patch) with its original permissions
|
|
221
|
+
* preserved, and only when the block is actually there: a second run finds no
|
|
222
|
+
* markers, writes nothing, and leaves the bytes identical, which is what makes
|
|
223
|
+
* the plugin's once-per-boot call idempotent.
|
|
224
|
+
*
|
|
225
|
+
* The official `- id: webserver` row every profile carries is a FOREIGN row -
|
|
226
|
+
* the strip keeps it verbatim (see stripManagedBlock). Nothing is appended, so
|
|
227
|
+
* the profile keeps the row's own bind expression and the CLI flags keep
|
|
228
|
+
* reaching it.
|
|
229
|
+
*
|
|
230
|
+
* A failure (an unreadable file, a read-only profile) is NOT swallowed here:
|
|
231
|
+
* callers report it, because a silent failure leaves a block that keeps
|
|
232
|
+
* swallowing the bind on every later start.
|
|
233
|
+
* @param profile - the managed profile name.
|
|
234
|
+
* @param home - DSH home (test seam).
|
|
235
|
+
* @returns what was removed; `removed: false` when there was nothing to do.
|
|
236
|
+
*/
|
|
237
|
+
export function removeManagedBlock(profile: string, home: string = dshHome()): LegacyBlockCleanup {
|
|
238
|
+
const file = profilePatchFile(profile, home)
|
|
239
|
+
if (!existsSync(file)) return { removed: false, host: undefined, port: undefined }
|
|
240
|
+
const content = readFileSync(file, 'utf8')
|
|
241
|
+
const state = managedBindOf(content)
|
|
242
|
+
const stripped = stripManagedBlock(content)
|
|
243
|
+
if (stripped === content) return { removed: false, host: undefined, port: undefined }
|
|
244
|
+
const mode = statSync(file).mode & 0o777
|
|
245
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
246
|
+
const temp = `${file}.remote-web-ui-tmp-${process.pid.toString(36)}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`
|
|
247
|
+
writeFileSync(temp, stripped, { mode })
|
|
248
|
+
renameSync(temp, file)
|
|
249
|
+
return { removed: true, host: state?.host, port: state?.port }
|
|
250
|
+
}
|