forked from agebrock/tunnel-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (77 loc) · 2.61 KB
/
index.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var net = require('net');
var _ = require('lodash');
var Connection = require('ssh2');
function createConfig(userConfig) {
var env = process.env;
var config = _.defaults(userConfig || {}, {
username: env.TUNNELSSH_USER || env.USER || env.USERNAME,
sshPort: 22,
srcPort: 0,
srcHost: 'localhost',
dstPort: null,
dstHost: 'localhost',
localHost: 'localhost'
});
if (!config.password && !config.privateKey) {
config.agent = config.agent || process.env.SSH_AUTH_SOCK;
}
if (!config.dstPort || !config.dstHost || !config.host) {
throw new Error('invalid configuration.')
}
if (config.localPort === undefined) {
config.localPort = config.dstPort;
}
return config;
}
function bindSSHConnection(config, server, netConnection) {
var sshConnection = new Connection();
sshConnection.on('error', function(err) {
server.emit('error', err);
sshConnection.end();
netConnection.end();
server.close();
});
server.emit('sshConnection', sshConnection, netConnection, server);
sshConnection.on('ready', function() {
sshConnection.forwardOut(
config.srcHost,
config.srcPort,
config.dstHost,
config.dstPort, function(err, sshStream) {
if (err) {
throw err;
}
sshStream.once('close', function() {
if (!config.keepAlive) {
sshConnection.end();
netConnection.end();
server.close();
}
});
server.emit('sshStream', sshStream, sshConnection, netConnection, server);
netConnection.pipe(sshStream).pipe(netConnection);
});
});
return sshConnection;
}
function createListener(server) {
server._conns = [];
server.on('sshConnection', function(sshConnection, netConnection, server) {
server._conns.push(sshConnection, netConnection);
});
server.on('close', function() {
server._conns.forEach(function(connection) {
connection.end();
});
});
return server;
}
function tunnel(configArgs, callback) {
var config = createConfig(configArgs);
var server = net.createServer(function(netConnection) {
server.emit('netConnection', netConnection, server);
bindSSHConnection(config, server, netConnection).connect(config);
});
return createListener(server).listen(config.localPort, config.localHost, callback);
}
module.exports = tunnel;