@paywalls-net/filter 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +28 -0
- package/package.json +25 -0
- package/src/index.js +231 -0
- package/src/user-agent-classification.js +214 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 paywalls-net
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# paywalls-net/client
|
|
2
|
+
|
|
3
|
+
SDK for integrating paywalls.net authorization services with CDN or edge environments.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @paywalls-net/client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Environment Variables
|
|
12
|
+
- `PAYWALLS_PUBLISHER_ID`: The unique identifier for the publisher using Paywalls.net services.
|
|
13
|
+
- `PAYWALLS_CLOUD_API_HOST`: The host for the Paywalls.net API. eg `https://cloud-api.paywalls.net`.
|
|
14
|
+
- `PAYWALLS_CLOUD_API_KEY`: The API key for accessing Paywalls.net services. NOTE: This key should be treated like a password and kept secret and stored in a secure secrets vault or environment variable.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
```javascript
|
|
18
|
+
import { init } from '@paywalls-net/client';
|
|
19
|
+
|
|
20
|
+
const handleRequest = await init('cloudflare');
|
|
21
|
+
|
|
22
|
+
export default {
|
|
23
|
+
async fetch(request, env, ctx) {
|
|
24
|
+
return handleRequest(request, env, ctx);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
```
|
|
28
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@paywalls-net/filter",
|
|
3
|
+
"description": "Client SDK for integrating paywalls.net bot filtering and authorization services into your server or CDN.",
|
|
4
|
+
"author": "paywalls.net",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"version": "1.0.5",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/paywalls-net/filter.git"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "index.js",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": "./src/index.js"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"ua-parser-js": "^2.0.4"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example publisher-hosted client code for a Cloudflare Worker that
|
|
3
|
+
* filters bot-like requests by using paywalls.net authorization services.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { classifyUserAgent } from './user-agent-classification.js';
|
|
7
|
+
|
|
8
|
+
async function logAccess(cfg, request, access) {
|
|
9
|
+
// Separate html from the status in the access object.
|
|
10
|
+
const { response, ...status } = access;
|
|
11
|
+
|
|
12
|
+
// Get all headers as a plain object (name-value pairs)
|
|
13
|
+
let headers = {};
|
|
14
|
+
for (const [key, value] of request.headers.entries()) {
|
|
15
|
+
headers[key] = value;
|
|
16
|
+
}
|
|
17
|
+
const url = new URL(request.url);
|
|
18
|
+
let body = {
|
|
19
|
+
account_id: cfg.paywallsPublisherId,
|
|
20
|
+
status: status,
|
|
21
|
+
method: request.method,
|
|
22
|
+
hostname: url.hostname,
|
|
23
|
+
resource: url.pathname + url.search,
|
|
24
|
+
user_agent: headers['user-agent'],
|
|
25
|
+
headers: headers
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const logResponse = await fetch(`${cfg.paywallsAPIHost}/api/filter/access/logs`, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
Authorization: `Bearer ${cfg.paywallsAPIKey}`
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify(body)
|
|
35
|
+
});
|
|
36
|
+
if (!logResponse.ok) {
|
|
37
|
+
console.error(`Error logging access: ${logResponse.status} ${logResponse.statusText}`);
|
|
38
|
+
// Optionally, you can handle the error here, e.g., retry or log to a different service
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Typedef for AgentStatus
|
|
44
|
+
* @typedef {Object} AgentStatus
|
|
45
|
+
* @property {string} access - Whether access is granted (allow, deny)
|
|
46
|
+
* @property {string} reason - The reason for the status (e.g., missing_user_agent, unknown_error)
|
|
47
|
+
* @property {object} response - The response object
|
|
48
|
+
* @property {number} response.code - The HTTP status code to be sent to the client
|
|
49
|
+
* @property {object} response.headers - The headers to be sent to the client
|
|
50
|
+
* @property {string} response.html - The HTML response to be sent to the client
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
*
|
|
55
|
+
* @param {*} cfg
|
|
56
|
+
* @param {Request} request - The incoming request object
|
|
57
|
+
* @returns {Promise<AgentStatus>} - The authorization for this agent
|
|
58
|
+
*/
|
|
59
|
+
async function checkAgentStatus(cfg, request) {
|
|
60
|
+
const userAgent = request.headers.get("User-Agent");
|
|
61
|
+
const token = getAcessToken(request);
|
|
62
|
+
|
|
63
|
+
if (!userAgent) {
|
|
64
|
+
console.error("Missing user-agent");
|
|
65
|
+
return {
|
|
66
|
+
access: 'deny',
|
|
67
|
+
reason: 'missing_user_agent',
|
|
68
|
+
response: { code: 401, html: "Unauthorized access." }
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const agentInfo = classifyUserAgent(userAgent);
|
|
73
|
+
|
|
74
|
+
const body = JSON.stringify({
|
|
75
|
+
account_id: cfg.paywallsPublisherId,
|
|
76
|
+
operator: agentInfo.operator,
|
|
77
|
+
agent: agentInfo.agent,
|
|
78
|
+
token: token
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const response = await fetch(`${cfg.paywallsAPIHost}/api/filter/agents/auth`, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
"Content-Type": "application/json",
|
|
85
|
+
Authorization: `Bearer ${cfg.paywallsAPIKey}`
|
|
86
|
+
},
|
|
87
|
+
body: body
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (!response.ok) {
|
|
91
|
+
console.error(`Failed to fetch agent auth: ${response.status} ${response.statusText}`);
|
|
92
|
+
return {
|
|
93
|
+
access: 'deny',
|
|
94
|
+
reason: 'unknown_error',
|
|
95
|
+
response: { code: 502, html: "Bad Gateway." }
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return response.json();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function getAcessToken(request) {
|
|
103
|
+
const authHeader = request.headers.get("Authorization");
|
|
104
|
+
if (!authHeader) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const token = authHeader.split(" ")[1];
|
|
109
|
+
return token;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function isFastlyKnownBot(request) {
|
|
113
|
+
const botScore = request.headers.get("X-Fastly-Bot-Score");
|
|
114
|
+
const isKnownBot = request.headers.get("X-Fastly-Known-Bot");
|
|
115
|
+
|
|
116
|
+
return isKnownBot === "true" || (botScore && parseInt(botScore) < 30);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function isCloudflareKnownBot(request) {
|
|
120
|
+
const cf = request.cf || {};
|
|
121
|
+
const botScore = cf.botManagement?.score;
|
|
122
|
+
const isKnownBot = cf.botManagement?.verifiedBot;
|
|
123
|
+
return isKnownBot || (botScore !== undefined && botScore < 30);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isTestBot(request) {
|
|
127
|
+
// check if the URL has a query parameter to always test as a bot
|
|
128
|
+
const url = new URL(request.url);
|
|
129
|
+
const uaParam = url.searchParams.get("user-agent");
|
|
130
|
+
return uaParam && uaParam.includes("bot");
|
|
131
|
+
}
|
|
132
|
+
function isPaywallsKnownBot(request) {
|
|
133
|
+
const userAgent = request.headers.get("User-Agent");
|
|
134
|
+
const uaClassification = classifyUserAgent(userAgent);
|
|
135
|
+
return uaClassification.operator && uaClassification.agent;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function isRecognizedBot(request) {
|
|
139
|
+
return isFastlyKnownBot(request) || isCloudflareKnownBot(request) || isTestBot(request) || isPaywallsKnownBot(request);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
function sendResponse(authz) {
|
|
144
|
+
let headers = {
|
|
145
|
+
"Content-Type": "text/html",
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
if (authz.response?.headers) {
|
|
149
|
+
for (const [key, value] of Object.entries(authz.response.headers)) {
|
|
150
|
+
headers[key] = value;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return new Response(authz.response?.html || "Payment required.", {
|
|
155
|
+
status: authz.response?.code || 402,
|
|
156
|
+
headers: headers
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Detect AI Bot and authorize it using paywalls.net.
|
|
162
|
+
* @param {Request} request
|
|
163
|
+
* @param {*} env
|
|
164
|
+
* @param {*} ctx
|
|
165
|
+
* @returns
|
|
166
|
+
*/
|
|
167
|
+
async function cloudflare(config = null) {
|
|
168
|
+
|
|
169
|
+
return async function handle(request, env, ctx) {
|
|
170
|
+
const paywallsConfig = {
|
|
171
|
+
paywallsAPIHost: env.PAYWALLS_CLOUD_API_HOST,
|
|
172
|
+
paywallsAPIKey: env.PAYWALLS_CLOUD_API_KEY,
|
|
173
|
+
paywallsPublisherId: env.PAYWALLS_PUBLISHER_ID
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
if (isRecognizedBot(request)) {
|
|
177
|
+
const authz = await checkAgentStatus(paywallsConfig, request);
|
|
178
|
+
|
|
179
|
+
ctx.waitUntil(logAccess(paywallsConfig, request, authz));
|
|
180
|
+
|
|
181
|
+
if (authz.access === 'deny') {
|
|
182
|
+
return sendResponse(authz);
|
|
183
|
+
} else {
|
|
184
|
+
console.log("Bot-like request allowed. Proceeding to origin/CDN.");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return fetch(request); // Proceed to origin/CDN
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
async function fastly(config) {
|
|
194
|
+
const paywallsConfig = {
|
|
195
|
+
paywallsAPIHost: config.get('PAYWALLS_CLOUD_API_HOST'),
|
|
196
|
+
paywallsAPIKey: config.get('PAYWALLS_API_KEY'),
|
|
197
|
+
paywallsPublisherId: config.get('PAYWALLS_PUBLISHER_ID')
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
return async function handle(request) {
|
|
201
|
+
if (isRecognizedBot(request)) {
|
|
202
|
+
const authz = await checkAgentStatus(paywallsConfig, request);
|
|
203
|
+
|
|
204
|
+
await logAccess(paywallsConfig, request, authz);
|
|
205
|
+
|
|
206
|
+
if (authz.access === 'deny') {
|
|
207
|
+
return sendResponse(authz);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return fetch(request, { backend: 'origin' });
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Initializes the appropriate handler based on the CDN.
|
|
219
|
+
* @param {string} cdn - The name of the CDN (e.g., 'cloudflare', 'fastly', 'cloudfront').
|
|
220
|
+
* @returns {Function} - The handler function for the specified CDN.
|
|
221
|
+
*/
|
|
222
|
+
export async function init(cdn, config = {}) {
|
|
223
|
+
switch (cdn.toLowerCase()) {
|
|
224
|
+
case 'cloudflare':
|
|
225
|
+
return await cloudflare(config);
|
|
226
|
+
case 'fastly':
|
|
227
|
+
return await fastly(config);
|
|
228
|
+
default:
|
|
229
|
+
throw new Error(`Unsupported CDN: ${cdn}`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { UAParser } from 'ua-parser-js';
|
|
2
|
+
|
|
3
|
+
const userAgentPatterns = [
|
|
4
|
+
{
|
|
5
|
+
operator: 'Anthropic',
|
|
6
|
+
agent: 'ClaudeBot',
|
|
7
|
+
usage: ['ai_training'],
|
|
8
|
+
user_initiated: 'no',
|
|
9
|
+
patterns: [/ClaudeBot/, /anthropic-ai/]
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
operator: 'Anthropic',
|
|
13
|
+
agent: 'Claude-User',
|
|
14
|
+
usage: ['ai_chat'],
|
|
15
|
+
user_initiated: 'yes',
|
|
16
|
+
patterns: [/Claude-User/]
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
operator: 'Anthropic',
|
|
20
|
+
agent: 'Claude-SearchBot',
|
|
21
|
+
usage: ['ai_indexing'],
|
|
22
|
+
user_initiated: 'maybe',
|
|
23
|
+
patterns: [/Claude-SearchBot/]
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
{
|
|
27
|
+
operator: 'Lumar',
|
|
28
|
+
agent: 'DeepCrawl',
|
|
29
|
+
usage: ['webmaster tools'],
|
|
30
|
+
user_initiated: 'no',
|
|
31
|
+
patterns: [/deepcrawl.com/]
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
{
|
|
35
|
+
operator: 'Google',
|
|
36
|
+
agent: 'Googlebot',
|
|
37
|
+
usage: ['search_indexing','ai_training'],
|
|
38
|
+
user_initiated: 'maybe',
|
|
39
|
+
patterns: [/Googlebot/]
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
operator: 'Google',
|
|
43
|
+
agent: 'Gemini-Deep-Research',
|
|
44
|
+
usage: ['ai_agents'],
|
|
45
|
+
user_initiated: 'maybe',
|
|
46
|
+
patterns: [/Gemini-Deep-Research/]
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
operator: 'Google',
|
|
50
|
+
agent: 'Google-Extended',
|
|
51
|
+
usage: ['ai_training'],
|
|
52
|
+
usage_prefs_only: true,
|
|
53
|
+
user_initiated: 'no',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
operator: 'Google',
|
|
57
|
+
agent: 'Googlebot-News',
|
|
58
|
+
usage: ['Google News'],
|
|
59
|
+
usage_prefs_only: true,
|
|
60
|
+
user_initiated: 'no'
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
operator: 'Google',
|
|
64
|
+
agent: 'Googlebot-Image',
|
|
65
|
+
usage: ['image indexing'],
|
|
66
|
+
user_initiated: 'no',
|
|
67
|
+
patterns: [/Googlebot-Image/]
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
operator: 'Google',
|
|
71
|
+
agent: 'Google-Site-Verification',
|
|
72
|
+
usage: ['site verification'],
|
|
73
|
+
user_initiated: 'no',
|
|
74
|
+
patterns: [/Google-Site-Verification/]
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
operator: 'Google',
|
|
78
|
+
agent: 'Google Web Preview',
|
|
79
|
+
usage: ['web preview'],
|
|
80
|
+
user_initiated: 'no',
|
|
81
|
+
patterns: [/Google Web Preview/]
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
operator: 'Google',
|
|
85
|
+
agent: 'Googlebot-Video',
|
|
86
|
+
usage: ['video indexing'],
|
|
87
|
+
user_initiated: 'no',
|
|
88
|
+
patterns: [/Googlebot-Video/]
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
operator: 'Google',
|
|
92
|
+
agent: 'FeedFetcher-Google',
|
|
93
|
+
usage: ['Feed crawling'],
|
|
94
|
+
user_initiated: 'yes',
|
|
95
|
+
patterns: [/FeedFetcher-Google/]
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
{
|
|
99
|
+
operator: 'OpenAI',
|
|
100
|
+
agent: 'GPTBot',
|
|
101
|
+
usage: ['ai_training'],
|
|
102
|
+
user_initiated: 'no',
|
|
103
|
+
patterns: [/GPTBot/]
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
operator: 'OpenAI',
|
|
107
|
+
agent: 'OAI-SearchBot',
|
|
108
|
+
usage: ['ai_indexing'],
|
|
109
|
+
user_initiated: 'no',
|
|
110
|
+
patterns: [/OAI-SearchBot/]
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
operator: 'OpenAI',
|
|
114
|
+
agent: 'ChatGPT-User',
|
|
115
|
+
usage: ['ai_chat'],
|
|
116
|
+
user_initiated: 'yes',
|
|
117
|
+
patterns: [/ChatGPT-User/]
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
{
|
|
122
|
+
operator: 'Meta',
|
|
123
|
+
agent: 'facebookexternalhit',
|
|
124
|
+
usage: ['content sharing'],
|
|
125
|
+
user_initiated: 'no',
|
|
126
|
+
patterns: [/facebookexternalhit/]
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
operator: 'Meta',
|
|
130
|
+
agent: 'meta-externalagent',
|
|
131
|
+
usage: ['ai_training'],
|
|
132
|
+
user_initiated: 'no',
|
|
133
|
+
patterns: [/meta-externalagent/]
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
operator: 'Meta',
|
|
137
|
+
agent: 'meta-externalfetcher',
|
|
138
|
+
usage: ['web preview'],
|
|
139
|
+
user_initiated: 'no',
|
|
140
|
+
patterns: [/meta-externalfetcher/]
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
{
|
|
144
|
+
operator: 'Perplexity',
|
|
145
|
+
agent: 'Perplexity-User',
|
|
146
|
+
usage: ['ai_chat'],
|
|
147
|
+
user_initiated: 'yes',
|
|
148
|
+
patterns: [/Perplexity-User/]
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
operator: 'Perplexity',
|
|
152
|
+
agent: 'PerplexityBot',
|
|
153
|
+
usage: ['ai_indexing'],
|
|
154
|
+
user_initiated: 'maybe',
|
|
155
|
+
patterns: [/PerplexityBot/]
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
{
|
|
159
|
+
operator: 'Cohere',
|
|
160
|
+
agent: 'cohere-ai',
|
|
161
|
+
usage: ['ai_training'],
|
|
162
|
+
user_initiated: 'no',
|
|
163
|
+
patterns: [/cohere-ai/i]
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
{
|
|
167
|
+
operator: 'Bing',
|
|
168
|
+
agent: 'BingBot',
|
|
169
|
+
usage: ['search_indexing','ai_indexing'],
|
|
170
|
+
user_initiated: 'maybe',
|
|
171
|
+
patterns: [/bingbot/i, /BingPreview/]
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
{
|
|
175
|
+
operator: 'Microsoft',
|
|
176
|
+
agent: 'BF-DirectLine',
|
|
177
|
+
usage: ['Bot Framework SDK'],
|
|
178
|
+
user_initiated: 'no',
|
|
179
|
+
patterns: [/BF-DirectLine/]
|
|
180
|
+
}
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Classifies the user agent string based on predefined patterns.
|
|
185
|
+
* @param {string} userAgent - The user agent string to classify.
|
|
186
|
+
* @returns {Object} An object containing the browser, OS, operator, usage, and user_initiated status.
|
|
187
|
+
*/
|
|
188
|
+
export function classifyUserAgent(userAgent) {
|
|
189
|
+
const parsedUA = new UAParser(userAgent).getResult();
|
|
190
|
+
|
|
191
|
+
const browser = parsedUA.browser.name || 'Unknown';
|
|
192
|
+
const os = parsedUA.os.name || 'Unknown';
|
|
193
|
+
|
|
194
|
+
for (const config of userAgentPatterns) {
|
|
195
|
+
if (!config.patterns) continue;
|
|
196
|
+
for (const pattern of config.patterns) {
|
|
197
|
+
if (pattern.test(userAgent)) {
|
|
198
|
+
return {
|
|
199
|
+
operator: config.operator,
|
|
200
|
+
agent: config.agent || browser,
|
|
201
|
+
usage: config.usage,
|
|
202
|
+
user_initiated: config.user_initiated,
|
|
203
|
+
browser,
|
|
204
|
+
os,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
browser,
|
|
212
|
+
os
|
|
213
|
+
};
|
|
214
|
+
}
|