-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherixDNSPod.js
178 lines (147 loc) · 6.38 KB
/
erixDNSPod.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
const axios = require("axios");
const erixConf = require("./erixConf");
exports.erixDNSPod = class {
constructor(log) {
this.log = log;
this.sys = new erixConf.erixConf('dnspod', log);
}
async getIp() {
try {
if (!this.sys.conf || !this.sys.conf.domains) {
return { ok: false, message: 'Configure is not loaded.' };
}
//get current ip address
var res = await axios.get('https://api.ipify.org?format=json');
this.sys.saveLog(1, 'Get current IP address:\t' + res.data.ip);
if (!this.sys.conf.currentIp || this.sys.conf.currentIp.ip !== res.data.ip) {
this.sys.conf.currentIp = res.data;
this.sys.conf.currentIp.lastUpdate = new Date();
this.sys.saveConf();
return { ok: true, expired: true };
}
this.sys.conf.currentIp.lastUpdate = new Date();
this.sys.saveConf();
return { ok: true, expired: false };
} catch (ex) {
this.sys.saveLog(3, 'error when getting ip address', ex.message);
return { ok: false, message: ex.message };
}
}
async getAllDNSRecs() {
try {
if (!this.sys.conf || !this.sys.conf.domains) {
return { ok: false, message: 'Configure is not loaded.' };
}
var ok = true;
for (let d of this.sys.conf.domains) {
for (let s of d.subs) {
var r = await this.getDNSRec(d, s);
if (!r.ok) {
this.sys.saveLog(2, r.message);
ok = false;
}
}
}
if (ok) { this.sys.saveConf(); }
return { ok: ok };
}
catch (ex) {
this.sys.saveLog(3, 'Get All DNS Records error', ex.message);
return { ok: false, message: ex.message };
}
}
async updateAllDNSRecs() {
try {
if (!this.sys.conf || !this.sys.conf.domains) {
return { ok: false, message: 'Configure is not loaded.' };
}
var ok = true;
for (let d of this.sys.conf.domains) {
for (let s of d.subs) {
var r = await this.updateDNS(d, s);
if (!r.ok) { ok = false; }
}
}
return { ok: ok };
}
catch (ex) {
this.sys.saveLog(3, 'Update All DNS Records error', ex.message);
return { ok: false, message: ex.message };
}
}
async getDNSRec(domain, sub) {
try {
var para = "login_token=" + this.sys.conf.id + ',' + this.sys.conf.token + "&format=json&domain=" + domain.name + '&sub_domain=' + sub.name;
var res = await axios.post(this.sys.conf.baseUrl + 'Record.List', para, { headers: this.sys.conf.headers });
if (res.data.status.code !== '1') {
this.sys.saveLog(3, 'Get DNS Record error:' + sub.name + '.' + domain.name, res.data.status);
return { ok: false, message: res.data.status };
}
domain.id = res.data.domain.id;
if (res.data.records.length === 1) {
sub.id = res.data.records[0].id;
sub.type = res.data.records[0].type;
sub.line = res.data.records[0].line;
sub.line_id = res.data.records[0].line_id;
}
return { ok: true };
} catch (ex) {
this.sys.saveLog(3, 'Error when get dns record:' + ex.message, {domain:domain, sub:sub});
return { ok: false, message: ex.message };
}
}
async updateDNS(domain, sub) {
try {
var para = "login_token=" + this.sys.conf.id + ',' + this.sys.conf.token;
para += "&format=json";
para += "&domain_id=" + domain.id;
para += "&record_id=" + sub.id;
para += "&sub_domain=" + sub.name;
para += "&record_type=" + sub.type;
para += "&record_line_id=" + sub.line_id;
para += "&value=" + this.sys.conf.currentIp.ip;
var res = await axios.post(this.sys.conf.baseUrl + 'Record.Modify', para, { headers: this.sys.conf.headers });
if (res.data.status.code !== '1') {
this.sys.saveLog(3, 'Failed update DNS Record:' + sub.name + '.' + domain.name, res.data.status);
return { ok: false, message: res.data.status };
}
this.sys.saveLog(1, 'Successed update DNS record:' + sub.name + '.' + domain.name);
return { ok: true };
} catch (ex) {
this.sys.saveLog(3, 'Update DNS record error:' + ex.message, { domain: domain, sub: sub });
return { ok: false, message: ex.message };
}
}
async setRemark(domain, sub, remark) {
try {
var para = "login_token=" + this.sys.conf.id + ',' + this.sys.conf.token;
para += "&format=json";
para += "&domain_id=" + domain.id;
para += "&record_id=" + sub.id;
para += "&remark=" + remark;
var res = await axios.post(this.sys.conf.baseUrl + 'Record.Modify', para, { headers: this.sys.conf.headers });
if (res.data.status.code !== '1') {
this.sys.saveLog(3, 'Update DNS Record error:', res.data.status);
return { ok: false, message: res.data.status };
}
this.sys.saveLog(1, 'Successed update remark of domain:' + sub.name + '.' + domain.name);
return { ok: true };
} catch (ex) {
this.sys.saveLog(3, 'Set remark failed:' + ex.message,{ domain: domain, sub: sub });
return { ok: false, message: ex.message };
}
}
async update() {
if (!this.sys.conf || !this.sys.conf.domains) {
return { ok: false, message: 'Configure is not loaded.' };
}
var res = await this.getAllDNSRecs();
if (!res) { return res; }
res = await this.getIp();
if (!res.ok) { return res; }
if (res.expired) {
this.updateAllDNSRecs();
}
this.sys.loadConf();
}
}