Compare commits

...

1 Commits

Author SHA1 Message Date
Julien Fontanet
71d2c28899 WiP: should proxy 2022-11-29 10:31:07 +01:00
2 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
import splitHost from 'split-host'
// https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/
export function shouldProxy(host, { NO_PROXY, no_proxy = NO_PROXY } = process.env) {
if (no_proxy == null) {
return true
}
if (no_proxy === '*') {
return false
}
const { hostname } = splitHost(host)
for (let entry of no_proxy.split(',')) {
entry = entry.trim()
if (entry[0] === '.') {
entry = entry.slice(1)
}
entry = splitHost(entry.trim())
console.log(hostname, entry.hostname)
if (hostname.endsWith(entry.hostname)) {
return false
}
}
return true
}

View File

@@ -0,0 +1,61 @@
import { shouldProxy } from './_shouldProxy.mjs'
import t from 'tap'
const ensureArray = v => (v === undefined ? [] : Array.isArray(v) ? v : [v])
;[
{
no_proxy: null,
ok: 'example.org',
},
{
no_proxy: '*',
nok: 'example.org',
},
{
no_proxy: 'example.org, example.com',
nok: ['example.org', 'example.org:1024', 'example.com'],
ok: 'example.net',
},
{
no_proxy: ['example.org', '.example.org'],
nok: ['example.org', 'example.org:1024', 'sub.example.org'],
ok: 'example.com',
},
// {
// no_proxy: 'example.org:1024',
// nok: ['example.org:1024', 'sub.example.org:1024'],
// ok: ['example.com', 'example.org'],
// },
{
no_proxy: '[::1]',
nok: ['[::1]', '[::1]:1024'],
ok: ['[::2]', '[0::1]'],
},
].forEach(({ no_proxy: noProxies, ok, nok }) => {
for (const no_proxy of ensureArray(noProxies)) {
const opts = { no_proxy }
t.test(String(no_proxy), function (t) {
ok = ensureArray(ok)
if (ok.length !== 0) {
t.test('should proxy', t => {
for (const host of ok) {
t.equal(shouldProxy(host, opts), true, host)
}
t.end()
})
}
nok = ensureArray(nok)
if (nok.length !== 0) {
t.test('should not proxy', t => {
for (const host of nok) {
t.equal(shouldProxy(host, opts), false, host)
}
t.end()
})
}
t.end()
})
}
})