Skip to content

Commit

Permalink
MaxMind supported
Browse files Browse the repository at this point in the history
  • Loading branch information
jason5ng32 committed Jul 18, 2024
1 parent 12f5c48 commit 7b1b8e1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
61 changes: 61 additions & 0 deletions api/maxmind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import maxmind from 'maxmind';
import { isValidIP } from '../common/valid-ip.js';
import { refererCheck } from '../common/referer-check.js';

let cityLookup, asnLookup;

// 异步初始化数据库
async function initDatabases() {
cityLookup = await maxmind.open('./common/maxmind-db/GeoLite2-City.mmdb');
asnLookup = await maxmind.open('./common/maxmind-db/GeoLite2-ASN.mmdb');
}

initDatabases();

export default (req, res) => {

// 限制只能从指定域名访问
const referer = req.headers.referer;
if (!refererCheck(referer)) {
return res.status(403).json({ error: referer ? 'Access denied' : 'What are you doing?' });
}

const ip = req.query.ip;
if (!ip) {
return res.status(400).json({ error: 'No IP address provided' });
}

// 检查 IP 地址是否合法
if (!isValidIP(ip)) {
return res.status(400).json({ error: 'Invalid IP address' });
}

// 获取请求语言
const lang = req.query.lang === 'zh-CN' || req.query.lang === 'en' || req.query.lang === 'fr' ? req.query.lang : 'en';

try {
const city = cityLookup.get(ip);
const asn = asnLookup.get(ip);
let result = modifyJson(ip, lang, city, asn);
res.json(result);
} catch (e) {
res.status(500).json({ error: e.message });
}
}

function modifyJson(ip, lang, city, asn) {
city = city || {};
asn = asn || {};
return {
ip,
city: city.city ? city.city.names[lang] || city.city.names.en : "N/A",
region: city.subdivisions ? city.subdivisions[0].names[lang] || city.subdivisions[0].names.en : "N/A",
country: city.country ? city.country.iso_code : "N/A",
country_name: city.country ? city.country.names[lang] : "N/A",
country_code: city.country ? city.country.iso_code : "N/A",
latitude: city.location ? city.location.latitude : "N/A",
longitude: city.location ? city.location.longitude : "N/A",
asn: asn.autonomous_system_number ? "AS" + asn.autonomous_system_number : "N/A",
org: asn.autonomous_system_organization ? asn.autonomous_system_organization : "N/A"
};
};
2 changes: 2 additions & 0 deletions backend-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import whois from './api/whois.js';
import ipapiisHandler from './api/ipapiis.js';
import invisibilitytestHandler from './api/invisibilitytest.js';
import macChecker from './api/macchecker.js';
import maxmindHandler from './api/maxmind.js';

dotenv.config();

Expand Down Expand Up @@ -137,6 +138,7 @@ app.get('/api/whois', whois);
app.get('/api/ipapiis', ipapiisHandler);
app.get('/api/invisibility', invisibilitytestHandler);
app.get('/api/macchecker', macChecker);
app.get('/api/maxmind', maxmindHandler);

// 使用查询参数处理所有配置请求
app.get('/api/configs', validateConfigs);
Expand Down
Binary file added common/maxmind-db/GeoLite2-ASN.mmdb
Binary file not shown.
Binary file added common/maxmind-db/GeoLite2-City.mmdb
Binary file not shown.
1 change: 1 addition & 0 deletions frontend/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const useMainStore = defineStore('main', {
{ id: 4, text: 'KeyCDN', url: '/api/keycdn?ip={{ip}}', enabled: true },
{ id: 5, text: 'IP.SB', url: '/api/ipsb?ip={{ip}}', enabled: true },
{ id: 6, text: 'IPAPI.is', url: '/api/ipapiis?ip={{ip}}', enabled: true },
{ id: 7, text: 'MaxMind', url: '/api/maxmind?ip={{ip}}&lang={{lang}}', enabled: true },
],
}),

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"express-slow-down": "^2.0.3",
"flag-icons": "^7.2.3",
"http-proxy-middleware": "^3.0.0",
"maxmind": "^4.3.20",
"nodemon": "^3.1.4",
"pinia": "^2.1.7",
"svgmap": "^2.10.1",
Expand Down

0 comments on commit 7b1b8e1

Please sign in to comment.