mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 08:55:47 +00:00
POST /api/report validates an untrusted report against the shared schema whitelist (free text cannot enter storage), caps it at 32KB and writes it to Workers KV under a 128-bit unguessable id with a user-chosen TTL (1/7/30 days); GET /api/report/:id serves the read-only report page and AI agents, behind a short edge cache since reports are immutable. The whole feature is env-gated on the three CLOUDFLARE_* variables and surfaced to the frontend as configs.reportSharing. Rate limiting is deliberately left to the deployment layer (edge rules / the env-gated global /api limiter), not the handler. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
37 lines
1.8 KiB
JavaScript
37 lines
1.8 KiB
JavaScript
// Validate environment variables exist to enable/disable frontend features
|
|
import { isReportSharingConfigured } from './share-report.js';
|
|
|
|
export default (req, res) => {
|
|
// defensive; app.get() in backend-server.js already gates method, but a
|
|
// dedicated smoke test asserts this branch directly against the handler.
|
|
if (req.method !== 'GET') {
|
|
return res.status(405).json({ message: 'Method Not Allowed' });
|
|
}
|
|
|
|
// Referer has already been validated by requireReferer middleware. Here we
|
|
// just read it to classify whether the caller is the canonical ipcheck.ing
|
|
// deployment ("originalSite") or someone self-hosting a fork.
|
|
const referer = req.headers.referer;
|
|
const hostname = referer ? new URL(referer).hostname : '';
|
|
const allowedHostnames = ['ipcheck.ing', 'www.ipcheck.ing', 'localtest.ipcheck.ing', 'dev.ipcheck.ing', 'test.ipcheck.ing'];
|
|
const originalSite = allowedHostnames.includes(hostname);
|
|
|
|
// IPINFO_API_TOKEN / CLOUDFLARE_API are the pre-rename spellings — keep
|
|
// reading them so existing deployments keep their features on upgrade.
|
|
const envConfigs = {
|
|
map: process.env.GOOGLE_MAP_API_KEY,
|
|
ipInfo: process.env.IPINFO_API_KEY || process.env.IPINFO_API_TOKEN,
|
|
ipChecking: process.env.IPCHECKING_API_KEY,
|
|
ip2location: process.env.IP2LOCATION_API_KEY,
|
|
originalSite,
|
|
cloudFlare: process.env.CLOUDFLARE_API_KEY || process.env.CLOUDFLARE_API,
|
|
ipapiis: process.env.IPAPIIS_API_KEY,
|
|
// Share-link feature gate: all three CLOUDFLARE_* KV variables present.
|
|
reportSharing: isReportSharingConfigured(),
|
|
};
|
|
let result = {};
|
|
for (const key in envConfigs) {
|
|
result[key] = !!envConfigs[key];
|
|
}
|
|
res.status(200).json(result);
|
|
}; |