diff options
| author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2015-05-16 16:32:13 -0700 |
|---|---|---|
| committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2015-05-16 16:32:13 -0700 |
| commit | cf51961d523222815cde064d88955aee88352eb5 (patch) | |
| tree | 5acbd2c1ce94af64ba9c0cff1669a8d200bb11fc /nodejs-zt1-client | |
| parent | c9fd8de007540df6f0d2ae7ad07b1c9daa128b93 (diff) | |
| download | infinitytier-cf51961d523222815cde064d88955aee88352eb5.tar.gz infinitytier-cf51961d523222815cde064d88955aee88352eb5.zip | |
.
Diffstat (limited to 'nodejs-zt1-client')
| -rw-r--r-- | nodejs-zt1-client/index.js | 71 | ||||
| -rw-r--r-- | nodejs-zt1-client/package.json | 14 | ||||
| -rw-r--r-- | nodejs-zt1-client/test-controller.js | 14 |
3 files changed, 99 insertions, 0 deletions
diff --git a/nodejs-zt1-client/index.js b/nodejs-zt1-client/index.js new file mode 100644 index 00000000..e44fe795 --- /dev/null +++ b/nodejs-zt1-client/index.js @@ -0,0 +1,71 @@ +'use strict' + +var request = require('request'); + +function ZT1Client(url,authToken) +{ + this.url = url; + this.authToken = authToken; +} + +ZT1Client.prototype._jsonGet = function(getPath,callback) +{ + request({ + url: this.url + getPath, + method: 'GET', + headers: { + 'X-ZT1-Auth': this.authToken + } + },function(error,response,body) { + if (error) + return callback(error,{}); + if (response.statusCode !== 200) + return callback(new Error('server responded with '+response.statusCode),{}); + return callback(null,(typeof body === 'string') ? JSON.parse(body) : null); + }); +}; + +ZT1Client.prototype.status = function(callback) +{ + request({ + url: this.url + 'controller', + method: 'GET', + headers: { + 'X-ZT1-Auth': this.authToken + } + },function(error,response,body) { + if (error) + return callback(error,{}); + var controllerStatus = {}; + if (typeof body === 'string') + controllerStatus = JSON.parse(body); + request({ + url: this.url + 'status', + method: 'GET', + headers: { + 'X-ZT1-Auth': this.authToken + } + },function(error,response,body) { + if (error) + return callback(error,{}); + if (response.statusCode !== 200) + return callback(new Error('server responded with '+response.statusCode),{}); + var nodeStatus = JSON.parse(body); + for(var k in controllerStatus) + nodeStatus[k] = controllerStatus[k]; + return callback(null,nodeStatus); + }.bind(this)); + }.bind(this)); +}; + +ZT1Client.prototype.networks = function(callback) +{ + this._jsonGet('network',callback); +}; + +ZT1Client.prototype.controllerNetworks = function(callback) +{ + this._jsonGet('controller/network',callback); +}; + +exports.ZT1Client = ZT1Client; diff --git a/nodejs-zt1-client/package.json b/nodejs-zt1-client/package.json new file mode 100644 index 00000000..c840d8ae --- /dev/null +++ b/nodejs-zt1-client/package.json @@ -0,0 +1,14 @@ +{ + "name": "nodejs-zt1-client", + "version": "1.0.0", + "description": "ZeroTier One Network Virtualization Service JSON API Client", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "ZeroTier, Inc.", + "license": "BSD", + "dependencies": { + "request": "^2.55.0" + } +} diff --git a/nodejs-zt1-client/test-controller.js b/nodejs-zt1-client/test-controller.js new file mode 100644 index 00000000..e7c4846f --- /dev/null +++ b/nodejs-zt1-client/test-controller.js @@ -0,0 +1,14 @@ +var ZT1Client = require('./index.js').ZT1Client; + +var zt1c = new ZT1Client('http://127.0.0.1:9993/','5d6181b71fae2684f9cc64ed'); + +zt1c.status(function(err,status) { + if (err) + console.log(err); + console.log(status); + zt1c.networks(function(err,networks) { + if (err) + console.log(err); + console.log(networks); + }); +}); |
