mirror of
https://github.com/vale981/autossh
synced 2025-03-04 09:01:37 -05:00
initial commit
This commit is contained in:
commit
91b27e11bd
6 changed files with 191 additions and 0 deletions
3
.babelrc
Normal file
3
.babelrc
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"presets": ["es2015"]
|
||||
}
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
node_modules
|
||||
*.log
|
17
demo.js
Normal file
17
demo.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
'use strict';
|
||||
const autossh = require('./index.js');
|
||||
|
||||
|
||||
autossh({
|
||||
host: '104.131.150.215',
|
||||
username: 'same',
|
||||
localPort: 60001,
|
||||
remotePort: 5432
|
||||
})
|
||||
.on('error', err => {
|
||||
console.error('ERROR: ', err);
|
||||
})
|
||||
.on('init', () => {
|
||||
console.log('connected.');
|
||||
});
|
||||
|
81
index.js
Normal file
81
index.js
Normal file
|
@ -0,0 +1,81 @@
|
|||
'use strict';
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||
|
||||
var _child_process = require('child_process');
|
||||
|
||||
var _events = require('events');
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
||||
|
||||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
||||
|
||||
var SshTunnel = function (_EventEmitter) {
|
||||
_inherits(SshTunnel, _EventEmitter);
|
||||
|
||||
function SshTunnel() {
|
||||
var conf = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
||||
var cb = arguments[1];
|
||||
|
||||
_classCallCheck(this, SshTunnel);
|
||||
|
||||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(SshTunnel).call(this));
|
||||
|
||||
_this.host = conf.host;
|
||||
_this.username = conf.username;
|
||||
_this.remotePort = conf.remotePort;
|
||||
_this.localPort = conf.localPort;
|
||||
|
||||
setImmediate(function () {
|
||||
if (!conf.host) return _this.emit('error', 'Missing host');
|
||||
if (!conf.username) return _this.emit('error', 'Missing username');
|
||||
if (!conf.remotePort) return _this.emit('error', 'Missing remotePort');
|
||||
if (!conf.localPort) return _this.emit('error', 'Missing localPort');
|
||||
|
||||
_this.execTunnel();
|
||||
_this.emit('init');
|
||||
process.on('exit', function () {
|
||||
_this.kill();
|
||||
});
|
||||
});
|
||||
return _this;
|
||||
}
|
||||
|
||||
_createClass(SshTunnel, [{
|
||||
key: 'execTunnel',
|
||||
value: function execTunnel() {
|
||||
var _this2 = this;
|
||||
|
||||
var host = this.host;
|
||||
var username = this.username;
|
||||
var localPort = this.localPort;
|
||||
var remotePort = this.remotePort;
|
||||
this.currentProcess = (0, _child_process.exec)('ssh -NL ' + localPort + ':localhost:' + remotePort + ' ' + username + '@' + host, function (err, stdout, stderr) {
|
||||
console.log('\nerr:', err);
|
||||
console.log('\nstdout:', stdout);
|
||||
console.log('\nstderr:', stderr);
|
||||
|
||||
if (err) _this2.emit('error', err);
|
||||
|
||||
if (!_this2.killed) _this2.execTunnel();
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'kill',
|
||||
value: function kill() {
|
||||
this.killed = true;
|
||||
this.currentProcess.kill();
|
||||
return this;
|
||||
}
|
||||
}]);
|
||||
|
||||
return SshTunnel;
|
||||
}(_events.EventEmitter);
|
||||
|
||||
function autoSSH(conf, cb) {
|
||||
return new SshTunnel(conf, cb);
|
||||
}
|
||||
|
||||
module.exports = autoSSH;
|
28
package.json
Normal file
28
package.json
Normal file
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"name": "autossh",
|
||||
"version": "0.0.1",
|
||||
"description": "Persistent SSH tunnels",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"transpile": "babel src -d ."
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/samueleaton/autossh.git"
|
||||
},
|
||||
"keywords": [
|
||||
"ssh",
|
||||
"tunnel",
|
||||
"autossh"
|
||||
],
|
||||
"author": "Sam Eaton",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/samueleaton/autossh/issues"
|
||||
},
|
||||
"homepage": "https://github.com/samueleaton/autossh#readme",
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.6.5",
|
||||
"babel-preset-es2015": "^6.6.0"
|
||||
}
|
||||
}
|
60
src/index.js
Normal file
60
src/index.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
import { exec } from 'child_process';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
class SshTunnel extends EventEmitter {
|
||||
constructor(conf = {}, cb) {
|
||||
super();
|
||||
this.host = conf.host;
|
||||
this.username = conf.username;
|
||||
this.remotePort = conf.remotePort;
|
||||
this.localPort = conf.localPort;
|
||||
|
||||
setImmediate(() => {
|
||||
if (!conf.host)
|
||||
return this.emit('error', 'Missing host');
|
||||
if (!conf.username)
|
||||
return this.emit('error', 'Missing username');
|
||||
if (!conf.remotePort)
|
||||
return this.emit('error', 'Missing remotePort');
|
||||
if (!conf.localPort)
|
||||
return this.emit('error', 'Missing localPort');
|
||||
|
||||
this.execTunnel();
|
||||
this.emit('init');
|
||||
process.on('exit', () => {
|
||||
this.kill();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
execTunnel() {
|
||||
const host = this.host;
|
||||
const username = this.username;
|
||||
const localPort = this.localPort;
|
||||
const remotePort = this.remotePort;
|
||||
this.currentProcess = exec(`ssh -NL ${localPort}:localhost:${remotePort} ${username}@${host}`,
|
||||
(err, stdout, stderr) => {
|
||||
console.log('\nerr:', err);
|
||||
console.log('\nstdout:', stdout);
|
||||
console.log('\nstderr:', stderr);
|
||||
|
||||
if (err) this.emit('error', err);
|
||||
|
||||
if (!this.killed)
|
||||
this.execTunnel();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
kill() {
|
||||
this.killed = true;
|
||||
this.currentProcess.kill();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
function autoSSH(conf, cb) {
|
||||
return new SshTunnel(conf, cb);
|
||||
}
|
||||
|
||||
module.exports = autoSSH;
|
Loading…
Add table
Reference in a new issue