2015-05-16 23:32:13 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
var request = require('request');
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
function ZT1ApiClient(url,authToken)
|
2015-05-16 23:32:13 +00:00
|
|
|
{
|
|
|
|
this.url = url;
|
|
|
|
this.authToken = authToken;
|
|
|
|
}
|
|
|
|
|
2015-05-25 21:21:05 +00:00
|
|
|
// Generate new ZeroTier identity -- mostly for testing
|
2015-05-26 16:03:39 +00:00
|
|
|
ZT1ApiClient.prototype.newIdentity = function(callback)
|
2015-05-25 21:21:05 +00:00
|
|
|
{
|
|
|
|
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),'');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
ZT1ApiClient.prototype._jsonGet = function(getPath,callback)
|
2015-05-16 23:32:13 +00:00
|
|
|
{
|
|
|
|
request({
|
|
|
|
url: this.url + getPath,
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'X-ZT1-Auth': this.authToken
|
|
|
|
}
|
|
|
|
},function(error,response,body) {
|
|
|
|
if (error)
|
2015-05-17 00:12:29 +00:00
|
|
|
return callback(error,null);
|
2015-05-16 23:32:13 +00:00
|
|
|
if (response.statusCode !== 200)
|
2015-05-17 00:12:29 +00:00
|
|
|
return callback(new Error('server responded with error: '+response.statusCode),null);
|
2015-05-16 23:32:13 +00:00
|
|
|
return callback(null,(typeof body === 'string') ? JSON.parse(body) : null);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
ZT1ApiClient.prototype.status = function(callback)
|
2015-05-16 23:32:13 +00:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
};
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
ZT1ApiClient.prototype.getNetworks = function(callback)
|
2015-05-16 23:32:13 +00:00
|
|
|
{
|
|
|
|
this._jsonGet('network',callback);
|
|
|
|
};
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
ZT1ApiClient.prototype.getPeers = function(callback)
|
2015-05-17 00:12:29 +00:00
|
|
|
{
|
|
|
|
this._jsonGet('peer',callback);
|
|
|
|
};
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
ZT1ApiClient.prototype.listControllerNetworks = function(callback)
|
2015-05-16 23:32:13 +00:00
|
|
|
{
|
|
|
|
this._jsonGet('controller/network',callback);
|
|
|
|
};
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
ZT1ApiClient.prototype.getControllerNetwork = function(nwid,callback)
|
2015-05-17 00:12:29 +00:00
|
|
|
{
|
|
|
|
this._jsonGet('controller/network/' + nwid,callback);
|
|
|
|
};
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
ZT1ApiClient.prototype.saveControllerNetwork = function(network,callback)
|
2015-05-17 00:12:29 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
ZT1ApiClient.prototype.getControllerNetworkMember = function(nwid,address,callback) {
|
2015-05-25 21:21:05 +00:00
|
|
|
this._jsonGet('controller/network/' + nwid + '/member/' + address,callback);
|
|
|
|
};
|
|
|
|
|
2015-05-26 16:03:39 +00:00
|
|
|
exports.ZT1ApiClient = ZT1ApiClient;
|