Check the proxy at startup instead of failing on first use
A proxy pointed at 127.0.0.1 from inside a container reaches the container itself, and the only sign was a connection error buried in the first search. Startup now probes the proxy over TCP and says what is wrong, naming host.docker.internal when loopback was configured. The README also covers the follow-up trap: even that address fails when the proxy listens on loopback only, so it shows how to check the bind address and what to change. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
ea19a74c12
commit
380f7bcf31
@@ -1,4 +1,5 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { connect } from "node:net";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { constants } from "node:fs";
|
||||
import { access } from "node:fs/promises";
|
||||
@@ -52,6 +53,40 @@ export function describeProxy(): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A quick TCP probe of the proxy. The usual mistake is pointing a container at
|
||||
* 127.0.0.1, which is the container itself — better to say so at startup than to
|
||||
* let every search fail with a connection error.
|
||||
*/
|
||||
export function checkProxy(timeoutMs = 4000): Promise<string | null> {
|
||||
if (!config.YTDLP_PROXY) return Promise.resolve(null);
|
||||
let target: URL;
|
||||
try {
|
||||
target = new URL(config.YTDLP_PROXY);
|
||||
} catch {
|
||||
return Promise.resolve("YTDLP_PROXY не является корректным URL");
|
||||
}
|
||||
const port = Number(target.port) || (target.protocol === "https:" ? 443 : 1080);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const socket = connect({ host: target.hostname, port });
|
||||
const done = (result: string | null) => {
|
||||
socket.destroy();
|
||||
resolve(result);
|
||||
};
|
||||
socket.setTimeout(timeoutMs);
|
||||
socket.once("connect", () => done(null));
|
||||
socket.once("timeout", () => done(`прокси ${target.hostname}:${port} не отвечает`));
|
||||
socket.once("error", (err: NodeJS.ErrnoException) => {
|
||||
const hint =
|
||||
target.hostname === "127.0.0.1" || target.hostname === "localhost"
|
||||
? " — внутри контейнера это сам контейнер; используйте host.docker.internal"
|
||||
: "";
|
||||
done(`прокси ${target.hostname}:${port} недоступен (${err.code ?? err.message})${hint}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export type CookieStatus = "ok" | "read-only" | "missing";
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user