agent.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // ZeroTier distributed HTTP test agent
  2. // ---------------------------------------------------------------------------
  3. // Customizable parameters:
  4. // Time between startup and first test attempt
  5. var TEST_STARTUP_LAG = 10000;
  6. // Maximum interval between test attempts (actual timing is random % this)
  7. var TEST_INTERVAL_MAX = (60000 * 10);
  8. // Test timeout in ms
  9. var TEST_TIMEOUT = 30000;
  10. // Where should I get other agents' IDs and POST results?
  11. var SERVER_HOST = '52.26.196.147';
  12. var SERVER_PORT = 18080;
  13. // Which port do agents use to serve up test data to each other?
  14. var AGENT_PORT = 18888;
  15. // Payload size in bytes
  16. var PAYLOAD_SIZE = 5000;
  17. // ---------------------------------------------------------------------------
  18. var ipaddr = require('ipaddr.js');
  19. var os = require('os');
  20. var http = require('http');
  21. var async = require('async');
  22. var express = require('express');
  23. var app = express();
  24. // Find our ZeroTier-assigned RFC4193 IPv6 address
  25. var thisAgentId = null;
  26. var interfaces = os.networkInterfaces();
  27. if (!interfaces) {
  28. console.error('FATAL: os.networkInterfaces() failed.');
  29. process.exit(1);
  30. }
  31. for(var ifname in interfaces) {
  32. var ifaddrs = interfaces[ifname];
  33. if (Array.isArray(ifaddrs)) {
  34. for(var i=0;i<ifaddrs.length;++i) {
  35. if (ifaddrs[i].family == 'IPv6') {
  36. try {
  37. var ipbytes = ipaddr.parse(ifaddrs[i].address).toByteArray();
  38. if ((ipbytes.length === 16)&&(ipbytes[0] == 0xfd)&&(ipbytes[9] == 0x99)&&(ipbytes[10] == 0x93)) {
  39. thisAgentId = '';
  40. for(var j=0;j<16;++j) {
  41. var tmp = ipbytes[j].toString(16);
  42. if (tmp.length === 1)
  43. thisAgentId += '0';
  44. thisAgentId += tmp;
  45. }
  46. }
  47. } catch (e) {
  48. console.error(e);
  49. }
  50. }
  51. }
  52. }
  53. }
  54. if (thisAgentId === null) {
  55. console.error('FATAL: no ZeroTier-assigned RFC4193 IPv6 addresses found on any local interface!');
  56. process.exit(1);
  57. }
  58. //console.log(thisAgentId);
  59. // Create a random (and therefore not very compressable) payload
  60. var payload = new Buffer(PAYLOAD_SIZE);
  61. for(var xx=0;xx<PAYLOAD_SIZE;++xx) {
  62. payload.writeUInt8(Math.round(Math.random() * 255.0),xx);
  63. }
  64. function agentIdToIp(agentId)
  65. {
  66. var ip = '';
  67. ip += agentId.substr(0,4);
  68. ip += ':';
  69. ip += agentId.substr(4,4);
  70. ip += ':';
  71. ip += agentId.substr(8,4);
  72. ip += ':';
  73. ip += agentId.substr(12,4);
  74. ip += ':';
  75. ip += agentId.substr(16,4);
  76. ip += ':';
  77. ip += agentId.substr(20,4);
  78. ip += ':';
  79. ip += agentId.substr(24,4);
  80. ip += ':';
  81. ip += agentId.substr(28,4);
  82. return ip;
  83. };
  84. var lastTestResult = null;
  85. var allOtherAgents = {};
  86. function doTest()
  87. {
  88. var submit = http.request({
  89. host: SERVER_HOST,
  90. port: SERVER_PORT,
  91. path: '/'+thisAgentId,
  92. method: 'POST'
  93. },function(res) {
  94. var body = '';
  95. res.on('data',function(chunk) { body += chunk.toString(); });
  96. res.on('end',function() {
  97. if (body) {
  98. try {
  99. var peers = JSON.parse(body);
  100. if (Array.isArray(peers)) {
  101. for(var xx=0;xx<peers.length;++xx)
  102. allOtherAgents[peers[xx]] = true;
  103. }
  104. } catch (e) {}
  105. }
  106. var agents = Object.keys(allOtherAgents);
  107. if (agents.length > 1) {
  108. var target = agents[Math.floor(Math.random() * agents.length)];
  109. while (target === thisAgentId)
  110. target = agents[Math.floor(Math.random() * agents.length)];
  111. var testRequest = null;
  112. var timeoutId = null;
  113. timeoutId = setTimeout(function() {
  114. if (testRequest !== null)
  115. testRequest.abort();
  116. timeoutId = null;
  117. },TEST_TIMEOUT);
  118. var startTime = Date.now();
  119. testRequest = http.get({
  120. host: agentIdToIp(target),
  121. port: AGENT_PORT,
  122. path: '/'
  123. },function(res) {
  124. var bytes = 0;
  125. res.on('data',function(chunk) { bytes += chunk.length; });
  126. res.on('end',function() {
  127. lastTestResult = {
  128. source: thisAgentId,
  129. target: target,
  130. time: (Date.now() - startTime),
  131. bytes: bytes,
  132. timedOut: (timeoutId === null),
  133. error: null
  134. };
  135. if (timeoutId !== null)
  136. clearTimeout(timeoutId);
  137. return setTimeout(doTest,Math.round(Math.random() * TEST_INTERVAL_MAX) + 1);
  138. });
  139. }).on('error',function(e) {
  140. lastTestResult = {
  141. source: thisAgentId,
  142. target: target,
  143. time: (Date.now() - startTime),
  144. bytes: 0,
  145. timedOut: (timeoutId === null),
  146. error: e.toString()
  147. };
  148. if (timeoutId !== null)
  149. clearTimeout(timeoutId);
  150. return setTimeout(doTest,Math.round(Math.random() * TEST_INTERVAL_MAX) + 1);
  151. });
  152. } else {
  153. return setTimeout(doTest,1000);
  154. }
  155. });
  156. }).on('error',function(e) {
  157. console.log('POST failed: '+e.toString());
  158. return setTimeout(doTest,1000);
  159. });
  160. if (lastTestResult !== null) {
  161. submit.write(JSON.stringify(lastTestResult));
  162. lastTestResult = null;
  163. }
  164. submit.end();
  165. };
  166. // Agents just serve up a test payload
  167. app.get('/',function(req,res) { return res.status(200).send(payload); });
  168. var expressServer = app.listen(AGENT_PORT,function () {
  169. // Start timeout-based loop
  170. setTimeout(doTest(),TEST_STARTUP_LAG);
  171. });