2020-07-17 10:03:38 +02:00
|
|
|
import requestIp from 'request-ip';
|
|
|
|
import { browserName, detectOS } from 'detect-browser';
|
2020-07-23 05:45:09 +02:00
|
|
|
import isLocalhost from 'is-localhost-ip';
|
2021-10-11 20:40:54 +02:00
|
|
|
import { WebServiceClient } from '@maxmind/geoip2-node';
|
|
|
|
const client = new WebServiceClient(process.env.MAXMIND_ID, process.env.MAXMIND_KEY);
|
2020-09-27 05:46:20 +02:00
|
|
|
|
2020-08-07 04:37:19 +02:00
|
|
|
import {
|
|
|
|
DESKTOP_OS,
|
|
|
|
MOBILE_OS,
|
|
|
|
DESKTOP_SCREEN_WIDTH,
|
|
|
|
LAPTOP_SCREEN_WIDTH,
|
|
|
|
MOBILE_SCREEN_WIDTH,
|
|
|
|
} from './constants';
|
2020-07-18 06:01:49 +02:00
|
|
|
|
2020-07-17 10:03:38 +02:00
|
|
|
export function getIpAddress(req) {
|
2020-07-18 09:25:29 +02:00
|
|
|
// Cloudflare
|
2020-07-17 10:03:38 +02:00
|
|
|
if (req.headers['cf-connecting-ip']) {
|
|
|
|
return req.headers['cf-connecting-ip'];
|
|
|
|
}
|
2020-07-18 09:25:29 +02:00
|
|
|
|
2020-07-17 10:03:38 +02:00
|
|
|
return requestIp.getClientIp(req);
|
|
|
|
}
|
|
|
|
|
2020-08-07 04:14:44 +02:00
|
|
|
export function getDevice(screen, browser, os) {
|
2020-08-07 04:37:19 +02:00
|
|
|
if (!screen) return;
|
|
|
|
|
2020-08-07 04:14:44 +02:00
|
|
|
const [width] = screen.split('x');
|
2020-07-17 10:03:38 +02:00
|
|
|
|
2020-08-07 04:14:44 +02:00
|
|
|
if (DESKTOP_OS.includes(os)) {
|
|
|
|
if (os === 'Chrome OS' || width < DESKTOP_SCREEN_WIDTH) {
|
|
|
|
return 'laptop';
|
|
|
|
}
|
|
|
|
return 'desktop';
|
|
|
|
} else if (MOBILE_OS.includes(os)) {
|
2020-08-08 05:36:57 +02:00
|
|
|
if (os === 'Amazon OS' || width > MOBILE_SCREEN_WIDTH) {
|
2020-08-07 04:14:44 +02:00
|
|
|
return 'tablet';
|
|
|
|
}
|
|
|
|
return 'mobile';
|
|
|
|
}
|
2020-08-07 04:37:19 +02:00
|
|
|
|
|
|
|
if (width >= DESKTOP_SCREEN_WIDTH) {
|
|
|
|
return 'desktop';
|
|
|
|
} else if (width >= LAPTOP_SCREEN_WIDTH) {
|
|
|
|
return 'laptop';
|
|
|
|
} else if (width >= MOBILE_SCREEN_WIDTH) {
|
|
|
|
return 'tablet';
|
|
|
|
} else {
|
|
|
|
return 'mobile';
|
|
|
|
}
|
2020-07-17 10:03:38 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 08:08:28 +02:00
|
|
|
export async function getLocation(req, ip) {
|
2020-07-18 09:25:29 +02:00
|
|
|
// Cloudflare
|
|
|
|
if (req.headers['cf-ipcountry']) {
|
|
|
|
return req.headers['cf-ipcountry'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore local ips
|
|
|
|
if (await isLocalhost(ip)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-11 20:40:54 +02:00
|
|
|
const result = await client.city(ip);
|
2021-10-11 19:53:24 +02:00
|
|
|
|
|
|
|
return {
|
2021-10-11 20:40:54 +02:00
|
|
|
country: result?.country.isoCode,
|
|
|
|
region: result?.subdivisions?.[0]?.names?.en,
|
|
|
|
city: result?.city?.names?.en,
|
2021-10-11 19:53:24 +02:00
|
|
|
};
|
2020-07-17 10:03:38 +02:00
|
|
|
}
|
2020-08-07 04:14:44 +02:00
|
|
|
|
|
|
|
export async function getClientInfo(req, { screen }) {
|
2020-08-21 04:17:27 +02:00
|
|
|
const userAgent = req.headers['user-agent'];
|
2020-08-07 04:14:44 +02:00
|
|
|
const ip = getIpAddress(req);
|
2021-10-11 19:53:24 +02:00
|
|
|
const { country, region, city } = await getLocation(req, ip);
|
2020-08-07 04:14:44 +02:00
|
|
|
const browser = browserName(userAgent);
|
|
|
|
const os = detectOS(userAgent);
|
|
|
|
const device = getDevice(screen, browser, os);
|
|
|
|
|
2021-10-11 08:08:28 +02:00
|
|
|
return { userAgent, browser, os, ip, country, device, region, city };
|
2020-08-07 04:14:44 +02:00
|
|
|
}
|