-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
72 lines (61 loc) · 1.99 KB
/
server.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const express = require('express');
const got = require('got');
require("dotenv").config();
const { CF_ZONE_ID, CF_TKN } = process.env;
const url = "https://api.cloudflare.com/client/v4/zones/" + CF_ZONE_ID + "/dns_records";
let app = express();
app.get('/nic/update', async(req, res) => {
let recordID;
let previousIP;
const hostname = req.query.hostname
console.log("Hostname: " + hostname);
let remoteAddress = res.socket.remoteAddress.split(":");
const ip = remoteAddress[remoteAddress.length - 1];
console.log("Client IP: " + ip);
//Getting Record ID from cloudflare
try {
const response = await got(url, {
searchParams: {
name: hostname
},
headers: {
"Authorization": "Bearer " + CF_TKN,
"Accept": "application/json"
},
responseType: 'json'
});
recordID = response.body.result[0].id;
previousIP = response.body.result[0].content;
} catch (error) {
console.log(error);
//=> 'Internal server error ...'
}
console.log("Previous IP: " + previousIP);
// Updating record if required
if (ip != previousIP) {
try {
const response = await got.put(url + "/" + recordID, {
json: {
type: "A",
name: hostname,
ttl: 120,
content: ip
},
headers: {
"Authorization": "Bearer " + CF_TKN,
"Accept": "application/json",
"Content-Type": "application/json"
},
responseType: 'json'
});
console.log("Success: " + response.body.success);
} catch (error) {
console.log("ERROR: " + error);
//=> 'Internal server error ...'
}
res.send("good " + ip);
} else {
res.send("nochg " + ip);
}
});
app.listen(80);