parse server user-agents better

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-08-11 22:45:52 +02:00
parent 6934959d5b
commit fd92e703e4
3 changed files with 11 additions and 7 deletions

View File

@@ -23,7 +23,7 @@ export function parseUserAgent(ua?: string | null) {
} as const; } as const;
} }
const userAgentServerList: string[] = [ const userAgentServerList = [
// Node.js libraries // Node.js libraries
'node', 'node',
'node-fetch', 'node-fetch',
@@ -35,6 +35,8 @@ const userAgentServerList: string[] = [
// Python libraries // Python libraries
'python-requests', 'python-requests',
'python-urllib', 'python-urllib',
'aiohttp',
'python',
// Ruby libraries // Ruby libraries
'Faraday', 'Faraday',
@@ -69,13 +71,13 @@ const userAgentServerList: string[] = [
function isServer(userAgent: string) { function isServer(userAgent: string) {
const match = userAgentServerList.some((server) => const match = userAgentServerList.some((server) =>
userAgent.includes(server) userAgent.toLowerCase().includes(server.toLowerCase())
); );
if (match) { if (match) {
return true; return true;
} }
return !!userAgent.match(/^[^\s]+\/[\d.]+$/); return !!userAgent.match(/^([^\s]+\/[\d.]+\s*)+$/);
} }
export function getDevice(ua: string) { export function getDevice(ua: string) {

View File

@@ -11,7 +11,7 @@ import {
toISOString, toISOString,
} from '@openpanel/common'; } from '@openpanel/common';
import type { IServiceCreateEventPayload, IServiceEvent } from '@openpanel/db'; import type { IServiceCreateEventPayload, IServiceEvent } from '@openpanel/db';
import { createEvent, getProfileIdCached } from '@openpanel/db'; import { createEvent } from '@openpanel/db';
import { getLastScreenViewFromProfileId } from '@openpanel/db/src/services/event.service'; import { getLastScreenViewFromProfileId } from '@openpanel/db/src/services/event.service';
import { eventsQueue, findJobByPrefix, sessionsQueue } from '@openpanel/queue'; import { eventsQueue, findJobByPrefix, sessionsQueue } from '@openpanel/queue';
import type { import type {

View File

@@ -23,7 +23,7 @@ export function parseUserAgent(ua?: string | null) {
} as const; } as const;
} }
const userAgentServerList: string[] = [ const userAgentServerList = [
// Node.js libraries // Node.js libraries
'node', 'node',
'node-fetch', 'node-fetch',
@@ -35,6 +35,8 @@ const userAgentServerList: string[] = [
// Python libraries // Python libraries
'python-requests', 'python-requests',
'python-urllib', 'python-urllib',
'aiohttp',
'python',
// Ruby libraries // Ruby libraries
'Faraday', 'Faraday',
@@ -69,13 +71,13 @@ const userAgentServerList: string[] = [
function isServer(userAgent: string) { function isServer(userAgent: string) {
const match = userAgentServerList.some((server) => const match = userAgentServerList.some((server) =>
userAgent.includes(server) userAgent.toLowerCase().includes(server.toLowerCase())
); );
if (match) { if (match) {
return true; return true;
} }
return !!userAgent.match(/^[^\s]+\/[\d.]+$/); return !!userAgent.match(/^([^\s]+\/[\d.]+\s*)+$/);
} }
export function getDevice(ua: string) { export function getDevice(ua: string) {