Domain validation. Filter domain from referrers.

This commit is contained in:
Mike Cao 2020-08-28 21:34:20 -07:00
parent a9765c7ea7
commit 5a4cde854a
13 changed files with 51 additions and 23 deletions

View file

@ -1,5 +1,7 @@
export const AUTH_COOKIE_NAME = 'umami.auth';
export const DOMAIN_REGEX = /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/;
export const DESKTOP_SCREEN_WIDTH = 1920;
export const LAPTOP_SCREEN_WIDTH = 1024;
export const MOBILE_SCREEN_WIDTH = 479;

View file

@ -98,12 +98,6 @@ export function getDateArray(data, startDate, endDate, unit) {
function findData(t) {
const x = data.find(e => {
console.log(
new Date(e.t),
getLocalTime(new Date(e.t)),
getLocalTime(new Date(e.t)).getTime(),
normalize(new Date(t)).getTime(),
);
return getLocalTime(new Date(e.t)).getTime() === normalize(new Date(t)).getTime();
});

View file

@ -1,8 +1,8 @@
import firstBy from 'thenby';
import { BROWSERS, ISO_COUNTRIES, DEVICES } from './constants';
import { removeTrailingSlash } from './format';
import { removeTrailingSlash, getDomainName } from './url';
export const urlFilter = (data, { domain, raw }) => {
export const urlFilter = (data, { raw }) => {
const isValidUrl = url => {
return url !== '' && !url.startsWith('#');
};
@ -30,7 +30,7 @@ export const urlFilter = (data, { domain, raw }) => {
return obj;
}
const url = cleanUrl(x.startsWith('/') ? `http://${domain}${x}` : x);
const url = cleanUrl(`http://x${x}`);
if (url) {
if (!obj[url]) {
@ -49,7 +49,8 @@ export const urlFilter = (data, { domain, raw }) => {
};
export const refFilter = (data, { domain, domainOnly, raw }) => {
const regex = new RegExp(domain.startsWith('http') ? domain : `http[s]?://${domain}`);
const domainName = getDomainName(domain);
const regex = new RegExp(`http[s]?://${domainName}`);
const isValidRef = ref => {
return ref !== '' && !ref.startsWith('/') && !ref.startsWith('#');
@ -63,7 +64,7 @@ export const refFilter = (data, { domain, domainOnly, raw }) => {
try {
const { hostname, origin, pathname, searchParams, protocol } = new URL(url);
if (hostname === domain || regex.test(url)) {
if (hostname === domainName) {
return null;
}

View file

@ -62,7 +62,3 @@ export function formatLongNumber(value) {
return formatNumber(n);
}
export function removeTrailingSlash(url) {
return url.length > 1 && url.endsWith('/') ? url.slice(0, -1) : url;
}

View file

@ -347,9 +347,11 @@ export function getPageviews(
return Promise.resolve([]);
}
export function getRankings(website_id, start_at, end_at, type, table) {
export function getRankings(website_id, start_at, end_at, type, table, domain) {
const db = getDatabase();
const filter = domain ? `and ${type} not like '%${domain}%'` : '';
if (db === POSTGRESQL) {
return prisma.$queryRaw(
`
@ -357,6 +359,7 @@ export function getRankings(website_id, start_at, end_at, type, table) {
from ${table}
where website_id=$1
and created_at between $2 and $3
${filter}
group by 1
order by 2 desc
`,
@ -373,6 +376,7 @@ export function getRankings(website_id, start_at, end_at, type, table) {
from ${table}
where website_id=?
and created_at between ? and ?
${filter}
group by 1
order by 2 desc
`,

11
lib/url.js Normal file
View file

@ -0,0 +1,11 @@
export function removeTrailingSlash(url) {
return url.length > 1 && url.endsWith('/') ? url.slice(0, -1) : url;
}
export function getDomainName(str) {
try {
return new URL(str).hostname;
} catch {
return str;
}
}