This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
169 lines (152 loc) · 4.23 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
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
/*jshint esversion: 6 */
const ipHelper = require('ip');
const {DNS} = require('@google-cloud/dns');
const settings = require('./settings.json');
const dns = new DNS();
/**
* HTTP Cloud Function.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.updateHost = function(req, res) {
var token = req.query.token || req.body.token;
var ipv4 = req.query.ipv4 || req.body.ipv4;
var ipv6 = req.query.ipv6 || req.body.ipv6;
var host = req.query.host || req.body.host;
var zone = req.query.zone || req.body.zone || settings.dnsZone;
if (token != settings.secretToken) {
respondWithError(401, 'unauthorized', 'Login Required', res);
return;
}
if (!host) {
respondWithError(400, 'missing host', 'Provide a valid host name', res);
return;
}
if (!settings.allowedHosts.includes('*') && !settings.allowedHosts.includes(host)) {
respondWithError(401, 'illegal host', 'Host "' + host + '" is not allowed', res);
return;
}
if (!host.endsWith('.')) {
host += '.';
}
if (!ipv4 && !ipv6) {
var ipAddr = req.ip;
if (ipHelper.isV4Format(ipAddr)) {
ipv4 = ipAddr;
} else if (ipHelper.isV6Format(ipAddr)) {
ipv6 = ipAddr;
} else {
respondWithError(
400,
'missing ip',
'Could not evaluate ip address. Please provide with request.',
res
);
return;
}
}
if (ipv4 && !ipHelper.isV4Format(ipv4)) {
respondWithError(
400,
'illegal IPv4',
'Could not parse IPv4 address: ' + ipv4,
res
);
return;
}
if (ipv6 && !ipHelper.isV6Format(ipv6)) {
respondWithError(
400,
'illegal IPv6',
'Could not parse IPv6 address: ' + ipv6,
res
);
return;
}
console.log({
zone: zone,
host: host,
ipv4: ipv4,
ipv6: ipv6
});
updateHosts(zone, host, ipv4, ipv6)
.then(data => {
res.status(200).json(data);
})
.catch(err =>
respondWithError(
err.code || 500,
err.title || 'API error',
err.message,
res
)
);
};
function respondWithError(status, title, detail, res) {
let err = { code: status, title: title, detail: detail };
console.error(err);
res.status(status).json(err);
}
function updateHosts(zone, host, ipv4, ipv6) {
var dnsClient = dns;
var dnsZone = dnsClient.zone(zone);
return updateRecords(dnsZone, host, ipv4, ipv6)
.then(() => {
return {
code: '200',
values: {
host: host,
ipv4: ipv4,
ipv6: ipv6
}
};
});
}
function getOldRecords(zone, host, ipv4, ipv6) {
return zone
.getRecords({ name: host, filterByTypes_: { A: ipv4, AAAA: ipv6 } })
.then(data => {
var oldRecord = data[0];
if (oldRecord.length < 1) {
throw {
code: 400,
title: 'illegal host',
message: 'Host "' + host + '" not found.'
};
}
return oldRecord;
});
}
function updateRecords(zone, host, ipv4, ipv6) {
return getOldRecords(
zone,
host,
typeof ipv4 != 'undefined',
typeof ipv6 != 'undefined'
).then(oldRecords => {
let newRecords = [];
if (ipv4) {
newRecords.push(
zone.record('A', {
name: host,
ttl: settings.ttl,
data: ipv4
})
);
}
if (ipv6) {
newRecords.push(
zone.record('AAAA', {
name: host,
ttl: settings.ttl,
data: ipv6
})
);
}
return zone.createChange({
add: newRecords,
delete: oldRecords
});
});
}