@lucky_zone/net-proxy 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +32 -2
- package/client.js +747 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +11 -0
- package/index.js +522 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +45 -4
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<svg width="36" height="36" viewBox="0 0 36 36" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<circle cx="18" cy="18" r="11.2" stroke="url(#netProxyGlobe)" stroke-width="2.2"/>
|
|
3
|
+
<path d="M7.46 14.2h21.08M7.46 21.8h21.08" stroke="url(#netProxyGlobe)" stroke-width="2" stroke-linecap="round"/>
|
|
4
|
+
<ellipse cx="18" cy="18" rx="5.4" ry="11.2" stroke="#F2A33C" stroke-width="2"/>
|
|
5
|
+
<defs>
|
|
6
|
+
<linearGradient id="netProxyGlobe" x1="7" y1="7" x2="29" y2="29" gradientUnits="userSpaceOnUse">
|
|
7
|
+
<stop stop-color="#6FC8FF"/>
|
|
8
|
+
<stop offset="1" stop-color="#3F77D8"/>
|
|
9
|
+
</linearGradient>
|
|
10
|
+
</defs>
|
|
11
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host half of the network-proxy plugin: route only the domains the user lists
|
|
3
|
+
* through the configured proxy server, and everything else directly.
|
|
4
|
+
*
|
|
5
|
+
* Neither an environment variable nor an undici dispatcher can express an
|
|
6
|
+
* allowlist: `NO_PROXY` only removes hosts, it cannot add them. This plugin
|
|
7
|
+
* therefore starts a loopback router and points the harness's own proxy policy
|
|
8
|
+
* (undici's global dispatcher plus the child environment) at that router. The
|
|
9
|
+
* router decides per connection: a listed host is tunnelled through the user's
|
|
10
|
+
* proxy, any other host is reached directly.
|
|
11
|
+
*
|
|
12
|
+
* Editing the proxy or the domain list updates the running router in place, so
|
|
13
|
+
* the installed policy and every child environment keep pointing at one stable
|
|
14
|
+
* loopback port; only switching the plugin off, or clearing the list or the
|
|
15
|
+
* proxy, releases the policy.
|
|
16
|
+
* @module @lucky_zone/net-proxy
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import http from 'node:http'
|
|
20
|
+
import https from 'node:https'
|
|
21
|
+
import net from 'node:net'
|
|
22
|
+
import tls from 'node:tls'
|
|
23
|
+
import { createRequire } from 'node:module'
|
|
24
|
+
import { join } from 'node:path'
|
|
25
|
+
import { pathToFileURL } from 'node:url'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Import one package that the running installation owns.
|
|
29
|
+
*
|
|
30
|
+
* A bundle installed from a directory is a link, and the linked package has no
|
|
31
|
+
* `node_modules` of its own; the profile's module-resolution interception routes
|
|
32
|
+
* the names this package lists in `peerDependencies` to the installation. When a
|
|
33
|
+
* deployment resolves those names by another route, the installation anchor is
|
|
34
|
+
* the second attempt.
|
|
35
|
+
* @param specifier - bare package specifier to load.
|
|
36
|
+
* @returns the module namespace.
|
|
37
|
+
*/
|
|
38
|
+
async function importInstallationPackage(specifier) {
|
|
39
|
+
try {
|
|
40
|
+
return await import(specifier)
|
|
41
|
+
} catch (error) {
|
|
42
|
+
const anchor = process.env.DSH_INSTALL_DIR
|
|
43
|
+
if (anchor !== undefined && anchor !== '') {
|
|
44
|
+
try {
|
|
45
|
+
const anchorRequire = createRequire(join(anchor, 'package.json'))
|
|
46
|
+
return await import(pathToFileURL(anchorRequire.resolve(specifier)).href)
|
|
47
|
+
} catch {
|
|
48
|
+
// Report the plain import failure: it names the missing package.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
throw error
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const Schema = (await importInstallationPackage('@deepseek-ai/schemastery')).default
|
|
56
|
+
const httpProxy = await importInstallationPackage('@deepseek-ai/dsh-http-proxy')
|
|
57
|
+
|
|
58
|
+
/** Settings this plugin exposes to the Plugins page and its sidebar panel. */
|
|
59
|
+
export const Config = Schema.object({
|
|
60
|
+
enabled: Schema.boolean()
|
|
61
|
+
.default(false)
|
|
62
|
+
.description('Route the listed domains through the proxy server.')
|
|
63
|
+
.volatile(),
|
|
64
|
+
proxyUrl: Schema.string()
|
|
65
|
+
.default('')
|
|
66
|
+
.description('Proxy address with an http:// or https:// scheme, such as http://127.0.0.1:7890.')
|
|
67
|
+
.volatile(),
|
|
68
|
+
domains: Schema.string()
|
|
69
|
+
.default('github.com\ngithubusercontent.com')
|
|
70
|
+
.description('Hosts routed through the proxy, one per line or comma-separated; each matches its subdomains. Everything else connects directly.')
|
|
71
|
+
.volatile(),
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
/** Largest proxy response head the router accepts before giving up. */
|
|
75
|
+
const MAX_RESPONSE_HEAD = 64 * 1024
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Split configured text into domains.
|
|
79
|
+
* @param text - the configured value, separated by newlines, commas, or spaces.
|
|
80
|
+
* @returns domain entries without duplicates, in the order written.
|
|
81
|
+
*/
|
|
82
|
+
function parseDomains(text) {
|
|
83
|
+
const seen = new Set()
|
|
84
|
+
const entries = []
|
|
85
|
+
for (const raw of String(text).split(/[\s,]+/)) {
|
|
86
|
+
const entry = raw.trim()
|
|
87
|
+
if (entry === '') continue
|
|
88
|
+
const key = entry.toLowerCase()
|
|
89
|
+
if (seen.has(key)) continue
|
|
90
|
+
seen.add(key)
|
|
91
|
+
entries.push(entry)
|
|
92
|
+
}
|
|
93
|
+
return entries
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Whether the harness can route through this address. */
|
|
97
|
+
function supportedProxyUrl(value) {
|
|
98
|
+
try {
|
|
99
|
+
const url = new URL(value)
|
|
100
|
+
return url.protocol === 'http:' || url.protocol === 'https:'
|
|
101
|
+
} catch {
|
|
102
|
+
return false
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The proxy address without credentials, for log lines. */
|
|
107
|
+
function displayProxy(value) {
|
|
108
|
+
try {
|
|
109
|
+
const url = new URL(value)
|
|
110
|
+
return `${url.protocol}//${url.host}`
|
|
111
|
+
} catch {
|
|
112
|
+
return 'the configured address'
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Split one allowlist entry into host and optional port, accepting a bracketed
|
|
118
|
+
* IPv6 literal without mistaking its colons for a port separator.
|
|
119
|
+
* @param entry - one already-trimmed entry.
|
|
120
|
+
* @returns host and, when the entry carries one, its port text.
|
|
121
|
+
*/
|
|
122
|
+
function splitEntry(entry) {
|
|
123
|
+
if (entry.startsWith('[')) {
|
|
124
|
+
const close = entry.indexOf(']')
|
|
125
|
+
if (close !== -1) {
|
|
126
|
+
const rest = entry.slice(close + 1)
|
|
127
|
+
return { host: entry.slice(1, close), port: rest.startsWith(':') ? rest.slice(1) : undefined }
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const colon = entry.indexOf(':')
|
|
131
|
+
if (colon !== -1 && entry.indexOf(':', colon + 1) === -1) {
|
|
132
|
+
return { host: entry.slice(0, colon), port: entry.slice(colon + 1) }
|
|
133
|
+
}
|
|
134
|
+
return { host: entry, port: undefined }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Whether a host names this machine, where a proxy cannot meaningfully reach it.
|
|
139
|
+
* @param hostname - a connection's target host, bracketed or not.
|
|
140
|
+
* @returns true for a loopback or unspecified address.
|
|
141
|
+
*/
|
|
142
|
+
function isLoopbackHost(hostname) {
|
|
143
|
+
const host = String(hostname).replace(/^\[|\]$/g, '').replace(/\.$/, '').toLowerCase()
|
|
144
|
+
if (host === 'localhost' || host.endsWith('.localhost')) return true
|
|
145
|
+
if (host === '::1' || host === '::' || host === '0.0.0.0') return true
|
|
146
|
+
if (/^127\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/.test(host)) return true
|
|
147
|
+
const mappedHigh = /^::ffff:([0-9a-f]{1,4}):[0-9a-f]{1,4}$/.exec(host)?.[1]
|
|
148
|
+
return mappedHigh !== undefined && Number.parseInt(mappedHigh, 16) >>> 8 === 127
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Whether one host is listed for proxying.
|
|
153
|
+
*
|
|
154
|
+
* An entry names a host and matches it together with every subdomain under it;
|
|
155
|
+
* a leading `.` or `*.` is accepted, an entry may carry a `:port`, and `*`
|
|
156
|
+
* matches every host. CIDR ranges are not matched, matching the harness's own
|
|
157
|
+
* bypass rules. Loopback is never proxied, even through `*`, so local services
|
|
158
|
+
* cannot be sent to a remote proxy.
|
|
159
|
+
* @param domains - parsed allowlist entries.
|
|
160
|
+
* @param hostname - the connection's target host.
|
|
161
|
+
* @param port - the connection's target port.
|
|
162
|
+
* @returns true when the connection must go through the proxy.
|
|
163
|
+
*/
|
|
164
|
+
function listedForProxy(domains, hostname, port) {
|
|
165
|
+
const host = String(hostname).replace(/^\[|\]$/g, '').replace(/\.$/, '').toLowerCase()
|
|
166
|
+
if (host === '' || isLoopbackHost(host)) return false
|
|
167
|
+
for (const raw of domains) {
|
|
168
|
+
const entry = raw.toLowerCase()
|
|
169
|
+
if (entry === '*') return true
|
|
170
|
+
const split = splitEntry(entry)
|
|
171
|
+
if (split.port !== undefined && split.port !== String(port)) continue
|
|
172
|
+
const candidate = split.host.replace(/^\*?\./, '').replace(/\.$/, '')
|
|
173
|
+
if (candidate === '') continue
|
|
174
|
+
if (host === candidate || host.endsWith(`.${candidate}`)) return true
|
|
175
|
+
}
|
|
176
|
+
return false
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Format a tunnel authority for a CONNECT request line.
|
|
181
|
+
* @param host - target host, possibly an IPv6 literal.
|
|
182
|
+
* @param port - target port.
|
|
183
|
+
* @returns `host:port`, bracketing an IPv6 literal.
|
|
184
|
+
*/
|
|
185
|
+
function authority(host, port) {
|
|
186
|
+
return host.includes(':') && !host.startsWith('[') ? `[${host}]:${port}` : `${host}:${port}`
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Read a CONNECT authority as the client wrote it.
|
|
191
|
+
* @param value - the request's authority text.
|
|
192
|
+
* @returns host and port, or undefined when it cannot be read.
|
|
193
|
+
*/
|
|
194
|
+
function parseAuthority(value) {
|
|
195
|
+
const text = String(value)
|
|
196
|
+
const split = splitEntry(text)
|
|
197
|
+
const host = split.host.replace(/^\[|\]$/g, '')
|
|
198
|
+
if (host === '') return undefined
|
|
199
|
+
const port = split.port === undefined ? 443 : Number(split.port)
|
|
200
|
+
return { host, port: Number.isInteger(port) && port > 0 ? port : 443 }
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Start the loopback router that applies the domain allowlist.
|
|
205
|
+
* @param options - the upstream proxy and the domains to send through it.
|
|
206
|
+
* @returns the listening port, an in-place update, and a disposer.
|
|
207
|
+
*/
|
|
208
|
+
function createRouter(options) {
|
|
209
|
+
const state = { upstream: options.upstream, domains: options.domains }
|
|
210
|
+
const sockets = new Set()
|
|
211
|
+
const track = (socket) => {
|
|
212
|
+
sockets.add(socket)
|
|
213
|
+
socket.on('close', () => { sockets.delete(socket) })
|
|
214
|
+
return socket
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Read the current upstream settings.
|
|
219
|
+
* @returns transport choice, host, port, and the Proxy-Authorization header value.
|
|
220
|
+
*/
|
|
221
|
+
const proxyOf = () => {
|
|
222
|
+
const url = new URL(state.upstream)
|
|
223
|
+
const secure = url.protocol === 'https:'
|
|
224
|
+
return {
|
|
225
|
+
secure,
|
|
226
|
+
host: url.hostname,
|
|
227
|
+
port: Number(url.port) || (secure ? 443 : 80),
|
|
228
|
+
auth: url.username === '' && url.password === ''
|
|
229
|
+
? undefined
|
|
230
|
+
: `Basic ${Buffer.from(`${decodeURIComponent(url.username)}:${decodeURIComponent(url.password)}`).toString('base64')}`,
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const server = http.createServer((req, res) => {
|
|
235
|
+
let target
|
|
236
|
+
try {
|
|
237
|
+
target = new URL(req.url ?? '')
|
|
238
|
+
} catch {
|
|
239
|
+
res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' })
|
|
240
|
+
res.end('net-proxy: absolute-form request URL required')
|
|
241
|
+
return
|
|
242
|
+
}
|
|
243
|
+
if (target.protocol !== 'http:') {
|
|
244
|
+
res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' })
|
|
245
|
+
res.end('net-proxy: https requests must use CONNECT')
|
|
246
|
+
return
|
|
247
|
+
}
|
|
248
|
+
const port = Number(target.port) || 80
|
|
249
|
+
const viaProxy = listedForProxy(state.domains, target.hostname, port)
|
|
250
|
+
const headers = { ...req.headers }
|
|
251
|
+
delete headers['proxy-connection']
|
|
252
|
+
let requestOptions
|
|
253
|
+
let transport = http
|
|
254
|
+
if (viaProxy) {
|
|
255
|
+
const proxy = proxyOf()
|
|
256
|
+
if (proxy.auth !== undefined) headers['proxy-authorization'] = proxy.auth
|
|
257
|
+
requestOptions = {
|
|
258
|
+
host: proxy.host,
|
|
259
|
+
port: proxy.port,
|
|
260
|
+
method: req.method,
|
|
261
|
+
path: target.href,
|
|
262
|
+
headers,
|
|
263
|
+
}
|
|
264
|
+
if (proxy.secure) transport = https
|
|
265
|
+
} else {
|
|
266
|
+
delete headers['proxy-authorization']
|
|
267
|
+
headers.host = target.host
|
|
268
|
+
requestOptions = {
|
|
269
|
+
host: target.hostname,
|
|
270
|
+
port,
|
|
271
|
+
method: req.method,
|
|
272
|
+
path: `${target.pathname}${target.search}`,
|
|
273
|
+
headers,
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const forward = transport.request(requestOptions, (answer) => {
|
|
277
|
+
res.writeHead(answer.statusCode ?? 502, answer.headers)
|
|
278
|
+
answer.pipe(res)
|
|
279
|
+
})
|
|
280
|
+
forward.on('error', () => {
|
|
281
|
+
if (!res.headersSent) res.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' })
|
|
282
|
+
res.end('net-proxy: request failed')
|
|
283
|
+
})
|
|
284
|
+
req.pipe(forward)
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
server.on('connect', (req, client, head) => {
|
|
288
|
+
client.pause()
|
|
289
|
+
track(client)
|
|
290
|
+
const target = parseAuthority(req.url)
|
|
291
|
+
if (target === undefined) {
|
|
292
|
+
client.end('HTTP/1.1 400 Bad Request\r\n\r\n')
|
|
293
|
+
return
|
|
294
|
+
}
|
|
295
|
+
const allowed = listedForProxy(state.domains, target.host, target.port)
|
|
296
|
+
openTunnel(target, allowed).then((remote) => {
|
|
297
|
+
track(remote)
|
|
298
|
+
if (client.destroyed) {
|
|
299
|
+
remote.destroy()
|
|
300
|
+
return
|
|
301
|
+
}
|
|
302
|
+
client.write('HTTP/1.1 200 Connection Established\r\n\r\n')
|
|
303
|
+
if (head.length > 0) remote.write(head)
|
|
304
|
+
remote.pipe(client)
|
|
305
|
+
client.pipe(remote)
|
|
306
|
+
client.resume()
|
|
307
|
+
remote.on('error', () => { client.destroy() })
|
|
308
|
+
client.on('error', () => { remote.destroy() })
|
|
309
|
+
}).catch((error) => {
|
|
310
|
+
const status = typeof error?.status === 'number' ? error.status : 502
|
|
311
|
+
if (!client.destroyed) client.end(`HTTP/1.1 ${status} Bad Gateway\r\n\r\n`)
|
|
312
|
+
})
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
server.on('clientError', (_error, socket) => {
|
|
316
|
+
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n')
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Connect to the target directly, or ask the upstream proxy for a tunnel.
|
|
321
|
+
* @param target - host and port to reach.
|
|
322
|
+
* @param allowed - whether the host is on the allowlist.
|
|
323
|
+
* @returns a socket already past any proxy handshake.
|
|
324
|
+
*/
|
|
325
|
+
function openTunnel(target, allowed) {
|
|
326
|
+
if (!allowed) {
|
|
327
|
+
return new Promise((resolve, reject) => {
|
|
328
|
+
const socket = net.connect(target.port, target.host, () => { resolve(socket) })
|
|
329
|
+
socket.once('error', reject)
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
const proxy = proxyOf()
|
|
333
|
+
return new Promise((resolve, reject) => {
|
|
334
|
+
let settled = false
|
|
335
|
+
const socket = proxy.secure
|
|
336
|
+
? tls.connect({ host: proxy.host, port: proxy.port, servername: proxy.host })
|
|
337
|
+
: net.connect(proxy.port, proxy.host)
|
|
338
|
+
const fail = (error) => {
|
|
339
|
+
if (settled) return
|
|
340
|
+
settled = true
|
|
341
|
+
socket.destroy()
|
|
342
|
+
reject(error)
|
|
343
|
+
}
|
|
344
|
+
socket.once('error', fail)
|
|
345
|
+
socket.once(proxy.secure ? 'secureConnect' : 'connect', () => {
|
|
346
|
+
const lines = [
|
|
347
|
+
`CONNECT ${authority(target.host, target.port)} HTTP/1.1`,
|
|
348
|
+
`Host: ${authority(target.host, target.port)}`,
|
|
349
|
+
]
|
|
350
|
+
if (proxy.auth !== undefined) lines.push(`Proxy-Authorization: ${proxy.auth}`)
|
|
351
|
+
socket.write(`${lines.join('\r\n')}\r\n\r\n`)
|
|
352
|
+
})
|
|
353
|
+
let buffer = Buffer.alloc(0)
|
|
354
|
+
const onData = (chunk) => {
|
|
355
|
+
buffer = Buffer.concat([buffer, chunk])
|
|
356
|
+
const end = buffer.indexOf('\r\n\r\n')
|
|
357
|
+
if (end < 0) {
|
|
358
|
+
if (buffer.length > MAX_RESPONSE_HEAD) fail(new Error('proxy response head is too large'))
|
|
359
|
+
return
|
|
360
|
+
}
|
|
361
|
+
socket.pause()
|
|
362
|
+
socket.off('data', onData)
|
|
363
|
+
const headText = buffer.subarray(0, end).toString('latin1')
|
|
364
|
+
const rest = buffer.subarray(end + 4)
|
|
365
|
+
const status = Number(/^HTTP\/1\.[01] (\d{3})/.exec(headText)?.[1] ?? 0)
|
|
366
|
+
if (status !== 200) {
|
|
367
|
+
const error = new Error(`proxy answered ${status}`)
|
|
368
|
+
error.status = status
|
|
369
|
+
fail(error)
|
|
370
|
+
return
|
|
371
|
+
}
|
|
372
|
+
settled = true
|
|
373
|
+
if (rest.length > 0) socket.unshift(rest)
|
|
374
|
+
resolve(socket)
|
|
375
|
+
}
|
|
376
|
+
socket.on('data', onData)
|
|
377
|
+
socket.once('close', () => { if (!settled) fail(new Error('proxy closed the connection')) })
|
|
378
|
+
})
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return new Promise((resolve, reject) => {
|
|
382
|
+
server.once('error', reject)
|
|
383
|
+
server.listen(0, '127.0.0.1', () => {
|
|
384
|
+
const address = server.address()
|
|
385
|
+
if (typeof address !== 'object' || address === null) {
|
|
386
|
+
reject(new Error('net-proxy: router did not report a port'))
|
|
387
|
+
return
|
|
388
|
+
}
|
|
389
|
+
resolve({
|
|
390
|
+
port: address.port,
|
|
391
|
+
update(next) {
|
|
392
|
+
if (typeof next.upstream === 'string' && next.upstream !== '') state.upstream = next.upstream
|
|
393
|
+
if (Array.isArray(next.domains)) state.domains = next.domains
|
|
394
|
+
},
|
|
395
|
+
close: async () => {
|
|
396
|
+
for (const socket of sockets) socket.destroy()
|
|
397
|
+
sockets.clear()
|
|
398
|
+
await new Promise((done) => { server.close(() => { done() }) })
|
|
399
|
+
},
|
|
400
|
+
})
|
|
401
|
+
})
|
|
402
|
+
})
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Present the router as the environment the harness's proxy installer reads.
|
|
407
|
+
* @param port - the router's loopback port.
|
|
408
|
+
* @returns an EnvLookup for both schemes.
|
|
409
|
+
*/
|
|
410
|
+
function routerEnvironment(port) {
|
|
411
|
+
const value = `http://127.0.0.1:${port}`
|
|
412
|
+
return {
|
|
413
|
+
get(name) {
|
|
414
|
+
const key = String(name).toLowerCase()
|
|
415
|
+
if (key === 'http_proxy' || key === 'https_proxy') return { value }
|
|
416
|
+
return undefined
|
|
417
|
+
},
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Keep the process policy in step with the live configuration.
|
|
423
|
+
* @param ctx - owning plugin context.
|
|
424
|
+
* @param config - validated plugin configuration, whose volatile fields are live references.
|
|
425
|
+
*/
|
|
426
|
+
export function apply(ctx, config) {
|
|
427
|
+
let disposed = false
|
|
428
|
+
let generation = 0
|
|
429
|
+
let session
|
|
430
|
+
let tail = Promise.resolve()
|
|
431
|
+
|
|
432
|
+
const read = () => ({
|
|
433
|
+
enabled: config.enabled.get() === true,
|
|
434
|
+
proxyUrl: String(config.proxyUrl.get() ?? '').trim(),
|
|
435
|
+
domains: String(config.domains.get() ?? ''),
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
const stop = async (current) => {
|
|
439
|
+
try {
|
|
440
|
+
await current.release()
|
|
441
|
+
} catch (error) {
|
|
442
|
+
ctx.logger.warn('net-proxy: releasing the previous policy failed: %s', error instanceof Error ? error.message : String(error))
|
|
443
|
+
}
|
|
444
|
+
try {
|
|
445
|
+
await current.router.close()
|
|
446
|
+
} catch (error) {
|
|
447
|
+
ctx.logger.warn('net-proxy: closing the previous router failed: %s', error instanceof Error ? error.message : String(error))
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const sync = async () => {
|
|
452
|
+
if (disposed) return
|
|
453
|
+
const token = ++generation
|
|
454
|
+
const state = read()
|
|
455
|
+
const domains = parseDomains(state.domains)
|
|
456
|
+
const usable = supportedProxyUrl(state.proxyUrl)
|
|
457
|
+
const active = state.enabled && usable && domains.length > 0
|
|
458
|
+
const previous = session
|
|
459
|
+
|
|
460
|
+
if (!active) {
|
|
461
|
+
session = undefined
|
|
462
|
+
if (previous !== undefined) await stop(previous)
|
|
463
|
+
if (state.enabled && !usable) {
|
|
464
|
+
ctx.logger.warn('net-proxy: "Use proxy" is on but no usable proxy server is set; requests connect directly')
|
|
465
|
+
} else if (state.enabled && domains.length === 0) {
|
|
466
|
+
ctx.logger.warn('net-proxy: the proxied-domain list is empty; requests connect directly')
|
|
467
|
+
}
|
|
468
|
+
return
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (previous !== undefined) {
|
|
472
|
+
previous.router.update({ upstream: state.proxyUrl, domains })
|
|
473
|
+
ctx.logger.info('net-proxy: %d listed domain(s) go through %s; everything else connects directly', domains.length, displayProxy(state.proxyUrl))
|
|
474
|
+
return
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
let router
|
|
478
|
+
try {
|
|
479
|
+
router = await createRouter({ upstream: state.proxyUrl, domains })
|
|
480
|
+
} catch (error) {
|
|
481
|
+
ctx.logger.error('net-proxy: starting the domain router failed: %s', error instanceof Error ? error.message : String(error))
|
|
482
|
+
return
|
|
483
|
+
}
|
|
484
|
+
if (disposed || token !== generation) {
|
|
485
|
+
await router.close()
|
|
486
|
+
return
|
|
487
|
+
}
|
|
488
|
+
try {
|
|
489
|
+
const release = await httpProxy.installProxyFromEnvironment(routerEnvironment(router.port), (message) => {
|
|
490
|
+
ctx.logger.warn('net-proxy: %s', message)
|
|
491
|
+
})
|
|
492
|
+
if (disposed || token !== generation) {
|
|
493
|
+
await release()
|
|
494
|
+
await router.close()
|
|
495
|
+
return
|
|
496
|
+
}
|
|
497
|
+
session = { release, router }
|
|
498
|
+
ctx.logger.info('net-proxy: %d listed domain(s) go through %s; everything else connects directly', domains.length, displayProxy(state.proxyUrl))
|
|
499
|
+
} catch (error) {
|
|
500
|
+
await router.close()
|
|
501
|
+
ctx.logger.error('net-proxy: installing the proxy policy failed: %s', error instanceof Error ? error.message : String(error))
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const schedule = () => {
|
|
506
|
+
tail = tail.then(sync, sync)
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
ctx.effect(() => {
|
|
510
|
+
schedule()
|
|
511
|
+
const off = ctx.on('loader/volatile-update', schedule)
|
|
512
|
+
return async () => {
|
|
513
|
+
disposed = true
|
|
514
|
+
generation += 1
|
|
515
|
+
off()
|
|
516
|
+
const previous = session
|
|
517
|
+
session = undefined
|
|
518
|
+
if (previous !== undefined) await stop(previous)
|
|
519
|
+
await tail.catch(() => {})
|
|
520
|
+
}
|
|
521
|
+
}, 'net-proxy: domain allowlist policy')
|
|
522
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Network proxy",
|
|
4
|
+
"description": "Routes only the domains you list through your proxy server; every other host connects directly. Covers the Host's own requests (model calls, web fetch, HTTP MCP) and commands or terminals started afterwards. Set the proxy server and the domain list on this plugin's row page in Plugins (click the row title), or in the Network proxy panel in the left sidebar. Each entry also matches its subdomains; localhost and loopback always connect directly; only http:// and https:// proxies are supported. If the proxy becomes unreachable, requests to the listed domains fail until you turn off Use proxy or remove them from the list."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "网络代理",
|
|
4
|
+
"description": "只把列表中列出的域名交给你的代理服务器,其余主机一律直连。覆盖宿主自身的请求(模型调用、网页抓取、HTTP MCP),以及之后新启动的命令与终端。在「插件管理」中点击本插件这一行的标题打开配置,或使用左侧栏的「网络代理」面板。条目同时匹配子域名;localhost 与本机回环始终直连;仅支持 http:// 与 https:// 代理。代理不可用时,列表内域名的请求会失败,直到你关闭「使用代理」或把这些域名从列表中移除。"
|
|
5
|
+
}
|
|
6
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lucky_zone/net-proxy",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "DeepSeek Harness bundle that routes only listed domains through an HTTP proxy and connects everything else directly.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.js",
|
|
8
|
+
"./client": "./client.js",
|
|
9
|
+
"./locale/*.json": "./locale/*.json",
|
|
10
|
+
"./icon": "./icon.svg"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"index.js",
|
|
14
|
+
"client.js",
|
|
15
|
+
"cordis.patch.yml",
|
|
16
|
+
"locale/*.json",
|
|
17
|
+
"icon.svg",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@deepseek-ai/dsh-http-proxy": "*",
|
|
23
|
+
"@deepseek-ai/schemastery": "*"
|
|
24
|
+
},
|
|
25
|
+
"dsh": {
|
|
26
|
+
"bundle": {
|
|
27
|
+
"patch": "./cordis.patch.yml"
|
|
28
|
+
},
|
|
29
|
+
"client": {
|
|
30
|
+
"platform": "web",
|
|
31
|
+
"immediately": true
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"author": "lucky_zone",
|
|
36
|
+
"keywords": [
|
|
37
|
+
"dsh",
|
|
38
|
+
"dsh-plugin",
|
|
39
|
+
"deepseek-harness",
|
|
40
|
+
"proxy",
|
|
41
|
+
"network-proxy",
|
|
42
|
+
"allowlist"
|
|
43
|
+
],
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
}
|
|
47
|
+
}
|