-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
88 lines (76 loc) · 2.99 KB
/
function.js
File metadata and controls
88 lines (76 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function handler(event) {
const request = event.request;
let uri;
try {
uri = request.uri ? decodeURIComponent(request.uri).trim().toLowerCase() : '';
} catch (_e) {
return createNotFoundResponse();
}
// ====================================================
// Block requests with no user agent
// ====================================================
const userAgentHeader = request.headers['user-agent'];
if (!userAgentHeader || !userAgentHeader.value || !userAgentHeader.value.trim()) {
return createNotFoundResponse();
}
// =====================================================
// Always Allow robots.txt, ads.txt
// =====================================================
if (/^\/(robots\.txt|ads\.txt)$/.test(uri)) {
return request;
}
// ====================================================
// Chrome Private Prefetch Proxy — traffic-advice
// ====================================================
if (uri === '/.well-known/traffic-advice') {
return createTrafficAdviceResponse();
}
// ====================================================
// Obvious security scans
// ====================================================
if (isSecurityScanUri(uri)) {
return createNotFoundResponse();
}
const ua = userAgentHeader.value.toLowerCase();
// ====================================================
// DENIES scrapper bots
// ====================================================
if (isScrapperBot(ua)) {
return createNotFoundResponse();
}
// Pass through
return request;
}
function isSecurityScanUri(uri) {
return (
uri === '/ip' ||
uri.includes('/.env') ||
uri.startsWith('/.git') ||
/\.(php\d?|sql|bak|phtml|phar)$/.test(uri) ||
/^\/(images?|img|wp-includes|static|wp|wordpress|old|new|blog|backup|cgi-bin|admin|administrator|wp-admin|phpmyadmin|pma)(\/|$)/.test(uri)
);
}
function isScrapperBot(normalizedUserAgent) {
return /presto|trident|crios|fxios|yaapp_android|yasearchbrowser|ev-crawler|seamus the search engine|dataforseobot|\bptst\//.test(normalizedUserAgent);
}
function createNotFoundResponse() {
return {
statusCode: 404,
statusDescription: 'Not Found',
headers: {"content-type": {value: "text/plain"}},
body: 'Not Found'
};
}
function createTrafficAdviceResponse() {
return {
statusCode: 200,
headers: {
'content-type': {value: 'application/trafficadvice+json'},
'permissions-policy': {value: 'browsing-topics=(), prefetch=()'},
'cache-control': {value: 'max-age=63072000'},
'traffic-advice': {value: '1.0'}
},
body: '[{ "user_agent": "prefetch-proxy", "google_prefetch_proxy_eap": { "fraction": 1.0 } },{ "user_agent": "*", "accept": { "purpose": { "prefetch": true, "prerender": true },"sec-purpose": { "prefetch": true, "prerender": true }} }]'
};
}
export {handler};