* Initial Typescript models. * Re-add realtime data * get distinct sessions for session metrics * Add queries for new schema. * Fix Typo. * Add some api/team endpoints. * Fix destructure error. * Fix getWebsites call. * Ignore typescript build errors. * Fix enum issue. * add clickhouse route to deleteWebsite * Fix Website auth. * Updated lint-staged config. * Add permission checks. * Add user role api. * Fix error when updating website. * Fix isAdmin check. Fix Schema. * Initial conversion to react-basics. * Remove user/team transfer from website update. * delete website in relational query * Fix login secure token creation. * Add event type to event. * Allow user to be added to team with role. * Updated login form. * Add Role to TeamUser. * Add database migration. * Refactored permissions check. Updated redis lib. * Feat/um 114 roles and permissions (#1683) * Auth checkpoint. * Merge branch 'dev' into feat/um-114-roles-and-permissions * Add 02 migration. * Added lib/types. * Updated schema. * Updated roles and permissions logic. * Implement react-basics styles. Fix queries. * Update website details layout. * Add 01 migration. * Fix admin create. * Update react-basics. Co-authored-by: Francis Cao <franciscao@gmail.com> Co-authored-by: Mike Cao <mike@mikecao.com> Co-authored-by: Mike Cao <moocao@gmail.com>
112 lines
2.7 KiB
TypeScript
112 lines
2.7 KiB
TypeScript
const { Resolver } = require('dns').promises;
|
|
import isbot from 'isbot';
|
|
import ipaddr from 'ipaddr.js';
|
|
import { createToken, unauthorized, send, badRequest, forbidden } from 'next-basics';
|
|
import { savePageView, saveEvent } from 'queries';
|
|
import { useCors, useSession } from 'lib/middleware';
|
|
import { getJsonBody, getIpAddress } from 'lib/request';
|
|
import { secret } from 'lib/crypto';
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
export interface NextApiRequestCollect extends NextApiRequest {
|
|
session: {
|
|
id: string;
|
|
websiteId: string;
|
|
hostname: string;
|
|
browser: string;
|
|
os: string;
|
|
device: string;
|
|
screen: string;
|
|
language: string;
|
|
country: string;
|
|
};
|
|
}
|
|
|
|
export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
|
|
await useCors(req, res);
|
|
|
|
if (isbot(req.headers['user-agent']) && !process.env.DISABLE_BOT_CHECK) {
|
|
return unauthorized(res);
|
|
}
|
|
|
|
const { type, payload } = getJsonBody(req);
|
|
|
|
const { referrer, event_name: eventName, event_data: eventData } = payload;
|
|
let { url } = payload;
|
|
|
|
// Validate eventData is JSON
|
|
const valid = eventData && typeof eventData === 'object' && !Array.isArray(eventData);
|
|
|
|
if (!valid) {
|
|
return badRequest(res, 'Event Data must be in the form of a JSON Object.');
|
|
}
|
|
|
|
const ignoreIps = process.env.IGNORE_IP;
|
|
const ignoreHostnames = process.env.IGNORE_HOSTNAME;
|
|
|
|
if (ignoreIps || ignoreHostnames) {
|
|
const ips = [];
|
|
|
|
if (ignoreIps) {
|
|
ips.push(...ignoreIps.split(',').map(n => n.trim()));
|
|
}
|
|
|
|
if (ignoreHostnames) {
|
|
const resolver = new Resolver();
|
|
const promises = ignoreHostnames
|
|
.split(',')
|
|
.map(n => resolver.resolve4(n.trim()).catch(() => {}));
|
|
|
|
await Promise.all(promises).then(resolvedIps => {
|
|
ips.push(...resolvedIps.filter(n => n).flatMap(n => n));
|
|
});
|
|
}
|
|
|
|
const clientIp = getIpAddress(req);
|
|
|
|
const blocked = ips.find(ip => {
|
|
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;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
|
|
if (blocked) {
|
|
return forbidden(res);
|
|
}
|
|
}
|
|
|
|
await useSession(req, res);
|
|
|
|
const session = req.session;
|
|
|
|
if (process.env.REMOVE_TRAILING_SLASH) {
|
|
url = url.replace(/\/$/, '');
|
|
}
|
|
|
|
if (type === 'pageview') {
|
|
await savePageView({ ...session, url, referrer });
|
|
} else if (type === 'event') {
|
|
await saveEvent({
|
|
...session,
|
|
url,
|
|
referrer,
|
|
eventName,
|
|
eventData,
|
|
});
|
|
} else {
|
|
return badRequest(res);
|
|
}
|
|
|
|
const token = createToken(session, secret());
|
|
|
|
return send(res, token);
|
|
};
|