Refactored send. Purged pages api routes.

This commit is contained in:
Mike Cao 2025-01-31 00:15:39 -08:00
parent 5205551ca8
commit 85382e25af
69 changed files with 286 additions and 4118 deletions

View file

@ -324,6 +324,20 @@ export const BROWSERS = {
yandexbrowser: 'Yandex',
};
export const IP_ADDRESS_HEADERS = [
'cf-connecting-ip',
'x-client-ip',
'x-forwarded-for',
'do-connecting-ip',
'fastly-client-ip',
'true-client-ip',
'x-real-ip',
'x-cluster-client-ip',
'x-forwarded',
'forwarded',
'x-appengine-user-ip',
];
export const MAP_FILE = '/datamaps.world.json';
export const ISO_COUNTRIES = {

View file

@ -1,34 +1,45 @@
import path from 'path';
import { getClientIp } from 'request-ip';
import { browserName, detectOS } from 'detect-browser';
import isLocalhost from 'is-localhost-ip';
import ipaddr from 'ipaddr.js';
import maxmind from 'maxmind';
import { safeDecodeURIComponent } from 'next-basics';
import {
DESKTOP_OS,
MOBILE_OS,
DESKTOP_SCREEN_WIDTH,
LAPTOP_SCREEN_WIDTH,
MOBILE_SCREEN_WIDTH,
IP_ADDRESS_HEADERS,
} from './constants';
import { NextApiRequestCollect } from 'pages/api/send';
let lookup;
export function getIpAddress(req: NextApiRequestCollect) {
const customHeader = String(process.env.CLIENT_IP_HEADER).toLowerCase();
export function getIpAddress(headers: Headers) {
const customHeader = process.env.CLIENT_IP_HEADER;
// Custom header
if (customHeader !== 'undefined' && req.headers[customHeader]) {
return req.headers[customHeader];
}
// Cloudflare
else if (req.headers['cf-connecting-ip']) {
return req.headers['cf-connecting-ip'];
if (customHeader && headers.get(customHeader)) {
return headers.get(customHeader);
}
return getClientIp(req);
const header = IP_ADDRESS_HEADERS.find(name => {
return headers.get(name);
});
const ip = headers.get(header);
if (header === 'x-forwarded-for') {
return ip?.split[',']?.[0]?.trim();
}
if (header === 'forwarded') {
const match = ip.match(/for=(\[?[0-9a-fA-F:.]+\]?)/);
if (match) {
return match[1];
}
}
return ip;
}
export function getDevice(screen: string, os: string) {
@ -67,7 +78,7 @@ function getRegionCode(country: string, region: string) {
return region.includes('-') ? region : `${country}-${region}`;
}
function safeDecodeCfHeader(s: string | undefined | null): string | undefined | null {
function decodeHeader(s: string | undefined | null): string | undefined | null {
if (s === undefined || s === null) {
return s;
}
@ -75,36 +86,38 @@ function safeDecodeCfHeader(s: string | undefined | null): string | undefined |
return Buffer.from(s, 'latin1').toString('utf-8');
}
export async function getLocation(ip: string, req: NextApiRequestCollect) {
export async function getLocation(ip: string, headers: Headers) {
// Ignore local ips
if (await isLocalhost(ip)) {
return;
}
// Cloudflare headers
if (req.headers['cf-ipcountry']) {
const country = safeDecodeCfHeader(req.headers['cf-ipcountry']);
const subdivision1 = safeDecodeCfHeader(req.headers['cf-region-code']);
const city = safeDecodeCfHeader(req.headers['cf-ipcity']);
if (!process.env.SKIP_LOCATION_HEADERS) {
// Cloudflare headers
if (headers.get('cf-ipcountry')) {
const country = decodeHeader(headers.get('cf-ipcountry'));
const subdivision1 = decodeHeader(headers.get('cf-region-code'));
const city = decodeHeader(headers.get('cf-ipcity'));
return {
country,
subdivision1: getRegionCode(country, subdivision1),
city,
};
}
return {
country,
subdivision1: getRegionCode(country, subdivision1),
city,
};
}
// Vercel headers
if (req.headers['x-vercel-ip-country']) {
const country = safeDecodeURIComponent(req.headers['x-vercel-ip-country']);
const subdivision1 = safeDecodeURIComponent(req.headers['x-vercel-ip-country-region']);
const city = safeDecodeURIComponent(req.headers['x-vercel-ip-city']);
// Vercel headers
if (headers.get('x-vercel-ip-country')) {
const country = decodeHeader(headers.get('x-vercel-ip-country'));
const subdivision1 = decodeHeader(headers.get('x-vercel-ip-country-region'));
const city = decodeHeader(headers.get('x-vercel-ip-city'));
return {
country,
subdivision1: getRegionCode(country, subdivision1),
city,
};
return {
country,
subdivision1: getRegionCode(country, subdivision1),
city,
};
}
}
// Database lookup
@ -131,22 +144,22 @@ export async function getLocation(ip: string, req: NextApiRequestCollect) {
}
}
export async function getClientInfo(req: NextApiRequestCollect) {
const userAgent = req.body?.payload?.userAgent || req.headers['user-agent'];
const ip = req.body?.payload?.ip || getIpAddress(req);
const location = await getLocation(ip, req);
export async function getClientInfo(request: Request, payload: Record<string, any>) {
const userAgent = payload?.userAgent || request.headers.get('user-agent');
const ip = payload?.ip || getIpAddress(request.headers);
const location = await getLocation(ip, request.headers);
const country = location?.country;
const subdivision1 = location?.subdivision1;
const subdivision2 = location?.subdivision2;
const city = location?.city;
const browser = browserName(userAgent);
const os = detectOS(userAgent) as string;
const device = getDevice(req.body?.payload?.screen, os);
const device = getDevice(payload?.screen, os);
return { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device };
}
export function hasBlockedIp(req: NextApiRequestCollect) {
export function hasBlockedIp(clientIp: string) {
const ignoreIps = process.env.IGNORE_IP;
if (ignoreIps) {
@ -156,17 +169,19 @@ export function hasBlockedIp(req: NextApiRequestCollect) {
ips.push(...ignoreIps.split(',').map(n => n.trim()));
}
const clientIp = getIpAddress(req);
return ips.find(ip => {
if (ip === clientIp) return true;
if (ip === clientIp) {
return true;
}
// CIDR notation
if (ip.indexOf('/') > 0) {
const addr = ipaddr.parse(clientIp);
const range = ipaddr.parseCIDR(ip);
if (addr.kind() === range[0].kind() && addr.match(range)) return true;
if (addr.kind() === range[0].kind() && addr.match(range)) {
return true;
}
}
});
}

View file

@ -1,26 +1,20 @@
import { serializeError } from 'serialize-error';
import { getWebsiteSession, getWebsite } from 'queries';
import { Website, Session } from '@prisma/client';
import { getClient, redisEnabled } from '@umami/redis-client';
import { getWebsiteSession, getWebsite } from 'queries';
export async function fetchWebsite(websiteId: string): Promise<Website> {
let website = null;
try {
if (redisEnabled) {
const redis = getClient();
if (redisEnabled) {
const redis = getClient();
website = await redis.fetch(`website:${websiteId}`, () => getWebsite(websiteId), 86400);
} else {
website = await getWebsite(websiteId);
}
website = await redis.fetch(`website:${websiteId}`, () => getWebsite(websiteId), 86400);
} else {
website = await getWebsite(websiteId);
}
if (!website || website.deletedAt) {
return null;
}
} catch (e) {
// eslint-disable-next-line no-console
console.log('FETCH WEBSITE ERROR:', serializeError(e));
if (!website || website.deletedAt) {
return null;
}
return website;
@ -29,21 +23,16 @@ export async function fetchWebsite(websiteId: string): Promise<Website> {
export async function fetchSession(websiteId: string, sessionId: string): Promise<Session> {
let session = null;
try {
if (redisEnabled) {
const redis = getClient();
if (redisEnabled) {
const redis = getClient();
session = await redis.fetch(
`session:${sessionId}`,
() => getWebsiteSession(websiteId, sessionId),
86400,
);
} else {
session = await getWebsiteSession(websiteId, sessionId);
}
} catch (e) {
// eslint-disable-next-line no-console
console.log('FETCH SESSION ERROR:', serializeError(e));
session = await redis.fetch(
`session:${sessionId}`,
() => getWebsiteSession(websiteId, sessionId),
86400,
);
} else {
session = await getWebsiteSession(websiteId, sessionId);
}
if (!session) {

View file

@ -1,105 +0,0 @@
import cors from 'cors';
import debug from 'debug';
import { getClient, redisEnabled } from '@umami/redis-client';
import { getAuthToken, parseShareToken } from 'lib/auth';
import { ROLES } from 'lib/constants';
import { secret } from 'lib/crypto';
import { getSession } from 'lib/session';
import {
badRequest,
createMiddleware,
notFound,
parseSecureToken,
unauthorized,
} from 'next-basics';
import { NextApiRequestCollect } from 'pages/api/send';
import { getUser } from '../queries';
const log = debug('umami:middleware');
export const useCors = createMiddleware(
cors({
// Cache CORS preflight request 24 hours by default
maxAge: Number(process.env.CORS_MAX_AGE) || 86400,
}),
);
export const useSession = createMiddleware(async (req, res, next) => {
try {
const session = await getSession(req as NextApiRequestCollect);
if (!session) {
log('useSession: Session not found');
return badRequest(res, 'Session not found.');
}
(req as any).session = session;
} catch (e: any) {
if (e.message.startsWith('Website not found')) {
return notFound(res, e.message);
}
return badRequest(res, e.message);
}
next();
});
export const useAuth = createMiddleware(async (req, res, next) => {
const token = getAuthToken(req);
const payload = parseSecureToken(token, secret());
const shareToken = await parseShareToken(req as any);
let user = null;
const { userId, authKey, grant } = payload || {};
if (userId) {
user = await getUser(userId);
} else if (redisEnabled && authKey) {
const redis = getClient();
const key = await redis.get(authKey);
if (key?.userId) {
user = await getUser(key.userId);
}
}
if (process.env.NODE_ENV === 'development') {
log('useAuth:', { token, shareToken, payload, user, grant });
}
if (!user?.id && !shareToken) {
log('useAuth: User not authorized');
return unauthorized(res);
}
if (user) {
user.isAdmin = user.role === ROLES.admin;
}
(req as any).auth = {
user,
grant,
token,
shareToken,
authKey,
};
next();
});
export const useValidate = async (schema, req, res) => {
return createMiddleware(async (req: any, res, next) => {
try {
const rules = schema[req.method];
if (rules) {
rules.validateSync({ ...req.query, ...req.body });
}
} catch (e: any) {
return badRequest(res, e.message);
}
next();
})(req, res);
};

View file

@ -1,94 +0,0 @@
import { secret, uuid, visitSalt } from 'lib/crypto';
import { getClientInfo } from 'lib/detect';
import { parseToken } from 'next-basics';
import { NextApiRequestCollect } from 'pages/api/send';
import { createSession } from 'queries';
import clickhouse from './clickhouse';
import { fetchSession, fetchWebsite } from './load';
import { SessionData } from 'lib/types';
export async function getSession(req: NextApiRequestCollect): Promise<SessionData> {
const { payload } = req.body;
if (!payload) {
throw new Error('Invalid payload.');
}
// Check if cache token is passed
const cacheToken = req.headers['x-umami-cache'];
if (cacheToken) {
const result = await parseToken(cacheToken, secret());
// Token is valid
if (result) {
return result;
}
}
// Verify payload
const { website: websiteId, hostname, screen, language } = payload;
// Find website
const website = await fetchWebsite(websiteId);
if (!website) {
throw new Error(`Website not found: ${websiteId}.`);
}
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
await getClientInfo(req);
const sessionId = uuid(websiteId, hostname, ip, userAgent);
const visitId = uuid(sessionId, visitSalt());
// Clickhouse does not require session lookup
if (clickhouse.enabled) {
return {
id: sessionId,
websiteId,
visitId,
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
subdivision2,
city,
ip,
userAgent,
};
}
// Find session
let session = await fetchSession(websiteId, sessionId);
// 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,
});
} catch (e: any) {
if (!e.message.toLowerCase().includes('unique constraint')) {
throw e;
}
}
}
return { ...session, visitId };
}