From ba7809367a8ef1c628bc927e2d4148b6dc43fa46 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 26 May 2015 09:01:58 -0700 Subject: JS stuff reorg. --- js/zt1-api-client/index.js | 160 +++++++++++++++++++++++++++++++++++++++++ js/zt1-api-client/package.json | 15 ++++ js/zt1-api-client/test.js | 33 +++++++++ nodejs-zt1-client/index.js | 160 ----------------------------------------- nodejs-zt1-client/package.json | 15 ---- nodejs-zt1-client/test.js | 33 --------- 6 files changed, 208 insertions(+), 208 deletions(-) create mode 100644 js/zt1-api-client/index.js create mode 100644 js/zt1-api-client/package.json create mode 100644 js/zt1-api-client/test.js delete mode 100644 nodejs-zt1-client/index.js delete mode 100644 nodejs-zt1-client/package.json delete mode 100644 nodejs-zt1-client/test.js diff --git a/js/zt1-api-client/index.js b/js/zt1-api-client/index.js new file mode 100644 index 00000000..55f7fb24 --- /dev/null +++ b/js/zt1-api-client/index.js @@ -0,0 +1,160 @@ +'use strict' + +var request = require('request'); + +function ZT1Client(url,authToken) +{ + this.url = url; + this.authToken = authToken; +} + +// Generate new ZeroTier identity -- mostly for testing +ZT1Client.prototype.newIdentity = function(callback) +{ + request({ + url: this.url + 'newIdentity', + method: 'GET', + json: false, + headers: { + 'X-ZT1-Auth': this.authToken + } + },function(error,response,body) { + if (error) + return callback(error,null); + if (response.statusCode === 200) + return callback(null,body); + return callback(new Error('server responded with error: '+response.statusCode),''); + }); +} + +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,null); + if (response.statusCode !== 200) + return callback(new Error('server responded with error: '+response.statusCode),null); + 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.getNetworks = function(callback) +{ + this._jsonGet('network',callback); +}; + +ZT1Client.prototype.getPeers = function(callback) +{ + this._jsonGet('peer',callback); +}; + +ZT1Client.prototype.listControllerNetworks = function(callback) +{ + this._jsonGet('controller/network',callback); +}; + +ZT1Client.prototype.getControllerNetwork = function(nwid,callback) +{ + this._jsonGet('controller/network/' + nwid,callback); +}; + +ZT1Client.prototype.saveControllerNetwork = function(network,callback) +{ + if ((typeof network.nwid !== 'string')||(network.nwid.length !== 16)) + return callback(new Error('Missing required field: nwid'),null); + + // The ZT1 service is type variation intolerant, so recreate our submission with the correct types + var n = { + nwid: network.nwid + }; + if (network.name) + n.name = network.name.toString(); + if ('private' in network) + n.private = (network.private) ? true : false; + if ('enableBroadcast' in network) + n.enableBroadcast = (network.enableBroadcast) ? true : false; + if ('allowPassiveBridging' in network) + n.allowPassiveBridging = (network.allowPassiveBridging) ? true : false; + if ('v4AssignMode' in network) { + if (network.v4AssignMode) + n.v4AssignMode = network.v4AssignMode.toString(); + else n.v4AssignMode = 'none'; + } + if ('v6AssignMode' in network) { + if (network.v6AssignMode) + n.v6AssignMode = network.v6AssignMode.toString(); + else n.v4AssignMode = 'none'; + } + if ('multicastLimit' in network) { + if (typeof network.multicastLimit === 'number') + n.multicastLimit = network.multicastLimit; + else n.multicastLimit = parseInt(network.multicastLimit.toString()); + } + if (Array.isArray(network.relays)) + n.relays = network.relays; + if (Array.isArray(network.ipAssignmentPools)) + n.ipAssignmentPools = network.ipAssignmentPools; + if (Array.isArray(network.rules)) + n.rules = network.rules; + + request({ + url: this.url + 'controller/network/' + n.nwid, + method: 'POST', + json: true, + body: n, + headers: { + 'X-ZT1-Auth': this.authToken + } + },function(err,response,body) { + if (err) + return callback(err,null); + if (response.statusCode !== 200) + return callback(new Error('server responded with error: '+response.statusCode),null); + return callback(null,(typeof body === 'string') ? JSON.parse(body) : body); + }); +}; + +ZT1Client.prototype.getControllerNetworkMember = function(nwid,address,callback) { + this._jsonGet('controller/network/' + nwid + '/member/' + address,callback); +}; + +exports.ZT1Client = ZT1Client; diff --git a/js/zt1-api-client/package.json b/js/zt1-api-client/package.json new file mode 100644 index 00000000..953f6d37 --- /dev/null +++ b/js/zt1-api-client/package.json @@ -0,0 +1,15 @@ +{ + "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": { + "async": "^0.9.0", + "request": "^2.55.0" + } +} diff --git a/js/zt1-api-client/test.js b/js/zt1-api-client/test.js new file mode 100644 index 00000000..347ca1a1 --- /dev/null +++ b/js/zt1-api-client/test.js @@ -0,0 +1,33 @@ +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); + else console.log(status); + + zt1c.getNetworks(function(err,networks) { + if (err) + console.log(err); + else console.log(networks); + + zt1c.getPeers(function(err,peers) { + if (err) + console.log(err); + else console.log(peers); + + if (status.controller) { + zt1c.saveControllerNetwork({ + nwid: status.address + 'dead01', + name: 'test network', + private: true + },function(err,network) { + if (err) + console.log(err); + else console.log(network); + }); + } + }); + }); +}); diff --git a/nodejs-zt1-client/index.js b/nodejs-zt1-client/index.js deleted file mode 100644 index 55f7fb24..00000000 --- a/nodejs-zt1-client/index.js +++ /dev/null @@ -1,160 +0,0 @@ -'use strict' - -var request = require('request'); - -function ZT1Client(url,authToken) -{ - this.url = url; - this.authToken = authToken; -} - -// Generate new ZeroTier identity -- mostly for testing -ZT1Client.prototype.newIdentity = function(callback) -{ - request({ - url: this.url + 'newIdentity', - method: 'GET', - json: false, - headers: { - 'X-ZT1-Auth': this.authToken - } - },function(error,response,body) { - if (error) - return callback(error,null); - if (response.statusCode === 200) - return callback(null,body); - return callback(new Error('server responded with error: '+response.statusCode),''); - }); -} - -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,null); - if (response.statusCode !== 200) - return callback(new Error('server responded with error: '+response.statusCode),null); - 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.getNetworks = function(callback) -{ - this._jsonGet('network',callback); -}; - -ZT1Client.prototype.getPeers = function(callback) -{ - this._jsonGet('peer',callback); -}; - -ZT1Client.prototype.listControllerNetworks = function(callback) -{ - this._jsonGet('controller/network',callback); -}; - -ZT1Client.prototype.getControllerNetwork = function(nwid,callback) -{ - this._jsonGet('controller/network/' + nwid,callback); -}; - -ZT1Client.prototype.saveControllerNetwork = function(network,callback) -{ - if ((typeof network.nwid !== 'string')||(network.nwid.length !== 16)) - return callback(new Error('Missing required field: nwid'),null); - - // The ZT1 service is type variation intolerant, so recreate our submission with the correct types - var n = { - nwid: network.nwid - }; - if (network.name) - n.name = network.name.toString(); - if ('private' in network) - n.private = (network.private) ? true : false; - if ('enableBroadcast' in network) - n.enableBroadcast = (network.enableBroadcast) ? true : false; - if ('allowPassiveBridging' in network) - n.allowPassiveBridging = (network.allowPassiveBridging) ? true : false; - if ('v4AssignMode' in network) { - if (network.v4AssignMode) - n.v4AssignMode = network.v4AssignMode.toString(); - else n.v4AssignMode = 'none'; - } - if ('v6AssignMode' in network) { - if (network.v6AssignMode) - n.v6AssignMode = network.v6AssignMode.toString(); - else n.v4AssignMode = 'none'; - } - if ('multicastLimit' in network) { - if (typeof network.multicastLimit === 'number') - n.multicastLimit = network.multicastLimit; - else n.multicastLimit = parseInt(network.multicastLimit.toString()); - } - if (Array.isArray(network.relays)) - n.relays = network.relays; - if (Array.isArray(network.ipAssignmentPools)) - n.ipAssignmentPools = network.ipAssignmentPools; - if (Array.isArray(network.rules)) - n.rules = network.rules; - - request({ - url: this.url + 'controller/network/' + n.nwid, - method: 'POST', - json: true, - body: n, - headers: { - 'X-ZT1-Auth': this.authToken - } - },function(err,response,body) { - if (err) - return callback(err,null); - if (response.statusCode !== 200) - return callback(new Error('server responded with error: '+response.statusCode),null); - return callback(null,(typeof body === 'string') ? JSON.parse(body) : body); - }); -}; - -ZT1Client.prototype.getControllerNetworkMember = function(nwid,address,callback) { - this._jsonGet('controller/network/' + nwid + '/member/' + address,callback); -}; - -exports.ZT1Client = ZT1Client; diff --git a/nodejs-zt1-client/package.json b/nodejs-zt1-client/package.json deleted file mode 100644 index 953f6d37..00000000 --- a/nodejs-zt1-client/package.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "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": { - "async": "^0.9.0", - "request": "^2.55.0" - } -} diff --git a/nodejs-zt1-client/test.js b/nodejs-zt1-client/test.js deleted file mode 100644 index 347ca1a1..00000000 --- a/nodejs-zt1-client/test.js +++ /dev/null @@ -1,33 +0,0 @@ -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); - else console.log(status); - - zt1c.getNetworks(function(err,networks) { - if (err) - console.log(err); - else console.log(networks); - - zt1c.getPeers(function(err,peers) { - if (err) - console.log(err); - else console.log(peers); - - if (status.controller) { - zt1c.saveControllerNetwork({ - nwid: status.address + 'dead01', - name: 'test network', - private: true - },function(err,network) { - if (err) - console.log(err); - else console.log(network); - }); - } - }); - }); -}); -- cgit v1.2.3