-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
312 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,5 @@ jspm_packages | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
start.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: node_js | ||
cache: | ||
directories: | ||
- node_modules | ||
notifications: | ||
email: false | ||
node_js: | ||
- '7' | ||
- '6' | ||
- '4' | ||
before_script: | ||
- npm prune | ||
after_success: | ||
- npm run semantic-release | ||
branches: | ||
except: | ||
- /^v\d+\.\d+\.\d+$/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
MIT License | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Xiaoxin Lu | ||
Copyright (c) Xiaoxin Lu <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
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 | |
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Dynamic DNS client for Digital Ocean | ||
|
||
1. Get current public IP address | ||
2. Compares it to subdomain A record | ||
3. If different update a subdomain A record with current IP address | ||
|
||
## Install | ||
|
||
``` | ||
npm install -g doddns | ||
``` | ||
|
||
### Create a script to launch it: | ||
|
||
``` | ||
#!/bin/bash | ||
export SERVER_NAME=home_server.example.com | ||
export DIGITAL_OCEAN_TOKEN=xxxxxxx | ||
doddns | ||
``` | ||
|
||
### Throw it in your crontab | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
if (!process.env.DIGITAL_OCEAN_TOKEN || !process.env.SERVER_NAME) { | ||
console.error('Missing required environment variables: DIGITAL_OCEAN_TOKEN or SERVER_NAME') | ||
} else { | ||
require('./lib')().catch((error) => console.error(error)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const digitalocean = require('digitalocean') | ||
const publicIp = require('public-ip') | ||
const Promise = require('bluebird') | ||
|
||
module.exports = function () { | ||
const currentIpAddress = Promise.try(() => publicIp.v4()) | ||
const client = digitalocean.client(process.env.DIGITAL_OCEAN_TOKEN) | ||
const domainName = process.env.SERVER_NAME | ||
const parts = domainName.split('.') | ||
const domain = parts.slice(1).join('.') | ||
const subDomain = parts[0] | ||
const domainRecordPromise = getDomainRecord(client, subDomain, domain) | ||
|
||
return Promise.join(domainRecordPromise, currentIpAddress, (record, currentIp) => { | ||
console.log(`existing IP: ${record.ip}\ncurrent IP: ${currentIp}`) | ||
|
||
if (record.ip !== currentIp) { | ||
console.log(`IP needs to be updated to ${currentIp}`) | ||
return client.domains.updateRecord(domain, record.id, { | ||
name: subDomain, | ||
type: 'A', | ||
data: currentIp | ||
}) | ||
} else { | ||
console.log('Does not need to be updated') | ||
} | ||
}) | ||
} | ||
|
||
function getDomainRecord (client, subDomain, domain) { | ||
return Promise.try(() => client.domains.listRecords(domain)) | ||
.then(records => _(records).filter({type: 'A', name: subDomain}).first()) | ||
.then(record => { | ||
if (record == null) { | ||
throw new Error(`Unable to find domain A record for subdomain "${subDomain}" and domain "${domain}"`) | ||
} | ||
|
||
return { | ||
id: record.id, | ||
ip: record.data | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "doddns", | ||
"version": "0.0.0-development", | ||
"description": "Dynamic DNS Client that uses Digital Ocean DNS API to update a subdomain A record with current IP address", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard && mocha", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post" | ||
}, | ||
"bin": "./index.js", | ||
"keywords": [ | ||
"DigialOcean", | ||
"ip", | ||
"domain", | ||
"ddns", | ||
"dns", | ||
"dynamic" | ||
], | ||
"engines": { | ||
"node": ">4.4.7" | ||
}, | ||
"author": "Xiaoxin Lu <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"bluebird": "^3.4.6", | ||
"digitalocean": "^0.9.1", | ||
"lodash": "^4.15.0", | ||
"public-ip": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^3.2.0", | ||
"proxyquire": "^1.7.11", | ||
"sinon": "^1.17.7", | ||
"sinon-as-promised": "^4.0.2", | ||
"standard": "^8.6.0", | ||
"semantic-release": "^6.3.2" | ||
}, | ||
"repository": "hyperlink/do-dynamic-dns" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* eslint-env node, mocha */ | ||
|
||
'use strict' | ||
|
||
const sinon = require('sinon') | ||
require('sinon-as-promised') | ||
const proxyquire = require('proxyquire').noCallThru() | ||
const assert = require('assert') | ||
|
||
describe('Dynamic DNS Client', function () { | ||
const client = { | ||
domains: { | ||
listRecords: function () { throw new Error('not stubbed') }, | ||
updateRecord: function () { throw new Error('not stubbed') } | ||
} | ||
} | ||
|
||
const publicIp = { | ||
v4: function () { throw new Error('not stubbed') } | ||
} | ||
|
||
let sandbox, dnsClient | ||
|
||
before(function () { | ||
dnsClient = proxyquire('../lib', { | ||
'public-ip': publicIp, | ||
'digitalocean': { | ||
client: function () { | ||
return client | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create() | ||
}) | ||
|
||
afterEach(function () { | ||
sandbox.restore() | ||
process.env = {} | ||
}) | ||
|
||
it('should do nothing if IP does not change', function () { | ||
process.env = { | ||
DIGITAL_OCEAN_TOKEN: 'xxxx', | ||
SERVER_NAME: 'server.example.com' | ||
} | ||
|
||
sandbox.stub(publicIp, 'v4').resolves('10.0.1.23') | ||
sandbox.stub(client.domains, 'listRecords').resolves([ | ||
{ | ||
id: 123, | ||
type: 'A', | ||
name: 'server', | ||
data: '10.0.1.23', | ||
priority: null, | ||
port: null, | ||
weight: null | ||
}, | ||
{ | ||
id: 124, | ||
type: 'NS', | ||
name: '@', | ||
data: 'ns1.digitalocean.com', | ||
priority: null, | ||
port: null, | ||
weight: null | ||
} | ||
]) | ||
|
||
sandbox.stub(client.domains, 'updateRecord') | ||
|
||
return dnsClient().then(function () { | ||
sinon.assert.calledOnce(publicIp.v4) | ||
sinon.assert.calledWithExactly(client.domains.listRecords, 'example.com') | ||
sinon.assert.notCalled(client.domains.updateRecord) | ||
}) | ||
}) | ||
|
||
it('should update domain record if IP does change', function () { | ||
process.env = { | ||
DIGITAL_OCEAN_TOKEN: 'xxxx', | ||
SERVER_NAME: 'server.example.com' | ||
} | ||
|
||
sandbox.stub(publicIp, 'v4').resolves('10.0.1.15') | ||
sandbox.stub(client.domains, 'listRecords').resolves([ | ||
{ | ||
id: 123, | ||
type: 'A', | ||
name: 'server', | ||
data: '10.0.1.23', | ||
priority: null, | ||
port: null, | ||
weight: null | ||
}, | ||
{ | ||
id: 124, | ||
type: 'NS', | ||
name: '@', | ||
data: 'ns1.digitalocean.com', | ||
priority: null, | ||
port: null, | ||
weight: null | ||
} | ||
]) | ||
|
||
sandbox.stub(client.domains, 'updateRecord') | ||
|
||
return dnsClient().then(function () { | ||
sinon.assert.calledOnce(publicIp.v4) | ||
sinon.assert.calledWithExactly(client.domains.listRecords, 'example.com') | ||
sinon.assert.calledWithExactly(client.domains.updateRecord, 'example.com', 123, { | ||
name: 'server', | ||
type: 'A', | ||
data: '10.0.1.15' | ||
}) | ||
}) | ||
}) | ||
|
||
it('should fail if unable to find domain record', function () { | ||
process.env = { | ||
DIGITAL_OCEAN_TOKEN: 'xxxx', | ||
SERVER_NAME: 'server.example.com' | ||
} | ||
|
||
sandbox.stub(publicIp, 'v4').resolves('10.0.1.15') | ||
sandbox.stub(client.domains, 'listRecords').resolves([ | ||
{ | ||
id: 124, | ||
type: 'NS', | ||
name: '@', | ||
data: 'ns1.digitalocean.com', | ||
priority: null, | ||
port: null, | ||
weight: null | ||
} | ||
]) | ||
|
||
sandbox.stub(client.domains, 'updateRecord') | ||
|
||
return dnsClient() | ||
.then(function () { | ||
throw new Error('Expected promise to fail!') | ||
}) | ||
.catch(function (error) { | ||
assert.equal(error.message, 'Unable to find domain A record for subdomain "server" and domain "example.com"') | ||
sinon.assert.calledOnce(publicIp.v4) | ||
sinon.assert.calledWithExactly(client.domains.listRecords, 'example.com') | ||
sinon.assert.notCalled(client.domains.updateRecord) | ||
}) | ||
}) | ||
|
||
it('should exit if required environment variables are not set', function () { | ||
sandbox.stub(console, 'error') | ||
require('../') | ||
sinon.assert.calledWithExactly(console.error, 'Missing required environment variables: DIGITAL_OCEAN_TOKEN or SERVER_NAME') | ||
}) | ||
}) |