dsh-ris-ide 1.7.2 → 1.8.0
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.md +43 -4
- package/bin/check-listing.mjs +247 -0
- package/bin/proxy-janitor.mjs +183 -0
- package/bin/risd.mjs +287 -0
- package/bin/run-with-log.ps1 +43 -5
- package/lib/client-codemirror.js +92 -66
- package/lib/client.js +774 -89
- package/lib/index.js +2256 -21
- package/lib/janitor.js +377 -0
- package/lib/proxy.js +1577 -0
- package/lib/proxybridge-bundle.js +798 -0
- package/lib/redirector.js +2558 -0
- package/lib/risd/build.js +91 -0
- package/lib/risd/debug.js +374 -0
- package/lib/risd/git.js +207 -0
- package/lib/risd/leases.js +124 -0
- package/lib/risd/lifecycle.js +148 -0
- package/lib/risd/log.js +59 -0
- package/lib/risd/ping.js +94 -0
- package/lib/risd/programs.js +348 -0
- package/lib/risd/server.js +1478 -0
- package/lib/risd/state.js +137 -0
- package/lib/risd/ui.js +1705 -0
- package/lib/risd/view.js +79 -0
- package/lib/risd-client.js +282 -0
- package/lib/standalone/window.html +14 -16
- package/lib/startup.js +114 -0
- package/package.json +7 -2
- package/skills/dsh-ris-ide-debug/SKILL.md +37 -2
- package/test/client-build.test.mjs +113 -0
- package/test/client-viewer.test.mjs +314 -20
- package/test/companion.test.mjs +65 -0
- package/test/dsh-origin.test.mjs +140 -0
- package/test/dsh-window-refresh.test.mjs +100 -0
- package/test/harness.test.mjs +9 -0
- package/test/host-noloop.test.mjs +74 -0
- package/test/host-tail.test.mjs +54 -5
- package/test/host-window.test.mjs +4 -4
- package/test/inherit-engine.test.mjs +175 -0
- package/test/install-phase.test.mjs +36 -0
- package/test/janitor.test.mjs +172 -0
- package/test/lifecycle.test.mjs +109 -0
- package/test/merge-owners.test.mjs +155 -0
- package/test/proxy-restart.test.mjs +37 -0
- package/test/proxy-route.test.mjs +599 -0
- package/test/proxy-window.test.mjs +45 -0
- package/test/proxy.test.mjs +914 -0
- package/test/proxybridge-bundle.test.mjs +687 -0
- package/test/redirector-bundle.test.mjs +391 -0
- package/test/redirector.test.mjs +571 -0
- package/test/risd-api.test.mjs +470 -0
- package/test/risd-bridge-degrade.test.mjs +101 -0
- package/test/risd-build.test.mjs +20 -0
- package/test/risd-cli-home.test.mjs +50 -0
- package/test/risd-client.test.mjs +130 -0
- package/test/risd-debug.test.mjs +261 -0
- package/test/risd-election.test.mjs +104 -0
- package/test/risd-engine.test.mjs +132 -0
- package/test/risd-entry.test.mjs +118 -0
- package/test/risd-git.test.mjs +109 -0
- package/test/risd-handoff.test.mjs +116 -0
- package/test/risd-heal.test.mjs +50 -0
- package/test/risd-helpers.mjs +108 -0
- package/test/risd-host-p2.test.mjs +147 -0
- package/test/risd-janitor-guard.test.mjs +46 -0
- package/test/risd-lease.test.mjs +83 -0
- package/test/risd-lifecycle.test.mjs +210 -0
- package/test/risd-log-follow.test.mjs +227 -0
- package/test/risd-mode.test.mjs +161 -0
- package/test/risd-ping.test.mjs +99 -0
- package/test/risd-programs.test.mjs +169 -0
- package/test/risd-review-p2.test.mjs +217 -0
- package/test/risd-shutdown.test.mjs +97 -0
- package/test/risd-teardown.test.mjs +87 -0
- package/test/risd-tools.test.mjs +211 -0
- package/test/risd-ui.test.mjs +445 -0
- package/test/risd-write.test.mjs +170 -0
- package/test/startup-reconcile.test.mjs +100 -0
- package/test/windows.test.mjs +37 -2
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redirector (ProxyBridge) adapter tests.
|
|
3
|
+
*
|
|
4
|
+
* The interesting half is the mapping: our rule table is program + hosts + ports,
|
|
5
|
+
* and it has to come out as a `.pbprofile` ProxyBridge accepts — a wrong process
|
|
6
|
+
* pattern silently routes nothing, so each shape is pinned here.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import assert from 'node:assert/strict'
|
|
10
|
+
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync, mkdirSync } from 'node:fs'
|
|
11
|
+
import { tmpdir } from 'node:os'
|
|
12
|
+
import { join } from 'node:path'
|
|
13
|
+
import test from 'node:test'
|
|
14
|
+
|
|
15
|
+
const home = mkdtempSync(join(tmpdir(), 'ris-redirector-home-'))
|
|
16
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
17
|
+
|
|
18
|
+
const { __test } = await import('../lib/redirector.js')
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Every probe that could reach the real Windows side is stubbed for the whole
|
|
22
|
+
* file. A test run must never enumerate, kill or elevate the user's actual
|
|
23
|
+
* ProxyBridge_CLI — it is running next to us, and "clean up" would take it down.
|
|
24
|
+
*/
|
|
25
|
+
const BASE_HOOKS = {
|
|
26
|
+
windowsPids: () => [],
|
|
27
|
+
localProc: () => 0,
|
|
28
|
+
windowsPidsAsync: async () => [],
|
|
29
|
+
windowsAlive: () => false,
|
|
30
|
+
windowsAliveAsync: async () => false,
|
|
31
|
+
taskkill: () => 'ERROR: Access is denied.',
|
|
32
|
+
}
|
|
33
|
+
__test.setProbeHooks(BASE_HOOKS)
|
|
34
|
+
process.on('exit', () => __test.resetProbeHooks())
|
|
35
|
+
/** Use one extra stub for a single test, then fall back to the safe defaults. */
|
|
36
|
+
const useHooks = (hooks) => {
|
|
37
|
+
__test.resetProbeHooks()
|
|
38
|
+
__test.setProbeHooks({ ...BASE_HOOKS, ...hooks })
|
|
39
|
+
__test.clearProbeCaches()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
const config = (rules, extra = {}) => ({
|
|
44
|
+
socks: { host: '10.3.91.121', port: 1080 },
|
|
45
|
+
listenPort: 11081,
|
|
46
|
+
rules,
|
|
47
|
+
...extra,
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('a Windows command line never carries a WSL-only /mnt path', async () => {
|
|
51
|
+
const { windowsPathOf, redirectorCommand } = await import('../lib/redirector.js')
|
|
52
|
+
// Fabricated drive so the assertion does not depend on this machine's layout:
|
|
53
|
+
// wslpath cannot resolve it, which also exercises the drive-letter fallback.
|
|
54
|
+
const exe = windowsPathOf('/mnt/x/Program Files/ProxyBridge/ProxyBridge_CLI.exe')
|
|
55
|
+
assert.match(exe, /^X:\\Program Files\\ProxyBridge\\ProxyBridge_CLI\.exe$/,
|
|
56
|
+
'Start-Process cannot resolve /mnt paths: they are WSL-only')
|
|
57
|
+
const already = 'D:\\apps\\ProxyBridge_CLI.exe'
|
|
58
|
+
assert.equal(windowsPathOf(already), already, 'an already-Windows path is left alone')
|
|
59
|
+
// Only arguments that are existing files are treated as paths (a `/d` or
|
|
60
|
+
// `/p:key=value` switch is not), so the profile has to exist here — exactly the
|
|
61
|
+
// situation at runtime, where writeRedirectorProfile just wrote it.
|
|
62
|
+
const profileDir = mkdtempSync(join(tmpdir(), 'ris-ide-plan-'))
|
|
63
|
+
const profile = join(profileDir, 'dsh-ris-ide.pbprofile')
|
|
64
|
+
writeFileSync(profile, '{}')
|
|
65
|
+
const plan = redirectorCommand({
|
|
66
|
+
cliPath: '/mnt/x/Program Files/ProxyBridge/ProxyBridge_CLI.exe',
|
|
67
|
+
profilePath: profile,
|
|
68
|
+
platform: 'win32',
|
|
69
|
+
})
|
|
70
|
+
assert.equal(plan.display.includes('/mnt/'), false, 'no mnt path may survive in the Windows command')
|
|
71
|
+
assert.ok(plan.display.includes('X:\\Program Files'), 'the executable must be spelled the Windows way')
|
|
72
|
+
const linux = redirectorCommand({ cliPath: '/mnt/x/vendor/ProxyBridge', profilePath: '/mnt/x/p.pbprofile', platform: 'linux' })
|
|
73
|
+
assert.equal(linux.command, 'sudo', 'the Linux side stays a sudo command')
|
|
74
|
+
assert.ok(linux.display.includes('/mnt/x/p.pbprofile'), 'Linux keeps its own paths')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('rule table becomes ProxyBridge rules', () => {
|
|
78
|
+
const { profile, rules } = __test.buildProfile(config([
|
|
79
|
+
{ id: 'java', program: 'java', hosts: '172.20.235.*', ports: '1521, 6890', enabled: true, workspace: '/ws' },
|
|
80
|
+
]))
|
|
81
|
+
assert.equal(profile.Version, '1.0')
|
|
82
|
+
assert.equal(profile.ProxyConfigs.length, 1)
|
|
83
|
+
assert.equal(profile.ProxyConfigs[0].Type, 'SOCKS5')
|
|
84
|
+
assert.equal(profile.ProxyConfigs[0].Host, '10.3.91.121')
|
|
85
|
+
assert.equal(profile.ProxyConfigs[0].SendDomainToProxy, true, 'let the proxy resolve names')
|
|
86
|
+
assert.equal(rules.length, 1)
|
|
87
|
+
// Bare names must match the image name on both sides: java.exe on Windows, java on Linux.
|
|
88
|
+
assert.equal(rules[0].ProcessName, '*java*')
|
|
89
|
+
assert.equal(rules[0].TargetHosts, '172.20.235.*')
|
|
90
|
+
assert.equal(rules[0].TargetPorts, '1521,6890', 'whitespace is removed: the CLI parses a bare list')
|
|
91
|
+
assert.equal(rules[0].Action, 'PROXY')
|
|
92
|
+
// The GUI's own export omits the key and ProxyBridge defaults it to false, so we
|
|
93
|
+
// do not invent it; what matters is that nothing here routes loopback traffic.
|
|
94
|
+
assert.equal('LocalhostViaProxy' in rules[0], false)
|
|
95
|
+
assert.equal('TargetDomains' in rules[0], false, 'a "*" domain would drop bare-IP targets')
|
|
96
|
+
assert.equal(rules[0].ProxyConfigId, profile.ProxyConfigs[0].Id)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('a blank axis is NOT "anything": the rule is skipped until it is filled', () => {
|
|
100
|
+
// A blank host/port used to be coerced to `*`, which silently proxied the whole
|
|
101
|
+
// internet for a half-written row. "Everything" must be typed as `*`; a blank
|
|
102
|
+
// axis means the rule does not apply at all.
|
|
103
|
+
const blank = __test.buildProfile(config([
|
|
104
|
+
{ id: 'git', program: 'git', hosts: '', ports: '', enabled: true },
|
|
105
|
+
]))
|
|
106
|
+
assert.deepEqual(blank.rules, [])
|
|
107
|
+
assert.match(blank.skipped.join(' '), /缺 IP/)
|
|
108
|
+
|
|
109
|
+
const explicit = __test.buildProfile(config([
|
|
110
|
+
{ id: 'git', program: 'git', hosts: '*', ports: '*', enabled: true },
|
|
111
|
+
]))
|
|
112
|
+
assert.equal(explicit.rules[0].TargetHosts, '*')
|
|
113
|
+
assert.equal(explicit.rules[0].TargetPorts, '*')
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('a path-shaped program is pinned to its directory', () => {
|
|
117
|
+
assert.equal(__test.processPattern('navicat.exe'), 'navicat.exe')
|
|
118
|
+
assert.equal(__test.processPattern('D:\\Program Files\\YonCode\\devkit\\jdk21\\bin\\java.exe'),
|
|
119
|
+
'D:\\Program Files\\YonCode\\devkit\\jdk21\\bin\\*.exe')
|
|
120
|
+
assert.equal(__test.processPattern('/opt/jdk/bin/java'), '/opt/jdk/bin/*')
|
|
121
|
+
assert.equal(__test.processPattern(''), '')
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('disabled and program-less rules are skipped, not silently emptied', () => {
|
|
125
|
+
const { rules, skipped } = __test.buildProfile(config([
|
|
126
|
+
{ id: 'off', program: 'java', hosts: '1.2.3.4', ports: '443', enabled: false },
|
|
127
|
+
{ id: 'empty', program: '', hosts: '1.2.3.4', ports: '443', enabled: true },
|
|
128
|
+
{ id: 'on', program: 'curl', hosts: '1.2.3.4', ports: '443', enabled: true },
|
|
129
|
+
]))
|
|
130
|
+
assert.deepEqual(rules.map((rule) => rule.Name), ['on'])
|
|
131
|
+
assert.equal(skipped.length, 2)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
test('the engine can be the upstream instead of the SOCKS server', () => {
|
|
135
|
+
const { profile } = __test.buildProfile(config([
|
|
136
|
+
{ id: 'java', program: 'java', hosts: '1.2.3.4', ports: '443', enabled: true },
|
|
137
|
+
]), { upstream: 'engine' })
|
|
138
|
+
assert.equal(profile.ProxyConfigs[0].Type, 'HTTP')
|
|
139
|
+
assert.equal(profile.ProxyConfigs[0].Host, '127.0.0.1')
|
|
140
|
+
assert.equal(profile.ProxyConfigs[0].Port, '11081')
|
|
141
|
+
assert.equal(profile.ProxyConfigs[0].SendDomainToProxy, false)
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
test('the profile is written private, and an empty one is refused', () => {
|
|
145
|
+
const path = join(home, 'proxybridge', 'custom.pbprofile')
|
|
146
|
+
const written = __test.writeRedirectorProfile(config([
|
|
147
|
+
{ id: 'java', program: 'java', hosts: '1.2.3.4', ports: '443', enabled: true },
|
|
148
|
+
]), { path })
|
|
149
|
+
assert.equal(written.rules, 1)
|
|
150
|
+
assert.equal(statSync(path).mode & 0o777, 0o600, 'may hold a proxy password')
|
|
151
|
+
assert.equal(JSON.parse(readFileSync(path, 'utf8')).ProxyRules.length, 1)
|
|
152
|
+
assert.throws(() => __test.writeRedirectorProfile(config([
|
|
153
|
+
{ id: 'off', program: 'java', hosts: '1.2.3.4', ports: '443', enabled: false },
|
|
154
|
+
]), { path: join(home, 'proxybridge', 'empty.pbprofile') }), /至少一条/)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('the start command carries the elevation requirement of each platform', () => {
|
|
158
|
+
const linux = __test.redirectorCommand({ cliPath: '/x/ProxyBridge', profilePath: '/y/p.pbprofile', platform: 'linux' })
|
|
159
|
+
assert.equal(linux.command, 'sudo')
|
|
160
|
+
assert.deepEqual(linux.args.slice(0, 2), ['/x/ProxyBridge', '--profile'])
|
|
161
|
+
assert.equal(linux.needsElevation, true)
|
|
162
|
+
const windows = __test.redirectorCommand({ cliPath: 'C:\\pb\\ProxyBridge_CLI.exe', profilePath: 'C:\\p\\p.pbprofile', platform: 'win32' })
|
|
163
|
+
assert.equal(windows.command, 'C:\\pb\\ProxyBridge_CLI.exe', 'Windows elevates the launch, not the command')
|
|
164
|
+
assert.equal(windows.needsElevation, true)
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('detection reports the two sides independently', () => {
|
|
168
|
+
// The distribution is downloaded into ~/.dsh-ris-ide/proxybridge/bundle; when it
|
|
169
|
+
// is not installed yet a machine-wide (manually extracted / packaged) copy is
|
|
170
|
+
// still honoured, which is what this test pins.
|
|
171
|
+
const vendor = __test.redirectorVendorDir({ platform: 'linux' })
|
|
172
|
+
mkdirSync(vendor, { recursive: true })
|
|
173
|
+
const fake = join(vendor, 'ProxyBridge')
|
|
174
|
+
writeFileSync(fake, '#!/bin/sh\necho "ProxyBridge CLI V9.9.9"\n', { encoding: 'utf8' })
|
|
175
|
+
chmodSync(fake, 0o755)
|
|
176
|
+
try {
|
|
177
|
+
__test.clearProbeCaches()
|
|
178
|
+
const status = __test.redirectorStatus()
|
|
179
|
+
assert.equal(status.local.available, true)
|
|
180
|
+
assert.equal(status.local.kind, 'linux-cli')
|
|
181
|
+
assert.equal(status.local.path, fake, 'the machine-wide fallback is used when no download exists')
|
|
182
|
+
assert.match(String(status.local.version), /^\d+\.\d+\.\d+$/)
|
|
183
|
+
// Windows side depends on the machine, so only its shape is checked.
|
|
184
|
+
assert.equal(typeof status.windows.available, 'boolean')
|
|
185
|
+
assert.equal(typeof status.windows.path, 'string')
|
|
186
|
+
} finally {
|
|
187
|
+
rmSync(fake, { force: true })
|
|
188
|
+
}
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
test('an explicit override wins over every search path', () => {
|
|
192
|
+
const dir = mkdtempSync(join(tmpdir(), 'ris-redirector-cli-'))
|
|
193
|
+
const cli = join(dir, 'ProxyBridge')
|
|
194
|
+
writeFileSync(cli, '#!/bin/sh\necho "x V1.2.3"\n', { encoding: 'utf8' })
|
|
195
|
+
chmodSync(cli, 0o755)
|
|
196
|
+
process.env.DSH_RIS_IDE_PROXYBRIDGE = cli
|
|
197
|
+
try {
|
|
198
|
+
const found = __test.findRedirector({ side: 'local' })
|
|
199
|
+
assert.equal(found.found, true)
|
|
200
|
+
assert.equal(found.path, cli)
|
|
201
|
+
} finally {
|
|
202
|
+
delete process.env.DSH_RIS_IDE_PROXYBRIDGE
|
|
203
|
+
rmSync(dir, { recursive: true, force: true })
|
|
204
|
+
}
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
test('the pid file records which side owns the process', () => {
|
|
208
|
+
// A Windows redirector cannot be signalled from WSL: stop goes through
|
|
209
|
+
// taskkill, so the record has to say 'windows' or the wrong killer is used.
|
|
210
|
+
__test.writeRedirectorPid(4321, 'windows')
|
|
211
|
+
assert.deepEqual(__test.redirectorPidRecord(), { pid: 4321, side: 'windows' })
|
|
212
|
+
__test.writeRedirectorPid(4321)
|
|
213
|
+
assert.deepEqual(__test.redirectorPidRecord(), { pid: 4321, side: 'local' })
|
|
214
|
+
__test.clearRedirectorPid()
|
|
215
|
+
assert.deepEqual(__test.redirectorPidRecord(), { pid: 0, side: 'local' })
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test('stopping a redirector that is not running is a no-op, not a throw', () => {
|
|
219
|
+
__test.clearRedirectorPid()
|
|
220
|
+
assert.deepEqual(__test.stopRedirectorSync(), { stopped: false, reason: '未在运行' })
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
test('a missing override falls back to the machine-wide copy', () => {
|
|
224
|
+
// The explicit override is only a preference: a stale path must not hide a real
|
|
225
|
+
// installation (the downloaded cache replaces the old in-repo vendor folder).
|
|
226
|
+
const vendor = __test.redirectorVendorDir({ platform: 'linux' })
|
|
227
|
+
mkdirSync(vendor, { recursive: true })
|
|
228
|
+
const fake = join(vendor, 'ProxyBridge')
|
|
229
|
+
writeFileSync(fake, '#!/bin/sh\necho "x V1.0.0"\n', { encoding: 'utf8' })
|
|
230
|
+
chmodSync(fake, 0o755)
|
|
231
|
+
const real = process.env.DSH_RIS_IDE_PROXYBRIDGE
|
|
232
|
+
process.env.DSH_RIS_IDE_PROXYBRIDGE = join(home, 'proxybridge', 'vendor', 'linux', 'missing-ProxyBridge')
|
|
233
|
+
try {
|
|
234
|
+
__test.clearProbeCaches()
|
|
235
|
+
const found = __test.findRedirector({ side: 'local' })
|
|
236
|
+
assert.equal(found.found, true)
|
|
237
|
+
assert.equal(found.path, fake)
|
|
238
|
+
} finally {
|
|
239
|
+
if (real === undefined) delete process.env.DSH_RIS_IDE_PROXYBRIDGE
|
|
240
|
+
else process.env.DSH_RIS_IDE_PROXYBRIDGE = real
|
|
241
|
+
rmSync(fake, { force: true })
|
|
242
|
+
}
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
test('discovery separates the pid file from a hand-started process', () => {
|
|
246
|
+
// The user pastes the sudo command, so nothing writes our pid file; the /proc
|
|
247
|
+
// scan is what stops the dialog from claiming "not running".
|
|
248
|
+
__test.clearRedirectorPid()
|
|
249
|
+
useHooks({})
|
|
250
|
+
const found = __test.detectRunningRedirector()
|
|
251
|
+
// per side + the aggregate: one pid cannot describe two machines (M1)
|
|
252
|
+
assert.deepEqual(Object.keys(found).sort(), ['local', 'pid', 'side', 'source', 'windows'])
|
|
253
|
+
assert.ok(['', 'pidfile', 'proc', 'process'].includes(found.source), `unexpected source: ${found.source}`)
|
|
254
|
+
assert.equal(typeof found.local.pid, 'number')
|
|
255
|
+
assert.equal(typeof found.windows.pid, 'number')
|
|
256
|
+
const status = __test.redirectorStatus()
|
|
257
|
+
assert.equal(typeof status.runningSource, 'string', 'the view says HOW it was found')
|
|
258
|
+
assert.equal(status.running, found.pid > 0)
|
|
259
|
+
assert.equal(typeof status.runningWindows, 'boolean', 'the view separates the two sides')
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
test('a pid file that names a dead process is not "running"', () => {
|
|
263
|
+
const dir = join(home, 'proxybridge')
|
|
264
|
+
mkdirSync(dir, { recursive: true })
|
|
265
|
+
__test.writeRedirectorPid(999999)
|
|
266
|
+
assert.equal(__test.redirectorPid(), 0)
|
|
267
|
+
__test.clearRedirectorPid()
|
|
268
|
+
assert.equal(existsSync(join(dir, 'proxybridge.local.pid')), false)
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
test('each side keeps its own pid record (M1)', () => {
|
|
272
|
+
useHooks({})
|
|
273
|
+
__test.clearRedirectorPid()
|
|
274
|
+
__test.writeRedirectorPid(100, 'local')
|
|
275
|
+
__test.writeRedirectorPid(200, 'windows')
|
|
276
|
+
// One shared file made the second start erase the first side's pid: the engine
|
|
277
|
+
// then ran on with nothing pointing at it.
|
|
278
|
+
assert.deepEqual(__test.redirectorPidRecord('local'), { pid: 100, side: 'local' })
|
|
279
|
+
assert.deepEqual(__test.redirectorPidRecord('windows'), { pid: 200, side: 'windows' })
|
|
280
|
+
assert.equal(existsSync(__test.redirectorSidePidPath('local')), true)
|
|
281
|
+
assert.equal(existsSync(__test.redirectorSidePidPath('windows')), true)
|
|
282
|
+
__test.clearRedirectorPid('windows')
|
|
283
|
+
assert.deepEqual(__test.redirectorPidRecord('windows'), { pid: 0, side: 'windows' })
|
|
284
|
+
assert.deepEqual(__test.redirectorPidRecord('local'), { pid: 100, side: 'local' })
|
|
285
|
+
__test.clearRedirectorPid()
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
test('the old single pid file is migrated to the side it names (M1)', () => {
|
|
289
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
290
|
+
const dir = join(home, 'proxybridge')
|
|
291
|
+
mkdirSync(dir, { recursive: true })
|
|
292
|
+
__test.clearRedirectorPid()
|
|
293
|
+
writeFileSync(join(dir, 'proxybridge.pid'), '777 windows\n')
|
|
294
|
+
useHooks({})
|
|
295
|
+
__test.detectRunningRedirector()
|
|
296
|
+
assert.equal(existsSync(join(dir, 'proxybridge.windows.pid')), true, 'the side is kept, not defaulted to local')
|
|
297
|
+
assert.equal(existsSync(join(dir, 'proxybridge.pid')), false, 'one record, not two that can disagree')
|
|
298
|
+
__test.clearRedirectorPid()
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
test('the parked-service state has ONE path shared by writer and watchdog (B2)', async () => {
|
|
302
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
303
|
+
const mod = await import('../lib/redirector.js')
|
|
304
|
+
const canonical = __test.serviceStatePath()
|
|
305
|
+
assert.equal(canonical, join(__test.redirectorHome(), 'proxybridge-service.json'))
|
|
306
|
+
const legacy = join(home, 'proxybridge-service.json')
|
|
307
|
+
mkdirSync(__test.redirectorHome(), { recursive: true })
|
|
308
|
+
rmSync(canonical, { force: true })
|
|
309
|
+
writeFileSync(legacy, JSON.stringify({ stoppedByUs: true, originalStartType: 'Automatic' }))
|
|
310
|
+
__test.migrateLegacyServiceState()
|
|
311
|
+
assert.equal(existsSync(canonical), true, 'the watchdog reads the canonical path')
|
|
312
|
+
assert.equal(existsSync(legacy), false, 'leaving the old copy behind makes two readers disagree')
|
|
313
|
+
// The teardown must read exactly the file we write, or Set-Service/Start-Service
|
|
314
|
+
// never runs and ProxyBridgeSvc stays parked on Manual forever.
|
|
315
|
+
mod.writeWatchdogScript()
|
|
316
|
+
const script = readFileSync(join(home, 'proxybridge-watchdog.ps1'), 'utf8')
|
|
317
|
+
assert.ok(script.includes(mod.windowsPathOf(canonical)), 'watchdog state file != writer state file')
|
|
318
|
+
rmSync(canonical, { force: true })
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
test('a reload reports success only when the new process is proven (B4)', async () => {
|
|
322
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
323
|
+
__test.writeWatchdogPid(process.pid)
|
|
324
|
+
try {
|
|
325
|
+
// The watchdog says ok but the pid it names is not alive: that is a failure.
|
|
326
|
+
useHooks({
|
|
327
|
+
requestReload: ({ id }) => {
|
|
328
|
+
writeFileSync(__test.reloadDonePath(), JSON.stringify({ ok: true, id, pid: 424242, count: 1 }))
|
|
329
|
+
return true
|
|
330
|
+
},
|
|
331
|
+
})
|
|
332
|
+
const dead = await __test.reloadRedirector({ side: 'windows', timeoutMs: 1500 })
|
|
333
|
+
assert.equal(dead.reloaded, false)
|
|
334
|
+
assert.match(dead.reason, /已不在运行/)
|
|
335
|
+
|
|
336
|
+
// A structured done naming a live new process: success, and the NEW pid is
|
|
337
|
+
// recorded (M2) — keeping the old one made stop aim at a dead process.
|
|
338
|
+
const mod = await import('../lib/redirector.js')
|
|
339
|
+
mod.writeCliSpec({ exe: 'C:\\pb\\ProxyBridge_CLI.exe', args: ['--profile', 'C:\\p.pbprofile'], log: 'C:\\log.txt' })
|
|
340
|
+
useHooks({
|
|
341
|
+
windowsAlive: (pid) => pid === 3001,
|
|
342
|
+
windowsAliveAsync: async (pid) => pid === 3001,
|
|
343
|
+
requestReload: ({ id }) => {
|
|
344
|
+
writeFileSync(__test.reloadDonePath(), JSON.stringify({ ok: true, id, pid: 3001, count: 1 }))
|
|
345
|
+
return true
|
|
346
|
+
},
|
|
347
|
+
})
|
|
348
|
+
const ok = await __test.reloadRedirector({ side: 'windows', timeoutMs: 1500 })
|
|
349
|
+
assert.equal(ok.reloaded, true)
|
|
350
|
+
assert.equal(ok.pid, 3001)
|
|
351
|
+
assert.deepEqual(__test.redirectorPidRecord('windows'), { pid: 3001, side: 'windows' })
|
|
352
|
+
|
|
353
|
+
// A failed watchdog report is surfaced instead of the old unconditional "done".
|
|
354
|
+
useHooks({
|
|
355
|
+
requestReload: ({ id }) => {
|
|
356
|
+
writeFileSync(__test.reloadFailedPath(), JSON.stringify({ ok: false, id, pid: 0, error: 'no cli spec' }))
|
|
357
|
+
return true
|
|
358
|
+
},
|
|
359
|
+
})
|
|
360
|
+
const failed = await __test.reloadRedirector({ side: 'windows', timeoutMs: 1500 })
|
|
361
|
+
assert.equal(failed.reloaded, false)
|
|
362
|
+
assert.match(failed.reason, /重载失败.*no cli spec/)
|
|
363
|
+
|
|
364
|
+
// Silence is not success either.
|
|
365
|
+
useHooks({ requestReload: () => true })
|
|
366
|
+
const timeout = await __test.reloadRedirector({ side: 'windows', timeoutMs: 400 })
|
|
367
|
+
assert.equal(timeout.reloaded, false)
|
|
368
|
+
assert.match(timeout.reason, /未在 \d+s 内完成重载/)
|
|
369
|
+
} finally {
|
|
370
|
+
useHooks({})
|
|
371
|
+
}
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
test('stopping something we did not record reports it instead of "未在运行" (B5)', () => {
|
|
375
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
376
|
+
__test.clearRedirectorPid()
|
|
377
|
+
// A hand-started CLI is visible on the Windows side but has no pid file; the old
|
|
378
|
+
// stop killed nothing and still answered stopped:true, so the user believed the
|
|
379
|
+
// interception was gone.
|
|
380
|
+
useHooks({ windowsPids: () => [5555], windowsAlive: (pid) => pid === 5555 })
|
|
381
|
+
try {
|
|
382
|
+
const result = __test.stopRedirectorSync({ graceMs: 100 })
|
|
383
|
+
assert.equal(result.stopped, false, 'taskkill failed: the process is still there')
|
|
384
|
+
assert.match(String(result.reason), /检测到进程但不在记录中|管理员权限/)
|
|
385
|
+
} finally {
|
|
386
|
+
useHooks({})
|
|
387
|
+
__test.clearRedirectorPid()
|
|
388
|
+
}
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
test('the elevated helper rejects output from a previous run (B1)', async () => {
|
|
392
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
393
|
+
const localPath = join(home, 'dsh-ris-elevated.out')
|
|
394
|
+
writeFileSync(localPath, '@RUN pid=6432;alive=True\n')
|
|
395
|
+
let presentBeforeLaunch = null
|
|
396
|
+
// The stale file belongs to a previous run; it must be gone before the launch,
|
|
397
|
+
// and if a late writer brings it back the nonce must reject it.
|
|
398
|
+
const stale = await __test.psElevated('Write-Output hi', 700, {
|
|
399
|
+
localPath,
|
|
400
|
+
windowsPath: 'C:\\stale.out',
|
|
401
|
+
nonce: 'fresh',
|
|
402
|
+
pollMs: 50,
|
|
403
|
+
launch: () => {
|
|
404
|
+
presentBeforeLaunch = existsSync(localPath)
|
|
405
|
+
writeFileSync(localPath, '@RUN pid=6432;alive=True\npid=6432;alive=True\n')
|
|
406
|
+
return ''
|
|
407
|
+
},
|
|
408
|
+
})
|
|
409
|
+
assert.equal(presentBeforeLaunch, false, 'a leftover answer must never be read as this run')
|
|
410
|
+
assert.equal(stale.ok, false)
|
|
411
|
+
assert.match(stale.reason, /上一轮/)
|
|
412
|
+
|
|
413
|
+
const fresh = await __test.psElevated('Write-Output hi', 700, {
|
|
414
|
+
localPath,
|
|
415
|
+
windowsPath: 'C:\\fresh.out',
|
|
416
|
+
nonce: 'good',
|
|
417
|
+
pollMs: 50,
|
|
418
|
+
launch: () => {
|
|
419
|
+
writeFileSync(localPath, '@RUN good\npid=12;alive=True\n')
|
|
420
|
+
return ''
|
|
421
|
+
},
|
|
422
|
+
})
|
|
423
|
+
assert.equal(fresh.ok, true)
|
|
424
|
+
assert.match(fresh.output, /pid=12/)
|
|
425
|
+
})
|
|
426
|
+
|
|
427
|
+
test('status probes are cached and the async variant never blocks (M3)', async () => {
|
|
428
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
429
|
+
let syncCalls = 0
|
|
430
|
+
useHooks({ windowsPids: () => { syncCalls += 1; return [] } })
|
|
431
|
+
try {
|
|
432
|
+
__test.redirectorStatus()
|
|
433
|
+
__test.redirectorStatus()
|
|
434
|
+
assert.equal(syncCalls, 1, 'two renders inside the TTL must not probe twice')
|
|
435
|
+
__test.clearProbeCaches()
|
|
436
|
+
__test.redirectorStatus()
|
|
437
|
+
assert.equal(syncCalls, 2, 'a cleared cache probes again')
|
|
438
|
+
// The async route must never touch the blocking probe.
|
|
439
|
+
useHooks({
|
|
440
|
+
windowsPids: () => { throw new Error('sync probe used by the async path') },
|
|
441
|
+
windowsPidsAsync: async () => [],
|
|
442
|
+
})
|
|
443
|
+
const first = await __test.redirectorStatusAsync()
|
|
444
|
+
assert.equal(first.cached, false)
|
|
445
|
+
const second = await __test.redirectorStatusAsync()
|
|
446
|
+
assert.equal(second.cached, true, 'the second call inside the TTL is a cache hit')
|
|
447
|
+
} finally {
|
|
448
|
+
useHooks({})
|
|
449
|
+
}
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
test('reloadRedirectorsWithProfile writes the profile before reloading (B3 helper)', async () => {
|
|
453
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
454
|
+
useHooks({})
|
|
455
|
+
try {
|
|
456
|
+
const refused = await __test.reloadRedirectorsWithProfile(home, {
|
|
457
|
+
sides: ['windows', 'local'],
|
|
458
|
+
})
|
|
459
|
+
assert.equal(refused.ok, false)
|
|
460
|
+
assert.match(String(refused.reason), /规则未能写入/)
|
|
461
|
+
assert.equal(refused.written, null)
|
|
462
|
+
|
|
463
|
+
writeFileSync(join(home, 'proxy.json'), JSON.stringify(config([
|
|
464
|
+
{ id: 'java', program: 'java', hosts: '10.0.0.1', ports: '443', enabled: true },
|
|
465
|
+
])))
|
|
466
|
+
const idle = await __test.reloadRedirectorsWithProfile(home, { sides: ['windows', 'local'] })
|
|
467
|
+
assert.equal(idle.written.rules, 1)
|
|
468
|
+
// Nothing is running here: the helper says why instead of claiming a restart.
|
|
469
|
+
assert.equal(idle.ok, false)
|
|
470
|
+
assert.deepEqual(idle.skipped.map((item) => item.side).sort(), ['local', 'windows'])
|
|
471
|
+
} finally {
|
|
472
|
+
rmSync(join(home, 'proxy.json'), { force: true })
|
|
473
|
+
useHooks({})
|
|
474
|
+
}
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
test('the profile names a rule by its label so the ProxyBridge console stays readable', () => {
|
|
478
|
+
const labelled = __test.buildProfile(config([
|
|
479
|
+
{ id: 'yds-yoncode-java', label: 'YonCode java', program: 'java', hosts: '172.20.235.*', ports: '*', enabled: true },
|
|
480
|
+
]))
|
|
481
|
+
assert.equal(labelled.profile.ProxyRules[0].Name, 'YonCode java')
|
|
482
|
+
const plain = __test.buildProfile(config([
|
|
483
|
+
{ id: 'yds-java', program: 'java', hosts: '10.0.0.*', ports: '*', enabled: true },
|
|
484
|
+
]))
|
|
485
|
+
assert.equal(plain.profile.ProxyRules[0].Name, 'yds-java')
|
|
486
|
+
})
|
|
487
|
+
|
|
488
|
+
test('a stale stop marker never answers the next stop request (B-P2-7)', async () => {
|
|
489
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
490
|
+
const mod = await import('../lib/redirector.js')
|
|
491
|
+
// The watchdog wrote "done" for an earlier stop; nothing has been asked for the
|
|
492
|
+
// new one yet, so a plain "does the file exist" read would report success while
|
|
493
|
+
// the CLI is still intercepting.
|
|
494
|
+
writeFileSync(mod.stoppedMarkerPath(), 'done\n')
|
|
495
|
+
assert.equal(mod.stopCompleted(), true)
|
|
496
|
+
assert.equal(mod.requestRedirectorStop(), true)
|
|
497
|
+
assert.equal(mod.stopCompleted(), false, 'the request cleared the previous answer')
|
|
498
|
+
assert.equal(existsSync(mod.stopRequestPath()), true)
|
|
499
|
+
mod.clearStopSignals()
|
|
500
|
+
})
|
|
501
|
+
|
|
502
|
+
test('stopping a hand-started bridge is recorded instead of silently killed (B-P2-10)', () => {
|
|
503
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
504
|
+
__test.clearRedirectorPid()
|
|
505
|
+
useHooks({ windowsPids: () => [5555], windowsAlive: () => false, taskkill: () => 'SUCCESS' })
|
|
506
|
+
try {
|
|
507
|
+
const result = __test.stopRedirectorSync({ graceMs: 100 })
|
|
508
|
+
assert.equal(result.stopped, true)
|
|
509
|
+
assert.equal(result.foreign, true, 'the pid came from detection, not from our pid file')
|
|
510
|
+
const log = readFileSync(join(home, 'proxy-forward.log'), 'utf8')
|
|
511
|
+
assert.match(log, /非本工具记录的桥接 pid=5555/)
|
|
512
|
+
} finally {
|
|
513
|
+
useHooks({})
|
|
514
|
+
__test.clearRedirectorPid()
|
|
515
|
+
rmSync(join(home, 'proxy-forward.log'), { force: true })
|
|
516
|
+
}
|
|
517
|
+
})
|
|
518
|
+
|
|
519
|
+
test('the heartbeat ages out and the stop signals round-trip', async () => {
|
|
520
|
+
const tmpHome = mkdtempSync(join(tmpdir(), 'hb-'))
|
|
521
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = tmpHome
|
|
522
|
+
const mod = await import('../lib/redirector.js')
|
|
523
|
+
try {
|
|
524
|
+
assert.equal(mod.heartbeatAgeMs(), Number.POSITIVE_INFINITY, 'a missing heartbeat counts as stale')
|
|
525
|
+
assert.equal(mod.writeHeartbeat(), true)
|
|
526
|
+
assert.ok(mod.heartbeatAgeMs() < 2000, 'a fresh heartbeat must look fresh')
|
|
527
|
+
|
|
528
|
+
assert.equal(mod.stopCompleted(), false)
|
|
529
|
+
mod.clearStopSignals()
|
|
530
|
+
assert.equal(mod.requestRedirectorStop(), true)
|
|
531
|
+
assert.equal(existsSync(mod.stopRequestPath()), true)
|
|
532
|
+
// The marker is written by the watchdog, not by us: simulate its report.
|
|
533
|
+
writeFileSync(mod.stoppedMarkerPath(), 'done\n')
|
|
534
|
+
assert.equal(mod.stopCompleted(), true)
|
|
535
|
+
mod.clearStopSignals()
|
|
536
|
+
assert.equal(mod.stopCompleted(), false)
|
|
537
|
+
assert.equal(existsSync(mod.stopRequestPath()), false)
|
|
538
|
+
} finally {
|
|
539
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
540
|
+
rmSync(tmpHome, { recursive: true, force: true })
|
|
541
|
+
}
|
|
542
|
+
})
|
|
543
|
+
|
|
544
|
+
test('the elevated watchdog script carries the rules it needs to tear down', async () => {
|
|
545
|
+
const tmpHome = mkdtempSync(join(tmpdir(), 'wd-'))
|
|
546
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = tmpHome
|
|
547
|
+
const mod = await import('../lib/redirector.js')
|
|
548
|
+
try {
|
|
549
|
+
const script = mod.windowsWatchdogScript({
|
|
550
|
+
heartbeat: 'C:\\hb', stateFile: 'C:\\state.json', logFile: 'C:\\log.txt',
|
|
551
|
+
stopRequest: 'C:\\stop', stoppedMarker: 'C:\\done',
|
|
552
|
+
})
|
|
553
|
+
// A watchdog that stops on the wrong signal either kills a healthy bridge or
|
|
554
|
+
// leaves an elevated CLI behind — both are silent, so assert the pieces.
|
|
555
|
+
assert.match(script, /Test-Path \$stopFile/)
|
|
556
|
+
assert.match(script, /heartbeat-stale/)
|
|
557
|
+
assert.match(script, /Get-Process ProxyBridge_CLI .*Stop-Process -Force/)
|
|
558
|
+
assert.match(script, /Set-Service -Name ProxyBridgeSvc -StartupType \$state\.originalStartType/)
|
|
559
|
+
assert.match(script, /New-Item -ItemType File -Path \$doneFile/)
|
|
560
|
+
assert.match(script, /\$age -lt 20/)
|
|
561
|
+
|
|
562
|
+
const written = mod.writeWatchdogScript()
|
|
563
|
+
// It returns the WINDOWS path (that is where it runs); the file itself is ours.
|
|
564
|
+
assert.match(written, /proxybridge-watchdog\.ps1$/)
|
|
565
|
+
assert.equal(existsSync(join(tmpHome, 'proxybridge-watchdog.ps1')), true)
|
|
566
|
+
assert.match(readFileSync(join(tmpHome, 'proxybridge-watchdog.ps1'), 'utf8'), /ProxyBridgeSvc/)
|
|
567
|
+
} finally {
|
|
568
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
569
|
+
rmSync(tmpHome, { recursive: true, force: true })
|
|
570
|
+
}
|
|
571
|
+
})
|