2020-09-27 05:21:16 +02:00
|
|
|
import path from 'path';
|
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';
|
2020-07-18 09:25:29 +02:00
|
|
|
import maxmind from 'maxmind';
|
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';
|
2023-03-30 20:18:57 +02:00
|
|
|
import { NextApiRequestCollect } from 'pages/api/send';
|
2020-07-18 06:01:49 +02:00
|
|
|
|
2021-04-26 03:32:06 +02:00
|
|
|
let lookup;
|
|
|
|
|
2020-07-17 10:03:38 +02:00
|
|
|
export function getIpAddress(req) {
|
2021-11-21 18:39:22 +01:00
|
|
|
// Custom header
|
|
|
|
if (req.headers[process.env.CLIENT_IP_HEADER]) {
|
|
|
|
return req.headers[process.env.CLIENT_IP_HEADER];
|
|
|
|
}
|
2020-07-18 09:25:29 +02:00
|
|
|
// Cloudflare
|
2021-11-21 18:39:22 +01:00
|
|
|
else if (req.headers['cf-connecting-ip']) {
|
2020-07-17 10:03:38 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-17 03:55:51 +01:00
|
|
|
export function getDevice(screen, 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
|
|
|
}
|
|
|
|
|
2023-04-25 04:29:31 +02:00
|
|
|
export async function getLocation(ip, req) {
|
2020-07-18 09:25:29 +02:00
|
|
|
// Ignore local ips
|
|
|
|
if (await isLocalhost(ip)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:52:29 +02:00
|
|
|
if (req.headers['x-vercel-ip-country']) {
|
2023-04-26 01:29:47 +02:00
|
|
|
const country = req.headers['x-vercel-ip-country'];
|
|
|
|
const region = req.headers['x-vercel-ip-country-region'];
|
|
|
|
const city = req.headers['x-vercel-ip-city'];
|
|
|
|
|
2023-04-25 04:29:31 +02:00
|
|
|
return {
|
2023-04-26 01:29:47 +02:00
|
|
|
country,
|
2023-04-26 01:51:42 +02:00
|
|
|
subdivision1: region,
|
2023-04-26 01:29:47 +02:00
|
|
|
city: decodeURIComponent(city),
|
2023-04-25 04:29:31 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-18 09:25:29 +02:00
|
|
|
// Database lookup
|
2021-04-26 03:32:06 +02:00
|
|
|
if (!lookup) {
|
2023-03-29 04:24:36 +02:00
|
|
|
const dir = path.join(process.cwd(), 'geo');
|
|
|
|
|
|
|
|
lookup = await maxmind.open(path.resolve(dir, 'GeoLite2-City.mmdb'));
|
2021-04-26 03:32:06 +02:00
|
|
|
}
|
2020-07-18 09:25:29 +02:00
|
|
|
|
|
|
|
const result = lookup.get(ip);
|
|
|
|
|
2023-04-25 04:29:31 +02:00
|
|
|
if (result) {
|
|
|
|
return {
|
|
|
|
country: result.country?.iso_code ?? result?.registered_country?.iso_code,
|
|
|
|
subdivision1: result.subdivisions?.[0]?.iso_code,
|
|
|
|
subdivision2: result.subdivisions?.[1]?.names?.en,
|
|
|
|
city: result.city?.names?.en,
|
|
|
|
};
|
|
|
|
}
|
2020-07-17 10:03:38 +02:00
|
|
|
}
|
2020-08-07 04:14:44 +02:00
|
|
|
|
2023-03-30 20:18:57 +02:00
|
|
|
export async function getClientInfo(req: NextApiRequestCollect, { 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);
|
2023-04-25 04:29:31 +02:00
|
|
|
const location = await getLocation(ip, req);
|
2023-03-16 00:39:55 +01:00
|
|
|
const country = location?.country;
|
|
|
|
const subdivision1 = location?.subdivision1;
|
|
|
|
const subdivision2 = location?.subdivision2;
|
|
|
|
const city = location?.city;
|
2020-08-07 04:14:44 +02:00
|
|
|
const browser = browserName(userAgent);
|
|
|
|
const os = detectOS(userAgent);
|
2023-03-30 20:18:57 +02:00
|
|
|
const device = getDevice(screen, os);
|
2020-08-07 04:14:44 +02:00
|
|
|
|
2023-02-17 03:55:51 +01:00
|
|
|
return { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device };
|
2020-08-07 04:14:44 +02:00
|
|
|
}
|
2022-03-11 04:01:33 +01:00
|
|
|
|
2023-03-30 20:18:57 +02:00
|
|
|
export function getJsonBody<T>(req): T {
|
2022-03-12 07:17:47 +01:00
|
|
|
if ((req.headers['content-type'] || '').indexOf('text/plain') !== -1) {
|
2022-03-11 04:01:33 +01:00
|
|
|
return JSON.parse(req.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
return req.body;
|
|
|
|
}
|