-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpn-allowlist.js
47 lines (42 loc) · 1.4 KB
/
vpn-allowlist.js
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
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
// Edit this string to include any source IP addresses that should be able to retrieve the current IP allowlist.
const AllowedIPs = "1.2.3.4,5.6.7.8"
async function handleRequest(request) {
// check we have a GET request
if (request.method !== "GET") {
return new Response(``, {status: 200})
}
// validate client IP
const clientIP = request.headers.get("CF-Connecting-IP")
if(AllowedIPs.search(clientIP) > -1){
try {
let values = await SSLAUTHORIZED.list()
var keys = [...values.keys]
while(!values.list_complete){
values = await SSLAUTHORIZED.list({ cursor: values.cursor })
keys.push(...values.keys)
}
if (!keys) {
return new Response(``, {status: 200})
}
const length = keys.length
var content = keys[0].name
if (length > 1) {
for (let i = 1; i < length; i++) {
content = content + `\n` + keys[i].name
}
}
return new Response(content, {status: 200})
}
catch (e) {
/* use `Error: ${e}` response string to debug */
return new Response(``, {status: 200})
}
}
else {
// if we made it here, the IP was rejected
return new Response(``, {status: 403})
}
}