8000 [pull] main from jason5ng32:main by pull[bot] · Pull Request #25 · comanche2008/MyIP · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ IPAPIIS_API_KEY=""
IP2LOCATION_API_KEY=""
MAC_LOOKUP_API_KEY=""
RIPESTAT_SOURCE_APP=""
# Shareable diagnostic reports — stored in Cloudflare Workers KV. Both values
# below must be set (together with CLOUDFLARE_API_KEY, which additionally
# needs the "Workers KV Storage: Edit" permission) or the share-link feature
# stays hidden. CLOUDFLARE_KV_NAMESPACE_ID is the namespace's hex ID from the
# dashboard, not its name.
CLOUDFLARE_ACCOUNT_ID=""
CLOUDFLARE_KV_NAMESPACE_ID=""
# MaxMind
MAXMIND_ACCOUNT_ID=""
MAXMIND_LICENSE_KEY=""
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ Download `GeoLite2-City.mmdb` and `GeoLite2-ASN.mmdb` from your MaxMind account
| `IPINFO_API_KEY` | No | `""` | API Token for IPInfo.io, used to obtain IP geolocation information through IPInfo.io |
| `IPAPIIS_API_KEY` | No | `""` | API Key for IPAPI.is, used to obtain IP geolocation information through IPAPI.is |
| `IP2LOCATION_API_KEY` | No | `""` | API Key for IP2Location.io, used to obtain IP geolocation information through IP2Location.io |
| `CLOUDFLARE_API_KEY` | No | `""` | API Key for Cloudflare, used to obtain AS system information through Cloudflare |
| `CLOUDFLARE_API_KEY` | No | `""` | API Key for Cloudflare, used to obtain AS system information; with the two KV variables below (and the "Workers KV Storage: Edit" token permission) it also powers shareable diagnostic reports |
| `CLOUDFLARE_ACCOUNT_ID` | No | `""` | Cloudflare account ID, required for storing shareable diagnostic reports in Workers KV |
| `CLOUDFLARE_KV_NAMESPACE_ID` | No | `""` | Hex ID (not the name) of the Workers KV namespace that stores shareable diagnostic reports |
| `RIPESTAT_SOURCE_APP` | No | `""` | Source app name for RIPE.net, used to obtain ASN history information through RIPE.net |
| `MAC_LOOKUP_API_KEY` | No | `""` | API Key for MAC Lookup, used to obtain MAC address information |
| `VITE_CURL_IPV4_DOMAIN` | No | `""` | Provides the IPv4 domain for the CURL API to users |
Expand Down
4 changes: 3 additions & 1 deletion README_FR.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ Téléchargez `GeoLite2-City.mmdb` et `GeoLite2-ASN.mmdb` depuis votre compte Ma
| `IPINFO_API_KEY` | Non | `""` | Jeton API pour IPInfo.io, utilisé pour obtenir des informations de géolocalisation sur l'adresse IP via IPInfo.io |
| `IPAPIIS_API_KEY` | Non | `""` | Clé API pour IPAPI.is, utilisée pour obtenir des informations de géolocalisation sur l'adresse IP via IPAPI.is |
| `IP2LOCATION_API_KEY` | Non | `""` | Clé API pour IP2Location.io, utilisée pour obtenir des informations de géolocalisation sur l'adresse IP via IP2Location.io |
| `CLOUDFLARE_API_KEY` | Non | `""` | Clé API pour Cloudflare, utilisée pour obtenir des informations sur le système AS via Cloudflare |
| `CLOUDFLARE_API_KEY` | Non | `""` | Clé API pour Cloudflare, utilisée pour les informations AS ; avec les deux variables KV ci-dessous (et la permission « Workers KV Storage: Edit »), elle alimente aussi le partage de rapports de diagnostic |
| `CLOUDFLARE_ACCOUNT_ID` | Non | `""` | ID du compte Cloudflare, requis pour stocker les rapports de diagnostic partageables dans Workers KV |
| `CLOUDFLARE_KV_NAMESPACE_ID` | Non | `""` | ID hexadécimal (pas le nom) de l'espace de noms Workers KV qui stocke les rapports de diagnostic partageables |
| `RIPESTAT_SOURCE_APP` | Non | `""` | Nom de l'application source pour RIPE.net, utilisé pour obtenir des informations sur l'historique ASN via RIPE.net |
| `MAC_LOOKUP_API_KEY` | Non | `""` | Clé API pour MAC Lookup, utilisée pour obtenir des informations sur l'adresse MAC via MAC Lookup |
| `VITE_CURL_IPV4_DOMAIN` | Non | `""` | Fournit aux utilisateurs le domaine IPv4 pour l'API CURL |
Expand Down
4 changes: 3 additions & 1 deletion README_TR.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ MaxMind hesabınızdan `GeoLite2-City.mmdb` ve `GeoLite2-ASN.mmdb` dosyalarını
| `IPINFO_API_KEY` | Hayır | `""` | IPInfo.io API token'ı, IP konum bilgisi almak için |
| `IPAPIIS_API_KEY` | Hayır | `""` | IPAPI.is API anahtarı, IP konum bilgisi almak için |
| `IP2LOCATION_API_KEY` | Hayır | `""` | IP2Location.io API anahtarı, IP konum bilgisi almak için |
| `CLOUDFLARE_API_KEY` | Hayır | `""` | Cloudflare API anahtarı, AS sistemi bilgisi almak için |
| `CLOUDFLARE_API_KEY` | Hayır | `""` | Cloudflare API anahtarı, AS sistemi bilgisi almak için; aşağıdaki iki KV değişkeniyle ("Workers KV Storage: Edit" izni gerekir) tanı raporu paylaşımını da sağlar |
| `CLOUDFLARE_ACCOUNT_ID` | Hayır | `""` | Cloudflare hesap kimliği, paylaşılabilir tanı raporlarını Workers KV'de saklamak için gerekli |
| `CLOUDFLARE_KV_NAMESPACE_ID` | Hayır | `""` | Paylaşılabilir tanı raporlarını saklayan Workers KV ad alanının onaltılık kimliği (adı değil) |
| `RIPESTAT_SOURCE_APP` | Hayır | `""` | RIPE.net kaynak uygulama adı, RIPE.net üzerinden ASN geçmiş bilgisi almak için |
| `MAC_LOOKUP_API_KEY` | Hayır | `""` | MAC Lookup API anahtarı, MAC adresi bilgisi almak için |
| `VITE_CURL_IPV4_DOMAIN` | Hayır | `""` | Kullanıcılara CURL API için IPv4 domain sağlar |
Expand Down
4 changes: 3 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ MyIP 依赖 MaxMind 提供的免费 **GeoLite2** 数据库(City + ASN)来进
| `IPINFO_API_KEY` | 否 | `""` | IPInfo.io 的 API Token,用于通过 IPInfo.io 获取 IP 归属地信息 |
| `IPAPIIS_API_KEY` | 否 | `""` | IPAPI.is 的 API Key,用于通过 IPAPI.is 获取 IP 归属地信息 |
| `IP2LOCATION_API_KEY` | 否 | `""` | IP2Location.io 的 API Key,用于通过 IP2Location.io 获取 IP 归属地信息 |
| `CLOUDFLARE_API_KEY` | 否 | `""` | Cloudflare 的 API Key,用于通过 Cloudflare 获取 AS 系统的信息 |
| `CLOUDFLARE_API_KEY` | 否 | `""` | Cloudflare 的 API Key,用于获取 AS 系统信息;配合下方两个 KV 变量(token 需追加 "Workers KV Storage: Edit" 权限)还用于诊断报告分享 |
| `CLOUDFLARE_ACCOUNT_ID` | 否 | `""` | Cloudflare 账号 ID,诊断报告分享(Workers KV 存储)所需 |
| `CLOUDFLARE_KV_NAMESPACE_ID` | 否 | `""` | 存储诊断分享报告的 Workers KV namespace 的十六进制 ID(不是名字) |
| `RIPESTAT_SOURCE_APP` | 否 | `""` | RIPE.net 的源应用名称,用于通过 RIPE.net 获取 ASN 的历史信息 |
| `MAC_LOOKUP_API_KEY` | 否 | `""` | MAC 查询的 API Key,用于通过 MAC Lookup 获取 MAC 地址的归属信息 |
| `VITE_CURL_IPV4_DOMAIN` | 否 | `""` | 为用户提供 CURL API 的 IPv4 域名 |
Expand Down
5 changes: 3 additions & 2 deletions api/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Roughly one handler file per route: IP-geolocation sources (`ipinfo-io` /
`cf-radar` / `asn-history` / `asn-connectivity` / `service-status` /
`google-map` / `github-stars` / `invisibility-test` / `dns-leak-test`), user
proxies (`get-user-info` / `update-user-achievement`), platform
(`configs` / `sentry-tunnel`). Each file's header comment states its route
and purpose — read those for specifics.
(`configs` / `sentry-tunnel` / `share-report`). Each file's header comment
states its route and purpose — read those for specifics.

## Conventions

Expand Down Expand Up @@ -64,6 +64,7 @@ these checks:
- `requireValidASN()` — `?asn=`, strips `AS`, rewrites to numeric
(`cf-radar` predates it and still validates inline).
- `requireValidProviderId()` — whitelists `?id=` against service-status slugs.
- `requireValidReportId()` — `/api/report/:id` route param (22-char base64url).

New param shape → new guard in `common/guards.js`, attached in
`backend-server.js`; never open-coded in the handler.
Expand Down
102 changes: 37 additions & 65 deletions api/cf-radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,36 @@ async function fetchFromCloudflare(endpoint) {
return response.json();
}

// ASN information
async function getASNInfo(asn) {
try {
return await fetchFromCloudflare(`/radar/entities/asns/${asn}`);
} catch (error) {
logger.error({ err: error }, 'Failed to fetch ASN info');
throw new Error('Failed to fetch ASN info');
}
};

// IP version distribution
async function getASNIPVersion(asn) {
try {
return await fetchFromCloudflare(`/radar/http/summary/ip_version?asn=${asn}&dateRange=7d`);
} catch (error) {
logger.error({ err: error }, 'Failed to fetch ASN IP version');
throw new Error('Failed to fetch ASN IP version');
}
// The five Radar segments backing one /api/cfradar response, keyed by the
// field name cleanUpResponseData expects.
const SEGMENTS = {
asnInfo: (asn) => `/radar/entities/asns/${asn}`,
ipVersion: (asn) => `/radar/http/summary/ip_version?asn=${asn}&dateRange=7d`,
httpProtocol: (asn) => `/radar/http/summary/http_protocol?asn=${asn}&dateRange=7d`,
deviceType: (asn) => `/radar/http/summary/device_type?asn=${asn}&dateRange=7d`,
botType: (asn) => `/radar/http/summary/bot_class?asn=${asn}&dateRange=7d`,
};

// HTTP protocol distribution
async function getASNHTTPProtocol(asn) {
try {
return await fetchFromCloudflare(`/radar/http/summary/http_protocol?asn=${asn}&dateRange=7d`);
} catch (error) {
logger.error({ err: error }, 'Failed to fetch ASN HTTP protocol');
throw new Error('Failed to fetch ASN HTTP protocol');
}
};

// Device distribution
async function getASNDeviceType(asn) {
try {
return await fetchFromCloudflare(`/radar/http/summary/device_type?asn=${asn}&dateRange=7d`);
} catch (error) {
logger.error({ err: error }, 'Failed to fetch ASN device type');
throw new Error('Failed to fetch ASN device type');
}
};

// Bot distribution
async function getASNBotType(asn) {
try {
return await fetchFromCloudflare(`/radar/http/summary/bot_class?asn=${asn}&dateRange=7d`);
} catch (error) {
logger.error({ err: error }, 'Failed to fetch ASN bot type');
throw new Error('Failed to fetch ASN bot type');
}
// Fetch all segments in parallel. A failed segment is dropped rather than
// failing the whole response — cleanUpResponseData / filterData already
// tolerate sparse data (small ASNs), so partial results degrade to missing
// fields. Logging is the handler's job: it warns on partial failure and
// errors only when every segment failed.
const getAllASNData = async (asn) => {
const names = Object.keys(SEGMENTS);
const settled = await Promise.allSettled(names.map((name) => fetchFromCloudflare(SEGMENTS[name](asn))));
const data = {};
const failed = [];
settled.forEach((result, i) => {
if (result.status === 'fulfilled') {
data[names[i]] = result.value;
} else {
failed.push({ name: names[i], reason: result.reason });
}
});
return { data, failed };
};

// Use Promise.all to make parallel requests
async function getAllASNData(asn) {
try {
const [asnInfo, ipVersion, httpProtocol, deviceType, botType] = await Promise.all([
10BC8 getASNInfo(asn),
getASNIPVersion(asn),
getASNHTTPProtocol(asn),
getASNDeviceType(asn),
getASNBotType(asn)
]);
return { asnInfo, ipVersion, httpProtocol, deviceType, botType };
} catch (error) {
logger.error({ err: error }, 'Failed to fetch all ASN data');
throw new Error('Failed to fetch all ASN data');
}
}

// Validate asn is valid
function isValidASN(asn) {
return /^[0-9]+$/.test(asn);
Expand Down Expand Up @@ -158,9 +121,18 @@ export default async (req, res) => {
}

try {
const { asnInfo, ipVersion, httpProtocol, deviceType, botType } = await getAllASNData(asn);
const { data, failed } = await getAllASNData(asn);

// One log line per request: a full wipe-out is a real failure
if (failed.length === Object.keys(SEGMENTS).length) {
logger.error({ err: failed[0].reason, asn }, 'cf-radar: all Radar segments failed');
return res.status(500).json({ error: 'Internal server error' });
}
if (failed.length > 0) {
logger.warn({ err: failed[0].reason, asn, segments: failed.map((f) => f.name) }, 'cf-radar: partial Radar segment failure');
}

const cleanedResponse = cleanUpResponseData({ asnInfo, ipVersion, httpProtocol, deviceType, botType });
const cleanedResponse = cleanUpResponseData(data);
const finalResponse = formatData(cleanedResponse);
filterData(finalResponse);

Expand Down
4 changes: 4 additions & 0 deletions api/configs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// 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.
Expand All @@ -24,6 +26,8 @@ export default (req, res) => {
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) {
Expand Down
130 changes: 130 additions & 0 deletions api/share-report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Shareable diagnostic report storage — POST /api/report validates an
// untrusted report against the shared schema whitelist and stores it in
// Cloudflare Workers KV (REST API, TTL-expired); GET /api/report/:id fetches
// one for the read-only /r/:id page (and for AI agents given a share link).
//
// The whole feature is env-gated: without the three CLOUDFLARE_* variables
// below both endpoints answer 503 and /api/configs reports
// `reportSharing: false`, so the share-link UI never shows.
//
// Abuse layering (write side): schema whitelist (no free-form text can be
// stored), a size cap (REPORT_MAX_BYTES), 128-bit unguessable ids, KV TTL.
// Rate limiting is
// deliberately NOT done here — it's a deployment concern: edge rules cover
// the hosted site, and the env-gated global /api limiter in
// backend-server.js (SECURITY_RATE_LIMIT) covers self-hosts.

import crypto from 'node:crypto';
import { fetchUpstream } from '../common/fetch-with-timeout.js';
import { validateReport, REPORT_TTL_DAYS, REPORT_MAX_BYTES } from '../common/report-schema.js';
import logger from '../common/logger.js';

// --- env / KV plumbing -------------------------------------------------------

// Read at request time (not module load) so tests and late-loaded dotenv both work.
const kvConfig = () => {
const apiKey = process.env.CLOUDFLARE_API_KEY || process.env.CLOUDFLARE_API;
const accountId = process.env.CLOUDFLARE_ACCOUNT_ID;
const namespaceId = process.env.CLOUDFLARE_KV_NAMESPACE_ID;
if (!apiKey || !accountId || !namespaceId) return null;
return { apiKey, accountId, namespaceId };
};

// Exposed for /api/configs (booleanized there).
export const isReportSharingConfigured = () => kvConfig() !== null;

// A tampered ttlDays is not worth a 400 — anything outside the whitelist is
// forced down to the shortest retention (1 day). Exported for tests.
export const normalizeTtlDays = (ttlDays) =>
(REPORT_TTL_DAYS.includes(ttlDays) ? ttlDays : REPORT_TTL_DAYS[0]);

const kvValueUrl = ({ accountId, namespaceId }, key, queryString = '') =>
`https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}${queryString}`;

// Surface Cloudflare's own error message (truncated) in thrown errors —
// a bare status code turns "multipart required" into a guessing game.
const kvErrorDetail = (response) =>
response.text().then((text) => text.slice(0, 300)).catch(() => '');

// --- handlers ------------------------------------------------------------------

// POST /api/report — body: { report, ttlDays }
const createReport = async (req, res) => {
// defensive; route gating covers this, but the smoke test asserts on it.
if (req.method !== 'POST') {
return res.status(405).json({ message: 'Method Not Allowed' });
}
const config = kvConfig();
if (!config) {
return res.status(503).json({ error: 'Report sharing is not configured' });
}

const { report, ttlDays } = req.body ?? {};
const { ok, errors } = validateReport(report);
if (!ok) {
return res.status(400).json({ error: 'Invalid report', details: errors });
}
const serialized = JSON.stringify(report);
if (Buffer.byteLength(serialized, 'utf8') > REPORT_MAX_BYTES) {
return res.status(413).json({ error: 'Report too large' });
}

try {
const id = crypto.randomBytes(16).toString('base64url');
const ttlSeconds = normalizeTtlDays(ttlDays) * 24 * 60 * 60;
const expiresAt = new Date(Date.now() + ttlSeconds * 1000).toISOString();
// Stored value is a { expiresAt, report } wrapper so readers (the
// report page, AI agents fetching the link) can show when the report
// will disappear — KV's own TTL isn't readable via the values GET.
//
// The KV write endpoint requires multipart/form-data with `value` +
// `metadata` fields (a raw body is a 400); fetch derives the
// multipart boundary from the FormData, so no manual Content-Type.
const form = new FormData();
form.append('value', JSON.stringify({ expiresAt, report }));
form.append('metadata', '{}');
const response = await fetchUpstream(
kvValueUrl(config, id, `?expiration_ttl=${ttlSeconds}`),
{
method: 'PUT',
headers: { 'Authorization': `Bearer ${config.apiKey}` },
body: form,
},
);
if (!response.ok) throw new Error(`KV write responded ${response.status}: ${await kvErrorDetail(response)}`);

return res.status(201).json({ id, expiresAt });
} catch (error) {
logger.error({ err: error }, 'Report share create failed');
return res.status(500).json({ error: error.message });
}
};

// GET /api/report/:id — id format is enforced by requireValidReportId().
export const getReport = async (req, res) => {
if (req.method !== 'GET') {
return res.status(405).json({ message: 'Method Not Allowed' });
}
const config = kvConfig();
if (!config) {
return res.status(503).json({ error: 'Report sharing is not configured' });
}

try {
const response = await fetchUpstream(kvValueUrl(config, req.params.id), {
headers: { 'Authorization': `Bearer ${config.apiKey}` },
});
if (response.status === 404) {
return res.status(404).json({ error: 'Report not found or expired' });
}
if (!response.ok) throw new Error(`KV read responded ${response.status}: ${await kvErrorDetail(response)}`);
// Stored values are validated { expiresAt, report } JSON; parse and
// re-serialize so a corrupt KV value fails here, not in the client.
return res.status(200).json(JSON.parse(await response.text()));
} catch (error) {
logger.error({ err: error, reportId: req.params.id }, 'Report share read failed');
return res.status(500).json({ error: error.message });
}
};

export default createReport;
Loading
Loading
0