Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

parse server user-agents better

Carl-Gerhard Lindesvärdfd92e703e4d4
diff --git a/apps/api/src/utils/parseUserAgent.ts b/apps/api/src/utils/parseUserAgent.ts
index c8671e73f..af469ee7d 100644
--- a/apps/api/src/utils/parseUserAgent.ts
+++ b/apps/api/src/utils/parseUserAgent.ts
@@ -23,7 +23,7 @@ export function parseUserAgent(ua?: string | null) {
} as const;
}
-const userAgentServerList: string[] = [
+const userAgentServerList = [
// Node.js libraries
'node',
'node-fetch',
@@ -35,6 +35,8 @@ const userAgentServerList: string[] = [
// Python libraries
'python-requests',
'python-urllib',
+ 'aiohttp',
+ 'python',
// Ruby libraries
'Faraday',
@@ -69,13 +71,13 @@ const userAgentServerList: string[] = [
function isServer(userAgent: string) {
const match = userAgentServerList.some((server) =>
- userAgent.includes(server)
+ userAgent.toLowerCase().includes(server.toLowerCase())
);
if (match) {
return true;
}
- return !!userAgent.match(/^[^\s]+\/[\d.]+$/);
+ return !!userAgent.match(/^([^\s]+\/[\d.]+\s*)+$/);
}
export function getDevice(ua: string) {
diff --git a/apps/worker/src/jobs/events.incoming-event.ts b/apps/worker/src/jobs/events.incoming-event.ts
index 88822761c..9dfb4fa3e 100644
--- a/apps/worker/src/jobs/events.incoming-event.ts
+++ b/apps/worker/src/jobs/events.incoming-event.ts
@@ -11,7 +11,7 @@ import {
toISOString,
} from '@openpanel/common';
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 { eventsQueue, findJobByPrefix, sessionsQueue } from '@openpanel/queue';
import type {
diff --git a/apps/worker/src/utils/parse-user-agent.ts b/apps/worker/src/utils/parse-user-agent.ts
index c8671e73f..af469ee7d 100644
--- a/apps/worker/src/utils/parse-user-agent.ts
+++ b/apps/worker/src/utils/parse-user-agent.ts
@@ -23,7 +23,7 @@ export function parseUserAgent(ua?: string | null) {
} as const;
}
-const userAgentServerList: string[] = [
+const userAgentServerList = [
// Node.js libraries
'node',
'node-fetch',
@@ -35,6 +35,8 @@ const userAgentServerList: string[] = [
// Python libraries
'python-requests',
'python-urllib',
+ 'aiohttp',
+ 'python',
// Ruby libraries
'Faraday',
@@ -69,13 +71,13 @@ const userAgentServerList: string[] = [
function isServer(userAgent: string) {
const match = userAgentServerList.some((server) =>
- userAgent.includes(server)
+ userAgent.toLowerCase().includes(server.toLowerCase())
);
if (match) {
return true;
}
- return !!userAgent.match(/^[^\s]+\/[\d.]+$/);
+ return !!userAgent.match(/^([^\s]+\/[\d.]+\s*)+$/);
}
export function getDevice(ua: string) {