tunnel-ssh
Advanced tools
Comparing version 0.0.5 to 1.0.0
@@ -1,24 +0,22 @@ | ||
var Tunnel = require('../'); | ||
var tunnel = require('../'); | ||
var config = { | ||
remotePort: 27017, //localport | ||
localPort: 27017, //remoteport | ||
verbose: true, // dump information to stdout | ||
sshConfig: { //ssh2 configuration | ||
host: '<yourRemoteIp>', | ||
port: 22, | ||
username: 'root', | ||
privateKey: require('fs').readFileSync('<pathToKeyFile>'), | ||
passphrase: 'verySecretString' // option see ssh2 config | ||
} | ||
host: '93.180.157.151', | ||
username: 'root', | ||
dstPort: 27017, | ||
localPort: 3000 | ||
}; | ||
var x = new Tunnel(config); | ||
x.connect(function (error) { | ||
console.log(error); | ||
// start useing the tunnel | ||
// Bsp. Try mongo shell "#mongo" | ||
var server = tunnel(config, function () { | ||
console.log('connected'); | ||
}); | ||
server.on('sshStream', function (sshStream, sshConnection, netConnection, server) { | ||
sshStream.on('close', function () { | ||
console.log('TCP :: CLOSED'); | ||
netConnection.end(); | ||
server.close(); | ||
}); | ||
}); | ||
147
index.js
@@ -0,110 +1,73 @@ | ||
var net = require('net'); | ||
var debug = require('debug')('tunnel-ssh'); | ||
var _ = require('lodash'); | ||
var Connection = require('ssh2'); | ||
var net = require('net'); | ||
function createConfig(userConfig) { | ||
var env = process.env; | ||
function SSHTunnel(config, callback) { | ||
var self = this; | ||
self._verbose = config.verbose || false; | ||
self._config = config; | ||
if (callback) { | ||
self.connect(callback); | ||
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; | ||
} | ||
} | ||
SSHTunnel.prototype.log = function () { | ||
if (this._verbose) { | ||
console.log.apply(null, arguments); | ||
if (!config.dstPort || !config.dstHost || !config.host) { | ||
throw new Error('invalid configuration.') | ||
} | ||
}; | ||
SSHTunnel.prototype.close = function (callback) { | ||
var self = this; | ||
this.server.close(function (error) { | ||
self.connection.end(); | ||
if (callback) { | ||
callback(error); | ||
} | ||
}); | ||
}; | ||
SSHTunnel.prototype.connect = function (callback) { | ||
var self = this, | ||
disabled = self._config.disabled, | ||
remotePort = self._config.remotePort, | ||
localPort = self._config.localPort, | ||
remoteHost = self._config.remoteHost || '127.0.0.1'; | ||
if(disabled){ | ||
return callback(null); | ||
if (!config.localPort) { | ||
config.localPort = config.dstPort; | ||
} | ||
var c = self.connection = new Connection(); | ||
c.on('ready', function () { | ||
return config; | ||
} | ||
self.server = net.createServer(function (connection) { | ||
var buffers = []; | ||
function bindSSHConnection(config, server, netConnection) { | ||
var addBuffer = function (data) { | ||
buffers.push(data); | ||
}; | ||
connection.on('data', addBuffer); | ||
c.forwardOut('', 0, remoteHost, remotePort, function (error, ssh) { | ||
if (error){ | ||
// close connection | ||
connection.removeAllListeners(); | ||
connection.end(); | ||
c.emit('error', error); | ||
return; | ||
var sshConnection = new Connection(); | ||
sshConnection.on('ready', function () { | ||
//sshConnection._sock.unref(); | ||
//server.unref(); | ||
server.emit('sshConnection', sshConnection, netConnection, server); | ||
sshConnection.forwardOut( | ||
config.srcHost, | ||
config.srcPort, | ||
config.dstHost, | ||
config.dstPort, function (err, sshStream) { | ||
if (err) { | ||
throw err; | ||
} | ||
while (buffers.length) { | ||
ssh.write(buffers.shift()); | ||
} | ||
connection.removeListener('data', addBuffer); | ||
ssh.on('data', function (buf) { | ||
connection.write(buf); | ||
sshStream.once('close', function () { | ||
sshConnection.end(); | ||
if (!config.keepAlive) { | ||
netConnection.end(); | ||
server.close(); | ||
} | ||
}); | ||
connection.on('data', function (buf) { | ||
ssh.write(buf); | ||
}); | ||
connection.on('end', function () { | ||
self.log('connection::end'); | ||
ssh.removeAllListeners(); | ||
ssh.end(); | ||
}); | ||
ssh.on('end', function () { | ||
self.log('ssh::end'); | ||
connection.removeAllListeners(); | ||
connection.end(); | ||
}); | ||
server.emit('sshStream', sshStream, sshConnection, netConnection, server); | ||
netConnection.pipe(sshStream).pipe(netConnection); | ||
}); | ||
}); | ||
self.server.listen(localPort, callback); | ||
// handle error when self.server.listen fails (e.g. port is already used) | ||
self.server.on('error', function(err){ | ||
callback(err); | ||
self.connection.end(); | ||
}); | ||
}); | ||
return sshConnection; | ||
} | ||
c.on('error', function (err) { | ||
self.log('ssh2::error:: ' + err); | ||
callback(err); | ||
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) | ||
}); | ||
c.on('end', function () { | ||
self.log('ssh2::end'); | ||
}); | ||
c.on('close', function (err) { | ||
self.log('ssh2::close'); | ||
}); | ||
server.listen(config.localPort, config.localHost, callback); | ||
return server; | ||
} | ||
c.connect(self._config.sshConfig); | ||
}; | ||
module.exports = SSHTunnel; | ||
module.exports = tunnel; |
{ | ||
"name": "tunnel-ssh", | ||
"version": "0.0.5", | ||
"description": "Easy ssh tunneling", | ||
"version": "1.0.0", | ||
"description": "Easy extendable SSH tunnel", | ||
"main": "index.js", | ||
"scripts": { | ||
"scripts": {}, | ||
"repository": { | ||
"type":"git", | ||
"url":"https://github.com/Finanzchef24-GmbH/tunnel-ssh" | ||
}, | ||
"keywords": [ | ||
"tunnel", | ||
"ssh" | ||
"tunnel", | ||
"ssh", | ||
"mysql" | ||
], | ||
"author": "christoph.hagenbrock@googlemail.com", | ||
"author": { | ||
"name": "Christoph Hagenbrock", | ||
"email": "christoph.hagenbrock@googlemail.com" | ||
}, | ||
"license": "ISC", | ||
"dependencies": { | ||
"ssh2": "^0.2.23" | ||
"debug": "^2.1.3", | ||
"lodash": "^3.5.0", | ||
"ssh2": "^0.2.25" | ||
} | ||
} |
Tunnel-SSH | ||
========== | ||
Simple SSH tunneling in node.js | ||
This is a public test of the upcoming release 1.0.0 | ||
## Install ## | ||
npm install tunnel-ssh | ||
## Example (connect to remote mongo) ## | ||
## Examples ## | ||
See the examples for further configuration options. | ||
The config object is passed to the ssh2 config, | ||
if you have problems connecting to a server check the [ssh2](https://github.com/mscdex/ssh2) | ||
docs. | ||
Minimal: | ||
```js | ||
var tunnel = require('tunnel-ssh'); | ||
var config = { | ||
remotePort: 27017, //localport | ||
localPort: 27017, //remoteport | ||
verbose: true, // dump information to stdout | ||
disabled: false, //set this to true to disable tunnel (useful to keep architecture for local connections) | ||
sshConfig: { //ssh2 configuration (https://github.com/mscdex/ssh2) | ||
host: '<yourRemoteIp>', | ||
port: 22, | ||
username: 'root', | ||
privateKey: require('fs').readFileSync('<pathToKeyFile>'), | ||
passphrase: 'verySecretString' // option see ssh2 config | ||
} | ||
host: '172.16.0.8', | ||
dstPort: 3306 | ||
}; | ||
var server = tunnel(config, function (error, result) { | ||
//you can start using your resources here. (mongodb, mysql, ....) | ||
console.log('connected'); | ||
}); | ||
``` | ||
var tunnel = new Tunnel(config); | ||
tunnel.connect(function (error) { | ||
console.log(error); | ||
//or start your remote connection here .... | ||
//mongoose.connect(...); | ||
Map local port to 3000 | ||
```js | ||
//close tunnel to exit script | ||
tunnel.close(); | ||
}); | ||
## Example (create a tunnel for mysql) ## | ||
```javascript | ||
var config = { | ||
remoteHost: '<mysql server host>', // mysql server host | ||
remotePort: 3306, // mysql server port | ||
localPort: 33333, // a available local port | ||
verbose: true, // dump information to stdout | ||
disabled: false, //set this to true to disable tunnel (useful to keep architecture for local connections) | ||
sshConfig: { //ssh2 configuration (https://github.com/mscdex/ssh2) | ||
host: '<ssh server host>', | ||
port: 22, | ||
username: 'root', | ||
privateKey: require('fs').readFileSync('<pathToKeyFile>'), | ||
passphrase: 'verySecretString' // option see ssh2 config | ||
} | ||
host: '172.16.0.8', | ||
username: 'root', | ||
dstPort: 3306, | ||
localPort: 3000 | ||
}; | ||
var tunnel = new Tunnel(config); | ||
tunnel.connect(function (error) { | ||
console.log(error); | ||
//or start your remote connection here .... | ||
//mongoose.connect(...); | ||
//close tunnel to exit script | ||
tunnel.close(); | ||
var server = tunnel(config, function (error, result) { | ||
console.log('connected'); | ||
}); | ||
``` | ||
mysql config | ||
```javascript | ||
mysql_config = { | ||
host: '127.0.0.1', | ||
port: '33333', | ||
user: 'xxx', | ||
password: 'xxx' | ||
} | ||
``` | ||
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7202
9
178
1
3
45
4
2
+ Addeddebug@^2.1.3
+ Addedlodash@^3.5.0
+ Addeddebug@2.6.9(transitive)
+ Addedlodash@3.10.1(transitive)
+ Addedms@2.0.0(transitive)
Updatedssh2@^0.2.25