index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict'
  2. var request = require('request');
  3. function ZT1Client(url,authToken)
  4. {
  5. this.url = url;
  6. this.authToken = authToken;
  7. }
  8. // Generate new ZeroTier identity -- mostly for testing
  9. ZT1Client.prototype.newIdentity = function(callback)
  10. {
  11. request({
  12. url: this.url + 'newIdentity',
  13. method: 'GET',
  14. json: false,
  15. headers: {
  16. 'X-ZT1-Auth': this.authToken
  17. }
  18. },function(error,response,body) {
  19. if (error)
  20. return callback(error,null);
  21. if (response.statusCode === 200)
  22. return callback(null,body);
  23. return callback(new Error('server responded with error: '+response.statusCode),'');
  24. });
  25. }
  26. ZT1Client.prototype._jsonGet = function(getPath,callback)
  27. {
  28. request({
  29. url: this.url + getPath,
  30. method: 'GET',
  31. headers: {
  32. 'X-ZT1-Auth': this.authToken
  33. }
  34. },function(error,response,body) {
  35. if (error)
  36. return callback(error,null);
  37. if (response.statusCode !== 200)
  38. return callback(new Error('server responded with error: '+response.statusCode),null);
  39. return callback(null,(typeof body === 'string') ? JSON.parse(body) : null);
  40. });
  41. };
  42. ZT1Client.prototype.status = function(callback)
  43. {
  44. request({
  45. url: this.url + 'controller',
  46. method: 'GET',
  47. headers: {
  48. 'X-ZT1-Auth': this.authToken
  49. }
  50. },function(error,response,body) {
  51. if (error)
  52. return callback(error,{});
  53. var controllerStatus = {};
  54. if (typeof body === 'string')
  55. controllerStatus = JSON.parse(body);
  56. request({
  57. url: this.url + 'status',
  58. method: 'GET',
  59. headers: {
  60. 'X-ZT1-Auth': this.authToken
  61. }
  62. },function(error,response,body) {
  63. if (error)
  64. return callback(error,{});
  65. if (response.statusCode !== 200)
  66. return callback(new Error('server responded with '+response.statusCode),{});
  67. var nodeStatus = JSON.parse(body);
  68. for(var k in controllerStatus)
  69. nodeStatus[k] = controllerStatus[k];
  70. return callback(null,nodeStatus);
  71. }.bind(this));
  72. }.bind(this));
  73. };
  74. ZT1Client.prototype.getNetworks = function(callback)
  75. {
  76. this._jsonGet('network',callback);
  77. };
  78. ZT1Client.prototype.getPeers = function(callback)
  79. {
  80. this._jsonGet('peer',callback);
  81. };
  82. ZT1Client.prototype.listControllerNetworks = function(callback)
  83. {
  84. this._jsonGet('controller/network',callback);
  85. };
  86. ZT1Client.prototype.getControllerNetwork = function(nwid,callback)
  87. {
  88. this._jsonGet('controller/network/' + nwid,callback);
  89. };
  90. ZT1Client.prototype.saveControllerNetwork = function(network,callback)
  91. {
  92. if ((typeof network.nwid !== 'string')||(network.nwid.length !== 16))
  93. return callback(new Error('Missing required field: nwid'),null);
  94. // The ZT1 service is type variation intolerant, so recreate our submission with the correct types
  95. var n = {
  96. nwid: network.nwid
  97. };
  98. if (network.name)
  99. n.name = network.name.toString();
  100. if ('private' in network)
  101. n.private = (network.private) ? true : false;
  102. if ('enableBroadcast' in network)
  103. n.enableBroadcast = (network.enableBroadcast) ? true : false;
  104. if ('allowPassiveBridging' in network)
  105. n.allowPassiveBridging = (network.allowPassiveBridging) ? true : false;
  106. if ('v4AssignMode' in network) {
  107. if (network.v4AssignMode)
  108. n.v4AssignMode = network.v4AssignMode.toString();
  109. else n.v4AssignMode = 'none';
  110. }
  111. if ('v6AssignMode' in network) {
  112. if (network.v6AssignMode)
  113. n.v6AssignMode = network.v6AssignMode.toString();
  114. else n.v4AssignMode = 'none';
  115. }
  116. if ('multicastLimit' in network) {
  117. if (typeof network.multicastLimit === 'number')
  118. n.multicastLimit = network.multicastLimit;
  119. else n.multicastLimit = parseInt(network.multicastLimit.toString());
  120. }
  121. if (Array.isArray(network.relays))
  122. n.relays = network.relays;
  123. if (Array.isArray(network.ipAssignmentPools))
  124. n.ipAssignmentPools = network.ipAssignmentPools;
  125. if (Array.isArray(network.rules))
  126. n.rules = network.rules;
  127. request({
  128. url: this.url + 'controller/network/' + n.nwid,
  129. method: 'POST',
  130. json: true,
  131. body: n,
  132. headers: {
  133. 'X-ZT1-Auth': this.authToken
  134. }
  135. },function(err,response,body) {
  136. if (err)
  137. return callback(err,null);
  138. if (response.statusCode !== 200)
  139. return callback(new Error('server responded with error: '+response.statusCode),null);
  140. return callback(null,(typeof body === 'string') ? JSON.parse(body) : body);
  141. });
  142. };
  143. ZT1Client.prototype.getControllerNetworkMember = function(nwid,address,callback) {
  144. this._jsonGet('controller/network/' + nwid + '/member/' + address,callback);
  145. };
  146. exports.ZT1Client = ZT1Client;