-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
189 lines (167 loc) · 4.94 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*jshint esversion: 6 */
const ipHelper = require('ip');
const { DNS } = require('@google-cloud/dns');
settings = {
useToken: process.env.USE_TOKEN,
dnsZone: process.env.DNS_ZONE,
secretToken: process.env.SECRET_TOKEN,
allowedHosts: process.env.ALLOWED_HOSTS.split(","),
ttl: process.env.TTL
}
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) {
provided_secret = ""
if (settings.useToken) {
provided_secret = req.query.token || req.body.token;
}
else if (req.headers.authorization) {
const base64Credentials = req.headers.authorization.split(' ')[1];
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii');
const [username, password] = credentials.split(':');
provided_secret = password
}
else {
respondWithError(401, 'unauthorized', 'Login Required', res);
return;
}
console.log(req.originalUrl)
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 (!provided_secret || !settings.secretToken || provided_secret != 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: %s, host: %s, ipv4: %s, ipv6: %s', zone, host, ipv4, ipv6);
updateHosts(zone, host, ipv4, ipv6)
.then(data => {
res.status(200).send('good');
})
.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
});
});
}