server.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // ZeroTier distributed HTTP test coordinator and result-reporting server
  2. // ---------------------------------------------------------------------------
  3. // Customizable parameters:
  4. var SERVER_PORT = 18080;
  5. // ---------------------------------------------------------------------------
  6. var fs = require('fs');
  7. var express = require('express');
  8. var app = express();
  9. app.use(function(req,res,next) {
  10. req.rawBody = '';
  11. req.on('data', function(chunk) { req.rawBody += chunk.toString(); });
  12. req.on('end', function() { return next(); });
  13. });
  14. var knownAgents = {};
  15. app.post('/:agentId',function(req,res) {
  16. var agentId = req.params.agentId;
  17. if ((!agentId)||(agentId.length !== 32))
  18. return res.status(404).send('');
  19. if (req.rawBody) {
  20. var receiveTime = Date.now();
  21. var resultData = null;
  22. try {
  23. resultData = JSON.parse(req.rawBody);
  24. console.log(resultData.source+','+resultData.target+','+resultData.time+','+resultData.bytes+','+resultData.timedOut+',"'+((resultData.error) ? resultData.error : '')+'"');
  25. } catch (e) {}
  26. }
  27. knownAgents[agentId] = Date.now();
  28. return res.status(200).send(JSON.stringify(Object.keys(knownAgents)));
  29. });
  30. var expressServer = app.listen(SERVER_PORT,function () {
  31. console.log('LISTENING ON '+SERVER_PORT);
  32. console.log('');
  33. });