feat(usage): implement timeout and error handling for antigravity usage and subscription requests
- Add a 10-second timeout for fetch requests in getAntigravityUsage and getAntigravitySubscriptionInfo functions. - Include error logging for fetch failures in both functions. - Update headers to include "x-request-source" for MITM bypass. - Enhance proxyFetch with DNS resolution and MITM bypass capabilities. - Ensure proxyFetch is loaded in the API route for proper fetch patching.
This commit is contained in:
@@ -214,7 +214,10 @@ async function getAntigravityUsage(accessToken, providerSpecificData) {
|
|||||||
// First get project ID from subscription info
|
// First get project ID from subscription info
|
||||||
const projectId = await getAntigravityProjectId(accessToken);
|
const projectId = await getAntigravityProjectId(accessToken);
|
||||||
|
|
||||||
// Fetch quota data
|
// Fetch quota data with timeout
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10s timeout
|
||||||
|
|
||||||
const response = await fetch(ANTIGRAVITY_CONFIG.quotaApiUrl, {
|
const response = await fetch(ANTIGRAVITY_CONFIG.quotaApiUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -223,11 +226,15 @@ async function getAntigravityUsage(accessToken, providerSpecificData) {
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"X-Client-Name": "antigravity",
|
"X-Client-Name": "antigravity",
|
||||||
"X-Client-Version": "1.107.0",
|
"X-Client-Version": "1.107.0",
|
||||||
|
"x-request-source": "local", // MITM bypass
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
...(projectId ? { project: projectId } : {})
|
...(projectId ? { project: projectId } : {})
|
||||||
}),
|
}),
|
||||||
|
signal: controller.signal,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
if (response.status === 403) {
|
if (response.status === 403) {
|
||||||
return {
|
return {
|
||||||
@@ -302,6 +309,7 @@ async function getAntigravityUsage(accessToken, providerSpecificData) {
|
|||||||
subscriptionInfo,
|
subscriptionInfo,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error("[Antigravity Usage] Error:", error.message, error.cause);
|
||||||
return { message: `Antigravity error: ${error.message}` };
|
return { message: `Antigravity error: ${error.message}` };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -323,20 +331,28 @@ async function getAntigravityProjectId(accessToken) {
|
|||||||
*/
|
*/
|
||||||
async function getAntigravitySubscriptionInfo(accessToken) {
|
async function getAntigravitySubscriptionInfo(accessToken) {
|
||||||
try {
|
try {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10s timeout
|
||||||
|
|
||||||
const response = await fetch(ANTIGRAVITY_CONFIG.loadProjectApiUrl, {
|
const response = await fetch(ANTIGRAVITY_CONFIG.loadProjectApiUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${accessToken}`,
|
"Authorization": `Bearer ${accessToken}`,
|
||||||
"User-Agent": ANTIGRAVITY_CONFIG.userAgent,
|
"User-Agent": ANTIGRAVITY_CONFIG.userAgent,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
"x-request-source": "local", // MITM bypass
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ metadata: CLIENT_METADATA, mode: 1 }),
|
body: JSON.stringify({ metadata: CLIENT_METADATA, mode: 1 }),
|
||||||
|
signal: controller.signal,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
if (!response.ok) return null;
|
if (!response.ok) return null;
|
||||||
|
|
||||||
return await response.json();
|
return await response.json();
|
||||||
} catch {
|
} catch (error) {
|
||||||
|
console.error("[Antigravity Subscription] Error:", error.message);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,60 @@ const originalFetch = globalThis.fetch;
|
|||||||
let proxyDispatcher = null;
|
let proxyDispatcher = null;
|
||||||
let proxyDispatcherUrl = null;
|
let proxyDispatcherUrl = null;
|
||||||
|
|
||||||
|
// Constants
|
||||||
|
const DNS_CACHE = {};
|
||||||
|
const MITM_BYPASS_HOSTS = ["cloudcode-pa.googleapis.com", "daily-cloudcode-pa.googleapis.com", "googleapis.com"];
|
||||||
|
const MITM_BYPASS_HEADER = "x-request-source";
|
||||||
|
const MITM_BYPASS_VALUE = "local";
|
||||||
|
const GOOGLE_DNS_SERVERS = ["8.8.8.8", "8.8.4.4"];
|
||||||
|
const HTTPS_PORT = 443;
|
||||||
|
const HTTP_SUCCESS_MIN = 200;
|
||||||
|
const HTTP_SUCCESS_MAX = 300;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve real IP using Google DNS (bypass system DNS)
|
||||||
|
*/
|
||||||
|
async function resolveRealIP(hostname) {
|
||||||
|
if (DNS_CACHE[hostname]) return DNS_CACHE[hostname];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const dns = await import("dns");
|
||||||
|
const { promisify } = await import("util");
|
||||||
|
const resolver = new dns.Resolver();
|
||||||
|
resolver.setServers(GOOGLE_DNS_SERVERS);
|
||||||
|
const resolve4 = promisify(resolver.resolve4.bind(resolver));
|
||||||
|
const addresses = await resolve4(hostname);
|
||||||
|
DNS_CACHE[hostname] = addresses[0];
|
||||||
|
return addresses[0];
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(`[ProxyFetch] DNS resolve failed for ${hostname}:`, error.message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if request should bypass MITM DNS redirect
|
||||||
|
*/
|
||||||
|
function shouldBypassMitmDns(url, options) {
|
||||||
|
if (!options?.headers) return false;
|
||||||
|
|
||||||
|
const headers = options.headers;
|
||||||
|
const hasLocalMarker = headers[MITM_BYPASS_HEADER] === MITM_BYPASS_VALUE ||
|
||||||
|
headers[MITM_BYPASS_HEADER.charAt(0).toUpperCase() + MITM_BYPASS_HEADER.slice(1)] === MITM_BYPASS_VALUE;
|
||||||
|
|
||||||
|
if (!hasLocalMarker) {
|
||||||
|
// Debug: log when bypass is not triggered
|
||||||
|
const hostname = new URL(url).hostname;
|
||||||
|
if (MITM_BYPASS_HOSTS.some(host => hostname.includes(host))) {
|
||||||
|
console.warn(`[ProxyFetch] MITM bypass NOT triggered for ${hostname} - missing header`);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hostname = new URL(url).hostname;
|
||||||
|
return MITM_BYPASS_HOSTS.some(host => hostname.includes(host));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get proxy URL from environment
|
* Get proxy URL from environment
|
||||||
*/
|
*/
|
||||||
@@ -68,18 +122,81 @@ async function getDispatcher(proxyUrl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Patched fetch with proxy support and fallback to direct connection
|
* Create HTTPS request with manual socket connection (bypass DNS)
|
||||||
|
*/
|
||||||
|
async function createBypassRequest(parsedUrl, realIP, options) {
|
||||||
|
const https = await import("https");
|
||||||
|
const net = await import("net");
|
||||||
|
const { Readable } = require("stream");
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const socket = new net.Socket();
|
||||||
|
|
||||||
|
socket.connect(HTTPS_PORT, realIP, () => {
|
||||||
|
const reqOptions = {
|
||||||
|
socket,
|
||||||
|
servername: parsedUrl.hostname,
|
||||||
|
rejectUnauthorized: false,
|
||||||
|
path: parsedUrl.pathname + parsedUrl.search,
|
||||||
|
method: options.method || "POST",
|
||||||
|
headers: {
|
||||||
|
...options.headers,
|
||||||
|
Host: parsedUrl.hostname,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const req = https.request(reqOptions, (res) => {
|
||||||
|
const response = {
|
||||||
|
ok: res.statusCode >= HTTP_SUCCESS_MIN && res.statusCode < HTTP_SUCCESS_MAX,
|
||||||
|
status: res.statusCode,
|
||||||
|
statusText: res.statusMessage,
|
||||||
|
headers: new Map(Object.entries(res.headers)),
|
||||||
|
body: Readable.toWeb(res),
|
||||||
|
text: async () => {
|
||||||
|
const chunks = [];
|
||||||
|
for await (const chunk of res) chunks.push(chunk);
|
||||||
|
return Buffer.concat(chunks).toString();
|
||||||
|
},
|
||||||
|
json: async () => JSON.parse(await response.text()),
|
||||||
|
};
|
||||||
|
resolve(response);
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on("error", reject);
|
||||||
|
if (options.body) {
|
||||||
|
req.write(typeof options.body === "string" ? options.body : JSON.stringify(options.body));
|
||||||
|
}
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("error", reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Patched fetch with proxy support and MITM DNS bypass
|
||||||
*/
|
*/
|
||||||
async function patchedFetch(url, options = {}) {
|
async function patchedFetch(url, options = {}) {
|
||||||
const targetUrl = typeof url === "string" ? url : url.toString();
|
const targetUrl = typeof url === "string" ? url : url.toString();
|
||||||
const proxyUrl = normalizeProxyUrl(getProxyUrl(targetUrl));
|
|
||||||
|
|
||||||
|
// MITM DNS bypass: resolve real IP for googleapis.com when x-request-source: local
|
||||||
|
if (shouldBypassMitmDns(targetUrl, options)) {
|
||||||
|
try {
|
||||||
|
const parsedUrl = new URL(targetUrl);
|
||||||
|
const realIP = await resolveRealIP(parsedUrl.hostname);
|
||||||
|
if (realIP) return await createBypassRequest(parsedUrl, realIP, options);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(`[ProxyFetch] MITM bypass failed: ${error.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normal proxy handling
|
||||||
|
const proxyUrl = normalizeProxyUrl(getProxyUrl(targetUrl));
|
||||||
if (proxyUrl) {
|
if (proxyUrl) {
|
||||||
try {
|
try {
|
||||||
const dispatcher = await getDispatcher(proxyUrl);
|
const dispatcher = await getDispatcher(proxyUrl);
|
||||||
return await originalFetch(url, { ...options, dispatcher });
|
return await originalFetch(url, { ...options, dispatcher });
|
||||||
} catch (proxyError) {
|
} catch (proxyError) {
|
||||||
// Fallback to direct connection if proxy fails
|
|
||||||
console.warn(`[ProxyFetch] Proxy failed, falling back to direct: ${proxyError.message}`);
|
console.warn(`[ProxyFetch] Proxy failed, falling back to direct: ${proxyError.message}`);
|
||||||
return originalFetch(url, options);
|
return originalFetch(url, options);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// Ensure proxyFetch is loaded to patch globalThis.fetch
|
||||||
|
import "open-sse/index.js";
|
||||||
|
|
||||||
import { getProviderConnectionById, updateProviderConnection } from "@/lib/localDb";
|
import { getProviderConnectionById, updateProviderConnection } from "@/lib/localDb";
|
||||||
import { getUsageForProvider } from "open-sse/services/usage.js";
|
import { getUsageForProvider } from "open-sse/services/usage.js";
|
||||||
import { getExecutor } from "open-sse/executors/index.js";
|
import { getExecutor } from "open-sse/executors/index.js";
|
||||||
|
|||||||
@@ -261,12 +261,16 @@ const PROVIDERS = {
|
|||||||
"User-Agent": ANTIGRAVITY_CONFIG.loadCodeAssistUserAgent,
|
"User-Agent": ANTIGRAVITY_CONFIG.loadCodeAssistUserAgent,
|
||||||
"X-Goog-Api-Client": ANTIGRAVITY_CONFIG.loadCodeAssistApiClient,
|
"X-Goog-Api-Client": ANTIGRAVITY_CONFIG.loadCodeAssistApiClient,
|
||||||
"Client-Metadata": ANTIGRAVITY_CONFIG.loadCodeAssistClientMetadata,
|
"Client-Metadata": ANTIGRAVITY_CONFIG.loadCodeAssistClientMetadata,
|
||||||
|
"x-request-source": "local", // MITM passthrough marker
|
||||||
};
|
};
|
||||||
const metadata = getOAuthClientMetadata();
|
const metadata = getOAuthClientMetadata();
|
||||||
|
|
||||||
// Fetch user info
|
// Fetch user info
|
||||||
const userInfoRes = await fetch(`${ANTIGRAVITY_CONFIG.userInfoUrl}?alt=json`, {
|
const userInfoRes = await fetch(`${ANTIGRAVITY_CONFIG.userInfoUrl}?alt=json`, {
|
||||||
headers: { Authorization: `Bearer ${tokens.access_token}` },
|
headers: {
|
||||||
|
Authorization: `Bearer ${tokens.access_token}`,
|
||||||
|
"x-request-source": "local", // MITM passthrough marker
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const userInfo = userInfoRes.ok ? await userInfoRes.json() : {};
|
const userInfo = userInfoRes.ok ? await userInfoRes.json() : {};
|
||||||
|
|
||||||
|
|||||||
@@ -201,20 +201,17 @@ const server = https.createServer(sslOptions, async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const model = extractModel(req.url, bodyBuffer);
|
const model = extractModel(req.url, bodyBuffer);
|
||||||
console.log(`📡 intercepted: ${req.url} | model: ${model}`);
|
|
||||||
const mappedModel = getMappedModel(model);
|
const mappedModel = getMappedModel(model);
|
||||||
|
|
||||||
if (!mappedModel) {
|
if (!mappedModel) {
|
||||||
return passthrough(req, res, bodyBuffer);
|
return passthrough(req, res, bodyBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`🔀 ${model} → ${mappedModel}`);
|
|
||||||
return intercept(req, res, bodyBuffer, mappedModel);
|
return intercept(req, res, bodyBuffer, mappedModel);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(LOCAL_PORT, () => {
|
server.listen(LOCAL_PORT, () => {
|
||||||
console.log(`🚀 MITM ready on :${LOCAL_PORT} → ${ROUTER_URL}`);
|
console.log(`🚀 MITM ready on :${LOCAL_PORT}`);
|
||||||
console.log(`📡 Intercepting: ${TARGET_HOSTS.join(", ")}`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("error", (error) => {
|
server.on("error", (error) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user