You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/packages/geoip/lib.ts

35 lines
1.2 KiB
TypeScript

4 years ago
import 'hydrooj';
4 years ago
import fs from 'fs';
import path from 'path';
import { Reader } from 'maxmind';
const buffer = fs.readFileSync(path.resolve(__dirname, 'GeoLite2-City.mmdb'));
const reader = new Reader(buffer);
export interface Result {
location?: string,
continent?: string,
country?: string,
city?: string,
display: string
}
export function lookup(ip: string, locale: string): Result {
4 years ago
const res: any = reader.get(ip);
if (!res) return { display: 'Unknown address'.translate(locale) };
const ret: Result = { display: '' };
4 years ago
if (res.location) ret.location = res.location;
if (res.continent) ret.continent = res.continent.names[locale] || res.continent.names.en;
if (res.country || res.registered_country) {
ret.country = (res.country || res.registered_country).names[locale]
|| (res.country || res.registered_country).names.en;
}
if (res.city) ret.city = res.city.names[locale] || res.city.names.en;
ret.display = `${ret.continent} ${ret.country}${ret.city ? ` ${ret.city}` : ''}`;
4 years ago
return ret;
}
export const provider = '<a href="http://www.maxmind.com" target="_blank">MaxMind</a>';
4 years ago
global.Hydro.lib.geoip = { provider, lookup };