http-tunnel-proxy.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env node
  2. var UDP_PORT_START = 9994;
  3. var UDP_PORT_COUNT = 16384;
  4. var HTTP_PORT = 8080;
  5. var LONG_POLLING_TIMEOUT = 25000;
  6. var http = require('http');
  7. var dgram = require('dgram');
  8. // clients[token] = [ most recent HTTP activity, assigned UDP socket ]
  9. var clients = {};
  10. // GETs[token] = [ [ request, timestamp ], ... ]
  11. var GETs = {};
  12. // mappings[localPort+'/'+remoteIp+'/'+remotePort] = { ZT source: [ token ] }
  13. var mappings = {};
  14. // Array of available UDP sockets to assign randomly to clients
  15. var udpSocketPool = [];
  16. function onIncomingUdp(socket,message,remoteIp,remotePort)
  17. {
  18. if (message.length > 16) {
  19. var mappingKey = socket.localPort + '/' + remoteIp + '/' + remotePort;
  20. var mapping = mappings[mappingKey];
  21. if (mapping) {
  22. var ztDestination = message.readUIntBE(8,5);
  23. if (ztDestination in mapping) {
  24. }
  25. }
  26. }
  27. }
  28. function onOutgoingUdp(token,socket,message,remoteIp,remotePort)
  29. {
  30. if (message.length > 16) {
  31. var ztDestination = message.readUIntBE(8,5);
  32. var ztSource = (message.length >= 28) ? message.readUIntBE(13,5) ? 0;
  33. if ((ztSource & 0xff00000000) == 0xff00000000) // fragment
  34. ztSource = 0;
  35. if ((ztDestination !== 0)&&((ztDestination & 0xff00000000) !== 0xff00000000)) {
  36. socket.send(message,0,message.length,remotePort,remoteIp);
  37. }
  38. }
  39. }
  40. function doHousekeeping()
  41. {
  42. }
  43. for(var udpPort=UDP_PORT_START;udpPort<(UDP_PORT_START+UDP_PORT_COUNT)++udpPort) {
  44. var socket = dgram.createSocket('udp4',function(message,rinfo) { onIncomingUdp(socket,message,rinfo.address,rinfo.port); });
  45. socket.on('listening',function() {
  46. console.log('Listening on '+socket.localPort);
  47. udpSocketPool.push(socket);
  48. }
  49. socket.on('error',function() {
  50. console.log('Error listening on '+socket.localPort);
  51. socket.close();
  52. })
  53. socket.bind(udpPort);
  54. }
  55. server = http.createServer(function(request,response) {
  56. console.log(request.socket.remoteAddress+" "+request.method+" "+request.url);
  57. try {
  58. // /<proxy token>/<ignored>/...
  59. var urlSp = request.url.split('/');
  60. if ((urlSp.length >= 3)&&(udpSocketPool.length > 0)) {
  61. var token = urlSp[1]; // urlSp[0] == '' since URLs start with /
  62. if (token.length >= 8) {
  63. var client = clients[token];
  64. if (!Array.isArray(client)) {
  65. client = [ Date.now(),udpSocketPool[Math.floor(Math.random() * udpSocketPool.length)] ];
  66. clients[token] = client;
  67. } else client[0] = Date.now();
  68. if (request.method === "GET") {
  69. // /<proxy token>/<ignored> ... waits via old skool long polling
  70. } else if (request.method === "POST") {
  71. // /<proxy token>/<ignored>/<dest ip>/<dest port>
  72. if (urlSp.length === 5) {
  73. var ipSp = urlSp[3].split('.');
  74. var port = parseInt(urlSp[4],10);
  75. // Note: do not allow the use of this proxy to talk to privileged ports
  76. if ((ipSp.length === 4)&&(port >= 1024)&&(port <= 0xffff)) {
  77. var ip = [ parseInt(ipSp[0]),parseInt(ipSp[1]),parseInt(ipSp[2]),parseInt(ipSp[3]) ];
  78. if ( (ip[0] > 0)
  79. &&(ip[0] < 240)
  80. &&(ip[0] !== 127)
  81. &&(ip[1] >= 0)
  82. &&(ip[1] <= 255)
  83. &&(ip[2] >= 0)
  84. &&(ip[2] <= 255)
  85. &&(ip[3] > 0)
  86. &&(ip[3] < 255) ) {
  87. var postData = null;
  88. request.on('data',function(chunk) {
  89. postData = ((postData === null) ? chunk : Buffer.concat([ postData,chunk ]));
  90. });
  91. request.on('end',function() {
  92. if (postData !== null)
  93. onOutgoingUdp(token,client[1],postData,urlSp[3],port);
  94. response.writeHead(200,{'Content-Length':0,'Pragma':'no-cache','Cache-Control':'no-cache'});
  95. response.end();
  96. });
  97. return; // no 400 -- read from stream
  98. } // else 400
  99. } // else 400
  100. } // else 400
  101. } // else 400
  102. } // else 400
  103. } // else 400
  104. } catch (e) {} // 400
  105. response.writeHead(400,{'Content-Length':0,'Pragma':'no-cache','Cache-Control':'no-cache'});
  106. response.end();
  107. return;
  108. });
  109. setInterval(doHousekeeping,5000);
  110. server.setTimeout(120000);
  111. server.listen(HTTP_PORT);