Skip to content

Commit

Permalink
ssh minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
apocas committed Sep 16, 2019
1 parent c66aa6f commit 8937fc4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
10 changes: 2 additions & 8 deletions lib/modem.js
Expand Up @@ -69,7 +69,6 @@ var Modem = function(options) {
this.socketPath = opts.socketPath;
this.host = opts.host;
this.port = opts.port;
this.ssh = opts.ssh;
this.version = opts.version;
this.key = opts.key;
this.cert = opts.cert;
Expand Down Expand Up @@ -177,11 +176,6 @@ Modem.prototype.dial = function(options, callback) {
optionsf.headers.Upgrade = 'tcp';
}

if(this.protocol === 'ssh'){
this.ssh.host = this.host;
this.ssh.port = this.port;
}

if (this.socketPath) {
optionsf.socketPath = this.socketPath;
} else {
Expand All @@ -197,8 +191,8 @@ Modem.prototype.dial = function(options, callback) {
Modem.prototype.buildRequest = function(options, context, data, callback) {
var self = this;
var opts = self.protocol === 'ssh' ? Object.assign(options, {
agent: ssh(self.ssh),
protocol: 'http:'
agent: ssh({'host': self.host, 'port': self.port}),
protocol: 'http'
}) : options;

var req = http[self.protocol === 'ssh' ? 'http': self.protocol].request(opts, function() {});
Expand Down
50 changes: 25 additions & 25 deletions lib/ssh.js
@@ -1,30 +1,30 @@
var Client = require('ssh2').Client,
http = require('http');
http = require('http');

module.exports = function(opt){
var conn = new Client();
var agent = new http.Agent();

agent.createConnection = function(options, fn){
conn.on('ready', function() {
conn.exec('docker system dial-stdio', function(err, stream){
if(err){
conn.end();
agent.destroy();
return ;
}

fn(null, stream);
module.exports = function(opt) {
var conn = new Client();
var agent = new http.Agent();

stream.on('close', () => {
conn.end();
agent.destroy();
})
});
}).connect(opt);
agent.createConnection = function(options, fn) {
conn.on('ready', function() {
conn.exec('docker system dial-stdio', function(err, stream) {
if(err){
conn.end();
agent.destroy();
return ;
}

conn.on('end', () => agent.destroy());
}
fn(null, stream);

return agent;
};
stream.on('close', () => {
conn.end();
agent.destroy();
})
});
}).connect(opt);

conn.on('end', () => agent.destroy());
}

return agent;
};

0 comments on commit 8937fc4

Please sign in to comment.