-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (59 loc) · 2.46 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module.exports = function(domain,token,minutes = 5, noParamReq = false, onResponse = undefined) {
const https = require('https');
class DuckDNSUpdater{
constructor(domain,token,minutes,noParamReq,onResponse){
this.YOURDOMAIN = domain || "domain";
this.YOURTOKEN = token || "token";
this.MINUTES = minutes;
this.URL = `https://www.duckdns.org/update?domains=${this.YOURDOMAIN}&token=${this.YOURTOKEN}&verbose=true`;
if(noParamReq) this.URL = `https://www.duckdns.org/update/${this.YOURDOMAIN}/${this.YOURTOKEN}`;
if(typeof onResponse !== 'function') this.onResponse = this.responseCallback.bind(this);
else this.onResponse = onResponse.bind(this);
this.listener = undefined;
}
start(){
if(this.listener == undefined){
this.updateServer(this.URL);
this.listener = setInterval(() => this.updateServer(this.URL), this.MINUTES*1000*60);
}
}
stop(){
clearTimeout(this.listener);
this.listener = undefined;
}
updateServer(URL){
this.https_redirect(URL,this.onResponse);
}
responseCallback(data){
let dataArr = data.split("\n");
if(dataArr[0]=="OK"){
let infos = "";
if(dataArr.length > 1 && dataArr[1] && dataArr[3]){
infos = ", ip: "+dataArr[1]+", "+dataArr[3];
}
console.log("|DUCKDNS|info| Pinged "+this.YOURDOMAIN+infos);
}
else console.log("|DUCKDNS|WARN| Error, response: \n"+data);
}
https_redirect(link,callback){
https.get(link, (resp)=>{
let data = '';
if(resp.statusCode == 301){
this.https_redirect(resp.headers.location,callback);
return;
}
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
callback(data);
});
}).on("error", (err) => {
callback("Error: " + err.message);
});
}
}
return new DuckDNSUpdater(domain,token,minutes,noParamReq,onResponse);
}