Skip to content

Commit ea3adff

Browse files
committed
feat: dynamic dns client
1 parent 5f734e9 commit ea3adff

File tree

9 files changed

+312
-6
lines changed

9 files changed

+312
-6
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
start.sh

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: node_js
2+
cache:
3+
directories:
4+
- node_modules
5+
notifications:
6+
email: false
7+
node_js:
8+
- '7'
9+
- '6'
10+
- '4'
11+
before_script:
12+
- npm prune
13+
after_success:
14+
- npm run semantic-release
15+
branches:
16+
except:
17+
- /^v\d+\.\d+\.\d+$/

LICENSE

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
MIT License
1+
The MIT License (MIT)
22

3-
Copyright (c) 2017 Xiaoxin Lu
3+
Copyright (c) Xiaoxin Lu <javascript@yahoo.com>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
copies of the Software, and to permit persons to whom the Software is
1010
furnished to do so, subject to the following conditions:
1111

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
1414

1515
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Dynamic DNS client for Digital Ocean
2+
3+
1. Get current public IP address
4+
2. Compares it to subdomain A record
5+
3. If different update a subdomain A record with current IP address
6+
7+
## Install
8+
9+
```
10+
npm install -g doddns
11+
```
12+
13+
### Create a script to launch it:
14+
15+
```
16+
#!/bin/bash
17+
export SERVER_NAME=home_server.example.com
18+
export DIGITAL_OCEAN_TOKEN=xxxxxxx
19+
doddns
20+
```
21+
22+
### Throw it in your crontab
23+

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
if (!process.env.DIGITAL_OCEAN_TOKEN || !process.env.SERVER_NAME) {
4+
console.error('Missing required environment variables: DIGITAL_OCEAN_TOKEN or SERVER_NAME')
5+
} else {
6+
require('./lib')().catch((error) => console.error(error))
7+
}

lib/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict'
2+
3+
const _ = require('lodash')
4+
const digitalocean = require('digitalocean')
5+
const publicIp = require('public-ip')
6+
const Promise = require('bluebird')
7+
8+
module.exports = function () {
9+
const currentIpAddress = Promise.try(() => publicIp.v4())
10+
const client = digitalocean.client(process.env.DIGITAL_OCEAN_TOKEN)
11+
const domainName = process.env.SERVER_NAME
12+
const parts = domainName.split('.')
13+
const domain = parts.slice(1).join('.')
14+
const subDomain = parts[0]
15+
const domainRecordPromise = getDomainRecord(client, subDomain, domain)
16+
17+
return Promise.join(domainRecordPromise, currentIpAddress, (record, currentIp) => {
18+
console.log(`existing IP: ${record.ip}\ncurrent IP: ${currentIp}`)
19+
20+
if (record.ip !== currentIp) {
21+
console.log(`IP needs to be updated to ${currentIp}`)
22+
return client.domains.updateRecord(domain, record.id, {
23+
name: subDomain,
24+
type: 'A',
25+
data: currentIp
26+
})
27+
} else {
28+
console.log('Does not need to be updated')
29+
}
30+
})
31+
}
32+
33+
function getDomainRecord (client, subDomain, domain) {
34+
return Promise.try(() => client.domains.listRecords(domain))
35+
.then(records => _(records).filter({type: 'A', name: subDomain}).first())
36+
.then(record => {
37+
if (record == null) {
38+
throw new Error(`Unable to find domain A record for subdomain "${subDomain}" and domain "${domain}"`)
39+
}
40+
41+
return {
42+
id: record.id,
43+
ip: record.data
44+
}
45+
})
46+
}

package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "doddns",
3+
"version": "0.0.0-development",
4+
"description": "Dynamic DNS Client that uses Digital Ocean DNS API to update a subdomain A record with current IP address",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "standard && mocha",
8+
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
9+
},
10+
"bin": "./index.js",
11+
"keywords": [
12+
"DigialOcean",
13+
"ip",
14+
"domain",
15+
"ddns",
16+
"dns",
17+
"dynamic"
18+
],
19+
"engines": {
20+
"node": ">4.4.7"
21+
},
22+
"author": "Xiaoxin Lu <javascript@yahoo.com>",
23+
"license": "MIT",
24+
"dependencies": {
25+
"bluebird": "^3.4.6",
26+
"digitalocean": "^0.9.1",
27+
"lodash": "^4.15.0",
28+
"public-ip": "^2.1.0"
29+
},
30+
"devDependencies": {
31+
"mocha": "^3.2.0",
32+
"proxyquire": "^1.7.11",
33+
"sinon": "^1.17.7",
34+
"sinon-as-promised": "^4.0.2",
35+
"standard": "^8.6.0",
36+
"semantic-release": "^6.3.2"
37+
},
38+
"repository": "hyperlink/do-dynamic-dns"
39+
}

test/test.ddns.js

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/* eslint-env node, mocha */
2+
3+
'use strict'
4+
5+
const sinon = require('sinon')
6+
require('sinon-as-promised')
7+
const proxyquire = require('proxyquire').noCallThru()
8+
const assert = require('assert')
9+
10+
describe('Dynamic DNS Client', function () {
11+
const client = {
12+
domains: {
13+
listRecords: function () { throw new Error('not stubbed') },
14+
updateRecord: function () { throw new Error('not stubbed') }
15+
}
16+
}
17+
18+
const publicIp = {
19+
v4: function () { throw new Error('not stubbed') }
20+
}
21+
22+
let sandbox, dnsClient
23+
24+
before(function () {
25+
dnsClient = proxyquire('../lib', {
26+
'public-ip': publicIp,
27+
'digitalocean': {
28+
client: function () {
29+
return client
30+
}
31+
}
32+
})
33+
})
34+
35+
beforeEach(function () {
36+
sandbox = sinon.sandbox.create()
37+
})
38+
39+
afterEach(function () {
40+
sandbox.restore()
41+
process.env = {}
42+
})
43+
44+
it('should do nothing if IP does not change', function () {
45+
process.env = {
46+
DIGITAL_OCEAN_TOKEN: 'xxxx',
47+
SERVER_NAME: 'server.example.com'
48+
}
49+
50+
sandbox.stub(publicIp, 'v4').resolves('10.0.1.23')
51+
sandbox.stub(client.domains, 'listRecords').resolves([
52+
{
53+
id: 123,
54+
type: 'A',
55+
name: 'server',
56+
data: '10.0.1.23',
57+
priority: null,
58+
port: null,
59+
weight: null
60+
},
61+
{
62+
id: 124,
63+
type: 'NS',
64+
name: '@',
65+
data: 'ns1.digitalocean.com',
66+
priority: null,
67+
port: null,
68+
weight: null
69+
}
70+
])
71+
72+
sandbox.stub(client.domains, 'updateRecord')
73+
74+
return dnsClient().then(function () {
75+
sinon.assert.calledOnce(publicIp.v4)
76+
sinon.assert.calledWithExactly(client.domains.listRecords, 'example.com')
77+
sinon.assert.notCalled(client.domains.updateRecord)
78+
})
79+
})
80+
81+
it('should update domain record if IP does change', function () {
82+
process.env = {
83+
DIGITAL_OCEAN_TOKEN: 'xxxx',
84+
SERVER_NAME: 'server.example.com'
85+
}
86+
87+
sandbox.stub(publicIp, 'v4').resolves('10.0.1.15')
88+
sandbox.stub(client.domains, 'listRecords').resolves([
89+
{
90+
id: 123,
91+
type: 'A',
92+
name: 'server',
93+
data: '10.0.1.23',
94+
priority: null,
95+
port: null,
96+
weight: null
97+
},
98+
{
99+
id: 124,
100+
type: 'NS',
101+
name: '@',
102+
data: 'ns1.digitalocean.com',
103+
priority: null,
104+
port: null,
105+
weight: null
106+
}
107+
])
108+
109+
sandbox.stub(client.domains, 'updateRecord')
110+
111+
return dnsClient().then(function () {
112+
sinon.assert.calledOnce(publicIp.v4)
113+
sinon.assert.calledWithExactly(client.domains.listRecords, 'example.com')
114+
sinon.assert.calledWithExactly(client.domains.updateRecord, 'example.com', 123, {
115+
name: 'server',
116+
type: 'A',
117+
data: '10.0.1.15'
118+
})
119+
})
120+
})
121+
122+
it('should fail if unable to find domain record', function () {
123+
process.env = {
124+
DIGITAL_OCEAN_TOKEN: 'xxxx',
125+
SERVER_NAME: 'server.example.com'
126+
}
127+
128+
sandbox.stub(publicIp, 'v4').resolves('10.0.1.15')
129+
sandbox.stub(client.domains, 'listRecords').resolves([
130+
{
131+
id: 124,
132+
type: 'NS',
133+
name: '@',
134+
data: 'ns1.digitalocean.com',
135+
priority: null,
136+
port: null,
137+
weight: null
138+
}
139+
])
140+
141+
sandbox.stub(client.domains, 'updateRecord')
142+
143+
return dnsClient()
144+
.then(function () {
145+
throw new Error('Expected promise to fail!')
146+
})
147+
.catch(function (error) {
148+
assert.equal(error.message, 'Unable to find domain A record for subdomain "server" and domain "example.com"')
149+
sinon.assert.calledOnce(publicIp.v4)
150+
sinon.assert.calledWithExactly(client.domains.listRecords, 'example.com')
151+
sinon.assert.notCalled(client.domains.updateRecord)
152+
})
153+
})
154+
155+
it('should exit if required environment variables are not set', function () {
156+
sandbox.stub(console, 'error')
157+
require('../')
158+
sinon.assert.calledWithExactly(console.error, 'Missing required environment variables: DIGITAL_OCEAN_TOKEN or SERVER_NAME')
159+
})
160+
})

0 commit comments

Comments
 (0)