-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (58 loc) · 1.43 KB
/
app.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
'use strict';
require('dotenv').config();
const publicIp = require('public-ip');
const fetch = require('node-fetch');
const moment = require('moment');
const logSymbols = require('log-symbols');
const timeFormat = "YYYY-DD-MM hh:mm";
const userAgent = 'GDDNS Updater';
const updateInterval = process.env.GDDNS_INT; // 900000 = 15 minutes
let currentIP = {
v4: null,
v6: null
};
let getIP = async () => {
let ip = {
v4: null,
v6: null
};
ip.v4 = await publicIp.v4();
ip.v6 = await publicIp.v6();
return ip;
};
startCheck();
setInterval(() => {
startCheck();
}, updateInterval);
function startCheck() {
let ip = getIP();
if (ip !== currentIP) {
currentIP = ip;
console.info(`${logSymbols.info} ${(moment().format(timeFormat))} | Updating IP to: ${ip.v4}`);
updateIP(ip.v4);
} else {
console.info( `${logSymbols.info} ${moment().format(timeFormat)} | No update required.`);
}
}
function updateIP(newIp) {
const url = `https://${process.env.GDDNS_USER}:${process.env.GDDNS_PASS}@domains.google.com/nic/update`;
let options = {
method: 'POST',
qs: {
hostname: process.env.GDDNS_HOST,
myip: newIp
},
headers: {
'User-Agent': userAgent
}
};
fetch(url, options)
.then(res => console.log(res))
.then(body => {
console.log(body);
console.log(`${logSymbols.success} Updated successfully`)
})
.catch(err => {
console.error(err)
});
}