Check if proxy exists when assigning, setting default proxy

This commit is contained in:
pvs 2022-01-04 09:04:50 -08:00
parent 3abeaaceaa
commit 1446179169
2 changed files with 25 additions and 5 deletions

View file

@ -3929,7 +3929,11 @@ export function set(key: string, ...values: string[]) {
throw msg
}
return config.set(...validateSetArgs(key, values))
const target = validateSetArgs(key, values)
key === "proxy" && Proxy.exists(target.slice(-1))
return config.set(...target)
}
/**
@ -4054,6 +4058,8 @@ export function autocontain(...args: string[]) {
pattern = saneMode ? `^https?://([^/]*\\.|)${pattern}/` : `^https?://[^/]*${pattern}/`
}
proxies.length && Proxy.exists(proxies)
return config.set("autocontain", pattern, proxies.length ? [container, proxies.join(",")].join("+") : container)
}

View file

@ -96,8 +96,22 @@ export const proxyFromUrl = (proxyUrl: string): ProxyInfo => {
}
}
const getProxies = async (): Promise<{ [key: string]: ProxyInfo }> => {
const userProxies = await config.getAsync("proxies")
export function exists(names: string[]) {
const currProxies = Object.keys(config.get("proxies"))
const missingProxies = names.filter(name => !currProxies.includes(name))
if (missingProxies.length) {
throw new Error(
`${
missingProxies.length === 1 ? "Proxy" : "Proxies"
} ${missingProxies.join(
", ",
)} does not exist. See :help proxyadd for more info.`,
)
}
}
const getProxies = (): { [key: string]: ProxyInfo } => {
const userProxies = config.get("proxies")
return Object.entries(userProxies).reduce((acc, [name, url]) => {
acc[name] = proxyFromUrl(url as string)
return acc
@ -107,11 +121,11 @@ const getProxies = async (): Promise<{ [key: string]: ProxyInfo }> => {
const getProxiesForUrl = async (url: string): Promise<ProxyInfo[]> => {
const aucon = new AutoContain()
const [, containerProxies] = await aucon.getAuconAndProxiesForUrl(url)
const proxies = await getProxies()
const proxies = getProxies()
const filteredProxies = Object.entries(proxies)
.filter(([name, ]) => containerProxies.includes(name))
.map(([, proxy]) => proxy)
const defaultProxy = await config.getAsync("proxy")
const defaultProxy = config.get("proxy")
if (
defaultProxy in proxies &&
!containerProxies.includes(defaultProxy)