2024-10-01 12:28:34 -05:00
|
|
|
import { ProxyAgent } from "proxy-agent";
|
2024-10-01 12:56:54 -05:00
|
|
|
import { PacProxyAgent } from "pac-proxy-agent";
|
|
|
|
import { I2P_PROXY, PAC_PROXY, TOR_PROXY } from "./env.js";
|
2024-10-01 12:28:34 -05:00
|
|
|
|
2024-10-01 12:56:54 -05:00
|
|
|
function buildPacURI() {
|
|
|
|
const statements: string[] = [];
|
2024-10-01 12:28:34 -05:00
|
|
|
|
2024-10-01 12:56:54 -05:00
|
|
|
if (I2P_PROXY) {
|
|
|
|
statements.push(
|
|
|
|
`
|
|
|
|
if (shExpMatch(host, "*.i2p"))
|
|
|
|
{
|
|
|
|
return "SOCKS5 ${I2P_PROXY}";
|
2024-10-01 12:28:34 -05:00
|
|
|
}
|
2024-10-01 12:56:54 -05:00
|
|
|
`.trim(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TOR_PROXY) {
|
|
|
|
statements.push(
|
|
|
|
`
|
|
|
|
if (shExpMatch(host, "*.onion"))
|
|
|
|
{
|
|
|
|
return "SOCKS5 ${TOR_PROXY}";
|
|
|
|
}
|
|
|
|
`.trim(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
statements.push('return "DIRECT";');
|
|
|
|
|
|
|
|
const PACFile = `
|
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
|
|
|
|
|
|
|
function FindProxyForURL(url, host)
|
|
|
|
{
|
|
|
|
${statements.join("\n")}
|
2024-10-01 12:28:34 -05:00
|
|
|
}
|
2024-10-01 12:56:54 -05:00
|
|
|
`.trim();
|
|
|
|
|
|
|
|
return "pac+data:application/x-ns-proxy-autoconfig;base64," + btoa(PACFile);
|
|
|
|
}
|
|
|
|
function buildProxy() {
|
|
|
|
if (PAC_PROXY) {
|
|
|
|
console.log(`Using PAC proxy file`);
|
|
|
|
return new PacProxyAgent(PAC_PROXY);
|
|
|
|
} else if (TOR_PROXY || I2P_PROXY) {
|
|
|
|
if (TOR_PROXY) console.log("Tor connection enabled");
|
|
|
|
if (I2P_PROXY) console.log("I2P connection enabled");
|
|
|
|
|
|
|
|
return new PacProxyAgent(buildPacURI());
|
|
|
|
} else return new ProxyAgent({ keepAlive: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
const agent = buildProxy();
|
2024-10-01 12:28:34 -05:00
|
|
|
|
|
|
|
export default agent;
|