fix: normalize wildcard gateway URLs for Codex

This commit is contained in:
2026-07-20 10:01:19 +08:00
parent 31e673f777
commit 14fae29618
6 changed files with 237 additions and 38 deletions
+31 -14
View File
@@ -73,9 +73,23 @@ export function getGatewayStatePaths(stateRoot = DEFAULT_STATE_ROOT) {
};
}
export function getGatewayBaseUrl(listenHost, listenPort) {
return `http://${listenHost}:${listenPort}`;
}
export function getGatewayListenerBaseUrl(listenHost, listenPort) {
return `http://${listenHost}:${listenPort}`;
}
export function getGatewayBaseUrl(listenHost, listenPort) {
// 0.0.0.0 is a valid bind address, but not a client destination. Keep the
// listener wildcard intact and use loopback for Codex, health probes, and UI
// links that originate on the same machine.
const clientHost = `${listenHost ?? ""}`.trim() === "0.0.0.0" ? "127.0.0.1" : listenHost;
return getGatewayListenerBaseUrl(clientHost, listenPort);
}
export function isGatewayBaseUrlForListener(baseUrl, listenHost, listenPort) {
const candidate = `${baseUrl ?? ""}`.trim();
return candidate === getGatewayBaseUrl(listenHost, listenPort)
|| candidate === getGatewayListenerBaseUrl(listenHost, listenPort);
}
export function getGatewayBaseUrlFromConfig(gatewayConfig) {
if (!gatewayConfig) {
@@ -408,16 +422,16 @@ export async function installForCurrentProvider({
const existingState = await readJsonFile(paths.statePath);
let originalBaseUrl = providerContext.currentBaseUrl;
if (providerContext.currentBaseUrl === localGatewayBaseUrl) {
if (!existingState?.original_base_url) {
throw new Error("Provider already points to the local gateway, but original_base_url is missing from state.");
}
originalBaseUrl = `${existingState.original_base_url}`;
}
if (originalBaseUrl === localGatewayBaseUrl) {
throw new Error("A real upstream_base_url could not be determined.");
}
if (isGatewayBaseUrlForListener(providerContext.currentBaseUrl, listenHost, listenPort)) {
if (!existingState?.original_base_url) {
throw new Error("Provider already points to the local gateway, but original_base_url is missing from state.");
}
originalBaseUrl = `${existingState.original_base_url}`;
}
if (isGatewayBaseUrlForListener(originalBaseUrl, listenHost, listenPort)) {
throw new Error("A real upstream_base_url could not be determined.");
}
const backupPath = path.join(paths.backupDir, `config-${new Date().toISOString().replace(/[:.]/g, "").replace("T", "-").slice(0, 15)}.toml`);
await copyFile(codexConfigPath, backupPath);
@@ -557,7 +571,10 @@ export async function launchUi({
const existingGatewayConfig = await readJsonFile(paths.configPath);
const stateGatewayBaseUrl = existingState?.gateway_base_url ? `${existingState.gateway_base_url}` : null;
const configGatewayBaseUrl = getGatewayBaseUrlFromConfig(existingGatewayConfig);
const managedGatewayBaseUrls = [requestedGatewayBaseUrl];
const managedGatewayBaseUrls = [
requestedGatewayBaseUrl,
getGatewayListenerBaseUrl(listenHost, listenPort),
];
for (const candidate of [stateGatewayBaseUrl, configGatewayBaseUrl]) {
if (candidate && !managedGatewayBaseUrls.includes(candidate)) {
managedGatewayBaseUrls.push(candidate);