umami/lib/utils.js

121 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-07-17 10:03:38 +02:00
import crypto from 'crypto';
import { v5 as uuid } from 'uuid';
import requestIp from 'request-ip';
import { browserName, detectOS } from 'detect-browser';
2020-07-18 09:25:29 +02:00
import maxmind from 'maxmind';
import geolite2 from 'geolite2-redist';
import isLocalhost from 'is-localhost-ip';
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/;
2020-07-17 10:03:38 +02:00
export function md5(s) {
return crypto.createHash('md5').update(s).digest('hex');
}
export function hash(s) {
return uuid(s, md5(process.env.HASH_SALT));
}
2020-07-18 06:01:49 +02:00
export function validHash(s) {
2020-07-18 09:25:29 +02:00
return UUID_REGEX.test(s);
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);
}
export function getDevice(req) {
const userAgent = req.headers['user-agent'];
const browser = browserName(userAgent);
const os = detectOS(userAgent);
return { userAgent, browser, os };
}
2020-07-18 09:25:29 +02:00
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;
2020-07-17 10:03:38 +02:00
}
2020-07-18 09:25:29 +02:00
export async function parseSessionRequest(req) {
if (req.method === 'POST') {
const ip = getIpAddress(req);
const { website_id, hostname, screen, language } = req.body;
const { userAgent, browser, os } = getDevice(req);
const country = await getCountry(req, ip);
const session_id = hash(`${website_id}${hostname}${ip}${userAgent}${os}`);
return {
website_id,
session_id,
hostname,
browser,
os,
screen,
language,
country,
};
}
return {};
2020-07-17 10:03:38 +02:00
}
export function parseCollectRequest(req) {
if (req.method === 'POST') {
const { type, payload } = req.body;
if (payload.session) {
const {
2020-07-17 10:03:38 +02:00
url,
referrer,
2020-07-19 11:23:15 +02:00
event_type,
event_value,
session: { website_id, session_id, time, hash: validationHash },
} = payload;
if (
validHash(website_id) &&
validHash(session_id) &&
validHash(validationHash) &&
hash(`${website_id}${session_id}${time}`) === validationHash
) {
return {
success: 1,
type,
2020-07-19 08:54:25 +02:00
website_id,
session_id,
url,
referrer,
2020-07-19 11:23:15 +02:00
event_type,
event_value,
};
}
2020-07-17 10:03:38 +02:00
}
}
return { success: 0 };
2020-07-17 10:03:38 +02:00
}