agent.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // ZeroTier distributed HTTP test agent
  2. // ---------------------------------------------------------------------------
  3. // Customizable parameters:
  4. // Maximum interval between test attempts (actual timing is random % this)
  5. var TEST_INTERVAL_MAX = (60000 * 10);
  6. // Test timeout in ms
  7. var TEST_TIMEOUT = 60000;
  8. // Where should I get other agents' IDs and POST results?
  9. var SERVER_HOST = '174.136.102.178';
  10. var SERVER_PORT = 18080;
  11. // Which port do agents use to serve up test data to each other?
  12. var AGENT_PORT = 18888;
  13. // Payload size in bytes
  14. var PAYLOAD_SIZE = 5000;
  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. function agentIdToIp(agentId)
  63. {
  64. var ip = '';
  65. ip += agentId.substr(0,4);
  66. ip += ':';
  67. ip += agentId.substr(4,4);
  68. ip += ':';
  69. ip += agentId.substr(8,4);
  70. ip += ':';
  71. ip += agentId.substr(12,4);
  72. ip += ':';
  73. ip += agentId.substr(16,4);
  74. ip += ':';
  75. ip += agentId.substr(20,4);
  76. ip += ':';
  77. ip += agentId.substr(24,4);
  78. ip += ':';
  79. ip += agentId.substr(28,4);
  80. return ip;
  81. };
  82. var lastTestResult = null;
  83. var allOtherAgents = [];
  84. function doTest()
  85. {
  86. var submit = http.request({
  87. host: SERVER_HOST,
  88. port: SERVER_PORT,
  89. path: '/'+thisAgentId,
  90. method: 'POST'
  91. },function(res) {
  92. var body = '';
  93. res.on('data',function(chunk) { body += chunk.toString(); });
  94. res.on('end',function() {
  95. if (body) {
  96. try {
  97. var peers = JSON.parse(body);
  98. if (Array.isArray(peers))
  99. allOtherAgents = peers;
  100. } catch (e) {}
  101. }
  102. if (allOtherAgents.length > 1) {
  103. var target = allOtherAgents[Math.floor(Math.random() * allOtherAgents.length)];
  104. while (target === thisAgentId)
  105. target = allOtherAgents[Math.floor(Math.random() * allOtherAgents.length)];
  106. var testRequest = null;
  107. var timeoutId = null;
  108. timeoutId = setTimeout(function() {
  109. if (testRequest !== null)
  110. testRequest.abort();
  111. timeoutId = null;
  112. },TEST_TIMEOUT);
  113. var startTime = Date.now();
  114. testRequest = http.get({
  115. host: agentIdToIp(target),
  116. port: AGENT_PORT,
  117. path: '/'
  118. },function(res) {
  119. var bytes = 0;
  120. res.on('data',function(chunk) { bytes += chunk.length; });
  121. res.on('end',function() {
  122. lastTestResult = {
  123. source: thisAgentId,
  124. target: target,
  125. time: (Date.now() - startTime),
  126. bytes: bytes,
  127. timedOut: (timeoutId === null),
  128. error: null
  129. };
  130. if (timeoutId !== null)
  131. clearTimeout(timeoutId);
  132. return setTimeout(doTest,Math.round(Math.random() * TEST_INTERVAL_MAX) + 1);
  133. });
  134. }).on('error',function(e) {
  135. lastTestResult = {
  136. source: thisAgentId,
  137. target: target,
  138. time: (Date.now() - startTime),
  139. bytes: 0,
  140. timedOut: (timeoutId === null),
  141. error: e.toString()
  142. };
  143. if (timeoutId !== null)
  144. clearTimeout(timeoutId);
  145. return setTimeout(doTest,Math.round(Math.random() * TEST_INTERVAL_MAX) + 1);
  146. });
  147. } else {
  148. return setTimeout(doTest,1000);
  149. }
  150. });
  151. }).on('error',function(e) {
  152. console.log('POST failed: '+e.toString());
  153. return setTimeout(doTest,1000);
  154. });
  155. if (lastTestResult !== null) {
  156. submit.write(JSON.stringify(lastTestResult));
  157. lastTestResult = null;
  158. }
  159. submit.end();
  160. };
  161. // Agents just serve up a test payload
  162. app.get('/',function(req,res) { return res.status(200).send(payload); });
  163. var expressServer = app.listen(AGENT_PORT,function () {
  164. // Start timeout-based loop
  165. doTest();
  166. });