agent.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // ---------------------------------------------------------------------------
  2. // Customizable parameters:
  3. // How frequently in ms to run tests
  4. //var RUN_TEST_EVERY = (60 * 5 * 1000);
  5. var RUN_TEST_EVERY = 1000;
  6. // Maximum test duration in milliseconds (must be less than RUN_TEST_EVERY)
  7. var TEST_DURATION = (60 * 1000);
  8. // Where should I contact to register and query a list of other nodes?
  9. var SERVER_HOST = '174.136.102.178';
  10. var SERVER_PORT = 18080;
  11. // Which port should agents use for their HTTP?
  12. var AGENT_PORT = 18888;
  13. // Payload size in bytes
  14. var PAYLOAD_SIZE = 100000;
  15. // ---------------------------------------------------------------------------
  16. var ipaddr = require('ipaddr.js');
  17. var os = require('os');
  18. var http = require('http');
  19. var async = require('async');
  20. var express = require('express');
  21. var app = express();
  22. // Find our ZeroTier-assigned RFC4193 IPv6 address
  23. var thisAgentId = null;
  24. var interfaces = os.networkInterfaces();
  25. if (!interfaces) {
  26. console.error('FATAL: os.networkInterfaces() failed.');
  27. process.exit(1);
  28. }
  29. for(var ifname in interfaces) {
  30. var ifaddrs = interfaces[ifname];
  31. if (Array.isArray(ifaddrs)) {
  32. for(var i=0;i<ifaddrs.length;++i) {
  33. if (ifaddrs[i].family == 'IPv6') {
  34. try {
  35. var ipbytes = ipaddr.parse(ifaddrs[i].address).toByteArray();
  36. if ((ipbytes.length === 16)&&(ipbytes[0] == 0xfd)&&(ipbytes[9] == 0x99)&&(ipbytes[10] == 0x93)) {
  37. thisAgentId = '';
  38. for(var j=0;j<16;++j) {
  39. var tmp = ipbytes[j].toString(16);
  40. if (tmp.length === 1)
  41. thisAgentId += '0';
  42. thisAgentId += tmp;
  43. }
  44. }
  45. } catch (e) {
  46. console.error(e);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. if (thisAgentId === null) {
  53. console.error('FATAL: no ZeroTier-assigned RFC4193 IPv6 addresses found on any local interface!');
  54. process.exit(1);
  55. }
  56. //console.log(thisAgentId);
  57. // Create a random (and therefore not very compressable) payload
  58. var payload = new Buffer(PAYLOAD_SIZE);
  59. for(var xx=0;xx<PAYLOAD_SIZE;++xx) {
  60. payload.writeUInt8(Math.round(Math.random() * 255.0),xx);
  61. }
  62. // Incremented for each test
  63. var testCounter = 0;
  64. function registerAndGetPeers(callback)
  65. {
  66. http.get({
  67. host: SERVER_HOST,
  68. port: SERVER_PORT,
  69. path: '/'+thisAgentId
  70. },function(res) {
  71. var body = '';
  72. res.on('data',function(chunk) { body += chunk.toString(); });
  73. res.on('end',function() {
  74. try {
  75. var peers = JSON.parse(body);
  76. if (Array.isArray(peers))
  77. return callback(null,peers);
  78. else return callback(new Error('invalid JSON response from server'),null);
  79. } catch (e) {
  80. return callback(new Error('invalid JSON response from server'),null);
  81. }
  82. });
  83. }).on('error',function(e) {
  84. return callback(e,null);
  85. });
  86. };
  87. function performTestOnAllPeers(peers,callback)
  88. {
  89. var allResults = {};
  90. var timedOut = false;
  91. var endOfTestTimer = setTimeout(function() {
  92. timedOut = true;
  93. return callback(allResults);
  94. },TEST_DURATION);
  95. var testStartTime = Date.now();
  96. async.each(peers,function(peer,next) {
  97. if (timedOut)
  98. return next(null);
  99. if (peer.length !== 32)
  100. return next(null);
  101. var connectionStartTime = Date.now();
  102. allResults[peer] = {
  103. testStart: testStartTime,
  104. start: connectionStartTime,
  105. end: null,
  106. error: null,
  107. bytes: 0,
  108. test: testCounter
  109. };
  110. var peerHost = '';
  111. peerHost += peer.substr(0,4);
  112. peerHost += ':';
  113. peerHost += peer.substr(4,4);
  114. peerHost += ':';
  115. peerHost += peer.substr(8,4);
  116. peerHost += ':';
  117. peerHost += peer.substr(12,4);
  118. peerHost += ':';
  119. peerHost += peer.substr(16,4);
  120. peerHost += ':';
  121. peerHost += peer.substr(20,4);
  122. peerHost += ':';
  123. peerHost += peer.substr(24,4);
  124. peerHost += ':';
  125. peerHost += peer.substr(28,4);
  126. http.get({
  127. host: peerHost,
  128. port: AGENT_PORT,
  129. path: '/'
  130. },function(res) {
  131. var bytes = 0;
  132. res.on('data',function(chunk) {
  133. bytes += chunk.length;
  134. });
  135. res.on('end',function() {
  136. if (timedOut)
  137. return next(null);
  138. allResults[peer] = {
  139. testStart: testStartTime,
  140. start: connectionStartTime,
  141. end: Date.now(),
  142. error: null,
  143. bytes: bytes,
  144. test: testCounter
  145. };
  146. return next(null);
  147. });
  148. }).on('error',function(e) {
  149. if (timedOut)
  150. return next(null);
  151. allResults[peer] = {
  152. testStart: testStartTime,
  153. start: connectionStartTime,
  154. end: Date.now(),
  155. error: e.toString(),
  156. bytes: 0,
  157. test: testCounter
  158. };
  159. return next(null);
  160. });
  161. },function(err) {
  162. if (!timedOut) {
  163. clearTimeout(endOfTestTimer);
  164. return callback(allResults);
  165. }
  166. });
  167. };
  168. // Agents just serve up a test payload
  169. app.get('/',function(req,res) {
  170. return res.status(200).send(payload);
  171. });
  172. var expressServer = app.listen(AGENT_PORT,function () {
  173. registerAndGetPeers(function(err,peers) {
  174. if (err) {
  175. console.error('FATAL: unable to contact or query server: '+err.toString());
  176. process.exit(1);
  177. }
  178. setInterval(function() {
  179. ++testCounter;
  180. registerAndGetPeers(function(err,peers) {
  181. if (err) {
  182. console.error('WARNING: unable to contact or query server, test aborted: '+err.toString());
  183. return;
  184. }
  185. performTestOnAllPeers(peers,function(results) {
  186. //console.log(results);
  187. var submit = http.request({
  188. host: SERVER_HOST,
  189. port: SERVER_PORT,
  190. path: '/'+thisAgentId,
  191. method: 'POST'
  192. },function(res) {
  193. }).on('error',function(e) {
  194. console.error('WARNING: unable to submit results to server: '+err.toString());
  195. });
  196. submit.write(JSON.stringify(results));
  197. submit.end();
  198. });
  199. });
  200. },RUN_TEST_EVERY);
  201. });
  202. });