Guest User

Untitled

a guest
Apr 17th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const { spawn } = require('child_process');
  4. const dgram = require('dgram');
  5.  
  6. // SSH credentials for the public server you want to proxy to
  7. const HOST = 'hostname';
  8. const USER = 'username';
  9. const PASS = 'password';
  10.  
  11. // Public port of the server
  12. const PORT = 25565;
  13.  
  14.  
  15. const run = () => {
  16.  
  17. console.log('Starting UDP server...');
  18. const server = dgram.createSocket('udp4');
  19.  
  20. server.bind(4445, () => {
  21. server.addMembership('224.0.2.60');
  22. console.log('Waiting for Minecraft LAN server...');
  23. });
  24.  
  25. let ssh = null;
  26. let offlineTimeout = 0;
  27.  
  28. server.on('message', (msg, info) => {
  29. const [ motd, port ] = String(msg)
  30. .match(/\[MOTD\]([^\[]*)\[\/MOTD\]\[AD\]([^\[]*)\[\/AD\]/)
  31. .slice(1, 3);
  32.  
  33. if (!ssh) {
  34. console.log(`Found server: ${motd} (${info.address}:${port})`);
  35. console.log('Starting reverse SSH proxy...');
  36. ssh = spawn('plink', [
  37. [ USER, HOST ].join('@'),
  38. '-pw',
  39. PASS,
  40. '-N',
  41. '-C',
  42. '-R',
  43. [ '*', PORT, info.address, port ].join(':')
  44. ]);
  45. ssh.stderr.pipe(process.stderr);
  46. ssh.stdout.pipe(process.stdout);
  47. } else {
  48. console.log(new Date().toLocaleString() + ` Got ping from: ${motd} (${info.address}:${port})`);
  49. }
  50. clearTimeout(offlineTimeout);
  51. offlineTimeout = setTimeout(() => {
  52. console.log('Closing reverse SSH proxy due to timeout from Minecraft LAN server...');
  53. if (ssh) {
  54. ssh.kill();
  55. ssh = null;
  56. }
  57. server.close(() =>
  58. run());
  59. }, 10000);
  60. });
  61. };
  62.  
  63. run();
Add Comment
Please, Sign In to add comment