mirror of
https://github.com/umami-software/umami.git
synced 2026-02-20 12:35:38 +01:00
add prefecture and city info
This commit is contained in:
parent
911eae897f
commit
75f49d3042
3 changed files with 26 additions and 17 deletions
|
|
@ -2,7 +2,6 @@
|
|||
import path from 'path';
|
||||
import requestIp from 'request-ip';
|
||||
import { browserName, detectOS } from 'detect-browser';
|
||||
import isLocalhost from 'is-localhost-ip';
|
||||
import maxmind from 'maxmind';
|
||||
|
||||
import {
|
||||
|
|
@ -56,17 +55,7 @@ export function getDevice(screen, browser, os) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function getCountry(req, ip) {
|
||||
// Cloudflare
|
||||
if (req.headers['cf-ipcountry']) {
|
||||
return req.headers['cf-ipcountry'];
|
||||
}
|
||||
|
||||
// Ignore local ips
|
||||
if (await isLocalhost(ip)) {
|
||||
return;
|
||||
}
|
||||
|
||||
export async function getGeoInfo(ip) {
|
||||
// Database lookup
|
||||
if (!lookup) {
|
||||
lookup = await maxmind.open(path.resolve('node_modules/.geo/GeoLite2-City.mmdb'));
|
||||
|
|
@ -74,20 +63,32 @@ export async function getCountry(req, ip) {
|
|||
|
||||
const result = lookup.get(ip);
|
||||
|
||||
console.log(JSON.stringify(result));
|
||||
if (!result) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return result?.country?.iso_code;
|
||||
console.log(result.subdivisions);
|
||||
|
||||
const city = result.city?.names.en;
|
||||
const country = result.country?.iso_code;
|
||||
const prefecture = result.subdivisions[0]?.iso_code;
|
||||
|
||||
return {
|
||||
city,
|
||||
country,
|
||||
prefecture,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getClientInfo(req, { screen }) {
|
||||
const userAgent = req.headers['user-agent'];
|
||||
const ip = getIpAddress(req);
|
||||
const country = await getCountry(req, ip);
|
||||
const { country, prefecture, city } = await getGeoInfo(ip);
|
||||
const browser = browserName(userAgent);
|
||||
const os = detectOS(userAgent);
|
||||
const device = getDevice(screen, browser, os);
|
||||
|
||||
return { userAgent, browser, os, ip, country, device };
|
||||
return { userAgent, browser, os, ip, country, prefecture, city, device };
|
||||
}
|
||||
|
||||
export function getJsonBody(req) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue