mirror of
https://github.com/vale981/autossh
synced 2025-03-05 09:21:40 -05:00
polls port to verify that connection is established
This commit is contained in:
parent
e819b26796
commit
540cae824f
2 changed files with 90 additions and 20 deletions
46
index.js
46
index.js
|
@ -38,6 +38,10 @@ var AutoSSH = function (_EventEmitter) {
|
|||
_this.remotePort = conf.remotePort;
|
||||
_this.localPort = conf.localPort || 'auto';
|
||||
|
||||
_this.debounceCount = 0;
|
||||
_this.maxDebounceCount = 25;
|
||||
_this.debounceTimeout = 50;
|
||||
|
||||
setImmediate(function () {
|
||||
var confErrors = _this.getConfErrors(conf);
|
||||
|
||||
|
@ -54,6 +58,7 @@ var AutoSSH = function (_EventEmitter) {
|
|||
_this.localPort = freePort;
|
||||
|
||||
_this.execTunnel(function () {
|
||||
_this.debounceConnection(function () {
|
||||
_this.emit('connect', {
|
||||
kill: _this.kill,
|
||||
pid: _this.currentProcess.pid,
|
||||
|
@ -65,6 +70,7 @@ var AutoSSH = function (_EventEmitter) {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
process.on('exit', function () {
|
||||
_this.kill();
|
||||
|
@ -73,6 +79,36 @@ var AutoSSH = function (_EventEmitter) {
|
|||
}
|
||||
|
||||
_createClass(AutoSSH, [{
|
||||
key: 'debounceConnection',
|
||||
value: function debounceConnection(cb) {
|
||||
var _this2 = this;
|
||||
|
||||
if (this.debounceCount >= this.maxDebounceCount) {
|
||||
this.emit('error', 'Max debounce count reached. Aborting...');
|
||||
return this.kill();
|
||||
}
|
||||
|
||||
this.isConnectionEstablished(function (result) {
|
||||
if (result) return cb();
|
||||
console.log('Not Ready... Debounce!!!');
|
||||
setTimeout(function () {
|
||||
_this2.debounceCount++;
|
||||
_this2.debounceConnection(cb);
|
||||
}, _this2.debounceTimeout);
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'isConnectionEstablished',
|
||||
value: function isConnectionEstablished(cb) {
|
||||
var _this3 = this;
|
||||
|
||||
_portfinder2.default.getPort({ port: this.localPort }, function (err, freePort) {
|
||||
if (err) return cb(false);
|
||||
|
||||
if (_this3.localPort === freePort) return cb(false);else return cb(true);
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'getConfErrors',
|
||||
value: function getConfErrors(conf) {
|
||||
var errors = [];
|
||||
|
@ -104,17 +140,17 @@ var AutoSSH = function (_EventEmitter) {
|
|||
}, {
|
||||
key: 'execTunnel',
|
||||
value: function execTunnel(cb) {
|
||||
var _this2 = this;
|
||||
var _this4 = this;
|
||||
|
||||
this.currentProcess = (0, _child_process.exec)(this.generateExecString(), function (err, stdout, stderr) {
|
||||
if (/Address already in use/i.test(stderr)) {
|
||||
_this2.kill();
|
||||
return _this2.emit('error', stderr);
|
||||
_this4.kill();
|
||||
return _this4.emit('error', stderr);
|
||||
}
|
||||
|
||||
if (err) _this2.emit('error', err);
|
||||
if (err) _this4.emit('error', err);
|
||||
|
||||
if (!_this2.killed) _this2.execTunnel(function () {
|
||||
if (!_this4.killed) _this4.execTunnel(function () {
|
||||
return console.log('Restarting autossh...');
|
||||
});
|
||||
});
|
||||
|
|
36
src/index.js
36
src/index.js
|
@ -13,6 +13,10 @@ class AutoSSH extends EventEmitter {
|
|||
this.remotePort = conf.remotePort;
|
||||
this.localPort = conf.localPort || 'auto';
|
||||
|
||||
this.pollCount = 0;
|
||||
this.maxPollCount = 25;
|
||||
this.pollTimeout = 50;
|
||||
|
||||
setImmediate(() => {
|
||||
const confErrors = this.getConfErrors(conf);
|
||||
|
||||
|
@ -30,6 +34,7 @@ class AutoSSH extends EventEmitter {
|
|||
this.localPort = freePort;
|
||||
|
||||
this.execTunnel(() => {
|
||||
this.debounceConnection(() => {
|
||||
this.emit('connect', {
|
||||
kill: this.kill,
|
||||
pid: this.currentProcess.pid,
|
||||
|
@ -39,7 +44,7 @@ class AutoSSH extends EventEmitter {
|
|||
localPort: this.localPort
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -48,6 +53,35 @@ class AutoSSH extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
debounceConnection(cb) {
|
||||
if (this.pollCount >= this.maxPollCount) {
|
||||
this.emit('error', 'Max debounce count reached. Aborting...');
|
||||
return this.kill();
|
||||
}
|
||||
|
||||
this.isConnectionEstablished(result => {
|
||||
if (result)
|
||||
return cb();
|
||||
console.log('Not Ready... Debounce!!!');
|
||||
setTimeout(() => {
|
||||
this.pollCount++;
|
||||
this.debounceConnection(cb);
|
||||
}, this.pollTimeout);
|
||||
});
|
||||
}
|
||||
|
||||
isConnectionEstablished(cb) {
|
||||
portfinder.getPort({ port: this.localPort }, (err, freePort) => {
|
||||
if (err)
|
||||
return cb(false);
|
||||
|
||||
if (this.localPort === freePort)
|
||||
return cb(false);
|
||||
else
|
||||
return cb(true);
|
||||
});
|
||||
}
|
||||
|
||||
getConfErrors(conf) {
|
||||
const errors = [];
|
||||
if (!conf.localPort)
|
||||
|
|
Loading…
Add table
Reference in a new issue