index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 'use strict'
  2. var request = require('request');
  3. var constrainTypes = require('./constrain-types.js');
  4. // Types that fields must be in submissions -- used with constrainTypes to
  5. // ensure that submitted JSON objects are correctly typed since the JSON
  6. // API is very sensitive to this. This only includes writable fields since
  7. // non-writable and unknown fields are ignored.
  8. var REQUEST_TYPE_MAPS = {
  9. 'controller/network/*/relay': {
  10. 'address': 'string',
  11. 'phyAddress': 'string'
  12. },
  13. 'controller/network/*/rule': {
  14. 'ruleId': 'integer',
  15. 'nodeId': 'string',
  16. 'vlanId': 'integer',
  17. 'vlanPcp': 'integer',
  18. 'etherType': 'integer',
  19. 'macSource': 'string',
  20. 'macDest': 'string',
  21. 'ipSource': 'string',
  22. 'ipDest': 'string',
  23. 'ipTos': 'integer',
  24. 'ipProtocol': 'integer',
  25. 'ipSourcePort': 'integer',
  26. 'ipDestPort': 'integer',
  27. 'flags': 'integer',
  28. 'invFlags': 'integer',
  29. 'action': 'string'
  30. },
  31. 'controller/network/*/ipAssignmentPool': {
  32. 'network': 'string',
  33. 'netmaskBits': 'integer'
  34. },
  35. 'controller/network/*/member': {
  36. 'authorized': 'boolean',
  37. 'activeBridge': 'boolean',
  38. 'ipAssignments': [ 'string' ]
  39. },
  40. 'controller/network/*': {
  41. 'name': 'string',
  42. 'private': 'boolean',
  43. 'enableBroadcast': 'boolean',
  44. 'allowPassiveBridging': 'boolean',
  45. 'v4AssignMode': 'string',
  46. 'v6AssignMode': 'string',
  47. 'multicastLimit': 'integer',
  48. 'relays': [ this['controller/network/*/relay'] ],
  49. 'ipAssignmentPools': [ this['controller/network/*/ipAssignmentPool'] ],
  50. 'rules': [ this['controller/network/*/rule'] ]
  51. }
  52. };
  53. // URL must end with trailing slash e.g. http://127.0.0.1:9993/
  54. function ZT1ApiClient(url,authToken)
  55. {
  56. this.url = url;
  57. this.authToken = authToken;
  58. }
  59. // Simple JSON URI getter, for internal use.
  60. ZT1ApiClient.prototype._jsonGet = function(getPath,callback)
  61. {
  62. request({
  63. url: this.url + getPath,
  64. method: 'GET',
  65. headers: {
  66. 'X-ZT1-Auth': this.authToken
  67. }
  68. },function(error,response,body) {
  69. if (error)
  70. return callback(error,null);
  71. if (response.statusCode !== 200)
  72. return callback(new Error('server responded with error: '+response.statusCode),null);
  73. return callback(null,(typeof body === 'string') ? JSON.parse(body) : null);
  74. });
  75. };
  76. // Generate new ZeroTier identity -- mostly for testing
  77. ZT1ApiClient.prototype.newIdentity = function(callback)
  78. {
  79. request({
  80. url: this.url + 'newIdentity',
  81. method: 'GET',
  82. json: false,
  83. headers: {
  84. 'X-ZT1-Auth': this.authToken
  85. }
  86. },function(error,response,body) {
  87. if (error)
  88. return callback(error,null);
  89. if (response.statusCode === 200)
  90. return callback(null,body);
  91. return callback(new Error('server responded with error: '+response.statusCode),'');
  92. });
  93. }
  94. // Get node status -- returns a combination of regular status and (if present) controller info
  95. ZT1ApiClient.prototype.status = function(callback)
  96. {
  97. request({
  98. url: this.url + 'controller',
  99. method: 'GET',
  100. headers: {
  101. 'X-ZT1-Auth': this.authToken
  102. }
  103. },function(error,response,body) {
  104. if (error)
  105. return callback(error,null);
  106. var controllerStatus = {};
  107. if ((typeof body === 'string')&&(response.statusCode === 200))
  108. controllerStatus = JSON.parse(body);
  109. request({
  110. url: this.url + 'status',
  111. method: 'GET',
  112. headers: {
  113. 'X-ZT1-Auth': this.authToken
  114. }
  115. },function(error,response,body) {
  116. if (error)
  117. return callback(error,{});
  118. if (response.statusCode !== 200)
  119. return callback(new Error('server responded with '+response.statusCode),{});
  120. var nodeStatus = JSON.parse(body);
  121. for(var k in controllerStatus)
  122. nodeStatus[k] = controllerStatus[k];
  123. return callback(null,nodeStatus);
  124. }.bind(this));
  125. }.bind(this));
  126. };
  127. ZT1ApiClient.prototype.getNetworks = function(callback)
  128. {
  129. this._jsonGet('network',callback);
  130. };
  131. ZT1ApiClient.prototype.getPeers = function(callback)
  132. {
  133. this._jsonGet('peer',callback);
  134. };
  135. ZT1ApiClient.prototype.listControllerNetworks = function(callback)
  136. {
  137. this._jsonGet('controller/network',callback);
  138. };
  139. ZT1ApiClient.prototype.getControllerNetwork = function(nwid,callback)
  140. {
  141. this._jsonGet('controller/network/' + nwid,callback);
  142. };
  143. // If NWID is the special ##########______ format, a new NWID will
  144. // be generated server side and filled in in returned object.
  145. ZT1ApiClient.prototype.saveControllerNetwork = function(network,callback)
  146. {
  147. request({
  148. url: this.url + 'controller/network/' + n.nwid,
  149. method: 'POST',
  150. json: true,
  151. body: constrainTypes(network,REQUEST_TYPE_MAPS['controller/network/*']),
  152. headers: {
  153. 'X-ZT1-Auth': this.authToken
  154. }
  155. },function(err,response,body) {
  156. if (err)
  157. return callback(err,null);
  158. if (response.statusCode !== 200)
  159. return callback(new Error('server responded with error: '+response.statusCode),null);
  160. return callback(null,(typeof body === 'string') ? JSON.parse(body) : body);
  161. });
  162. };
  163. ZT1ApiClient.prototype.deleteControllerNetwork = function(nwid,callback) {
  164. request({
  165. url: this.url + 'controller/network/'+ nwid,
  166. method: 'DELETE',
  167. headers: {
  168. 'X-ZT1-Auth': this.authToken
  169. }
  170. },function(err,response,body) {
  171. if (err)
  172. return callback(err);
  173. else if (response.statusCode === 200)
  174. return callback(null);
  175. else return callback(new Error('server responded with error: '+response.statusCode));
  176. });
  177. };
  178. ZT1ApiClient.prototype.getControllerNetworkMember = function(nwid,address,callback) {
  179. this._jsonGet('controller/network/' + nwid + '/member/' + address,callback);
  180. };
  181. ZT1ApiClient.prototype.saveControllerNetworkMember = function(nwid,member,callback) {
  182. var m = constrainTypes(member,REQUEST_TYPE_MAPS['controller/network/*/member']);
  183. m.nwid = nwid;
  184. request({
  185. url: this.url + 'controller/network' + nwid + '/member/' + member.address,
  186. method: 'POST',
  187. json: true,
  188. body: m,
  189. headers: {
  190. 'X-ZT1-Auth': this.authToken
  191. }
  192. },function(err,response,body) {
  193. if (err)
  194. return callback(err,null);
  195. if (response.statusCode !== 200)
  196. return callback(new Error('server responded with error: '+response.statusCode),null);
  197. return callback(null,(typeof body === 'string') ? JSON.parse(body) : body);
  198. });
  199. };
  200. ZT1ApiClient.prototype.deleteControllerNetworkMember = function(nwid,address,callback) {
  201. request({
  202. url: this.url + 'controller/network/' + nwid + '/member/' + address,
  203. method: 'DELETE',
  204. headers: {
  205. 'X-ZT1-Auth': this.authToken
  206. }
  207. },function(err,response,body) {
  208. if (err)
  209. return callback(err);
  210. else if (response.statusCode === 200)
  211. return callback(null);
  212. else return callback(new Error('server responded with error: '+response.statusCode));
  213. });
  214. };
  215. exports.ZT1ApiClient = ZT1ApiClient;