mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 18:23:27 +00:00
7 handlers that take ?ip (ipinfo, ipapicom, ipchecking, ipsb, ipapiis,
ip2location, maxmind) each opened with the same 8-line block:
const ipAddress = req.query.ip;
if (!ipAddress) return res.status(400).json({ error: 'No IP...' });
if (!isValidIP(ipAddress)) return res.status(400).json({ ... });
Replace by attaching requireValidIP() to the matching routes in
backend-server.js. The handler bodies now assume ?ip has been validated
upstream and just read it:
const ipAddress = req.query.ip;
Handlers that accept other param shapes (ASN, MAC, domain, user ID) or
no params are untouched — they retain whatever handler-specific
validation they need.
Tests updated:
- tests/api-handlers.test.js: drop the 2×3 = 6 "rejects missing/malformed
ip" cases for ip-sb / ipapi-com / ipinfo-io, the 2×2 = 4 for
ipapi-is / ip2location-io, and the 2 for ipcheck-ing. Each set was
testing what the middleware now owns; tests/guards.test.js covers it.
The "reports missing API key after IP passes validation" case for
ipcheck-ing stays — that exercises handler logic beyond the middleware.
- tests/api-smoke.test.js: drop the maxmind "rejects missing and invalid
IP parameters" case (moved to middleware).
Net -13 tests vs pre-commit; 195 still pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
17 lines
515 B
JavaScript
17 lines
515 B
JavaScript
import { lookupMaxMind } from '../common/maxmind-service.js';
|
|
|
|
export default (req, res) => {
|
|
// IP presence + validity guaranteed by requireValidIP middleware.
|
|
const ip = req.query.ip;
|
|
|
|
// 获取请求语言
|
|
const supportedLanguages = ['zh-CN', 'en', 'fr', 'tr'];
|
|
const lang = supportedLanguages.includes(req.query.lang) ? req.query.lang : 'en';
|
|
|
|
try {
|
|
res.json(lookupMaxMind(ip, lang));
|
|
} catch (e) {
|
|
res.status(e.statusCode || 500).json({ error: e.message });
|
|
}
|
|
}
|