-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_updater.js
executable file
·189 lines (151 loc) · 6.63 KB
/
ip_updater.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var Promise = require('bluebird')
var _ = require('lodash');
var request = require('request');
var parseString = require('xml2js').parseString;
var request = Promise.promisify(request);//we make hhtp request promises compatible
var parseString = Promise.promisify(parseString);//making promises compatible to xml parsinf function
var config = require('./config')
//var log = new Log('debug', fs.createWriteStream('ip_check.log',{'flags': 'a'}));
//var config.ipifyPublicHttpServiceQuery = "https://api.ipify.org?format=text";
//var config.namesilo_target_domain = "kamansoft.com";
//var namesilo_target_resource_host = "lab"
//var namesilo_api_key = "b2fdae37c2932c3eb47f";
//var config.record_list_http_query_template = _.template("https://www.namesilo.com/api/dnsListRecords?version=1&type=xml&key=<%- apiKey %>&domain=<%- targetDomain %>")
//var config.update_http_query_template = _.template("https://www.namesilo.com/api/dnsUpdateRecord?version=1&type=xml&key=<%- apiKey %>&domain=<%- targetDomain %>&rrid=<%- targetResourceId %>&rrhost=<%- targetResourceHost %>&rrvalue=<%- value %>&rrttl=7207 ")
function ValidateIPaddress(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
return (true)
}
return (false)
}
/**
* this function is a wrapping for a promised request function
* as i want the promise to be rejected if the http respónse status isnt 200
* this resolves a request response
* @param queryUrl String, the url to request from some server
* @returns Promise
*/
function httpApiRequest(queryUrl) {
return new Promise(function (resolve, reject) {
//requesting
//console.log(queryUrl.split("/"))
var splitedQuery=queryUrl.split("/")
var urlDomain = splitedQuery[2];
console.log("atempt http request to: " + urlDomain)
//console.log("full query: "+queryUrl )
request(queryUrl, function (err, res, body) {
if (err) {
reject(err);
} else {
console.log("succes conecction to: " + urlDomain)
if (res.statusCode === 200) {
resolve(body)
} else {
reject({ message: "no 200 status code on " + urlDomain + " request", response: response.toJSON })
}
}
})
})
}
function namesiloApiPromisedReplyHandler(parsedResponseBody) {
return new Promise(function (resolve, reject) {
//console.log("handler "+parsedResponseBody.namesilo.reply[0].code[0])
if (parsedResponseBody.namesilo.reply[0].code[0] === '300' || parsedResponseBody.namesilo.reply[0].code[0] === '301') {
resolve(parsedResponseBody)
} else {
reject({ message: "non valid dns api response code", resposeReply: parsedResponseBody.namesilo.reply[0] })
}
})
}
/**
* checkValidRecord cheks for a record on a recordlist
* @param recordList array with all elements to search on
* @param record String element to find on recordList
* @returns the found value or false
*/
function isValidRecord(record, recordList) {
var result = _.find(recordList,
{
type: ['A'],
host: [record]
}
);
if (result !== undefined) {
return result
} else {
return false
}
}
/**
* ipUdater this function will do the main job, to update a namesilo record matching the value of fullNamesiloTargetHost
* param with one of the element on the recordlist
*
*/
function ipUpdater(recordList, ipToTargetHost, resourceHost) {
var target_record_data = isValidRecord(resourceHost+ "." + config.namesilo_target_domain, recordList);
if (target_record_data) {
var ipSetedOnTargetHost = target_record_data.value[0];
var targetResourceId = target_record_data.record_id[0];
console.log("server public ip is: " + ipToTargetHost + " and namesilo host record ip value is : " + ipSetedOnTargetHost)
if (ipToTargetHost !== ipSetedOnTargetHost) {
httpApiRequest(config.update_http_query_template(
{
apiKey: config.namesilo_api_key,
targetDomain: config.namesilo_target_domain,
targetResourceId: targetResourceId,
targetResourceHost: resourceHost,
value: ipToTargetHost
}))
.then(function (result) {
return parseString(result)
})
.then(function(result){
return namesiloApiPromisedReplyHandler(result)
})
.then(function(result){
console.log("record value for "+resourceHost+ "." + config.namesilo_target_domain+" succesfully updated")
//console.log(result.namesilo.reply[0])
})
.catch(function (err) {
console.log(err)
//log.error(err)
})
} else {
//console.log("no change on ip")
console.log("there is no need to update "+resourceHost+ "." + config.namesilo_target_domain+". Public ip and reosurce record value ip are equals")
}
} else {
console.log(resourceHost+ "." + config.namesilo_target_domain+" is not a valid record on namesilo")
}
}
function main() {
httpApiRequest(config.record_list_http_query_template({
apiKey: config.namesilo_api_key,
targetDomain: config.namesilo_target_domain
}))
.then(function (result) {
return parseString(result)
})
.then(function (result) {
return namesiloApiPromisedReplyHandler(result)
})
.then(function (result) {
return Promise.all([result, httpApiRequest(config.ipifyPublicHttpServiceQuery)])
})
.then(function (results) {
var ipToTargetHost = results[1];
if (ValidateIPaddress(ipToTargetHost)) {
var recordList = results[0].namesilo.reply[0].resource_record;
var promisedRecords = _.map(config.namesilo_target_resource_hosts, function (v, k, l) {
ipUpdater(recordList,ipToTargetHost,v);
}, this)
} else {
return Promise.reject("the retrived public ip addres isnt in a valid format")
}
})
.catch(function (err) {
console.log(err)
//log.error(err)
})
}
setInterval(main, config.ipifyPollPeriod);