-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathindex.js
135 lines (111 loc) · 4.08 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const net = require('net');
const { Client } = require('ssh2');
const os = require('os');
function autoClose(server, connection) {
connection.on('close', () => {
server.getConnections((error, count) => {
if (count === 0) {
server.close();
}
});
});
}
async function createServer(options) {
let serverOptions = Object.assign({}, options);
if (!serverOptions.port && !serverOptions.path) {
serverOptions = null;
}
return new Promise((resolve, reject) => {
let server = net.createServer();
let errorHandler = function (error) {
reject(error);
};
server.on('error', errorHandler);
process.on('uncaughtException', errorHandler);
server.listen(serverOptions);
server.on('listening', () => {
process.removeListener('uncaughtException', errorHandler);
resolve(server);
});
});
}
async function createSSHConnection(config) {
return new Promise(function (resolve, reject) {
let conn = new Client();
conn.on('ready', () => resolve(conn));
conn.on('error', reject);
conn.connect(config);
});
}
async function createTunnel( tunnelOptions, serverOptions, sshOptions, forwardOptions ) {
let sshOptionslocal = Object.assign({ port: 22, username: 'root' }, sshOptions);
let forwardOptionsLocal = Object.assign({ dstAddr: '0.0.0.0' }, forwardOptions);
let tunnelOptionsLocal = Object.assign({ autoClose: false, reconnectOnError: false }, tunnelOptions || {});
let server, sshConnection;
return new Promise(async function (resolve, reject) {
try {
sshConnection = await createSSHConnection(sshOptionslocal);
addListenerSshConnection(sshConnection);
} catch (e) {
if (server) {
server.close()
}
return reject(e);
}
try {
server = await createServer(serverOptions);
addListenerServer(server);
} catch (e) {
return reject(e);
}
function addListenerSshConnection(sshConnection) {
if (tunnelOptionsLocal.reconnectOnError) {
sshConnection.on('error', async () => {
sshConnection.isBroken = true;
sshConnection = await createSSHConnection(sshOptionslocal);
addListenerSshConnection(sshConnection);
});
}
}
function addListenerServer(server) {
if (tunnelOptionsLocal.reconnectOnError) {
server.on('error', async () => {
server = await createServer(serverOptions);
addListenerServer(server);
});
}
server.on('connection', onConnectionHandler);
server.on('close', () => sshConnection.end());
}
function onConnectionHandler(clientConnection) {
if (!forwardOptionsLocal.srcPort) {
forwardOptionsLocal.srcPort = server.address().port;
}
if (!forwardOptionsLocal.srcAddr) {
forwardOptionsLocal.srcAddr = server.address().address;
}
if (tunnelOptionsLocal.autoClose) {
autoClose(server, clientConnection);
}
if (sshConnection.isBroken) {
return;
}
sshConnection.forwardOut(
forwardOptionsLocal.srcAddr,
forwardOptionsLocal.srcPort,
forwardOptionsLocal.dstAddr,
forwardOptionsLocal.dstPort, (err, stream) => {
if (err) {
if (server) {
server.close()
}
sshConnection.emit("error", err);
} else {
clientConnection.pipe(stream).pipe(clientConnection);
}
});
}
resolve([server, sshConnection]);
});
}
exports.createTunnel = createTunnel;