Permalink
Browse files

start with 0.1.0

  • Loading branch information...
0 parents commit e0a6beb60aace7bd5bff734698aff83897d33df4 @ohsc committed Oct 20, 2012
Showing with 328 additions and 0 deletions.
  1. +11 −0 .gitignore
  2. +26 −0 LICENSE.md
  3. +47 −0 README.md
  4. +27 −0 package.json
  5. +179 −0 src/dnspod.js
  6. +38 −0 test/test.js
11 .gitignore
@@ -0,0 +1,11 @@
+.DS_Store
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.swp
+*.swo
+testing
+node_modules
26 LICENSE.md
@@ -0,0 +1,26 @@
+```
+Copyright (C) 2012 Chao Shen. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+* Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+* Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+```
47 README.md
@@ -0,0 +1,47 @@
+# Dnspod-client
+
+Dnspod-client is a client of [DNSPod](https://www.dnspod.com).
+It was originally designed for use with [node.js](http://nodejs.org).
+
+
+## Quick Examples
+
+```javascript
+var Dnspod = require('dnspod-client');
+
+var client = new Dnspod({
+ 'login_email': 'test@test.com',
+ 'login_password': 'test'
+});
+
+client.on('domainList',function(err, data){
+ if (err) {
+ throw err;
+ } else {
+ // done();
+ }
+}).domainList({length:5});
+
+client.on('getHostIp', function(err, message){
+ if (err) {
+ throw err;
+ } else {
+ console.log('get IP address: ' + message);
+ // done();
+ }
+}).getHostIp();
+
+```
+
+
+## Download
+
+You can install using Node Package Manager (npm):
+
+ npm install dnspod-client
+
+
+
+## License
+
+Copyright (c) 2012 Chao Shen. This software is licensed under the BSD License.
27 package.json
@@ -0,0 +1,27 @@
+{
+ "name": "dnspod-client",
+ "version": "0.1.0",
+ "description": "A DNSPOD Client",
+ "keywords": [
+ "dnspod", "ddns", "get ip"
+ ],
+ "homepage": "http://ohsc.github.com/dnspod-client",
+ "author": "Chao Shen <shen218@gmail.com>",
+ "repository": {
+ "type": "git", "url": "http://github.com/ohsc/dnspod-client.git"
+ },
+ "bugs" : {
+ "url" : "http://github.com/ohsc/dnspod-client/issues"
+ },
+ "licenses" : [{
+ "type" : "BSD",
+ "url" : "http://github.com/ohsc/dnspod-client/raw/master/LICENSE.md"
+ }],
+ "main": "./src/dnspod.js",
+ "devDependencies": {
+ "mocha": ">= 1.5.0"
+ },
+ "scripts": {
+ "test": "node_modules/.bin/mocha test/test.js"
+ }
+}
179 src/dnspod.js
@@ -0,0 +1,179 @@
+var util = require('util');
+var EventEmitter = require('events').EventEmitter;
+var HTTPS = require('https');
+var querystring = require('querystring');
+var net = require('net');
+
+function Dnspod(params, options) {
+ var self = this;
+ self.defParams = mergeJSON({
+ 'login_email': '',
+ 'login_password': '',
+ 'format': 'json',
+ 'lang': 'cn',
+ 'error_on_empty': 'yes'
+ }, params);
+
+ self.defOptions = mergeJSON({
+ host: 'dnsapi.cn',
+ port: 443,
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ 'Accept': 'text/json',
+ 'User-Agent': 'Node Dnspod Client/1.0.0'
+ }
+ }, options);
+}
+
+util.inherits(Dnspod, EventEmitter);
+
+Dnspod.prototype.getHostIp = function(){
+ var self = this;
+ var message = '';
+
+ var client = net.connect({
+ host:'ns1.dnspod.net',
+ port: 6666
+ }, function() {
+ // console.log('client connected');
+ }).on('data', function(data){
+ message = data.toString();
+ // console.log(message);
+ client.end();
+ }).on('end', function(){
+ // console.log('client disconnected');
+ self.emit('getHostIp', null, message);
+ });
+};
+
+Dnspod.prototype.request = function(url, params, eventListenerName){
+ var self = this;
+ var requestCallback = function(res) {
+ var resData = [];
+ res.on('data', function(data) {
+ resData.push(data);
+ }).on('end', function() {
+ try {
+ var jsonData = JSON.parse(resData.join(''));
+ self.emit(eventListenerName, null, jsonData);
+ } catch (ex) {
+ self.emit(eventListenerName, new Error('Request failed'));
+ }
+ });
+ };
+
+ var postParams = self.defParams;
+ if (params) {
+ postParams = mergeJSON(postParams, params);
+ }
+
+ var postData = querystring.stringify(postParams);
+
+ var postOptions = self.defOptions;
+ postOptions.path = url;
+ postOptions.headers['Content-Length'] = postData.length;
+
+ var req = HTTPS.request(postOptions, requestCallback);
+ req.write(postData);
+ req.end();
+ return req;
+};
+
+var mapper = {
+ infoVersion: 'Info.Version',
+ userDetail: 'User.Detail',
+ userModify: 'User.Modify',
+ userpasswdModify: 'Userpasswd.Modify',
+ useremailModify: 'Useremail.Modify',
+ telephoneverifyCode: 'Telephoneverify.Code',
+ userLog: 'User.Log',
+ domainCreate: 'Domain.Create',
+ domainList: 'Domain.List',
+ domainRemove: 'Domain.Remove',
+ domainStatus: 'Domain.Status',
+ domainInfo: 'Domain.Info',
+ domainLog: 'Domain.Log',
+ domainSearchenginepush: 'Domain.Searchenginepush',
+ domainUrlincn: 'Domain.Urlincn',
+ domainshareCreate: 'Domainshare.Create',
+ domainshareList: 'Domainshare.List',
+ domainshareModify: 'Domainshare.Modify',
+ domainshareRemove: 'Domainshare.Remove',
+ domainTransfer: 'Domain.Transfer',
+ domainLock: 'Domain.Lock',
+ domainLockstatus: 'Domain.Lockstatus',
+ domainUnlock: 'Domain.Unlock',
+ domainaliasList: 'Domainalias.List',
+ domainaliasCreate: 'Domainalias.Create',
+ domainaliasRemove: 'Domainalias.Remove',
+ domaingroupList: 'Domaingroup.List',
+ domaingroupCreate: 'Domaingroup.Create',
+ domaingroupModify: 'Domaingroup.Modify',
+ domaingroupRemove: 'Domaingroup.Remove',
+ domainChangegroup: 'Domain.Changegroup',
+ domainIsmark: 'Domain.Ismark',
+ domainRemark: 'Domain.Remark',
+ domainPurview: 'Domain.Purview',
+ domainAcquire: 'Domain.Acquire',
+ domainAcquiresend: 'Domain.Acquiresend',
+ domainAcquirevalidate: 'Domain.Acquirevalidate',
+ recordType: 'Record.Type',
+ recordLine: 'Record.Line',
+ recordCreate: 'Record.Create',
+ recordList: 'Record.List',
+ recordModify: 'Record.Modify',
+ recordRemove: 'Record.Remove',
+ recordDdns: 'Record.Ddns',
+ recordRemark: 'Record.Remark',
+ recordInfo: 'Record.Info',
+ recordStatus: 'Record.Status',
+ monitorListsubdomain: 'Monitor.Listsubdomain',
+ monitorListsubvalue: 'Monitor.Listsubvalue',
+ monitorList: 'Monitor.List',
+ monitorCreate: 'Monitor.Create',
+ monitorModify: 'Monitor.Modify',
+ monitorRemove: 'Monitor.Remove',
+ monitorInfo: 'Monitor.Info',
+ monitorSetstatus: 'Monitor.Setstatus',
+ monitorGethistory: 'Monitor.Gethistory',
+ monitorUserdesc: 'Monitor.Userdesc',
+ monitorGetdowns: 'Monitor.Getdowns'
+};
+
+for(key in mapper) {
+ Dnspod.prototype[key] = (function(key, value) {
+ return function(params){
+ this.request('/' + value, params, key);
+ };
+ })(key, mapper[key]);
+}
+
+
+/*
+* Recursively merge properties of two objects
+*/
+function mergeJSON(obj1, obj2) {
+
+ for (var p in obj2) {
+ try {
+ // Property in destination object set; update its value.
+ if ( obj2[p].constructor==Object ) {
+ obj1[p] = mergeJSON(obj1[p], obj2[p]);
+
+ } else {
+ obj1[p] = obj2[p];
+
+ }
+
+ } catch(e) {
+ // Property in destination object not set; create it and set its value.
+ obj1[p] = obj2[p];
+
+ }
+ }
+
+ return obj1;
+}
+
+module.exports = Dnspod;
38 test/test.js
@@ -0,0 +1,38 @@
+var Dnspod = require('../');
+
+
+
+describe('Domain', function(){
+ it('should return -1 when login failed', function(done){
+ var client = new Dnspod({
+ 'login_email': 'test@test.com',
+ 'login_password': 'test'
+ });
+
+ client.on('domainList',function(err, data){
+ if (err) {
+ throw err;
+ } else {
+ done();
+ }
+ }).domainList({length:5});
+ });
+});
+
+describe('IP', function(){
+ it('shound return ip address', function(done){
+ var client = new Dnspod({
+ 'login_email': 'test@test.com',
+ 'login_password': 'test'
+ });
+
+ client.on('getHostIp', function(err, message){
+ if (err) {
+ throw err;
+ } else {
+ console.log('get IP address: ' + message);
+ done();
+ }
+ }).getHostIp();
+ });
+});

0 comments on commit e0a6beb

Please sign in to comment.