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,914 @@
|
|
|
1
|
+
// Proxy engine tests: rule parsing/matching (pure) + loopback integration tests
|
|
2
|
+
// that run the real engine against a scripted fake SOCKS5 server.
|
|
3
|
+
import assert from 'node:assert/strict'
|
|
4
|
+
import { spawn } from 'node:child_process'
|
|
5
|
+
import { createServer, connect } from 'node:net'
|
|
6
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
7
|
+
import { tmpdir } from 'node:os'
|
|
8
|
+
import { join } from 'node:path'
|
|
9
|
+
import test from 'node:test'
|
|
10
|
+
|
|
11
|
+
// The proxy config/pid/log live in ONE machine-level directory now (that is the
|
|
12
|
+
// whole point of the shared engine), so every test run gets its own throwaway
|
|
13
|
+
// home instead of writing into the developer's ~/.dsh-ris-ide.
|
|
14
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = mkdtempSync(join(tmpdir(), 'ris-proxy-home-'))
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
allowAdmits,
|
|
18
|
+
allowList,
|
|
19
|
+
autoRules,
|
|
20
|
+
connectSocks,
|
|
21
|
+
DEFAULT_LISTEN_PORT,
|
|
22
|
+
DEFAULT_NO_PROXY,
|
|
23
|
+
engineSide,
|
|
24
|
+
hostMatches,
|
|
25
|
+
injectionFor,
|
|
26
|
+
isSocksCapableProgram,
|
|
27
|
+
MAX_RULES,
|
|
28
|
+
MAX_SHADOW_TARGETS,
|
|
29
|
+
mergeConfigs,
|
|
30
|
+
normalizeConfig,
|
|
31
|
+
parseHostSpec,
|
|
32
|
+
parseJsonc,
|
|
33
|
+
parsePortSpec,
|
|
34
|
+
pidAlive,
|
|
35
|
+
pidIsProxy,
|
|
36
|
+
programKey,
|
|
37
|
+
PROXY_MARKER,
|
|
38
|
+
proxyConfigPath,
|
|
39
|
+
proxyHomeDir,
|
|
40
|
+
proxyPid,
|
|
41
|
+
proxyPidPath,
|
|
42
|
+
proxyStatus,
|
|
43
|
+
readProxyConfig,
|
|
44
|
+
ruleEffectiveness,
|
|
45
|
+
shadowTargets,
|
|
46
|
+
startProxy,
|
|
47
|
+
stopProxy,
|
|
48
|
+
writeProxyConfig,
|
|
49
|
+
} from '../lib/proxy.js'
|
|
50
|
+
|
|
51
|
+
/** Wait a while without blocking the event loop. */
|
|
52
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
53
|
+
|
|
54
|
+
test('host specs: commas, wildcards, invalid fragments', () => {
|
|
55
|
+
assert.deepEqual(parseHostSpec('1.1.1.*, 10.0.0.5').patterns, ['1.1.1.*', '10.0.0.5'])
|
|
56
|
+
assert.deepEqual(parseHostSpec('bad host!').invalid, ['bad host!'])
|
|
57
|
+
assert.deepEqual(parseHostSpec('').patterns, [])
|
|
58
|
+
assert.deepEqual(parseHostSpec(undefined).patterns, [])
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('host matching: wildcard segments, DNS case-insensitivity, exact', () => {
|
|
62
|
+
assert.equal(hostMatches('1.1.1.*', '1.1.1.9'), true)
|
|
63
|
+
assert.equal(hostMatches('1.1.1.*', '1.1.2.9'), false)
|
|
64
|
+
assert.equal(hostMatches('*.example.com', 'a.EXAMPLE.com'), true)
|
|
65
|
+
assert.equal(hostMatches('10.0.0.5', '10.0.0.5'), true)
|
|
66
|
+
assert.equal(hostMatches('10.0.0.5', '10.0.0.6'), false)
|
|
67
|
+
// DNS is case-insensitive on BOTH sides — the pattern is not more literal than
|
|
68
|
+
// the host it is compared with (a stored `GitLab.Example.com` must still match).
|
|
69
|
+
assert.equal(hostMatches('GitLab.Example.com', 'gitlab.example.COM'), true)
|
|
70
|
+
assert.equal(hostMatches('*.Example.COM', 'a.example.com'), true)
|
|
71
|
+
assert.equal(hostMatches('*', 'anything'), true)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('port specs: lists and ranges, invalid fragments', () => {
|
|
75
|
+
assert.deepEqual(parsePortSpec('5432, 1000-1003').ports, [1000, 1001, 1002, 1003, 5432])
|
|
76
|
+
assert.deepEqual(parsePortSpec('70000, 5-3').invalid, ['70000', '5-3'])
|
|
77
|
+
assert.deepEqual(parsePortSpec('').ports, [])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('the generated rows are templates: four programs, disabled, never persisted', () => {
|
|
81
|
+
const config = normalizeConfig(undefined)
|
|
82
|
+
// Stored rows only — the shared file must not accumulate a template row per
|
|
83
|
+
// workspace, so the dialog asks for them separately.
|
|
84
|
+
assert.deepEqual(config.rules, [])
|
|
85
|
+
assert.equal(config.listenPort, DEFAULT_LISTEN_PORT)
|
|
86
|
+
assert.equal(config.socks.host, '')
|
|
87
|
+
// Only the two rows a yds project always needs; node/curl are not auto-generated
|
|
88
|
+
// (they would just be two more empty rows nobody asked for).
|
|
89
|
+
assert.deepEqual(autoRules('w1').map((rule) => rule.program), ['java', 'git'])
|
|
90
|
+
assert.equal(autoRules('w1').every((rule) => rule.enabled === false && rule.workspace === 'w1'), true)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('one switch per program for the whole machine, workspace is only a label', () => {
|
|
94
|
+
// ProxyBridge hooks the kernel and matches process names, so a rule cannot be
|
|
95
|
+
// scoped to a workspace: two workspaces asking for `java` are the same switch,
|
|
96
|
+
// and the first row keeps its provenance label.
|
|
97
|
+
const config = normalizeConfig({
|
|
98
|
+
rules: [
|
|
99
|
+
{ id: 'mine', program: 'Java', enabled: true, workspace: '/w1' },
|
|
100
|
+
{ id: 'mine-again', program: 'java', enabled: false, workspace: '/w1' },
|
|
101
|
+
{ id: 'other', program: 'java', enabled: true, workspace: '/w2' },
|
|
102
|
+
{ id: 'pinned', program: 'C:/Program Files/Navicat/navicat.exe', enabled: true, workspace: '/w2' },
|
|
103
|
+
],
|
|
104
|
+
})
|
|
105
|
+
assert.deepEqual(config.rules.map((rule) => `${rule.workspace}:${rule.program}`),
|
|
106
|
+
['/w1:Java', '/w2:C:/Program Files/Navicat/navicat.exe'])
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('driven endpoints keep their ids even when they share a program', () => {
|
|
110
|
+
// The yds bridge writes one row per JDBC endpoint, all labelled `jdbc`: they are
|
|
111
|
+
// destinations, not aliases of one program switch.
|
|
112
|
+
const config = normalizeConfig({
|
|
113
|
+
rules: [
|
|
114
|
+
{ id: 'yds-1', program: 'jdbc', hosts: '172.20.235.80', ports: '1521', enabled: true, source: 'yds' },
|
|
115
|
+
{ id: 'yds-2', program: 'jdbc', hosts: '172.20.235.81', ports: '6890', enabled: true, source: 'yds' },
|
|
116
|
+
],
|
|
117
|
+
})
|
|
118
|
+
assert.deepEqual(config.rules.map((rule) => rule.id), ['yds-1', 'yds-2'])
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('allow-list: every axis is required, and `*` is how you say "everything"', () => {
|
|
122
|
+
const config = normalizeConfig({
|
|
123
|
+
socks: { host: 'h', port: 1080 },
|
|
124
|
+
rules: [
|
|
125
|
+
{ id: 'a', program: 'java', hosts: '172.20.235.*', ports: '1521', enabled: true },
|
|
126
|
+
{ id: 'b', program: 'off', hosts: '', ports: '', enabled: false },
|
|
127
|
+
],
|
|
128
|
+
})
|
|
129
|
+
const allow = allowList(config)
|
|
130
|
+
assert.equal(allowAdmits(allow, '172.20.235.80', 1521), true)
|
|
131
|
+
assert.equal(allowAdmits(allow, '172.20.235.80', 6890), false)
|
|
132
|
+
assert.equal(allowAdmits(allow, '10.0.0.1', 1521), false)
|
|
133
|
+
// A blank axis is a half-written row, not an unrestricted one: it contributes
|
|
134
|
+
// nothing at all (that is what stops a typo from proxying the internet).
|
|
135
|
+
const blank = allowList(normalizeConfig({ rules: [{ id: 'x', program: 'p', hosts: '', ports: '', enabled: true }] }))
|
|
136
|
+
assert.equal(allowAdmits(blank, '1.2.3.4', 9999), false)
|
|
137
|
+
// The explicit spelling does open it up.
|
|
138
|
+
const open = allowList(normalizeConfig({ rules: [{ id: 'x', program: 'p', hosts: '*', ports: '*', enabled: true }] }))
|
|
139
|
+
assert.equal(allowAdmits(open, '1.2.3.4', 9999), true)
|
|
140
|
+
assert.equal(allowAdmits(allowList(normalizeConfig(undefined)), '1.2.3.4', 80), false)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('shadow targets: opt-in, literal IPv4 only, same port, deduplicated', () => {
|
|
144
|
+
const literal = normalizeConfig({ rules: [{ id: 'r', program: 'navicat', hosts: '172.20.235.80,172.20.235.80', ports: '1521,3306', enabled: true, shadow: true }] })
|
|
145
|
+
assert.deepEqual(shadowTargets(literal), [
|
|
146
|
+
{ host: '172.20.235.80', port: 1521, localPort: 1521 },
|
|
147
|
+
{ host: '172.20.235.80', port: 3306, localPort: 3306 },
|
|
148
|
+
])
|
|
149
|
+
const wildcard = normalizeConfig({ rules: [{ id: 'w', program: 'java', hosts: '10.0.0.*', ports: '1521', enabled: true, shadow: true }] })
|
|
150
|
+
assert.deepEqual(shadowTargets(wildcard), [])
|
|
151
|
+
// A program that speaks SOCKS needs no listener even when enabled + bindable.
|
|
152
|
+
const capable = normalizeConfig({ rules: [{ id: 'c', program: 'java', hosts: '1.2.3.4', ports: '1521', enabled: true }] })
|
|
153
|
+
assert.deepEqual(shadowTargets(capable), [])
|
|
154
|
+
// A program that cannot (jdbc/navicat/…) is mapped automatically — that is the
|
|
155
|
+
// only way it reaches the tunnel — unless the row says otherwise.
|
|
156
|
+
const needsMap = normalizeConfig({ rules: [{ id: 'n', program: 'navicat', hosts: '1.2.3.4', ports: '1521', enabled: true }] })
|
|
157
|
+
assert.deepEqual(shadowTargets(needsMap), [{ host: '1.2.3.4', port: 1521, localPort: 1521 }])
|
|
158
|
+
const optedOut = normalizeConfig({ rules: [{ id: 'o', program: 'navicat', hosts: '1.2.3.4', ports: '1521', enabled: true, shadow: false }] })
|
|
159
|
+
assert.deepEqual(shadowTargets(optedOut), [])
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('rule effectiveness: a rule only applies when EVERY axis is explicit and valid', () => {
|
|
163
|
+
const base = { program: 'java', hosts: '1.2.3.4', ports: '1521', enabled: true }
|
|
164
|
+
assert.deepEqual(ruleEffectiveness(base), { effective: true, reason: '' })
|
|
165
|
+
assert.deepEqual(ruleEffectiveness({ ...base, hosts: '*', ports: '*' }), { effective: true, reason: '' })
|
|
166
|
+
assert.deepEqual(ruleEffectiveness({ ...base, enabled: false }), { effective: false, reason: '未启用' })
|
|
167
|
+
assert.deepEqual(ruleEffectiveness({ ...base, enabled: 'true' }), { effective: false, reason: '未启用' })
|
|
168
|
+
assert.deepEqual(ruleEffectiveness({ ...base, program: ' ' }), { effective: false, reason: '缺程序' })
|
|
169
|
+
assert.deepEqual(ruleEffectiveness({ ...base, hosts: ' ' }), { effective: false, reason: '缺 IP(全匹配写 *)' })
|
|
170
|
+
assert.deepEqual(ruleEffectiveness({ ...base, ports: ' ' }), { effective: false, reason: '缺端口(全匹配写 *)' })
|
|
171
|
+
// A bad fragment NEXT TO a good one must reject the row, not be dropped: a typo
|
|
172
|
+
// would otherwise silently narrow the rule (or, worse, widen it) behind the
|
|
173
|
+
// user's back. Only-invalid specs would hit the "missing" branch first, so the
|
|
174
|
+
// valid fragment is what makes this the real invalid-fragment test.
|
|
175
|
+
assert.deepEqual(ruleEffectiveness({ ...base, hosts: '1.2.3.4, bad host!' }), { effective: false, reason: 'IP 有无效片段' })
|
|
176
|
+
assert.deepEqual(ruleEffectiveness({ ...base, ports: '1521, 70000' }), { effective: false, reason: '端口有无效片段' })
|
|
177
|
+
assert.deepEqual(ruleEffectiveness(undefined), { effective: false, reason: '未启用' })
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test('config caps: rules and shadow listeners are bounded', () => {
|
|
181
|
+
const rules = Array.from({ length: MAX_RULES + 10 }, (_, index) => ({
|
|
182
|
+
id: `r${index}`, program: `prog${index}`, hosts: '1.2.3.4', ports: '1521', enabled: true,
|
|
183
|
+
}))
|
|
184
|
+
assert.equal(normalizeConfig({ rules }).rules.length, MAX_RULES, 'a thousand rules is a typo, not a setup')
|
|
185
|
+
const shadows = Array.from({ length: MAX_SHADOW_TARGETS + 5 }, (_, index) => ({
|
|
186
|
+
host: '10.0.0.1', port: 3000 + index,
|
|
187
|
+
}))
|
|
188
|
+
assert.equal(normalizeConfig({ shadows }).shadows.length, MAX_SHADOW_TARGETS)
|
|
189
|
+
// Derived targets are capped too, not only the explicitly stored fallback list.
|
|
190
|
+
const hosts = Array.from({ length: 10 }, (_, index) => `10.0.${index}.1`).join(',')
|
|
191
|
+
const ports = Array.from({ length: 10 }, (_, index) => 2000 + index).join(',')
|
|
192
|
+
const derived = normalizeConfig({ rules: [{ id: 'many', program: 'navicat', hosts, ports, enabled: true, shadow: true }] })
|
|
193
|
+
assert.equal(shadowTargets(derived).length, MAX_SHADOW_TARGETS)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test('injection: the default is the LOCAL CONNECT proxy, so rules stay enforced', () => {
|
|
197
|
+
const config = normalizeConfig({
|
|
198
|
+
socks: { host: '10.3.91.121', port: 1080 },
|
|
199
|
+
rules: [{ id: 'j', program: 'java', hosts: '172.20.235.*', ports: '1521', enabled: true }],
|
|
200
|
+
})
|
|
201
|
+
const injected = injectionFor('C:\\Program Files\\Java\\java.exe', config)
|
|
202
|
+
// The program must NOT be handed the upstream SOCKS address directly: that
|
|
203
|
+
// would let it reach anything the SOCKS server can and silently ignore the
|
|
204
|
+
// rule's hosts/ports. It gets the engine's loopback CONNECT proxy instead, so
|
|
205
|
+
// every connection passes the allow-list.
|
|
206
|
+
assert.equal(injected.side, 'local')
|
|
207
|
+
assert.equal(injected.direct, false)
|
|
208
|
+
const local = `http://127.0.0.1:${DEFAULT_LISTEN_PORT}`
|
|
209
|
+
assert.equal(injected.env.ALL_PROXY, local)
|
|
210
|
+
assert.equal(injected.env.HTTP_PROXY, local)
|
|
211
|
+
assert.equal(injected.env.HTTPS_PROXY, local)
|
|
212
|
+
assert.equal(injected.env.http_proxy, local)
|
|
213
|
+
assert.equal(injected.env.https_proxy, local)
|
|
214
|
+
assert.equal(injected.env.NO_PROXY, DEFAULT_NO_PROXY)
|
|
215
|
+
assert.equal(injected.env.no_proxy, DEFAULT_NO_PROXY)
|
|
216
|
+
assert.equal(injected.httpProxyUrl, local)
|
|
217
|
+
assert.equal(injected.socksUrl, 'socks5h://10.3.91.121:1080')
|
|
218
|
+
assert.equal(Object.values(injected.env).includes('socks5h://10.3.91.121:1080'), false,
|
|
219
|
+
'no injected variable may point at the upstream SOCKS server')
|
|
220
|
+
// JVM: -DsocksProxyHost cannot point at an HTTP proxy, so the local path uses
|
|
221
|
+
// the HTTP flags only (java ignores proxy env vars by default).
|
|
222
|
+
assert.deepEqual(injected.jvmArgs, [
|
|
223
|
+
'-Dhttp.proxyHost=127.0.0.1',
|
|
224
|
+
`-Dhttp.proxyPort=${DEFAULT_LISTEN_PORT}`,
|
|
225
|
+
'-Dhttps.proxyHost=127.0.0.1',
|
|
226
|
+
`-Dhttps.proxyPort=${DEFAULT_LISTEN_PORT}`,
|
|
227
|
+
'-Dhttp.nonProxyHosts=localhost|127.*|[::1]',
|
|
228
|
+
])
|
|
229
|
+
assert.equal(injected.jvmArgs.some((arg) => arg.startsWith('-DsocksProxyHost')), false)
|
|
230
|
+
// lib/index.js calls us with `side: engineSide()`; that is "local", so a
|
|
231
|
+
// program started from the panel takes the same enforced path.
|
|
232
|
+
assert.deepEqual(injectionFor('java', config, { side: engineSide() }).env, injected.env)
|
|
233
|
+
assert.equal(programKey('C:\\x\\Navicat.exe'), 'navicat')
|
|
234
|
+
assert.equal(programKey('/usr/bin/node'), 'node')
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test('injection: `direct` is the ONLY way the upstream SOCKS reaches a program', () => {
|
|
238
|
+
const config = normalizeConfig({
|
|
239
|
+
socks: { host: '10.3.91.121', port: 1080 },
|
|
240
|
+
rules: [
|
|
241
|
+
{ id: 'plain', program: 'java', hosts: '*', ports: '*', enabled: true },
|
|
242
|
+
{ id: 'direct', program: 'git', hosts: '*', ports: '*', enabled: true, direct: true },
|
|
243
|
+
],
|
|
244
|
+
})
|
|
245
|
+
// Missing field ⇒ false, so every config file written before `direct` existed
|
|
246
|
+
// — and every caller that does not restate it — keeps the safe path.
|
|
247
|
+
assert.equal(config.rules[0].direct, false)
|
|
248
|
+
assert.equal(injectionFor('java', config).direct, false)
|
|
249
|
+
assert.equal(injectionFor('java', config).env.ALL_PROXY, `http://127.0.0.1:${DEFAULT_LISTEN_PORT}`)
|
|
250
|
+
// An explicit true hands over the upstream address (the pre-M1 behaviour).
|
|
251
|
+
const direct = injectionFor('git', config)
|
|
252
|
+
assert.equal(direct.direct, true)
|
|
253
|
+
assert.equal(direct.httpProxyUrl, null)
|
|
254
|
+
assert.equal(direct.env.ALL_PROXY, 'socks5h://10.3.91.121:1080')
|
|
255
|
+
assert.equal(direct.env.HTTPS_PROXY, 'socks5h://10.3.91.121:1080')
|
|
256
|
+
assert.deepEqual(direct.jvmArgs, ['-DsocksProxyHost=10.3.91.121', '-DsocksProxyPort=1080'])
|
|
257
|
+
// `direct` must survive the config round-trip, or a reload would silently flip
|
|
258
|
+
// the enforcement path; a non-boolean must never be treated as true.
|
|
259
|
+
assert.equal(normalizeConfig({ rules: [{ program: 'git', hosts: '*', ports: '*', direct: true }] }).rules[0].direct, true)
|
|
260
|
+
assert.equal(normalizeConfig({ rules: [{ program: 'git', hosts: '*', ports: '*', direct: 'yes' }] }).rules[0].direct, false)
|
|
261
|
+
// ...including an edit that does not restate it.
|
|
262
|
+
const stored = normalizeConfig({ rules: [{ id: 'd', program: 'git', hosts: '*', ports: '*', enabled: true, direct: true }] })
|
|
263
|
+
const merged = mergeConfigs(stored, { rules: [{ id: 'd', program: 'git', hosts: '*', ports: '*', enabled: true }] })
|
|
264
|
+
assert.equal(merged.rules[0].direct, true, 'an edit that omits direct keeps the stored decision')
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
test('injection: the OS boundary and programs that cannot take a proxy address', () => {
|
|
268
|
+
const config = normalizeConfig({
|
|
269
|
+
socks: { host: '10.3.91.121', port: 1080 },
|
|
270
|
+
rules: [
|
|
271
|
+
{ id: 'j', program: 'java', hosts: '*', ports: '*', enabled: true },
|
|
272
|
+
{ id: 'g', program: 'git', hosts: '*', ports: '*', enabled: true, direct: true },
|
|
273
|
+
{ id: 'n', program: 'navicat', hosts: '1.2.3.4', ports: '1521', enabled: true },
|
|
274
|
+
],
|
|
275
|
+
})
|
|
276
|
+
// A program on the other side of WSL/Windows cannot reach this machine's
|
|
277
|
+
// 127.0.0.1, so a non-direct row injects nothing there (the reason says so).
|
|
278
|
+
const remote = injectionFor('java', config, { side: 'remote' })
|
|
279
|
+
assert.equal(remote.side, 'remote')
|
|
280
|
+
assert.deepEqual(remote.env, {})
|
|
281
|
+
assert.match(remote.reason, /另一侧/)
|
|
282
|
+
const otherSide = engineSide() === 'windows' ? 'wsl' : 'windows'
|
|
283
|
+
assert.equal(injectionFor('java', config, { side: otherSide }).env.ALL_PROXY, undefined)
|
|
284
|
+
// ...unless the row is direct, which needs no local listener.
|
|
285
|
+
assert.equal(injectionFor('git', config, { side: 'remote' }).env.ALL_PROXY, 'socks5h://10.3.91.121:1080')
|
|
286
|
+
// A client that cannot be told a proxy address reaches the tunnel only through
|
|
287
|
+
// a same-port 127.0.0.1 listener; injection must not pretend otherwise.
|
|
288
|
+
assert.equal(isSocksCapableProgram('navicat'), false)
|
|
289
|
+
assert.equal(injectionFor('navicat', config), null)
|
|
290
|
+
assert.equal(injectionFor('python', config), null)
|
|
291
|
+
assert.equal(injectionFor('', config), null)
|
|
292
|
+
// No upstream configured ⇒ nothing to inject.
|
|
293
|
+
const noSocks = normalizeConfig({ rules: [{ program: 'java', hosts: '*', ports: '*', enabled: true }] })
|
|
294
|
+
assert.equal(injectionFor('java', noSocks), null)
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
test('injection: a rule that names the program but leaves an axis blank injects nothing', () => {
|
|
298
|
+
const config = normalizeConfig({
|
|
299
|
+
socks: { host: '10.3.91.121', port: 1080 },
|
|
300
|
+
rules: [
|
|
301
|
+
{ id: 'half', program: 'java', hosts: '', ports: '', enabled: true },
|
|
302
|
+
{ id: 'off', program: 'git', hosts: '*', ports: '*', enabled: false },
|
|
303
|
+
],
|
|
304
|
+
})
|
|
305
|
+
const injected = injectionFor('java', config)
|
|
306
|
+
assert.notEqual(injected, null, 'the rule matched, so the caller must be told WHY nothing was injected')
|
|
307
|
+
assert.deepEqual(injected.env, {})
|
|
308
|
+
assert.deepEqual(injected.jvmArgs, [])
|
|
309
|
+
assert.equal(injected.direct, false)
|
|
310
|
+
assert.match(injected.reason, /规则不完整/)
|
|
311
|
+
assert.match(injected.reason, /缺 IP/)
|
|
312
|
+
// An explicit `*` is effective and injects the loopback proxy.
|
|
313
|
+
const open = normalizeConfig({
|
|
314
|
+
socks: { host: '10.3.91.121', port: 1080 },
|
|
315
|
+
rules: [{ id: 'open', program: 'java', hosts: '*', ports: '*', enabled: true }],
|
|
316
|
+
})
|
|
317
|
+
assert.equal(injectionFor('java', open).env.HTTPS_PROXY, `http://127.0.0.1:${DEFAULT_LISTEN_PORT}`)
|
|
318
|
+
// No enabled rule at all ⇒ no settings (not even a reason object).
|
|
319
|
+
assert.equal(injectionFor('git', config), null)
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
test('JSONC config files round-trip through the ONE shared file', () => {
|
|
323
|
+
const dir = mkdtempSync(join(tmpdir(), 'ris-ide-proxy-'))
|
|
324
|
+
try {
|
|
325
|
+
const written = writeProxyConfig(dir, {
|
|
326
|
+
socks: { host: '10.3.91.121', port: 1080 },
|
|
327
|
+
rules: [{ id: 'db', program: '', hosts: '172.20.235.80', ports: '1521', enabled: true }],
|
|
328
|
+
})
|
|
329
|
+
assert.equal(written.rules.find((rule) => rule.id === 'db').enabled, true)
|
|
330
|
+
assert.equal(readProxyConfig(dir).socks.port, 1080)
|
|
331
|
+
assert.equal(readProxyConfig(dir).rules.find((rule) => rule.id === 'db').enabled, true)
|
|
332
|
+
// The file is machine-level: no workspace directory involved.
|
|
333
|
+
assert.equal(proxyConfigPath(), join(process.env.DSH_RIS_IDE_PROXY_HOME, 'proxy.json'))
|
|
334
|
+
assert.equal(proxyConfigPath().includes('.dsh-ris-ide/proxy.json'), false)
|
|
335
|
+
// A hand-edited file keeps its comments and still parses.
|
|
336
|
+
const text = `// mine\n${readFileSync(proxyConfigPath(), 'utf8')}`
|
|
337
|
+
writeFileSync(proxyConfigPath(), text)
|
|
338
|
+
assert.equal(readProxyConfig(dir).socks.host, '10.3.91.121')
|
|
339
|
+
assert.equal(parseJsonc('{ "a": [1,2,], /* x */ "b": "c://d", }').b, 'c://d')
|
|
340
|
+
} finally {
|
|
341
|
+
rmSync(dir, { recursive: true, force: true })
|
|
342
|
+
}
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
test('merge: replaceAllRules makes the payload the whole table (m10585)', () => {
|
|
346
|
+
const stored = normalizeConfig({
|
|
347
|
+
socks: { host: 'h', port: 1080 },
|
|
348
|
+
rules: [
|
|
349
|
+
{ id: 'user-1', program: 'java', hosts: '1.1.1.*', ports: '443', enabled: true },
|
|
350
|
+
{ id: 'yds-old', program: 'git', hosts: '1.1.1.1', ports: '1521', enabled: true, source: 'yds' },
|
|
351
|
+
],
|
|
352
|
+
})
|
|
353
|
+
const merged = mergeConfigs(stored, {
|
|
354
|
+
replaceAllRules: true,
|
|
355
|
+
rules: [{ id: 'yds-new', program: 'java', hosts: '2.2.2.*', ports: '*', enabled: true, source: 'yds' }],
|
|
356
|
+
})
|
|
357
|
+
assert.deepEqual(merged.rules.map((rule) => rule.id), ['yds-new'])
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
test('merge: yds-owned rows are replaced wholesale, user rows survive', () => {
|
|
361
|
+
const stored = normalizeConfig({
|
|
362
|
+
socks: { host: 'h', port: 1080 },
|
|
363
|
+
rules: [
|
|
364
|
+
{ id: 'user-1', program: 'java', hosts: '1.1.1.*', ports: '443', enabled: true },
|
|
365
|
+
{ id: 'yds-old', program: '', hosts: '1.1.1.1', ports: '1521', enabled: true, source: 'yds' },
|
|
366
|
+
],
|
|
367
|
+
})
|
|
368
|
+
const merged = mergeConfigs(stored, {
|
|
369
|
+
replaceRulesFrom: 'yds',
|
|
370
|
+
rules: [{ id: 'yds-new', hosts: '2.2.2.2', ports: '1521', enabled: true, source: 'yds' }],
|
|
371
|
+
})
|
|
372
|
+
assert.equal(merged.rules.some((rule) => rule.id === 'yds-old'), false)
|
|
373
|
+
assert.equal(merged.rules.some((rule) => rule.id === 'yds-new'), true)
|
|
374
|
+
assert.equal(merged.rules.some((rule) => rule.id === 'user-1'), true)
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* A minimal SOCKS5 server that dials whatever CONNECT asks for — enough to run
|
|
379
|
+
* the engine's real socket paths without an external tunnel.
|
|
380
|
+
* @param onTarget - receives `host:port` of every CONNECT.
|
|
381
|
+
* @returns `{ port, close }`.
|
|
382
|
+
*/
|
|
383
|
+
async function fakeSocks(onTarget) {
|
|
384
|
+
const server = createServer((client) => {
|
|
385
|
+
client.once('data', (greeting) => {
|
|
386
|
+
if (greeting[0] !== 0x05) {
|
|
387
|
+
client.destroy()
|
|
388
|
+
return
|
|
389
|
+
}
|
|
390
|
+
client.write(Buffer.from([0x05, 0x00]))
|
|
391
|
+
client.once('data', (request) => {
|
|
392
|
+
const atyp = request[3]
|
|
393
|
+
let host = ''
|
|
394
|
+
let offset = 4
|
|
395
|
+
if (atyp === 0x01) {
|
|
396
|
+
host = `${request[4]}.${request[5]}.${request[6]}.${request[7]}`
|
|
397
|
+
offset = 8
|
|
398
|
+
} else if (atyp === 0x03) {
|
|
399
|
+
const length = request[4]
|
|
400
|
+
host = request.subarray(5, 5 + length).toString('utf8')
|
|
401
|
+
offset = 5 + length
|
|
402
|
+
} else {
|
|
403
|
+
client.destroy()
|
|
404
|
+
return
|
|
405
|
+
}
|
|
406
|
+
const port = (request[offset] << 8) | request[offset + 1]
|
|
407
|
+
onTarget(`${host}:${port}`)
|
|
408
|
+
const upstream = connect({ host, port }, () => {
|
|
409
|
+
client.write(Buffer.from([0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]))
|
|
410
|
+
client.pipe(upstream)
|
|
411
|
+
upstream.pipe(client)
|
|
412
|
+
})
|
|
413
|
+
upstream.on('error', () => client.destroy())
|
|
414
|
+
})
|
|
415
|
+
})
|
|
416
|
+
})
|
|
417
|
+
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
|
|
418
|
+
return { port: server.address().port, close: () => new Promise((resolve) => server.close(resolve)) }
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** An unused loopback port (a fixed one collides with a straggler from a rebuild). */
|
|
422
|
+
async function freePort(host = '127.0.0.1') {
|
|
423
|
+
const server = createServer()
|
|
424
|
+
await new Promise((resolve) => server.listen(0, host, resolve))
|
|
425
|
+
const { port } = server.address()
|
|
426
|
+
await new Promise((resolve) => server.close(resolve))
|
|
427
|
+
return port
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** A one-line HTTP target that answers the request body with its own port. */
|
|
431
|
+
async function targetServer(port) {
|
|
432
|
+
const server = createServer((socket) => {
|
|
433
|
+
socket.once('data', () => socket.end(`HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok`))
|
|
434
|
+
})
|
|
435
|
+
await new Promise((resolve) => server.listen(port, '127.0.0.2', resolve))
|
|
436
|
+
return { close: () => new Promise((resolve) => server.close(resolve)) }
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** Speak HTTP CONNECT to the engine and report the status line. */
|
|
440
|
+
function connectThrough(port, host, targetPort) {
|
|
441
|
+
return new Promise((resolve) => {
|
|
442
|
+
const socket = connect({ host: '127.0.0.1', port }, () => {
|
|
443
|
+
socket.write(`CONNECT ${host}:${targetPort} HTTP/1.1\r\nHost: ${host}:${targetPort}\r\n\r\n`)
|
|
444
|
+
})
|
|
445
|
+
let text = ''
|
|
446
|
+
socket.on('data', (chunk) => {
|
|
447
|
+
text += chunk.toString('latin1')
|
|
448
|
+
if (text.includes('\r\n')) {
|
|
449
|
+
socket.destroy()
|
|
450
|
+
resolve(text.split('\r\n')[0])
|
|
451
|
+
}
|
|
452
|
+
})
|
|
453
|
+
socket.on('error', () => resolve('socket-error'))
|
|
454
|
+
socket.setTimeout(3000, () => { socket.destroy(); resolve('timeout') })
|
|
455
|
+
})
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* A SOCKS5 server whose behaviour per connection is scripted by the caller — the
|
|
460
|
+
* only way to exercise handshake edge cases (split replies, bad ATYP, refused
|
|
461
|
+
* CONNECT) that a well-behaved server never produces.
|
|
462
|
+
* @param script - receives the client socket right after accept.
|
|
463
|
+
* @returns `{ port, close }`; `close` destroys any still-open socket first.
|
|
464
|
+
*/
|
|
465
|
+
async function scriptedSocks(script) {
|
|
466
|
+
const sockets = new Set()
|
|
467
|
+
const server = createServer((client) => {
|
|
468
|
+
sockets.add(client)
|
|
469
|
+
client.on('close', () => sockets.delete(client))
|
|
470
|
+
client.on('error', () => client.destroy())
|
|
471
|
+
script(client)
|
|
472
|
+
})
|
|
473
|
+
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
|
|
474
|
+
return {
|
|
475
|
+
port: server.address().port,
|
|
476
|
+
close: async () => {
|
|
477
|
+
for (const socket of sockets) socket.destroy()
|
|
478
|
+
await new Promise((resolve) => server.close(resolve))
|
|
479
|
+
},
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Answer the SOCKS5 greeting, then run `onRequest` for the CONNECT request.
|
|
485
|
+
* @param client - accepted socket.
|
|
486
|
+
* @param onRequest - called after the CONNECT request arrives.
|
|
487
|
+
*/
|
|
488
|
+
function socksHandshake(client, onRequest) {
|
|
489
|
+
client.once('data', (greeting) => {
|
|
490
|
+
if (greeting[0] !== 0x05) {
|
|
491
|
+
client.destroy()
|
|
492
|
+
return
|
|
493
|
+
}
|
|
494
|
+
client.write(Buffer.from([0x05, 0x00]))
|
|
495
|
+
client.once('data', (request) => onRequest(request, client))
|
|
496
|
+
})
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** Resolve with the next chunk read from `socket`, or reject after `timeoutMs`. */
|
|
500
|
+
function firstData(socket, timeoutMs = 2000) {
|
|
501
|
+
return new Promise((resolve, reject) => {
|
|
502
|
+
const timer = setTimeout(() => reject(new Error('no tunnel data arrived')), timeoutMs)
|
|
503
|
+
socket.once('data', (chunk) => {
|
|
504
|
+
clearTimeout(timer)
|
|
505
|
+
resolve(chunk)
|
|
506
|
+
})
|
|
507
|
+
socket.once('error', (error) => {
|
|
508
|
+
clearTimeout(timer)
|
|
509
|
+
reject(error)
|
|
510
|
+
})
|
|
511
|
+
// connectSocks leaves the socket paused so bytes coalesced with the reply are
|
|
512
|
+
// not emitted before the caller attaches; reading directly must resume it.
|
|
513
|
+
socket.resume()
|
|
514
|
+
})
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* An HTTP origin on a loopback alias that records the request head it receives,
|
|
519
|
+
* so a test can prove what the engine actually forwarded.
|
|
520
|
+
* @param host - loopback address to bind.
|
|
521
|
+
* @param port - port to bind.
|
|
522
|
+
* @returns `{ requests, close }`.
|
|
523
|
+
*/
|
|
524
|
+
async function recordingTarget(host, port) {
|
|
525
|
+
const requests = []
|
|
526
|
+
const server = createServer((socket) => {
|
|
527
|
+
let text = ''
|
|
528
|
+
let recorded = false
|
|
529
|
+
socket.on('data', (chunk) => {
|
|
530
|
+
if (recorded) return
|
|
531
|
+
text += chunk.toString('latin1')
|
|
532
|
+
if (!text.includes('\r\n\r\n')) return
|
|
533
|
+
recorded = true
|
|
534
|
+
requests.push(text)
|
|
535
|
+
socket.end('HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok')
|
|
536
|
+
})
|
|
537
|
+
})
|
|
538
|
+
await new Promise((resolve) => server.listen(port, host, resolve))
|
|
539
|
+
return { requests, close: () => new Promise((resolve) => server.close(resolve)) }
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** Send an HTTP exchange to the engine's CONNECT proxy and return the reply text. */
|
|
543
|
+
function httpThrough(port, request, options = {}) {
|
|
544
|
+
return new Promise((resolve) => {
|
|
545
|
+
const socket = connect({ host: '127.0.0.1', port }, async () => {
|
|
546
|
+
if (Array.isArray(options.chunks)) {
|
|
547
|
+
for (const chunk of options.chunks) {
|
|
548
|
+
socket.write(chunk)
|
|
549
|
+
if (options.gapMs > 0) await sleep(options.gapMs)
|
|
550
|
+
}
|
|
551
|
+
return
|
|
552
|
+
}
|
|
553
|
+
socket.write(request)
|
|
554
|
+
})
|
|
555
|
+
let text = ''
|
|
556
|
+
socket.on('data', (chunk) => { text += chunk.toString('latin1') })
|
|
557
|
+
socket.on('close', () => resolve(text))
|
|
558
|
+
socket.on('error', () => resolve('socket-error'))
|
|
559
|
+
socket.setTimeout(4000, () => { socket.destroy(); resolve('timeout') })
|
|
560
|
+
})
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
test('engine: shadow listener + CONNECT allow-list, real sockets', async () => {
|
|
564
|
+
const dir = mkdtempSync(join(tmpdir(), 'ris-ide-engine-'))
|
|
565
|
+
const targets = []
|
|
566
|
+
const socks = await fakeSocks((target) => targets.push(target))
|
|
567
|
+
const shadowPort = await freePort('127.0.0.2')
|
|
568
|
+
const listenPort = await freePort('127.0.0.1')
|
|
569
|
+
const connectPort = await freePort('127.0.0.2')
|
|
570
|
+
const target = await targetServer(shadowPort)
|
|
571
|
+
// The CONNECT destination must be a real listener, or the engine rightly 502s.
|
|
572
|
+
const connectTarget = await targetServer(connectPort)
|
|
573
|
+
try {
|
|
574
|
+
writeProxyConfig(dir, {
|
|
575
|
+
socks: { host: '127.0.0.1', port: socks.port },
|
|
576
|
+
listenPort,
|
|
577
|
+
rules: [
|
|
578
|
+
{ id: 'shadow', program: 'probe-shadow', hosts: '127.0.0.2', ports: String(shadowPort), enabled: true, shadow: true },
|
|
579
|
+
// Explicit false: a blank program row would otherwise auto-map (the
|
|
580
|
+
// client behind it is unknown, and unmapped-by-default would strand it).
|
|
581
|
+
{ id: 'connect', program: 'probe-connect', hosts: '127.0.0.2', ports: String(connectPort), enabled: true, shadow: false },
|
|
582
|
+
],
|
|
583
|
+
})
|
|
584
|
+
const started = await startProxy(dir, { waitMs: 60 })
|
|
585
|
+
assert.equal(started.ok, true, started.error)
|
|
586
|
+
const status = await proxyStatus(dir)
|
|
587
|
+
assert.equal(status.running, true)
|
|
588
|
+
assert.deepEqual(status.listen.map((item) => item.port).sort(), [shadowPort, listenPort].sort())
|
|
589
|
+
assert.equal(status.listen.every((item) => item.open), true)
|
|
590
|
+
// Same-port shadow listener: a client that only knows 127.0.0.1 gets through.
|
|
591
|
+
const viaShadow = await new Promise((resolve) => {
|
|
592
|
+
const socket = connect({ host: '127.0.0.1', port: shadowPort }, () => socket.write('GET / HTTP/1.1\r\nHost: x\r\n\r\n'))
|
|
593
|
+
let text = ''
|
|
594
|
+
socket.on('data', (chunk) => { text += chunk.toString('latin1') })
|
|
595
|
+
socket.on('close', () => resolve(text))
|
|
596
|
+
socket.on('error', () => resolve('err'))
|
|
597
|
+
})
|
|
598
|
+
assert.match(viaShadow, /200 OK/)
|
|
599
|
+
assert.deepEqual(targets.slice(-1), [`127.0.0.2:${shadowPort}`])
|
|
600
|
+
// CONNECT to an allowed destination succeeds…
|
|
601
|
+
assert.match(await connectThrough(listenPort, '127.0.0.2', connectPort), /200 Connection Established/)
|
|
602
|
+
// …and a destination outside the rules is refused by the allow-list.
|
|
603
|
+
assert.match(await connectThrough(listenPort, '127.0.0.2', connectPort + 1), /403 Forbidden/)
|
|
604
|
+
// A second start is idempotent; stop really stops.
|
|
605
|
+
assert.equal((await startProxy(dir)).alreadyRunning, true)
|
|
606
|
+
assert.equal((await stopProxy(dir)).stopped, true)
|
|
607
|
+
assert.equal((await proxyStatus(dir)).running, false)
|
|
608
|
+
} finally {
|
|
609
|
+
await stopProxy(dir).catch(() => undefined)
|
|
610
|
+
await socks.close()
|
|
611
|
+
await target.close()
|
|
612
|
+
await connectTarget.close()
|
|
613
|
+
rmSync(dir, { recursive: true, force: true })
|
|
614
|
+
}
|
|
615
|
+
})
|
|
616
|
+
|
|
617
|
+
test('SOCKS5: a split domain reply is never accepted before it is complete', async () => {
|
|
618
|
+
// 05 00 00 03 0b "example.com" 00 50 — an 18-byte reply for ATYP=domain.
|
|
619
|
+
const reply = Buffer.concat([
|
|
620
|
+
Buffer.from([0x05, 0x00, 0x00, 0x03, 11]),
|
|
621
|
+
Buffer.from('example.com'),
|
|
622
|
+
Buffer.from([0x00, 80]),
|
|
623
|
+
])
|
|
624
|
+
let fragmentSent = false
|
|
625
|
+
const socks = await scriptedSocks((client) => {
|
|
626
|
+
socksHandshake(client, () => {
|
|
627
|
+
client.write(reply.subarray(0, 4))
|
|
628
|
+
fragmentSent = true
|
|
629
|
+
setTimeout(() => client.write(reply.subarray(4)), 80)
|
|
630
|
+
})
|
|
631
|
+
})
|
|
632
|
+
let upstream = null
|
|
633
|
+
try {
|
|
634
|
+
let settledWith = 'pending'
|
|
635
|
+
const pending = connectSocks({ host: '127.0.0.1', port: socks.port }, 'example.com', 80)
|
|
636
|
+
pending.then((socket) => { upstream = socket; settledWith = 'resolved' }, () => { settledWith = 'rejected' })
|
|
637
|
+
for (let attempt = 0; attempt < 100 && fragmentSent !== true; attempt += 1) await sleep(5)
|
|
638
|
+
await sleep(25)
|
|
639
|
+
// Four bytes are a header fragment, not a reply. The old `length < 4` guard
|
|
640
|
+
// (and the NaN successor that replaced it) resolved right here and leaked
|
|
641
|
+
// the remaining handshake bytes into the tunnel as garbage.
|
|
642
|
+
assert.equal(settledWith, 'pending')
|
|
643
|
+
await pending
|
|
644
|
+
assert.equal(settledWith, 'resolved')
|
|
645
|
+
} finally {
|
|
646
|
+
upstream?.destroy()
|
|
647
|
+
await socks.close()
|
|
648
|
+
}
|
|
649
|
+
})
|
|
650
|
+
|
|
651
|
+
test('SOCKS5: bytes coalesced with the reply tail stay in the tunnel stream', async () => {
|
|
652
|
+
const socks = await scriptedSocks((client) => {
|
|
653
|
+
socksHandshake(client, () => {
|
|
654
|
+
// One write: the reply and the first tunnel bytes share a TCP segment.
|
|
655
|
+
client.write(Buffer.concat([
|
|
656
|
+
Buffer.from([0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]),
|
|
657
|
+
Buffer.from('HELLO'),
|
|
658
|
+
]))
|
|
659
|
+
})
|
|
660
|
+
})
|
|
661
|
+
let upstream = null
|
|
662
|
+
try {
|
|
663
|
+
upstream = await connectSocks({ host: '127.0.0.1', port: socks.port }, '127.0.0.1', 25)
|
|
664
|
+
// Dropping the tail would leave the caller with a silent tunnel: a protocol
|
|
665
|
+
// that answers immediately loses its first bytes.
|
|
666
|
+
assert.equal((await firstData(upstream)).toString(), 'HELLO')
|
|
667
|
+
} finally {
|
|
668
|
+
upstream?.destroy()
|
|
669
|
+
await socks.close()
|
|
670
|
+
}
|
|
671
|
+
})
|
|
672
|
+
|
|
673
|
+
test('SOCKS5: IPv6 replies are accepted, unknown ATYP / refused CONNECT are not', async () => {
|
|
674
|
+
// 22-byte IPv6 reply (ATYP=4: 4-byte header + 16-byte address + 2-byte port),
|
|
675
|
+
// deliberately split mid-header.
|
|
676
|
+
const ipv6Reply = Buffer.from([0x05, 0x00, 0x00, 0x04, ...Array(16).fill(0), 0x00, 0x50])
|
|
677
|
+
assert.equal(ipv6Reply.length, 22)
|
|
678
|
+
const okSocks = await scriptedSocks((client) => {
|
|
679
|
+
socksHandshake(client, () => {
|
|
680
|
+
client.write(ipv6Reply.subarray(0, 5))
|
|
681
|
+
setTimeout(() => client.write(ipv6Reply.subarray(5)), 30)
|
|
682
|
+
})
|
|
683
|
+
})
|
|
684
|
+
let upstream = null
|
|
685
|
+
try {
|
|
686
|
+
upstream = await connectSocks({ host: '127.0.0.1', port: okSocks.port }, '::1', 80)
|
|
687
|
+
assert.equal(upstream !== null, true)
|
|
688
|
+
} finally {
|
|
689
|
+
upstream?.destroy()
|
|
690
|
+
await okSocks.close()
|
|
691
|
+
}
|
|
692
|
+
const badAtyp = await scriptedSocks((client) => {
|
|
693
|
+
socksHandshake(client, () => client.write(Buffer.from([0x05, 0x00, 0x00, 0x09, 0, 0, 0, 0, 0, 0])))
|
|
694
|
+
})
|
|
695
|
+
try {
|
|
696
|
+
await assert.rejects(connectSocks({ host: '127.0.0.1', port: badAtyp.port }, '1.2.3.4', 80), /ATYP=9/)
|
|
697
|
+
} finally {
|
|
698
|
+
await badAtyp.close()
|
|
699
|
+
}
|
|
700
|
+
const refused = await scriptedSocks((client) => {
|
|
701
|
+
socksHandshake(client, () => client.write(Buffer.from([0x05, 0x05, 0x00, 0x01, 0, 0, 0, 0, 0, 0])))
|
|
702
|
+
})
|
|
703
|
+
try {
|
|
704
|
+
await assert.rejects(connectSocks({ host: '127.0.0.1', port: refused.port }, '1.2.3.4', 80), /connection refused/)
|
|
705
|
+
} finally {
|
|
706
|
+
await refused.close()
|
|
707
|
+
}
|
|
708
|
+
})
|
|
709
|
+
|
|
710
|
+
test('SOCKS5: greeting failures and a missing upstream reject, never hang', async () => {
|
|
711
|
+
const notSocks = await scriptedSocks((client) => {
|
|
712
|
+
client.once('data', () => client.write(Buffer.from([0x04, 0x00])))
|
|
713
|
+
})
|
|
714
|
+
try {
|
|
715
|
+
await assert.rejects(connectSocks({ host: '127.0.0.1', port: notSocks.port }, '1.2.3.4', 80), /not a SOCKS5 server/)
|
|
716
|
+
} finally {
|
|
717
|
+
await notSocks.close()
|
|
718
|
+
}
|
|
719
|
+
const needsAuth = await scriptedSocks((client) => {
|
|
720
|
+
client.once('data', () => client.write(Buffer.from([0x05, 0x02])))
|
|
721
|
+
})
|
|
722
|
+
try {
|
|
723
|
+
await assert.rejects(connectSocks({ host: '127.0.0.1', port: needsAuth.port }, '1.2.3.4', 80), /auth rejected/)
|
|
724
|
+
} finally {
|
|
725
|
+
await needsAuth.close()
|
|
726
|
+
}
|
|
727
|
+
const badVersion = await scriptedSocks((client) => {
|
|
728
|
+
socksHandshake(client, () => client.write(Buffer.from([0x04, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0])))
|
|
729
|
+
})
|
|
730
|
+
try {
|
|
731
|
+
await assert.rejects(connectSocks({ host: '127.0.0.1', port: badVersion.port }, '1.2.3.4', 80), /回包版本不是 5/)
|
|
732
|
+
} finally {
|
|
733
|
+
await badVersion.close()
|
|
734
|
+
}
|
|
735
|
+
await assert.rejects(connectSocks({}, '1.2.3.4', 80), /not configured/)
|
|
736
|
+
})
|
|
737
|
+
|
|
738
|
+
test('engine: absolute-form HTTP is rewritten to origin-form, per connection', async () => {
|
|
739
|
+
const dir = mkdtempSync(join(tmpdir(), 'ris-ide-http-'))
|
|
740
|
+
const socks = await fakeSocks(() => undefined)
|
|
741
|
+
const listenPort = await freePort('127.0.0.1')
|
|
742
|
+
const httpPort = await freePort('127.0.0.2')
|
|
743
|
+
const deniedPort = await freePort('127.0.0.2')
|
|
744
|
+
const target = await recordingTarget('127.0.0.2', httpPort)
|
|
745
|
+
try {
|
|
746
|
+
writeProxyConfig(dir, {
|
|
747
|
+
socks: { host: '127.0.0.1', port: socks.port },
|
|
748
|
+
listenPort,
|
|
749
|
+
rules: [{ id: 'http', program: 'probe-http', hosts: '127.0.0.2', ports: String(httpPort), enabled: true, shadow: false }],
|
|
750
|
+
})
|
|
751
|
+
const started = await startProxy(dir, { waitMs: 60 })
|
|
752
|
+
assert.equal(started.ok, true, started.error)
|
|
753
|
+
// 1) absolute-form is accepted, and the origin server sees origin-form.
|
|
754
|
+
const ok = await httpThrough(listenPort, `GET http://127.0.0.2:${httpPort}/path?q=1 HTTP/1.1\r\nHost: 127.0.0.2:${httpPort}\r\nConnection: close\r\n\r\n`)
|
|
755
|
+
assert.match(ok, /200 OK/)
|
|
756
|
+
assert.match(target.requests[0], /^GET \/path\?q=1 HTTP\/1\.1/)
|
|
757
|
+
assert.equal(target.requests[0].includes('http://127.0.0.2'), false, 'the absolute URL must not reach the origin')
|
|
758
|
+
assert.equal(target.requests[0].includes(`Host: 127.0.0.2:${httpPort}`), true, 'other headers are preserved')
|
|
759
|
+
// 2) a request head split across packets is buffered, not 400'd.
|
|
760
|
+
const fragmented = await httpThrough(listenPort, null, {
|
|
761
|
+
chunks: [`GET http://127.0.0.2:${httpPort}/frag HTTP/1.1\r\nHost: x\r\n`, '\r\n'],
|
|
762
|
+
gapMs: 40,
|
|
763
|
+
})
|
|
764
|
+
assert.match(fragmented, /200 OK/)
|
|
765
|
+
assert.match(target.requests[1], /^GET \/frag HTTP\/1\.1/)
|
|
766
|
+
// 3) https:// in absolute form is a protocol error, not something to guess:
|
|
767
|
+
// forwarding plaintext to a TLS port merely resets the connection, so answer
|
|
768
|
+
// 400 with a reason the caller can act on. The host IS allowed by the rules,
|
|
769
|
+
// which proves the scheme check runs before the dial.
|
|
770
|
+
const tls = await httpThrough(listenPort, `GET https://127.0.0.2:${httpPort}/tls HTTP/1.1\r\nHost: 127.0.0.2:${httpPort}\r\n\r\n`)
|
|
771
|
+
assert.match(tls, /400 Bad Request/)
|
|
772
|
+
assert.match(tls, /X-Proxy-Error: https requires CONNECT/)
|
|
773
|
+
assert.match(tls, /HTTPS_PROXY/)
|
|
774
|
+
assert.equal(target.requests.length, 2, 'nothing is dialled for an https absolute-form request')
|
|
775
|
+
// 4) a destination outside the rules is denied before any dial, and the
|
|
776
|
+
// reason is visible in the response header.
|
|
777
|
+
const denied = await httpThrough(listenPort, `GET http://127.0.0.2:${deniedPort}/nope HTTP/1.1\r\nHost: x\r\n\r\n`)
|
|
778
|
+
assert.match(denied, /403 Forbidden/)
|
|
779
|
+
assert.equal(denied.includes('X-Proxy-Denied'), true)
|
|
780
|
+
// 5) an origin-form request line is not a proxy request: reject, never guess.
|
|
781
|
+
const relative = await httpThrough(listenPort, `GET /relative HTTP/1.1\r\nHost: x\r\n\r\n`)
|
|
782
|
+
assert.match(relative, /400 Bad Request/)
|
|
783
|
+
} finally {
|
|
784
|
+
await stopProxy(dir).catch(() => undefined)
|
|
785
|
+
await socks.close()
|
|
786
|
+
await target.close()
|
|
787
|
+
rmSync(dir, { recursive: true, force: true })
|
|
788
|
+
}
|
|
789
|
+
})
|
|
790
|
+
|
|
791
|
+
test('engine pid: a pidfile naming a stranger is discarded, never trusted', async () => {
|
|
792
|
+
const dir = mkdtempSync(join(tmpdir(), 'ris-ide-pid-'))
|
|
793
|
+
mkdirSync(proxyHomeDir(), { recursive: true })
|
|
794
|
+
try {
|
|
795
|
+
// Our own pid is alive but carries no engine marker.
|
|
796
|
+
writeFileSync(proxyPidPath(), String(process.pid))
|
|
797
|
+
assert.equal(pidIsProxy(process.pid), false)
|
|
798
|
+
assert.equal(proxyPid(dir), null)
|
|
799
|
+
assert.equal(existsSync(proxyPidPath()), false, 'the foreign pidfile is removed')
|
|
800
|
+
// startProxy must not report "already running" for it: the next check (no
|
|
801
|
+
// upstream configured) is what fails, which proves identity came first.
|
|
802
|
+
writeFileSync(proxyPidPath(), String(process.pid))
|
|
803
|
+
const started = await startProxy(dir, { config: normalizeConfig({}) })
|
|
804
|
+
assert.equal(started.ok, false)
|
|
805
|
+
assert.notEqual(started.alreadyRunning, true)
|
|
806
|
+
assert.match(started.error, /上游 SOCKS/)
|
|
807
|
+
// stopProxy must never signal a stranger either.
|
|
808
|
+
writeFileSync(proxyPidPath(), String(process.pid))
|
|
809
|
+
assert.deepEqual(await stopProxy(dir), { ok: true, stopped: false })
|
|
810
|
+
assert.equal(pidAlive(process.pid), true)
|
|
811
|
+
} finally {
|
|
812
|
+
rmSync(proxyPidPath(), { force: true })
|
|
813
|
+
rmSync(dir, { recursive: true, force: true })
|
|
814
|
+
}
|
|
815
|
+
})
|
|
816
|
+
|
|
817
|
+
test('engine pid: a dead pidfile is cleared', () => {
|
|
818
|
+
mkdirSync(proxyHomeDir(), { recursive: true })
|
|
819
|
+
try {
|
|
820
|
+
writeFileSync(proxyPidPath(), '999999999')
|
|
821
|
+
assert.equal(proxyPid(), null)
|
|
822
|
+
assert.equal(existsSync(proxyPidPath()), false)
|
|
823
|
+
} finally {
|
|
824
|
+
rmSync(proxyPidPath(), { force: true })
|
|
825
|
+
}
|
|
826
|
+
})
|
|
827
|
+
|
|
828
|
+
test('engine pid: a marker-carrying process is reused and stoppable', async () => {
|
|
829
|
+
const dir = mkdtempSync(join(tmpdir(), 'ris-ide-pid-'))
|
|
830
|
+
mkdirSync(proxyHomeDir(), { recursive: true })
|
|
831
|
+
const child = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 60000)', `--marker=${PROXY_MARKER}`], { stdio: 'ignore' })
|
|
832
|
+
let exited = false
|
|
833
|
+
child.once('exit', () => { exited = true })
|
|
834
|
+
try {
|
|
835
|
+
let ready = false
|
|
836
|
+
for (let attempt = 0; attempt < 100 && ready !== true; attempt += 1) {
|
|
837
|
+
ready = pidIsProxy(child.pid) === true
|
|
838
|
+
if (ready !== true) await sleep(20)
|
|
839
|
+
}
|
|
840
|
+
assert.equal(ready, true, 'the test child must look like the engine (marker in argv)')
|
|
841
|
+
writeFileSync(proxyPidPath(), String(child.pid))
|
|
842
|
+
assert.equal(proxyPid(dir), child.pid)
|
|
843
|
+
const started = await startProxy(dir, { config: normalizeConfig({}) })
|
|
844
|
+
assert.equal(started.alreadyRunning, true)
|
|
845
|
+
assert.equal(started.pid, child.pid)
|
|
846
|
+
assert.equal((await stopProxy(dir, { graceMs: 3000 })).stopped, true)
|
|
847
|
+
for (let attempt = 0; attempt < 100 && exited !== true; attempt += 1) await sleep(20)
|
|
848
|
+
assert.equal(exited, true)
|
|
849
|
+
} finally {
|
|
850
|
+
try { child.kill('SIGKILL') } catch { /* already gone */ }
|
|
851
|
+
rmSync(proxyPidPath(), { force: true })
|
|
852
|
+
rmSync(dir, { recursive: true, force: true })
|
|
853
|
+
}
|
|
854
|
+
})
|
|
855
|
+
|
|
856
|
+
test('reading a workspace that still has the old per-workspace file migrates it', () => {
|
|
857
|
+
const ws = mkdtempSync(join(tmpdir(), 'ris-legacy-ws-'))
|
|
858
|
+
const machine = process.env.DSH_RIS_IDE_PROXY_HOME
|
|
859
|
+
const saved = join(machine, 'proxy.json')
|
|
860
|
+
try {
|
|
861
|
+
// Start from "no shared config", with the legacy file inside the workspace.
|
|
862
|
+
rmSync(saved, { force: true })
|
|
863
|
+
mkdirSync(join(ws, '.dsh-ris-ide'), { recursive: true })
|
|
864
|
+
writeFileSync(join(ws, '.dsh-ris-ide', 'proxy.json'), JSON.stringify({
|
|
865
|
+
socks: { host: '10.3.91.121', port: 1080 },
|
|
866
|
+
rules: [{ id: 'old', program: 'navicat', hosts: '1.2.3.4', ports: '1521', enabled: true, shadow: true }],
|
|
867
|
+
}))
|
|
868
|
+
const config = readProxyConfig(ws)
|
|
869
|
+
assert.equal(config.socks.host, '10.3.91.121', 'the legacy rules are still honoured')
|
|
870
|
+
assert.equal(config.rules[0].workspace, ws, 'and labelled with the workspace they came from')
|
|
871
|
+
// Nothing is written until a save: reading must not move files around.
|
|
872
|
+
assert.equal(existsSync(saved), false)
|
|
873
|
+
} finally {
|
|
874
|
+
rmSync(ws, { recursive: true, force: true })
|
|
875
|
+
}
|
|
876
|
+
})
|
|
877
|
+
|
|
878
|
+
test('a rule keeps its provider label for the dialog and the profile', () => {
|
|
879
|
+
const config = normalizeConfig({
|
|
880
|
+
socks: { host: '10.0.0.1', port: 1080 },
|
|
881
|
+
rules: [
|
|
882
|
+
{ id: 'yds-yoncode-java', label: ' YonCode java ', program: 'D:\\YonCode\\devkit\\*\\bin\\java.exe', hosts: '172.20.235.*', ports: '', enabled: true, source: 'yds' },
|
|
883
|
+
{ id: 'plain', program: 'git', hosts: 'example.com', ports: '', enabled: true },
|
|
884
|
+
],
|
|
885
|
+
})
|
|
886
|
+
assert.equal(config.rules[0].label, 'YonCode java')
|
|
887
|
+
assert.equal(config.rules[1].label, '', 'an unlabelled row stays empty, not undefined')
|
|
888
|
+
})
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
test('a driven rule keeps its per-workspace owners across a config round trip', () => {
|
|
892
|
+
// yds writes one merged row per program with an `owners` map. Rebuilding rules
|
|
893
|
+
// from a fixed field list dropped it, after which `yds stop` could no longer
|
|
894
|
+
// withdraw only its own endpoints (review D-P0-1).
|
|
895
|
+
const owners = {
|
|
896
|
+
'/ws/a': { program: 'java', hosts: '10.0.0.1', ports: '*', enabled: true, shadow: false, direct: false },
|
|
897
|
+
'/ws/b': { program: 'java.exe', hosts: '10.0.0.2,10.0.0.3', ports: '*', enabled: true, shadow: true, direct: false },
|
|
898
|
+
}
|
|
899
|
+
const config = normalizeConfig({
|
|
900
|
+
socks: { host: '127.0.0.1', port: 1080 },
|
|
901
|
+
rules: [{
|
|
902
|
+
id: 'yds-java', label: 'yds java', program: 'java', hosts: '10.0.0.1,10.0.0.2,10.0.0.3', ports: '*',
|
|
903
|
+
enabled: true, shadow: false, direct: false, source: 'yds', workspace: '/ws/b', owners,
|
|
904
|
+
}],
|
|
905
|
+
})
|
|
906
|
+
const rule = config.rules.find((row) => row.id === 'yds-java')
|
|
907
|
+
assert.ok(rule, 'the driven row survives')
|
|
908
|
+
assert.deepEqual(Object.keys(rule.owners), ['/ws/a', '/ws/b'])
|
|
909
|
+
assert.equal(rule.owners['/ws/b'].hosts, '10.0.0.2,10.0.0.3')
|
|
910
|
+
assert.equal(rule.owners['/ws/b'].shadow, true)
|
|
911
|
+
// A row without owners must not gain an empty one.
|
|
912
|
+
const plain = normalizeConfig({ rules: [{ id: 'p', program: 'git', hosts: '*', ports: '*' }] })
|
|
913
|
+
assert.equal(plain.rules[0].owners, undefined)
|
|
914
|
+
})
|