umami/src/lib/session.ts

130 lines
2.9 KiB
TypeScript
Raw Normal View History

import { isUuid, secret, sessionSalt, uuid } from 'lib/crypto';
2023-08-31 01:40:32 +02:00
import { getClientInfo } from 'lib/detect';
2023-07-29 02:21:34 +02:00
import { parseToken } from 'next-basics';
2023-08-31 01:40:32 +02:00
import { NextApiRequestCollect } from 'pages/api/send';
2023-04-02 02:38:35 +02:00
import { createSession } from 'queries';
2023-05-16 05:41:12 +02:00
import cache from './cache';
2023-08-23 20:55:45 +02:00
import clickhouse from './clickhouse';
2023-07-26 08:59:08 +02:00
import { loadSession, loadWebsite } from './load';
2023-08-23 20:55:45 +02:00
export async function findSession(req: NextApiRequestCollect): Promise<{
id: any;
websiteId: string;
visitId: string;
2023-08-23 20:55:45 +02:00
hostname: string;
browser: string;
os: any;
device: string;
screen: string;
language: string;
country: any;
subdivision1: any;
subdivision2: any;
city: any;
ownerId: string;
}> {
2023-08-31 01:40:32 +02:00
const { payload } = req.body;
2020-08-09 08:48:43 +02:00
if (!payload) {
throw new Error('Invalid payload.');
2020-08-09 08:48:43 +02:00
}
// Check if cache token is passed
const cacheToken = req.headers['x-umami-cache'];
2020-10-03 05:33:46 +02:00
if (cacheToken) {
const result = await parseToken(cacheToken, secret());
2020-10-03 05:33:46 +02:00
if (result) {
2023-05-16 05:41:12 +02:00
await checkUserBlock(result?.ownerId);
2020-10-03 05:33:46 +02:00
return result;
}
}
// Verify payload
2022-11-01 07:42:37 +01:00
const { website: websiteId, hostname, screen, language } = payload;
2022-08-26 08:12:47 +02:00
// Check the hostname value for legality to eliminate dirty data
const validHostnameRegex = /^[\w-.]+$/;
if (!validHostnameRegex.test(hostname)) {
throw new Error('Invalid hostname.');
}
2023-07-29 00:52:21 +02:00
if (!isUuid(websiteId)) {
throw new Error('Invalid website ID.');
2020-08-12 05:05:40 +02:00
}
// Find website
2023-04-02 02:38:35 +02:00
const website = await loadWebsite(websiteId);
2022-08-29 22:04:58 +02:00
2023-04-02 02:38:35 +02:00
if (!website) {
throw new Error(`Website not found: ${websiteId}.`);
2020-08-21 04:17:27 +02:00
}
2020-08-12 05:05:40 +02:00
2023-05-16 05:41:12 +02:00
await checkUserBlock(website.userId);
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
await getClientInfo(req, payload);
2020-08-12 05:05:40 +02:00
const sessionId = uuid(websiteId, hostname, ip, userAgent);
const visitId = uuid(sessionId, sessionSalt());
2023-08-23 20:55:45 +02:00
// Clickhouse does not require session lookup
if (clickhouse.enabled) {
return {
id: sessionId,
websiteId,
visitId,
2023-08-23 20:55:45 +02:00
hostname,
browser,
os: os as any,
device,
screen,
language,
country,
subdivision1,
subdivision2,
city,
ownerId: website.userId,
};
}
2022-11-09 02:11:08 +01:00
// Find session
2023-04-03 02:23:12 +02:00
let session = await loadSession(sessionId);
2022-11-09 02:11:08 +01:00
// Create a session if not found
if (!session) {
try {
session = await createSession({
id: sessionId,
websiteId,
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
subdivision2,
city,
2022-11-09 02:11:08 +01:00
});
2023-03-30 20:18:57 +02:00
} catch (e: any) {
2022-11-09 02:11:08 +01:00
if (!e.message.toLowerCase().includes('unique constraint')) {
throw e;
}
}
}
return { ...session, ownerId: website.userId, visitId: visitId };
2023-05-16 05:41:12 +02:00
}
async function checkUserBlock(userId: string) {
if (process.env.ENABLE_BLOCKER && (await cache.fetchUserBlock(userId))) {
2023-06-01 20:54:24 +02:00
await cache.incrementUserBlock(userId);
2023-05-16 05:41:12 +02:00
throw new Error('Usage Limit.');
}
2020-08-05 07:45:05 +02:00
}