Skip to content

Commit

Permalink
Merge pull request #154 from SelfhostedPro/patch-1
Browse files Browse the repository at this point in the history
Update ssh.js to add error handling.
  • Loading branch information
apocas authored Oct 10, 2023
2 parents 8ac7608 + 78838be commit e575864
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions lib/ssh.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
var Client = require('ssh2').Client,
http = require('http');

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

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

fn(null, stream);

stream.once('close', () => {
conn.end();
agent.destroy();
fn(null, stream);

stream.addListener('error', (err) => {
handleError(err);
});
stream.once('close', () => {
conn.end();
agent.destroy();
});
});
});
}).connect(opt);

conn.once('end', () => agent.destroy());
}).on('error', (err) => {
handleError(err);
})
.connect(opt);
conn.once('end', () => agent.destroy());
return agent;

} catch (err) {
handleError(err);
}
};

return agent;
function handleError(err) {
conn.end();
agent.destroy();
throw err;
}
};

0 comments on commit e575864

Please sign in to comment.