-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
57 lines (48 loc) · 1.23 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
const url = require('url');
const Cloudflare = require('./lib/cloudflare')
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
// Get Credentials
const b64auth = (request.headers.get("Authorization") || '').split(' ')[1] || ''
const [username, password] = Buffer.from(b64auth, 'base64').toString().split(':')
if(!username || !password) {
return new Response('', {
headers: {
'content-type': 'text/plain',
},
status: 401,
})
}
// Parse Url
const requestUrl = url.parse(request.url, true)
if (requestUrl.pathname != '/update') {
return new Response('', {
headers: {
'content-type': 'text/plain',
},
status: 404,
})
}
const query = requestUrl.query
const hostname = query.hostname
const ip = query.ip
// Initialize cloudflare
const cf = new Cloudflare({
token: password,
})
const zone = await cf.findZone(username)
const record = await cf.findRecord(zone, hostname)
await cf.updateRecord(record, ip)
return new Response('ok', {
headers: {
'content-type': 'text/plain'
},
status: 200
})
}