WiP: should proxy
This commit is contained in:
28
packages/xo-server/src/_shouldProxy.mjs
Normal file
28
packages/xo-server/src/_shouldProxy.mjs
Normal 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
|
||||
}
|
||||
61
packages/xo-server/src/_shouldProxy.spec.mjs
Normal file
61
packages/xo-server/src/_shouldProxy.spec.mjs
Normal 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()
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user