mirror of
https://github.com/umami-software/umami.git
synced 2026-02-07 14:17:13 +01:00
Auth and session middleware.
This commit is contained in:
parent
590a70c2ff
commit
d81ee3932d
14 changed files with 142 additions and 73 deletions
45
lib/utils.js
45
lib/utils.js
|
|
@ -1,45 +0,0 @@
|
|||
import requestIp from 'request-ip';
|
||||
import { browserName, detectOS } from 'detect-browser';
|
||||
import isLocalhost from 'is-localhost-ip';
|
||||
import maxmind from 'maxmind';
|
||||
import geolite2 from 'geolite2-redist';
|
||||
|
||||
export function getIpAddress(req) {
|
||||
// Cloudflare
|
||||
if (req.headers['cf-connecting-ip']) {
|
||||
return req.headers['cf-connecting-ip'];
|
||||
}
|
||||
|
||||
return requestIp.getClientIp(req);
|
||||
}
|
||||
|
||||
export function getDevice(req) {
|
||||
const userAgent = req.headers['user-agent'];
|
||||
const browser = browserName(userAgent);
|
||||
const os = detectOS(userAgent);
|
||||
|
||||
return { userAgent, 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;
|
||||
}
|
||||
|
||||
// Database lookup
|
||||
const lookup = await geolite2.open('GeoLite2-Country', path => {
|
||||
return maxmind.open(path);
|
||||
});
|
||||
|
||||
const result = lookup.get(ip);
|
||||
|
||||
lookup.close();
|
||||
|
||||
return result.country.iso_code;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue