Skip to content
This repository
Browse code

add dnspod-ddns 0.1

  • Loading branch information...
commit 0c09a09ca41fe027898bd85efebc234448894e8a 0 parents
吼吼 / Chao Shen authored
12  .gitignore
... ...
@@ -0,0 +1,12 @@
  1
+.DS_Store
  2
+*.seed
  3
+*.log
  4
+*.csv
  5
+*.dat
  6
+*.out
  7
+*.pid
  8
+*.swp
  9
+*.swo
  10
+testing
  11
+node_modules
  12
+.nide
31  README.md
Source Rendered
... ...
@@ -0,0 +1,31 @@
  1
+# dnspod-ddns
  2
+
  3
+dnspod-ddns is a ddns updating tool of [DNSPod](http://www.dnspod.cn).
  4
+It was originally designed for use with [node.js](http://nodejs.org).
  5
+
  6
+
  7
+## Download
  8
+
  9
+You can install using Node Package Manager (npm):
  10
+
  11
+```sh
  12
+npm install -g dnspod-ddns
  13
+```
  14
+
  15
+## Usage
  16
+
  17
+dnspod-ddns is a command line tool. It will update your dns record after you execute the following. You can add it to crontab for continuously updating.
  18
+
  19
+```sh
  20
+dnspod-ddns --dns-domain yourdomain.com --dns-host yourhost --user youremail --passwd yourpassword
  21
+```
  22
+
  23
+Otherwise, you can run it in daemon mode.
  24
+
  25
+```sh
  26
+dnspod-ddns --dns-domain yourdomain.com --dns-host yourhost --user youremail --passwd yourpassword --daemon
  27
+```
  28
+
  29
+## License
  30
+
  31
+Copyright (c) 2012 Chao Shen. This software is licensed under the BSD License.
44  bin/cli.js
... ...
@@ -0,0 +1,44 @@
  1
+#!/usr/bin/env node
  2
+
  3
+var fs = require('fs'),
  4
+    program = require('commander'),
  5
+    ddns = require('../'),
  6
+    options;
  7
+
  8
+
  9
+program
  10
+    .version('0.0.1')
  11
+    .usage('[options] [value ...]')
  12
+    .option('-d, --dns-domain <domain>', 'Set domain name(*required)')
  13
+    .option('-H, --dns-host <host>', 'Set host name(*required)')
  14
+    .option('-u, --user <user>', 'Set user name(*required)')
  15
+    .option('-p, --passwd <password>', 'Set password(*required)')
  16
+    .option('-D, --daemon', 'Start as daemon mode')
  17
+    .parse(process.argv);
  18
+
  19
+if (!program.dnsDomain) {
  20
+    console.log('--dns-domain is required.');
  21
+    process.exit(-1);
  22
+} else if (!program.dnsHost) {
  23
+    console.log('--dns-host is required.');
  24
+    process.exit(-1);
  25
+} else if (!program.user) {
  26
+    console.log('--user is required.');
  27
+    process.exit(-1);
  28
+} else if (!program.passwd) {
  29
+    console.log('--passwd is required.');
  30
+    process.exit(-1);
  31
+} else {
  32
+    options = {
  33
+        domain: program.dnsDomain,
  34
+        sub_domain: program.dnsHost,
  35
+        login_email: program.user,
  36
+        login_password: program.passwd,
  37
+        is_daemon: program.daemon
  38
+    };
  39
+    console.log('received args %j', options);
  40
+    ddns.run(options);
  41
+}
  42
+
  43
+
  44
+
174  lib/index.js
... ...
@@ -0,0 +1,174 @@
  1
+var async = require('async'),
  2
+    Dnspod = require('dnspod-client'),
  3
+    TimeTicker = require('./timeTicker');
  4
+
  5
+function ctxFilter(paramsArr, extObj) {
  6
+    var subCtx = {},
  7
+        self = this;
  8
+    paramsArr.forEach(function (key) {
  9
+        if (self[key]) {
  10
+            subCtx[key] = self[key];
  11
+        }
  12
+    });
  13
+
  14
+    if (extObj) {
  15
+        Object.keys(extObj).forEach(function (key) {
  16
+            subCtx[key] = extObj[key];
  17
+        });
  18
+    }
  19
+
  20
+    return subCtx;
  21
+}
  22
+
  23
+function updateIp(context, doneFn) {
  24
+    var params = ctxFilter.bind(context),
  25
+        client = new Dnspod(params(['login_email', 'login_password']));
  26
+
  27
+    async.waterfall([
  28
+        function (callback) {
  29
+            client
  30
+                .recordList(params(['domain_id', 'sub_domain'], {length: 5}))
  31
+                .on('recordList', callback);
  32
+        },
  33
+        function (result, callback) {
  34
+            if (result.status.code === "1") {
  35
+                if (result.records.length > 0) {
  36
+                    context.record_id = result.records[0].id;
  37
+                    context.record_value = result.records[0].value;
  38
+                    callback(null);
  39
+                } else {
  40
+                    callback(new Error('Can not found any matched host.'));
  41
+                }
  42
+            } else {
  43
+                callback(new Error(result.status.message));
  44
+            }
  45
+        }
  46
+    ], function (err) {
  47
+        if (err) {
  48
+            console.log(err);
  49
+        }
  50
+        if (doneFn) {
  51
+            doneFn(err);
  52
+        }
  53
+    });
  54
+}
  55
+
  56
+function requestIds(context, doneFn) {
  57
+    var params = ctxFilter.bind(context),
  58
+        client = new Dnspod(params(['login_email', 'login_password']));
  59
+    async.waterfall([
  60
+        function (callback) {
  61
+            //request domain id
  62
+            client
  63
+                .domainInfo(params(['domain']))
  64
+                .on('domainInfo', callback);
  65
+        },
  66
+        function (result, callback) {
  67
+            if (result.status.code === "1") {
  68
+                //export to global
  69
+                context.domain_id = result.domain.id;
  70
+                callback(null);
  71
+            } else {
  72
+                callback(new Error(result.status.message));
  73
+            }
  74
+        },
  75
+        function (callback) {
  76
+            updateIp(context, callback);
  77
+        }
  78
+    ], doneFn);
  79
+}
  80
+
  81
+function updateDdns(context, doneFn) {
  82
+    var params = ctxFilter.bind(context),
  83
+        client = new Dnspod(params(['login_email', 'login_password']));
  84
+
  85
+    async.waterfall([
  86
+        function (callback) {
  87
+            client
  88
+                .getHostIp()
  89
+                .on('getHostIp', callback);
  90
+        },
  91
+        function (result, callback) {
  92
+            var oldIp = context.record_value;
  93
+
  94
+            console.log('Old ip address is %j, while new ip address is %j', oldIp, result);
  95
+
  96
+            if (result !== oldIp) {
  97
+                //export to global
  98
+                context.record_value = result;
  99
+                callback(null, true);
  100
+            } else {
  101
+                callback(null, false);
  102
+            }
  103
+        },
  104
+        function (result, callback) {
  105
+            if (result) {
  106
+                client
  107
+                    .recordDdns(params([
  108
+                        'domain_id',
  109
+                        'record_id',
  110
+                        'sub_domain',
  111
+                        'record_line'
  112
+                    ]))
  113
+                    .on('recordDdns', callback);
  114
+            }
  115
+        }
  116
+    ], function (err, result) {
  117
+        if (err) {
  118
+            console.log(err);
  119
+        } else {
  120
+            console.log(result);
  121
+        }
  122
+        if (doneFn) {
  123
+            doneFn(err, result);
  124
+        }
  125
+    });
  126
+}
  127
+
  128
+function runAsDaemon(context) {
  129
+    var ticker = new TimeTicker(60 * 1000, 5 * 60 * 1000);
  130
+    ticker.on('tick', function () {
  131
+        updateDdns(context, function (err, result) {
  132
+            if (err) {
  133
+                process.exit(-1);
  134
+            }
  135
+        });
  136
+    });
  137
+    ticker.on('wideTick', function () {
  138
+        updateIp(context, function (err, result) {
  139
+            if (err) {
  140
+                process.exit(-1);
  141
+            }
  142
+        });
  143
+    });
  144
+}
  145
+
  146
+function run(options) {
  147
+    var context = {
  148
+        domain: options.domain,
  149
+        sub_domain: options.sub_domain,
  150
+        login_email: options.login_email,
  151
+        login_password: options.login_password,
  152
+        domain_id: '',
  153
+        record_id: '',
  154
+        record_value: '',
  155
+        record_line: '默认'
  156
+    };
  157
+    requestIds(context, function (err, result) {
  158
+        if (err) {
  159
+            console.log('request ids failed with error: %j', err.message);
  160
+        } else {
  161
+            if (options.is_daemon) {
  162
+                runAsDaemon(context);
  163
+            } else {
  164
+                updateDdns(context);
  165
+            }
  166
+        }
  167
+    });
  168
+}
  169
+
  170
+module.exports = {
  171
+    requestIds: requestIds,
  172
+    updateDdns: updateDdns,
  173
+    run: run
  174
+};
20  lib/timeTicker.js
... ...
@@ -0,0 +1,20 @@
  1
+var EventEmitter = require('events').EventEmitter,
  2
+    util = require('util');
  3
+
  4
+function TimeTicker(interval, wideInterval) {
  5
+    var self = this,
  6
+        count = 0,
  7
+        len = wideInterval / interval,
  8
+        nextTick = function () {
  9
+            if (count === 0) {
  10
+                self.emit('wideTick');
  11
+            }
  12
+            self.emit('tick');
  13
+            count = (count + 1) % len;
  14
+            setTimeout(nextTick, interval);
  15
+        };
  16
+    process.nextTick(nextTick);
  17
+}
  18
+util.inherits(TimeTicker, EventEmitter);
  19
+
  20
+module.exports = TimeTicker;
46  package.json
... ...
@@ -0,0 +1,46 @@
  1
+{
  2
+  "name": "dnspod-ddns",
  3
+  "description": "A DNSPOD DDNS Client",
  4
+  "version": "0.1.0",
  5
+  "keywords": [
  6
+    "dnspod",
  7
+    "ddns",
  8
+    "update record"
  9
+  ],
  10
+  "homepage": "https://github.com/ohsc/dnspod-ddns",
  11
+  "author": {
  12
+    "name": "Chao Shen",
  13
+    "email": "shen218@gmail.com"
  14
+  },
  15
+  "repository": {
  16
+    "type": "git",
  17
+    "url": "https://github.com/ohsc/dnspod-ddns.git"
  18
+  },
  19
+  "bugs": {
  20
+    "url": "http://github.com/ohsc/dnspod-ddns/issues"
  21
+  },
  22
+  "licenses": [
  23
+    {
  24
+      "type": "BSD",
  25
+      "url": "http://github.com/ohsc/dnspod-ddns/raw/master/LICENSE.md"
  26
+    }
  27
+  ],
  28
+  "main": "./lib/index.js",
  29
+  "bin": {
  30
+    "dnspod-ddns": "./bin/cli.js"
  31
+  },
  32
+  "dependencies": {
  33
+    "dnspod-client": "0.1.2",
  34
+    "commander": "1.0.5",
  35
+    "async": "0.1.22"
  36
+  },
  37
+  "devDependencies": {
  38
+    "mocha": ">= 1.5.0",
  39
+    "jslint": ">= 0.1.9"
  40
+  },
  41
+  "scripts": {
  42
+    "test": "node_modules/.bin/mocha test/test.js",
  43
+    "lint": "find . -name '*.js' -not -regex './node_modules.*' -print0 | xargs -0 ./node_modules/.bin/jslint --sloppy --stupid --node --color --git"
  44
+  },
  45
+  "readmeFilename": "README.md"
  46
+}

0 notes on commit 0c09a09

Please sign in to comment.
Something went wrong with that request. Please try again.