-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtunnel.ts
executable file
·74 lines (55 loc) · 2.01 KB
/
tunnel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import figlet from 'figlet';
import { Command } from 'commander';
const program = new Command();
program
.version('1.0.0')
.description('ShampsTunnel - A customizable SSH reverse tunnel utility')
.option('-u, --user <user>', 'SSH user', 'linuxserver.io')
.option('-h, --host <host>', 'Remote host IP', 'tunnel.shamps.dev')
.option('-r, --remote-port <port>', 'Remote SSH port', '2222')
.option('-t, --tunnel-port <port>', 'Tunnel port on the server', '8080')
.option('-l, --local-port <port>', 'Local port to forward', '5173')
.addHelpText(
'after',
`
Examples:
# Basic usage
$ ./shamps-tunnel.js -u root -h example.com -r 2222 -t 9090 -l 3000
# Use default options
$ ./shamps-tunnel.js
`
);
program.parse(process.argv);
const options = program.opts();
const { user, host, remotePort, tunnelPort, localPort } = options;
const run = () => {
const command = `ssh -v ${user}@${host} -p ${remotePort} -R 0.0.0.0:${tunnelPort}:127.0.0.1:${localPort}`;
const args = ['-v', `${user}@${host}`, '-p', remotePort, '-R', `0.0.0.0:${tunnelPort}:127.0.0.1:${localPort}`];
figlet('ShampsTunnel', (err, data) => {
if (err) {
console.log('Something went wrong...');
console.dir(err);
return;
}
console.log(data);
console.log(`\nRunning tunnel from ${user}@${host}:${remotePort}`);
console.log(`Forwarding remote port ${tunnelPort} to localhost:${localPort}`);
});
console.log(command);
const sshProcess = spawn('ssh', args);
sshProcess.stdout.on('data', (data) => {
console.log(`STDOUT: ${data}`);
});
sshProcess.stderr.on('data', (data) => {
console.error(`STDERR: ${data}`);
});
sshProcess.on('close', (code) => {
console.log(`SSH process exited with code ${code}`);
});
sshProcess.on('error', (error) => {
console.error(`Error: ${error.message}`);
});
};
run();