Files
MyIP/api/invisibility-test.js
jason5ng32andClaude Opus 5.5 c29e48687b Refactor(layout): split backend-only code out of common/, group frontend utils
common/ had become the backend's library: 30 of its 41 modules were
imported by nothing in the frontend, so the name no longer said what
belonged there. It now holds only what the frontend or the build imports
too; backend-only code moved to server/, flat except for datasets/ (the
dataset engine and its readers). The *-db/ dataset directories stay in
common/: self-hosting docs and Docker volumes mount those paths.

frontend/utils/ keeps its app-wide modules at the root and gains ip/
(IP / ASN shaping and validation), report/ (the report pipeline) and
features/ (the pure layer of a single feature). Paths only — no logic
changes. The Dockerfile copies server/, and the AGENTS.md files say where
a new module goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:10:02 +08:00

85 lines
3.0 KiB
JavaScript

import { fetchUpstream } from '../common/fetch-with-timeout.js';
import logger from '../server/logger.js';
// If length is not 28 and is not a combination of letters and numbers, return false
function isValidUserID(userID) {
if (typeof userID !== 'string') return false;
if (userID.length !== 28 || !/^[a-zA-Z0-9]+$/.test(userID)) return false;
return true;
}
export default async (req, res) => {
const id = req.query.id;
if (!id) {
return res.status(400).json({ error: 'No ID provided' });
}
// Check if address is valid
if (!isValidUserID(id)) {
return res.status(400).json({ error: 'Invalid ID' });
}
const apikey = process.env.IPCHECKING_API_KEY;
if (!apikey) {
return res.status(500).json({ error: 'API key is missing' });
}
const apiEndpoint = process.env.IPCHECKING_API_ENDPOINT;
const url = new URL(`${apiEndpoint}/getpdresult/${id}?apikey=${apikey}`);
try {
const apiResponse = await fetchUpstream(url, {
headers: {
...req.headers,
}
});
// Upstream 404 = "result not computed yet" — a normal answer while
// the frontend polls after starting a test, not a failure. Translate
// to 200 {status:'pending'}
if (apiResponse.status === 404) {
return res.json({ status: 'pending' });
}
// Upstream 429 = monthly quota exhausted. Pass status + code through
// so the frontend can point at the sponsor path; not an error for us.
if (apiResponse.status === 429) {
const errorData = await apiResponse.json().catch(() => ({}));
return res.status(429).json({
error: errorData.error || 'Monthly quota exceeded',
code: 'quota_exceeded'
});
}
// Upstream 401/403. Pass the status through so the frontend prompts
// sign-in instead of retrying; keep it off the error logger.
if (apiResponse.status === 401 || apiResponse.status === 403) {
let detail = 'Sign in required';
try {
const errorData = await apiResponse.json();
detail = errorData.message || detail;
} catch { /* unreadable body — keep the generic detail */ }
return res.status(apiResponse.status).json({ error: detail });
}
// Catch upstream error
if (!apiResponse.ok) {
let errorDetail = '';
try {
const errorData = await apiResponse.json();
errorDetail = errorData.message || JSON.stringify(errorData);
} catch {
errorDetail = apiResponse.statusText;
}
throw new Error(`API responded with status: ${apiResponse.status} - ${errorDetail}`);
}
const data = await apiResponse.json();
res.json(data);
} catch (error) {
logger.error({ err: error }, 'invisibility-test upstream request failed');
res.status(500).json({ error: error.message });
}
};