-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (149 loc) · 4.14 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
var dns = require('@google-cloud/dns')({
projectId: 'my-project',
keyFilename: './key.json'
});
/*
* Usage:
* https:[GoogleCloudURL]/dns2?host=foo.bar.com&ip=192.168.1.1&zone=my-zone
* all query parameters are required.
*
* TTL and record type are hardcoded to 300 and 'A'. This assumes that there
* will only be one type A record for the given name and TTL.
*
* Security discussion:
* This uses service security key meaning that if the url and argument
* syntax are exposed, anyone can mess with your DNS server. For me
* this was not an issue, since I am using this to provide dynamic DNS
* service to a pfSense security device - if the pfSense device is hacked
* then the exposer of dynamic DNS is the least of my concerns.
*
* If you are using this code for a more public service, you probably want
* to use OAuth2 to secure it.
*
* This: https://github.com/GoogleCloudPlatform/google-cloud-node considers
* the DNS interface to be an alpha quality API and could be easily broken
* with further development.
*/
exports.dns2 = function dns2 (req, res) {
var ip = req.query.ip;
var host = req.query.host;
var zone = req.query.zone;
if (!ip || !host || !zone) {
res.send('Sorry'); // intentionally vague for security reasons
return;
}
try {
var zone = dns.zone(zone);
}
catch (err) {
// dns.zone doesn't throw many errors. You may have to check the
// console log to determine what went wrong here.
res.send(err);
return;
}
var query = {
name: host,
type: 'A',
maxResults: 1
};
zone.getRecords(query)
.then((data) => {
var oldRecord = data[0][0];
var newRecord = zone.record('a', {
"name": host,
"data": ip,
"ttl": 300
});
config = {
add: newRecord,
delete: oldRecord
};
return zone.createChange(config).then( (data) => console.log('Updating ' + host + ': ' + config.delete.data + ' => ' + ip));
})
.catch((err) => console.log(err));
res.send('done');
return;
};
/*
* This code works using googleapis rather than @google-cloud/dns
* It's here as a reference. I preferred the more readable version
* above.
*
var google = require('googleapis');
var dns = google.dns('v1');
var key = require('./key.json');
exports.dns1 = function dns1 (req, res) {
var ip = req.query.ip;
var host = req.query.host;
if (!ip || !host) {
res.send('Must include ip, host in query');
return;
}
var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/cloud-platform'], // an array of auth scopes
null
);
jwtClient.authorize(function (err, tokens) {
if (err) {
res.send('error1:' + err);
return;
}
// Get the existing record for this name (required to change)
var request = {
project: 'my-project', // Update
managedZone: 'my-zone', // Update
auth: jwtClient,
maxResults: 1,
name: host,
type: 'A',
};
var existingRecordSet;
dns.resourceRecordSets.list(request).then(function (response) {
if (response.rrsets.length >= 1) {
existingRecordSet = response.rrsets[0];
}
var changes = {
"kind": "dns#change",
"additions": [
{
"kind": "dns#resourceRecordSet",
"name": host,
"type": "A",
"ttl": 300,
"rrdatas": [
ip
]
}
],
"deletions": [
existingRecordSet
]
};
request = {
project: 'my-project', // Update
managedZone: 'project--zone', // Update
auth: jwtClient,
resource: changes
};
dns.changes.create(request, function (err, response) {
if (err) {
res.send('error2:' + err);
return;
}
if (existingRecordSet)
console.log('Updated ' + host + ': ' + existingRecordSet.rrdatas[0] + ' => ' + ip);
else
console.log('Inserted ' + host + ': ' + ip);
res.send(JSON.stringify(response));
});
}).catch(function(err) {
res.send('error2:' + err);
return;
})
});
};
*
*/