-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh-tunnel.ts
More file actions
49 lines (40 loc) · 1.15 KB
/
ssh-tunnel.ts
File metadata and controls
49 lines (40 loc) · 1.15 KB
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
import { SSHSession, SSHTunnel } from '../lib';
async function main() {
const session = new SSHSession({
host: 'example.com',
port: 22,
user: 'username',
autoDetectAgent: true
});
try {
console.log('Connecting to SSH server...');
await session.connect();
console.log('Authenticating...');
await session.authenticate({ useAgent: true });
console.log('Creating tunnel...');
const tunnel = new SSHTunnel({
session,
localHost: '127.0.0.1',
localPort: 3307, // Local MySQL port
remoteHost: 'localhost',
remotePort: 3306 // Remote MySQL port
});
await tunnel.start();
const address = tunnel.getLocalAddress();
console.log(`Tunnel started! Connect to localhost:${address?.port}`);
console.log('Press Ctrl+C to stop...');
// Keep running until interrupted
process.on('SIGINT', async () => {
console.log('\nStopping tunnel...');
await tunnel.stop();
await session.disconnect();
console.log('Stopped!');
process.exit(0);
});
} catch (err) {
console.error('Error:', err);
await session.disconnect();
process.exit(1);
}
}
main();