fix: route web-probe through YAML proxy

This commit is contained in:
Codex
2026-06-21 20:46:15 +00:00
parent 625e3a1ab2
commit 56f1ed44a4
3 changed files with 244 additions and 6 deletions
@@ -17,6 +17,7 @@ const runDir = path.resolve(process.env.UNIDESK_WEB_PROBE_RUN_DIR || ".state/web
const userScript = path.resolve(process.env.UNIDESK_WEB_PROBE_USER_SCRIPT || path.join(runDir, "user-script.mjs"));
const timeoutMs = positiveInteger(process.env.UNIDESK_WEB_PROBE_TIMEOUT_MS, 30000);
const viewport = parseViewport(process.env.UNIDESK_WEB_PROBE_VIEWPORT || "1440x900");
const playwrightProxy = proxyConfigFromEnv(baseUrl);
const artifactRecords = [];
const readinessRecords = [];
const stepRecords = [];
@@ -34,7 +35,7 @@ try {
const launcher = await import(pathToFileURL(path.resolve("scripts/src/browser-launcher.mjs")).href);
const { chromium } = await launcher.importPlaywright();
browser = await launcher.launchChromium(chromium);
context = await browser.newContext({ viewport });
context = await browser.newContext({ viewport, ...(playwrightProxy === null ? {} : { proxy: playwrightProxy }) });
auth = await authenticate(context);
if (!auth.ok) throw new Error("auth-login-failed");
page = await context.newPage();
@@ -55,6 +56,7 @@ try {
generatedAt: new Date().toISOString(),
startedAt,
baseUrl,
network: publicNetwork(playwrightProxy),
finalUrl: lastUrl,
lastUrl,
scriptSha256: userScriptSha256,
@@ -83,6 +85,7 @@ try {
generatedAt: new Date().toISOString(),
startedAt,
baseUrl,
network: publicNetwork(playwrightProxy),
finalUrl: lastUrl,
lastUrl,
scriptSha256: userScriptSha256,
@@ -365,6 +368,62 @@ function scriptHelpers() {
return helpers;
}
function proxyConfigFromEnv(targetBaseUrl) {
let target;
try {
target = new URL(targetBaseUrl);
} catch {
return null;
}
const noProxy = process.env.NO_PROXY || process.env.no_proxy || "";
if (noProxyMatches(target.hostname, noProxy)) return null;
const raw = target.protocol === "https:"
? process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy || process.env.HTTP_PROXY || process.env.http_proxy || ""
: process.env.HTTP_PROXY || process.env.http_proxy || process.env.ALL_PROXY || process.env.all_proxy || process.env.HTTPS_PROXY || process.env.https_proxy || "";
if (!raw) return null;
return { server: raw };
}
function noProxyMatches(hostname, rawList) {
const host = String(hostname || "").toLowerCase();
if (!host) return false;
return String(rawList || "").split(",").some((raw) => {
let item = raw.trim().toLowerCase();
if (!item) return false;
if (item === "*") return true;
item = item.replace(/^\*\./u, ".");
const portIndex = item.lastIndexOf(":");
if (portIndex > -1 && !item.includes("]")) item = item.slice(0, portIndex);
if (item.startsWith(".")) return host === item.slice(1) || host.endsWith(item);
return host === item;
});
}
function publicNetwork(proxy) {
return {
proxy: proxy === null ? { enabled: false, source: "env", valuesRedacted: true } : {
enabled: true,
source: "env",
server: publicProxyServer(proxy.server),
valuesRedacted: true,
},
valuesRedacted: true,
};
}
function publicProxyServer(raw) {
try {
const parsed = new URL(String(raw || ""));
parsed.username = "";
parsed.password = "";
const value = parsed.toString();
if (parsed.pathname === "/" && parsed.search === "" && parsed.hash === "") return value.replace(/\/$/u, "");
return value;
} catch {
return String(raw || "").replace(/\/\/[^/@]+@/u, "//[redacted]@");
}
}
function createLivePageProxy() {
return new Proxy({}, {
get(_target, prop) {