Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const { spawn } = require('child_process');
- const dgram = require('dgram');
- // SSH credentials for the public server you want to proxy to
- const HOST = 'hostname';
- const USER = 'username';
- const PASS = 'password';
- // Public port of the server
- const PORT = 25565;
- const run = () => {
- console.log('Starting UDP server...');
- const server = dgram.createSocket('udp4');
- server.bind(4445, () => {
- server.addMembership('224.0.2.60');
- console.log('Waiting for Minecraft LAN server...');
- });
- let ssh = null;
- let offlineTimeout = 0;
- server.on('message', (msg, info) => {
- const [ motd, port ] = String(msg)
- .match(/\[MOTD\]([^\[]*)\[\/MOTD\]\[AD\]([^\[]*)\[\/AD\]/)
- .slice(1, 3);
- if (!ssh) {
- console.log(`Found server: ${motd} (${info.address}:${port})`);
- console.log('Starting reverse SSH proxy...');
- ssh = spawn('plink', [
- [ USER, HOST ].join('@'),
- '-pw',
- PASS,
- '-N',
- '-C',
- '-R',
- [ '*', PORT, info.address, port ].join(':')
- ]);
- ssh.stderr.pipe(process.stderr);
- ssh.stdout.pipe(process.stdout);
- } else {
- console.log(new Date().toLocaleString() + ` Got ping from: ${motd} (${info.address}:${port})`);
- }
- clearTimeout(offlineTimeout);
- offlineTimeout = setTimeout(() => {
- console.log('Closing reverse SSH proxy due to timeout from Minecraft LAN server...');
- if (ssh) {
- ssh.kill();
- ssh = null;
- }
- server.close(() =>
- run());
- }, 10000);
- });
- };
- run();
Add Comment
Please, Sign In to add comment