@lucky_zone/net-proxy 1.0.0 → 1.1.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 +18 -2
- package/client.js +26 -0
- package/index.js +241 -64
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# Network proxy
|
|
2
2
|
|
|
3
|
-
Routes only the domains you list through one proxy server; every other host connects directly.
|
|
3
|
+
Routes only the domains you list through one proxy server; every other host connects directly, and a direct connection that fails can be retried once through the proxy.
|
|
4
4
|
|
|
5
5
|
## Use
|
|
6
6
|
|
|
7
7
|
- **Left sidebar → Network proxy**: the panel shows what is routed and edits the same settings.
|
|
8
8
|
- **Plugins → Network proxy** (click the row title): the row's configuration page with the same fields.
|
|
9
9
|
|
|
10
|
-
Fill in **Proxy server** (for example `http://127.0.0.1:7890`), list the **Proxied domains** that must go through it,
|
|
10
|
+
Fill in **Proxy server** (for example `http://127.0.0.1:7890`), list the **Proxied domains** that must go through it, keep **Use proxy** on, and click **Save**. Turning it off, or disabling the plugin, returns the process to the launch environment's own proxy settings.
|
|
11
11
|
|
|
12
12
|
## How domains match
|
|
13
13
|
|
|
@@ -17,6 +17,22 @@ Fill in **Proxy server** (for example `http://127.0.0.1:7890`), list the **Proxi
|
|
|
17
17
|
- CIDR ranges are not matched.
|
|
18
18
|
- `localhost`, `127.0.0.0/8`, `::1`, and `0.0.0.0` always connect directly, so the Web UI and local services never enter the proxy.
|
|
19
19
|
|
|
20
|
+
## Fallback on failure
|
|
21
|
+
|
|
22
|
+
With **Retry direct failures through the proxy** on (the default), a host outside the list is first approached directly; if that attempt fails, it is tried once more through the proxy server. The same applies to the Host's own requests and to commands and terminals started afterwards, because all of them reach the proxy through the plugin's loopback router.
|
|
23
|
+
|
|
24
|
+
What the fallback covers:
|
|
25
|
+
|
|
26
|
+
- A direct attempt that cannot resolve, connect, or times out (10 seconds).
|
|
27
|
+
- A TLS handshake that dies before the origin sends anything back: the tunnel is moved to the proxy and the client's buffered bytes are replayed, so the client never sees the switch.
|
|
28
|
+
- Plain-HTTP `GET` and `HEAD` requests that fail before any response arrives.
|
|
29
|
+
|
|
30
|
+
What it does not cover:
|
|
31
|
+
|
|
32
|
+
- After the origin has started answering, a later failure is not retried: the client already consumed part of the stream, and replaying it would corrupt the response.
|
|
33
|
+
- A plain-HTTP request with a body (`POST`, `PUT`, ...) is not replayed.
|
|
34
|
+
- Allowlisted hosts already use the proxy first; the fallback does not try them directly.
|
|
35
|
+
|
|
20
36
|
## What the proxy covers
|
|
21
37
|
|
|
22
38
|
- The Host's own `fetch` traffic for listed hosts: model requests, web fetch, and HTTP MCP. If your model endpoint also needs the proxy, add its host to the list.
|
package/client.js
CHANGED
|
@@ -35,6 +35,7 @@ window.__ModuleLoader__.load({
|
|
|
35
35
|
covers: 'What it covers',
|
|
36
36
|
coversHost: 'Host requests: model calls, web fetch, HTTP MCP',
|
|
37
37
|
coversChild: 'Commands and terminals started after the change',
|
|
38
|
+
coversFallback: 'Unlisted hosts retry through the proxy when a direct connection fails',
|
|
38
39
|
directAlways: 'localhost and 127.0.0.1 always connect directly',
|
|
39
40
|
enabledLabel: 'Use proxy',
|
|
40
41
|
enabledHint: 'Applies to the Host\u2019s own requests and to commands and terminals started afterwards.',
|
|
@@ -45,6 +46,8 @@ window.__ModuleLoader__.load({
|
|
|
45
46
|
domainsHint: 'One per line or comma-separated. Each entry also matches its subdomains; every other host connects directly.',
|
|
46
47
|
domainsInvalid: 'Write host names only, without a scheme or path, for example github.com.',
|
|
47
48
|
domainsPlaceholder: 'github.com',
|
|
49
|
+
fallbackLabel: 'Retry direct failures through the proxy',
|
|
50
|
+
fallbackHint: 'A host outside the list that cannot be reached directly is tried once more through the proxy server.',
|
|
48
51
|
save: 'Save',
|
|
49
52
|
saving: 'Saving\u2026',
|
|
50
53
|
saved: 'Saved',
|
|
@@ -76,6 +79,7 @@ window.__ModuleLoader__.load({
|
|
|
76
79
|
covers: '生效范围',
|
|
77
80
|
coversHost: '宿主请求:模型调用、网页抓取、HTTP MCP',
|
|
78
81
|
coversChild: '改动之后新启动的命令与终端',
|
|
82
|
+
coversFallback: '列表外站点直连失败时改用代理重试一次',
|
|
79
83
|
directAlways: 'localhost 与 127.0.0.1 始终直连',
|
|
80
84
|
enabledLabel: '使用代理',
|
|
81
85
|
enabledHint: '作用于宿主自身的请求,以及之后新启动的命令与终端。',
|
|
@@ -86,6 +90,8 @@ window.__ModuleLoader__.load({
|
|
|
86
90
|
domainsHint: '每行一个或用逗号分隔;条目同时匹配其子域名,其余主机一律直连。',
|
|
87
91
|
domainsInvalid: '只填主机名,不要带协议或路径,例如 github.com。',
|
|
88
92
|
domainsPlaceholder: 'github.com',
|
|
93
|
+
fallbackLabel: '直连失败时改用代理重试',
|
|
94
|
+
fallbackHint: '列表外的站点如果无法直连,会再通过代理服务器尝试一次。',
|
|
89
95
|
save: '保存',
|
|
90
96
|
saving: '保存中…',
|
|
91
97
|
saved: '已保存',
|
|
@@ -305,6 +311,7 @@ window.__ModuleLoader__.load({
|
|
|
305
311
|
enabled: value !== undefined && value !== null && value.enabled === true,
|
|
306
312
|
proxyUrl: value !== undefined && value !== null && typeof value.proxyUrl === 'string' ? value.proxyUrl : '',
|
|
307
313
|
domains: value !== undefined && value !== null && typeof value.domains === 'string' ? value.domains : '',
|
|
314
|
+
fallback: value !== undefined && value !== null && value.fallback === true,
|
|
308
315
|
}
|
|
309
316
|
}
|
|
310
317
|
|
|
@@ -436,6 +443,7 @@ window.__ModuleLoader__.load({
|
|
|
436
443
|
h('ul', { className: 'dsh-np-list' },
|
|
437
444
|
h('li', null, t('coversHost')),
|
|
438
445
|
h('li', null, t('coversChild')),
|
|
446
|
+
value.fallback ? h('li', null, t('coversFallback')) : null,
|
|
439
447
|
h('li', null, t('directAlways')),
|
|
440
448
|
),
|
|
441
449
|
),
|
|
@@ -482,6 +490,7 @@ window.__ModuleLoader__.load({
|
|
|
482
490
|
const dirty = draft.enabled !== value.enabled
|
|
483
491
|
|| proxyText !== value.proxyUrl
|
|
484
492
|
|| normalizeDomains(draft.domains) !== value.domains
|
|
493
|
+
|| draft.fallback !== value.fallback
|
|
485
494
|
const blocked = proxyInvalid || proxyMissing || domainsInvalid
|
|
486
495
|
const saving = phase === 'saving'
|
|
487
496
|
|
|
@@ -514,12 +523,14 @@ window.__ModuleLoader__.load({
|
|
|
514
523
|
{ op: 'set', path: ['enabled'], value: draft.enabled },
|
|
515
524
|
{ op: 'set', path: ['proxyUrl'], value: proxyText },
|
|
516
525
|
{ op: 'set', path: ['domains'], value: normalizeDomains(draft.domains) },
|
|
526
|
+
{ op: 'set', path: ['fallback'], value: draft.fallback },
|
|
517
527
|
], true)
|
|
518
528
|
|
|
519
529
|
const reset = () => write([
|
|
520
530
|
{ op: 'unset', path: ['enabled'] },
|
|
521
531
|
{ op: 'unset', path: ['proxyUrl'] },
|
|
522
532
|
{ op: 'unset', path: ['domains'] },
|
|
533
|
+
{ op: 'unset', path: ['fallback'] },
|
|
523
534
|
], false)
|
|
524
535
|
|
|
525
536
|
const feedback = failure
|
|
@@ -590,6 +601,21 @@ window.__ModuleLoader__.load({
|
|
|
590
601
|
? t('emptyDomains')
|
|
591
602
|
: t('domainsHint')),
|
|
592
603
|
),
|
|
604
|
+
h('div', { className: 'dsh-np-row' },
|
|
605
|
+
h('div', { className: 'dsh-np-rowText' },
|
|
606
|
+
h('span', { className: 'dsh-np-label' }, t('fallbackLabel')),
|
|
607
|
+
h('span', { className: 'dsh-np-hint' }, t('fallbackHint')),
|
|
608
|
+
),
|
|
609
|
+
h('button', {
|
|
610
|
+
type: 'button',
|
|
611
|
+
className: 'dsh-np-switch',
|
|
612
|
+
role: 'switch',
|
|
613
|
+
'aria-checked': draft.fallback,
|
|
614
|
+
'aria-label': t('fallbackLabel'),
|
|
615
|
+
disabled: saving,
|
|
616
|
+
onClick: () => { edit({ fallback: !draft.fallback }) },
|
|
617
|
+
}),
|
|
618
|
+
),
|
|
593
619
|
h('div', { className: 'dsh-np-actions' },
|
|
594
620
|
h('button', {
|
|
595
621
|
type: 'button',
|
package/index.js
CHANGED
|
@@ -69,11 +69,21 @@ export const Config = Schema.object({
|
|
|
69
69
|
.default('github.com\ngithubusercontent.com')
|
|
70
70
|
.description('Hosts routed through the proxy, one per line or comma-separated; each matches its subdomains. Everything else connects directly.')
|
|
71
71
|
.volatile(),
|
|
72
|
+
fallback: Schema.boolean()
|
|
73
|
+
.default(true)
|
|
74
|
+
.description('When a host outside the list cannot be reached directly, retry once through the proxy server.')
|
|
75
|
+
.volatile(),
|
|
72
76
|
})
|
|
73
77
|
|
|
74
78
|
/** Largest proxy response head the router accepts before giving up. */
|
|
75
79
|
const MAX_RESPONSE_HEAD = 64 * 1024
|
|
76
80
|
|
|
81
|
+
/** How long a direct connection may take before the proxy fallback is used. */
|
|
82
|
+
const DIRECT_CONNECT_TIMEOUT_MS = 10_000
|
|
83
|
+
|
|
84
|
+
/** Client bytes kept for replay when a direct tunnel is switched to the proxy. */
|
|
85
|
+
const MAX_TUNNEL_PENDING = 64 * 1024
|
|
86
|
+
|
|
77
87
|
/**
|
|
78
88
|
* Split configured text into domains.
|
|
79
89
|
* @param text - the configured value, separated by newlines, commas, or spaces.
|
|
@@ -206,7 +216,11 @@ function parseAuthority(value) {
|
|
|
206
216
|
* @returns the listening port, an in-place update, and a disposer.
|
|
207
217
|
*/
|
|
208
218
|
function createRouter(options) {
|
|
209
|
-
const state = {
|
|
219
|
+
const state = {
|
|
220
|
+
upstream: options.upstream,
|
|
221
|
+
domains: options.domains,
|
|
222
|
+
fallback: options.fallback === true,
|
|
223
|
+
}
|
|
210
224
|
const sockets = new Set()
|
|
211
225
|
const track = (socket) => {
|
|
212
226
|
sockets.add(socket)
|
|
@@ -231,6 +245,40 @@ function createRouter(options) {
|
|
|
231
245
|
}
|
|
232
246
|
}
|
|
233
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Open one forwarding request for an absolute-form client request.
|
|
250
|
+
* @param target - the request's URL.
|
|
251
|
+
* @param method - the request method.
|
|
252
|
+
* @param incoming - the client's request headers.
|
|
253
|
+
* @param viaProxy - whether to ask the upstream proxy instead of the origin.
|
|
254
|
+
* @returns the client request object the caller pipes into.
|
|
255
|
+
*/
|
|
256
|
+
const forwardRequest = (target, method, incoming, viaProxy) => {
|
|
257
|
+
const headers = { ...incoming }
|
|
258
|
+
delete headers['proxy-connection']
|
|
259
|
+
if (viaProxy) {
|
|
260
|
+
const proxy = proxyOf()
|
|
261
|
+
if (proxy.auth !== undefined) headers['proxy-authorization'] = proxy.auth
|
|
262
|
+
const transport = proxy.secure ? https : http
|
|
263
|
+
return transport.request({
|
|
264
|
+
host: proxy.host,
|
|
265
|
+
port: proxy.port,
|
|
266
|
+
method,
|
|
267
|
+
path: target.href,
|
|
268
|
+
headers,
|
|
269
|
+
})
|
|
270
|
+
}
|
|
271
|
+
delete headers['proxy-authorization']
|
|
272
|
+
headers.host = target.host
|
|
273
|
+
return http.request({
|
|
274
|
+
host: target.hostname,
|
|
275
|
+
port: Number(target.port) || 80,
|
|
276
|
+
method,
|
|
277
|
+
path: `${target.pathname}${target.search}`,
|
|
278
|
+
headers,
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
|
|
234
282
|
const server = http.createServer((req, res) => {
|
|
235
283
|
let target
|
|
236
284
|
try {
|
|
@@ -245,43 +293,35 @@ function createRouter(options) {
|
|
|
245
293
|
res.end('net-proxy: https requests must use CONNECT')
|
|
246
294
|
return
|
|
247
295
|
}
|
|
248
|
-
const
|
|
249
|
-
const
|
|
250
|
-
|
|
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', () => {
|
|
296
|
+
const viaProxy = listedForProxy(state.domains, target.hostname, Number(target.port) || 80)
|
|
297
|
+
const fail = () => {
|
|
298
|
+
if (res.writableEnded) return
|
|
281
299
|
if (!res.headersSent) res.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' })
|
|
282
300
|
res.end('net-proxy: request failed')
|
|
283
|
-
}
|
|
284
|
-
|
|
301
|
+
}
|
|
302
|
+
const send = (throughProxy) => {
|
|
303
|
+
const request = forwardRequest(target, req.method, req.headers, throughProxy)
|
|
304
|
+
request.on('response', (answer) => {
|
|
305
|
+
if (res.writableEnded) {
|
|
306
|
+
answer.destroy()
|
|
307
|
+
return
|
|
308
|
+
}
|
|
309
|
+
res.writeHead(answer.statusCode ?? 502, answer.headers)
|
|
310
|
+
answer.pipe(res)
|
|
311
|
+
})
|
|
312
|
+
request.on('error', () => {
|
|
313
|
+
const replayable = !throughProxy && state.fallback && !res.writableEnded
|
|
314
|
+
&& (req.method === 'GET' || req.method === 'HEAD')
|
|
315
|
+
if (!replayable) {
|
|
316
|
+
fail()
|
|
317
|
+
return
|
|
318
|
+
}
|
|
319
|
+
const retry = send(true)
|
|
320
|
+
req.pipe(retry)
|
|
321
|
+
})
|
|
322
|
+
return request
|
|
323
|
+
}
|
|
324
|
+
req.pipe(send(viaProxy))
|
|
285
325
|
})
|
|
286
326
|
|
|
287
327
|
server.on('connect', (req, client, head) => {
|
|
@@ -292,23 +332,25 @@ function createRouter(options) {
|
|
|
292
332
|
client.end('HTTP/1.1 400 Bad Request\r\n\r\n')
|
|
293
333
|
return
|
|
294
334
|
}
|
|
295
|
-
const
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
335
|
+
const throughProxy = () => {
|
|
336
|
+
openProxyTunnel(target).then((remote) => {
|
|
337
|
+
track(remote)
|
|
338
|
+
startRelay(client, remote, head)
|
|
339
|
+
}).catch((error) => { failClient(client, error) })
|
|
340
|
+
}
|
|
341
|
+
if (listedForProxy(state.domains, target.host, target.port)) {
|
|
342
|
+
throughProxy()
|
|
343
|
+
return
|
|
344
|
+
}
|
|
345
|
+
openDirect(target).then((direct) => {
|
|
346
|
+
track(direct)
|
|
347
|
+
startDirectRelay(client, direct, head, target)
|
|
348
|
+
}).catch(() => {
|
|
349
|
+
if (!state.fallback) {
|
|
350
|
+
failClient(client)
|
|
300
351
|
return
|
|
301
352
|
}
|
|
302
|
-
|
|
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`)
|
|
353
|
+
throughProxy()
|
|
312
354
|
})
|
|
313
355
|
})
|
|
314
356
|
|
|
@@ -317,18 +359,44 @@ function createRouter(options) {
|
|
|
317
359
|
})
|
|
318
360
|
|
|
319
361
|
/**
|
|
320
|
-
*
|
|
362
|
+
* Refuse one tunnel.
|
|
363
|
+
* @param client - the client socket.
|
|
364
|
+
* @param error - the proxy's refusal, whose status is relayed when it has one.
|
|
365
|
+
*/
|
|
366
|
+
function failClient(client, error) {
|
|
367
|
+
const status = typeof error?.status === 'number' ? error.status : 502
|
|
368
|
+
if (!client.destroyed) client.end(`HTTP/1.1 ${status} Bad Gateway\r\n\r\n`)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Connect to the target directly, bounded by the fallback timeout.
|
|
321
373
|
* @param target - host and port to reach.
|
|
322
|
-
* @
|
|
323
|
-
* @returns a socket already past any proxy handshake.
|
|
374
|
+
* @returns the connected socket.
|
|
324
375
|
*/
|
|
325
|
-
function
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
socket.
|
|
376
|
+
function openDirect(target) {
|
|
377
|
+
return new Promise((resolve, reject) => {
|
|
378
|
+
const socket = net.connect(target.port, target.host)
|
|
379
|
+
const timer = setTimeout(() => {
|
|
380
|
+
socket.destroy()
|
|
381
|
+
reject(new Error('direct connection timed out'))
|
|
382
|
+
}, DIRECT_CONNECT_TIMEOUT_MS)
|
|
383
|
+
socket.once('connect', () => {
|
|
384
|
+
clearTimeout(timer)
|
|
385
|
+
resolve(socket)
|
|
330
386
|
})
|
|
331
|
-
|
|
387
|
+
socket.once('error', (error) => {
|
|
388
|
+
clearTimeout(timer)
|
|
389
|
+
reject(error)
|
|
390
|
+
})
|
|
391
|
+
})
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Ask the upstream proxy for a tunnel to the target.
|
|
396
|
+
* @param target - host and port to reach.
|
|
397
|
+
* @returns a socket already past the proxy handshake.
|
|
398
|
+
*/
|
|
399
|
+
function openProxyTunnel(target) {
|
|
332
400
|
const proxy = proxyOf()
|
|
333
401
|
return new Promise((resolve, reject) => {
|
|
334
402
|
let settled = false
|
|
@@ -378,6 +446,113 @@ function createRouter(options) {
|
|
|
378
446
|
})
|
|
379
447
|
}
|
|
380
448
|
|
|
449
|
+
/**
|
|
450
|
+
* Hand an established tunnel to the client.
|
|
451
|
+
* @param client - the client socket.
|
|
452
|
+
* @param remote - the tunnel socket.
|
|
453
|
+
* @param head - bytes the client sent after its CONNECT headers.
|
|
454
|
+
*/
|
|
455
|
+
function startRelay(client, remote, head) {
|
|
456
|
+
if (client.destroyed) {
|
|
457
|
+
remote.destroy()
|
|
458
|
+
return
|
|
459
|
+
}
|
|
460
|
+
client.write('HTTP/1.1 200 Connection Established\r\n\r\n')
|
|
461
|
+
if (head.length > 0) remote.write(head)
|
|
462
|
+
remote.pipe(client)
|
|
463
|
+
client.pipe(remote)
|
|
464
|
+
client.resume()
|
|
465
|
+
remote.on('error', () => { client.destroy() })
|
|
466
|
+
client.on('error', () => { remote.destroy() })
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Relay a direct tunnel, switching it to the proxy when the origin never
|
|
471
|
+
* answers and the fallback is enabled.
|
|
472
|
+
*
|
|
473
|
+
* The client's bytes are forwarded immediately while a copy is kept. Until the
|
|
474
|
+
* origin sends its first byte back, the tunnel still carries a handshake, so a
|
|
475
|
+
* failed path can be replaced by the proxy connection and the buffered bytes
|
|
476
|
+
* replayed there; the client-side transport is end to end, so the client never
|
|
477
|
+
* observes the switch.
|
|
478
|
+
* @param client - the client socket.
|
|
479
|
+
* @param direct - the connected direct socket.
|
|
480
|
+
* @param head - bytes the client sent after its CONNECT headers.
|
|
481
|
+
* @param target - the tunnel's target.
|
|
482
|
+
*/
|
|
483
|
+
function startDirectRelay(client, direct, head, target) {
|
|
484
|
+
const pending = head.length > 0 ? [head] : []
|
|
485
|
+
let pendingBytes = head.length
|
|
486
|
+
let serverAnswered = false
|
|
487
|
+
let switched = false
|
|
488
|
+
let finished = false
|
|
489
|
+
let upstream = direct
|
|
490
|
+
|
|
491
|
+
client.write('HTTP/1.1 200 Connection Established\r\n\r\n')
|
|
492
|
+
if (head.length > 0) direct.write(head)
|
|
493
|
+
|
|
494
|
+
const finish = () => {
|
|
495
|
+
if (finished) return
|
|
496
|
+
finished = true
|
|
497
|
+
client.destroy()
|
|
498
|
+
direct.destroy()
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const switchToProxy = () => {
|
|
502
|
+
if (switched || serverAnswered || finished) return
|
|
503
|
+
if (!state.fallback || pendingBytes > MAX_TUNNEL_PENDING) {
|
|
504
|
+
finish()
|
|
505
|
+
return
|
|
506
|
+
}
|
|
507
|
+
switched = true
|
|
508
|
+
const previous = upstream
|
|
509
|
+
previous.removeAllListeners('data')
|
|
510
|
+
previous.removeAllListeners('error')
|
|
511
|
+
previous.removeAllListeners('close')
|
|
512
|
+
previous.on('error', () => {})
|
|
513
|
+
previous.destroy()
|
|
514
|
+
openProxyTunnel(target).then((remote) => {
|
|
515
|
+
if (finished || client.destroyed) {
|
|
516
|
+
remote.destroy()
|
|
517
|
+
return
|
|
518
|
+
}
|
|
519
|
+
track(remote)
|
|
520
|
+
upstream = remote
|
|
521
|
+
remote.on('data', (chunk) => {
|
|
522
|
+
serverAnswered = true
|
|
523
|
+
pending.length = 0
|
|
524
|
+
client.write(chunk)
|
|
525
|
+
})
|
|
526
|
+
remote.on('end', () => { finished = true; client.end() })
|
|
527
|
+
remote.on('error', () => { finished = true; client.destroy() })
|
|
528
|
+
for (const chunk of pending) remote.write(chunk)
|
|
529
|
+
pending.length = 0
|
|
530
|
+
}).catch(() => { finish() })
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
client.on('data', (chunk) => {
|
|
534
|
+
if (!serverAnswered) {
|
|
535
|
+
pending.push(chunk)
|
|
536
|
+
pendingBytes += chunk.length
|
|
537
|
+
if (pendingBytes > MAX_TUNNEL_PENDING) pending.length = 0
|
|
538
|
+
}
|
|
539
|
+
if (upstream.writable) upstream.write(chunk)
|
|
540
|
+
})
|
|
541
|
+
client.on('end', () => { if (upstream.writable) upstream.end() })
|
|
542
|
+
client.on('error', () => { if (upstream.writable) upstream.destroy() })
|
|
543
|
+
|
|
544
|
+
direct.on('data', (chunk) => {
|
|
545
|
+
serverAnswered = true
|
|
546
|
+
pending.length = 0
|
|
547
|
+
client.write(chunk)
|
|
548
|
+
})
|
|
549
|
+
direct.on('end', () => { if (!serverAnswered) switchToProxy() })
|
|
550
|
+
direct.on('error', () => { if (!serverAnswered) switchToProxy() })
|
|
551
|
+
direct.on('close', () => { if (!serverAnswered) switchToProxy() })
|
|
552
|
+
if (direct.destroyed || direct.readableEnded) switchToProxy()
|
|
553
|
+
|
|
554
|
+
client.resume()
|
|
555
|
+
}
|
|
381
556
|
return new Promise((resolve, reject) => {
|
|
382
557
|
server.once('error', reject)
|
|
383
558
|
server.listen(0, '127.0.0.1', () => {
|
|
@@ -391,6 +566,7 @@ function createRouter(options) {
|
|
|
391
566
|
update(next) {
|
|
392
567
|
if (typeof next.upstream === 'string' && next.upstream !== '') state.upstream = next.upstream
|
|
393
568
|
if (Array.isArray(next.domains)) state.domains = next.domains
|
|
569
|
+
if (typeof next.fallback === 'boolean') state.fallback = next.fallback
|
|
394
570
|
},
|
|
395
571
|
close: async () => {
|
|
396
572
|
for (const socket of sockets) socket.destroy()
|
|
@@ -433,6 +609,7 @@ export function apply(ctx, config) {
|
|
|
433
609
|
enabled: config.enabled.get() === true,
|
|
434
610
|
proxyUrl: String(config.proxyUrl.get() ?? '').trim(),
|
|
435
611
|
domains: String(config.domains.get() ?? ''),
|
|
612
|
+
fallback: config.fallback.get() === true,
|
|
436
613
|
})
|
|
437
614
|
|
|
438
615
|
const stop = async (current) => {
|
|
@@ -469,14 +646,14 @@ export function apply(ctx, config) {
|
|
|
469
646
|
}
|
|
470
647
|
|
|
471
648
|
if (previous !== undefined) {
|
|
472
|
-
previous.router.update({ upstream: state.proxyUrl, domains })
|
|
473
|
-
ctx.logger.info('net-proxy: %d listed domain(s) go through %s;
|
|
649
|
+
previous.router.update({ upstream: state.proxyUrl, domains, fallback: state.fallback })
|
|
650
|
+
ctx.logger.info('net-proxy: %d listed domain(s) go through %s; direct failures %s', domains.length, displayProxy(state.proxyUrl), state.fallback ? 'retry through the proxy' : 'do not fall back')
|
|
474
651
|
return
|
|
475
652
|
}
|
|
476
653
|
|
|
477
654
|
let router
|
|
478
655
|
try {
|
|
479
|
-
router = await createRouter({ upstream: state.proxyUrl, domains })
|
|
656
|
+
router = await createRouter({ upstream: state.proxyUrl, domains, fallback: state.fallback })
|
|
480
657
|
} catch (error) {
|
|
481
658
|
ctx.logger.error('net-proxy: starting the domain router failed: %s', error instanceof Error ? error.message : String(error))
|
|
482
659
|
return
|
|
@@ -495,7 +672,7 @@ export function apply(ctx, config) {
|
|
|
495
672
|
return
|
|
496
673
|
}
|
|
497
674
|
session = { release, router }
|
|
498
|
-
ctx.logger.info('net-proxy: %d listed domain(s) go through %s;
|
|
675
|
+
ctx.logger.info('net-proxy: %d listed domain(s) go through %s; direct failures %s', domains.length, displayProxy(state.proxyUrl), state.fallback ? 'retry through the proxy' : 'do not fall back')
|
|
499
676
|
} catch (error) {
|
|
500
677
|
await router.close()
|
|
501
678
|
ctx.logger.error('net-proxy: installing the proxy policy failed: %s', error instanceof Error ? error.message : String(error))
|
package/locale/en.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
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."
|
|
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. With “Retry direct failures through the proxy” on, a host outside the list that cannot be reached directly is tried once more through the proxy. If the proxy becomes unreachable, requests to the listed domains fail until you turn off Use proxy or remove them from the list."
|
|
5
5
|
}
|
|
6
6
|
}
|
package/locale/zh.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "网络代理",
|
|
4
|
-
"description": "只把列表中列出的域名交给你的代理服务器,其余主机一律直连。覆盖宿主自身的请求(模型调用、网页抓取、HTTP MCP),以及之后新启动的命令与终端。在「插件管理」中点击本插件这一行的标题打开配置,或使用左侧栏的「网络代理」面板。条目同时匹配子域名;localhost 与本机回环始终直连;仅支持 http:// 与 https://
|
|
4
|
+
"description": "只把列表中列出的域名交给你的代理服务器,其余主机一律直连。覆盖宿主自身的请求(模型调用、网页抓取、HTTP MCP),以及之后新启动的命令与终端。在「插件管理」中点击本插件这一行的标题打开配置,或使用左侧栏的「网络代理」面板。条目同时匹配子域名;localhost 与本机回环始终直连;仅支持 http:// 与 https:// 代理。打开「直连失败时改用代理重试」后,列表外的站点直连失败时会再通过代理尝试一次。代理不可用时,列表内域名的请求会失败,直到你关闭「使用代理」或把这些域名从列表中移除。"
|
|
5
5
|
}
|
|
6
6
|
}
|
package/package.json
CHANGED