Skip to content

Commit 6370098

Browse files
authored
Merge pull request #9 from tilfin/refactor
Refactor
2 parents e96fdca + dbdfbc4 commit 6370098

File tree

10 files changed

+116
-102
lines changed

10 files changed

+116
-102
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Dynamic DNS API Service for Google Cloud
66
This is a hub service to connect dynamic IP address endpoint as a home network at any time.
77
Your DNS record on Google Cloud is updated by posting this service from Home PC periodically.
88

9+
## Environment
10+
11+
Node.js 4.3 or Later
12+
913
## Configuration
1014
Edit _config/local.yml_
1115

app.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
var config = require('config').config;
2-
var restify = require('restify');
3-
var resource = require('./resource');
1+
'use strict';
42

5-
var server = restify.createServer({
3+
const config = require('config').config;
4+
const restify = require('restify');
5+
const resource = require('./resource');
6+
7+
const server = restify.createServer({
68
name: 'Dynamic DNS API Service for Google Cloud'
79
});
810

@@ -13,7 +15,7 @@ server.use(restify.bodyParser({ mapParams: true }));
1315
resource(server);
1416

1517
if (!module.parent) {
16-
var port = process.env.PORT || config.app.port;
18+
const port = process.env.PORT || config.app.port;
1719
server.listen(port);
1820
console.log("Start API Service port: %d", port);
1921
}

bin/gc-ddns

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env node
2+
3+
const program = require('commander');
4+
const config = require('config').config;
5+
const record = require('../model/record');
6+
const GcDnsClient = require('../model/gcdns');
7+
const CommonUtil = require('../util/common');
8+
9+
program
10+
.command('list')
11+
.description('list resource record sets')
12+
.action(function(){
13+
const client = new GcDnsClient();
14+
client.listPromise()
15+
.then(result => {
16+
console.log(JSON.stringify(result, null, 2));
17+
})
18+
.catch(err => {
19+
console.error(err);
20+
});
21+
});
22+
23+
program
24+
.command('register <host> <ip> [ttl]')
25+
.description('register A/AAAA record')
26+
.action(function(host, ip, ttl){
27+
const ipVersion = CommonUtil.validateIpVersion(ip);
28+
const recordType = CommonUtil.getAddressRecordType(ipVersion);
29+
if (recordType == null) {
30+
console.error('Invalid IP address');
31+
return;
32+
}
33+
34+
const recordTtl = parseInt(ttl || config.ttl, 10);
35+
if (isNaN(recordTtl)) {
36+
console.error('Invalid TTL');
37+
return;
38+
}
39+
40+
record.create(host, ip, recordType, recordTtl, function(err, result){
41+
console.log(JSON.stringify(result, null, 2));
42+
});
43+
});
44+
45+
program.parse(process.argv);

bin/gc-ddns.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

model/gcdns.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
var config = require('config').config.googleCloud;
2-
var restify = require('restify');
3-
var google = require('googleapis');
1+
'use strict';
2+
3+
const config = require('config').config.googleCloud;
4+
const google = require('googleapis');
45

56

67
function GcDnsClient() {
@@ -16,17 +17,16 @@ GcDnsClient.prototype = {
1617
},
1718

1819
listPromise: function(host, type) {
19-
var self = this;
20-
return self.dnsClientPromise()
21-
.then(function(dns) {
22-
var params = self.baseParams();
20+
return this.dnsClientPromise()
21+
.then(dns => {
22+
const params = this.baseParams();
2323
if (host) {
24-
params.name = host + self.FQDNSuffix;
24+
params.name = host + this.FQDNSuffix;
2525
}
2626
if (type) params.type = type;
2727

28-
return new Promise(function(resolve, reject) {
29-
dns.resourceRecordSets.list(params, function(err, resp) {
28+
return new Promise((resolve, reject) => {
29+
dns.resourceRecordSets.list(params, (err, resp) => {
3030
if (err) {
3131
reject(err);
3232
} else {
@@ -38,18 +38,17 @@ GcDnsClient.prototype = {
3838
},
3939

4040
createPromise: function(host, ip, recordType, ttl, existing) {
41-
var self = this;
42-
return self.dnsClientPromise()
43-
.then(function(dns) {
44-
var resRecordSet = {
41+
return this.dnsClientPromise()
42+
.then(dns => {
43+
const resRecordSet = {
4544
kind: 'dns#resourceRecordSet',
46-
name: host + self.FQDNSuffix,
45+
name: host + this.FQDNSuffix,
4746
type: recordType,
4847
rrdatas: [ip],
4948
ttl: ttl
5049
};
5150

52-
var params = self.baseParams();
51+
const params = this.baseParams();
5352
params.resource = {
5453
kind: 'dns#change',
5554
additions: [resRecordSet],
@@ -59,7 +58,7 @@ GcDnsClient.prototype = {
5958
params.resource.deletions = [existing];
6059
}
6160

62-
return new Promise(function(resolve, reject) {
61+
return new Promise((resolve, reject) => {
6362
dns.changes.create(params, function(err, resp) {
6463
if (err) {
6564
reject(err);
@@ -76,23 +75,22 @@ GcDnsClient.prototype = {
7675
return Promise.resolve(this.dnsClient);
7776
}
7877

79-
var self = this;
80-
return new Promise(function(resolve, reject) {
81-
var API_SCOPES = [
78+
return new Promise((resolve, reject) => {
79+
const API_SCOPES = [
8280
'https://www.googleapis.com/auth/ndev.clouddns.readwrite',
8381
'https://www.googleapis.com/auth/cloud-platform'
8482
];
8583

86-
var key = require(config.authKeyJsonFile);
84+
const key = require(config.authKeyJsonFile);
8785

88-
var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, API_SCOPES, null);
89-
jwtClient.authorize(function(err, tokens) {
86+
const jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, API_SCOPES, null);
87+
jwtClient.authorize((err, tokens) => {
9088
if (err) {
9189
reject(err);
9290
} else {
9391
google.options({ auth: jwtClient });
94-
self.dnsClient = google.dns('v1');
95-
resolve(self.dnsClient);
92+
this.dnsClient = google.dns('v1');
93+
resolve(this.dnsClient);
9694
}
9795
});
9896
});

model/record.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
var restify = require('restify');
2-
var GcDnsClient = require('../model/gcdns');
1+
'use strict';
2+
3+
const restify = require('restify');
4+
const GcDnsClient = require('../model/gcdns');
35

46

57
exports.create = function(host, ip, recordType, ttl, callback) {
6-
var client = new GcDnsClient();
8+
const client = new GcDnsClient();
79
client.listPromise(host, recordType)
8-
.then(function(resp) {
10+
.then(resp => {
911
return (resp.rrsets.length == 1) ? resp.rrsets[0] : null;
1012
})
11-
.then(function(existingRecord) {
13+
.then(existingRecord => {
1214
if (existingRecord && existingRecord.rrdatas[0] == ip) {
1315
return {
1416
message: 'IP address is unchanged.'
1517
};
1618
}
1719
return client.createPromise(host, ip, recordType, ttl, existingRecord);
1820
})
19-
.then(function(resp) {
21+
.then(resp => {
2022
callback(null, resp);
2123
})
22-
.catch(function(err) {
24+
.catch(err => {
2325
console.log(err);
2426
console.log(err.stack);
2527
callback(err);

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "gc-ddns",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Dynamic DNS API Service for Google Cloud",
55
"main": "app.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"start": "node app.js"
88
},
99
"repository": {
1010
"type": "git",
@@ -22,13 +22,14 @@
2222
},
2323
"homepage": "https://github.com/tilfin/gc-ddns#readme",
2424
"dependencies": {
25-
"commander": "^2.8.1",
26-
"config": "^1.15.0",
27-
"googleapis": "^2.1.3",
28-
"js-yaml": "^3.3.1",
29-
"restify": "^4.0.0"
25+
"commander": "^2.9.0",
26+
"config": "^1.21.0",
27+
"googleapis": "^2.1.7",
28+
"js-yaml": "^3.6.1",
29+
"restify": "^4.1.1"
3030
},
31-
"bin": {
32-
"gc-ddns": "gc-ddns.js"
31+
"bin": "./bin/gc-ddns",
32+
"engines": {
33+
"node": ">=4.3.0"
3334
}
3435
}

resource/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
var record = require('./record');
1+
'use strict';
2+
3+
const record = require('./record');
24

35
module.exports = function(server) {
46
server.post('/records', record.post);

resource/record.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
var restify = require('restify');
2-
var config = require('config').config;
3-
var CommonUtil = require('../util/common');
4-
var record = require('../model/record');
1+
'use strict';
2+
3+
const restify = require('restify');
4+
const config = require('config').config;
5+
const CommonUtil = require('../util/common');
6+
const record = require('../model/record');
57

68

79
function authorize(req, res, next) {
8-
var apiKey = req.params.key;
10+
const apiKey = req.params.key;
911
if (apiKey == config.app.apiKey) {
1012
return true;
1113
} else {
@@ -19,27 +21,27 @@ exports.post = function(req, res, next) {
1921
return;
2022
}
2123

22-
var host = req.params.host;
24+
const host = req.params.host;
2325
if (!host) {
2426
next(new restify.errors.BadRequestError('Invalid host'));
2527
return;
2628
}
2729

28-
var ip = req.params.ip || req.headers['x-forwarded-for'] || req.connection.remoteAddress;
29-
var ipVersion = CommonUtil.validateIpVersion(ip);
30-
var recordType = CommonUtil.getAddressRecordType(ipVersion);
30+
const ip = req.params.ip || req.headers['x-forwarded-for'] || req.connection.remoteAddress;
31+
const ipVersion = CommonUtil.validateIpVersion(ip);
32+
const recordType = CommonUtil.getAddressRecordType(ipVersion);
3133
if (recordType == null) {
3234
next(new restify.errors.BadRequestError('Invalid IP address'));
3335
return;
3436
}
3537

36-
var recordTtl = parseInt(req.params.ttl || config.ttl, 10);
38+
const recordTtl = parseInt(req.params.ttl || config.ttl, 10);
3739
if (isNaN(recordTtl)) {
3840
next(new restify.errors.BadRequestError('Invalid TTL'));
3941
return;
4042
}
4143

42-
record.create(host, ip, recordType, recordTtl, function(err, result) {
44+
record.create(host, ip, recordType, recordTtl, (err, result) => {
4345
if (err) {
4446
console.log(err);
4547
return next(err);

util/common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
exports.validateIpVersion = function(ip) {
24
if (ip.match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/)) {
35
return 4;

0 commit comments

Comments
 (0)