Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/public/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ <h2 class="settings-section-heading">
id="settings-proxy-urls"
class="settings-proxy-urls"
rows="4"
placeholder="http://proxy1:8080&#10;socks5://proxy2:1080"
placeholder="http://proxy1:8080&#10;http://user:pass@proxy2:8080&#10;socks5://proxy3:1080"
></textarea>
<button
class="btn btn--secondary proxy-test-btn"
Expand Down
29 changes: 25 additions & 4 deletions src/server/utils/http-proxy-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,32 @@ import type { TransportFetchOptions as OutgoingFetchOptions } from "../types";
const MAX_REDIRECTS = 5;
const CONNECT_TIMEOUT_MS = 8_000;

function parseProxyUrl(proxyUrl: string): { host: string; port: number } {
function parseProxyUrl(proxyUrl: string): {
host: string;
port: number;
auth: string | undefined;
} {
const url = new URL(proxyUrl);
return { host: url.hostname, port: Number(url.port) || 8080 };
const username = decodeURIComponent(url.username);
const password = decodeURIComponent(url.password);
const auth =
username || password
? `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`
: undefined;

return {
host: url.hostname,
port: Number(url.port) || (url.protocol === "http:" ? 80 : 443),
auth,
};
}

const _openConnectTunnel = (
proxyHost: string,
proxyPort: number,
targetHost: string,
targetPort: number,
proxyAuth: string | undefined,
timeoutMs: number = CONNECT_TIMEOUT_MS,
): Promise<net.Socket> =>
new Promise((resolve, reject) => {
Expand All @@ -26,8 +42,9 @@ const _openConnectTunnel = (
}, timeoutMs);

sock.once("connect", () => {
const authHeader = proxyAuth ? `Proxy-Authorization: ${proxyAuth}\r\n` : "";
sock.write(
`CONNECT ${targetHost}:${targetPort} HTTP/1.1\r\nHost: ${targetHost}:${targetPort}\r\n\r\n`,
`CONNECT ${targetHost}:${targetPort} HTTP/1.1\r\nHost: ${targetHost}:${targetPort}\r\n${authHeader}\r\n`,
);
});

Expand Down Expand Up @@ -59,6 +76,7 @@ const _openConnectTunnel = (
async function _openProxySocket(
proxyHost: string,
proxyPort: number,
proxyAuth: string | undefined,
targetHost: string,
targetPort: number,
useTls: boolean,
Expand All @@ -69,6 +87,7 @@ async function _openProxySocket(
proxyPort,
targetHost,
targetPort,
proxyAuth,
timeoutMs,
);
if (!useTls) return sock;
Expand Down Expand Up @@ -174,7 +193,8 @@ export async function fetchViaHttpProxy(
options: OutgoingFetchOptions = {},
timeoutMs?: number,
): Promise<Response> {
const { host: proxyHost, port: proxyPort } = parseProxyUrl(proxyUrl);
const { host: proxyHost, port: proxyPort, auth: proxyAuth } =
parseProxyUrl(proxyUrl);
const followRedirects = (options.redirect ?? "follow") !== "manual";
const method = options.method ?? "GET";

Expand All @@ -189,6 +209,7 @@ export async function fetchViaHttpProxy(
const sock = await _openProxySocket(
proxyHost,
proxyPort,
proxyAuth,
parsed.hostname,
port,
useTls,
Expand Down