kopia lustrzana https://github.com/c9/core
commit
912d0dd70e
|
@ -12,7 +12,22 @@ var fs = require("fs");
|
||||||
var tmp = require("tmp");
|
var tmp = require("tmp");
|
||||||
var debug = require("debug")("ssh");
|
var debug = require("debug")("ssh");
|
||||||
|
|
||||||
exports.buildArgs = function(prvkeyFile, host) {
|
function quote(str) {
|
||||||
|
return "'" + str.replace(/'/g, "'\\''") + "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
function addProxyCommand(args, proxy) {
|
||||||
|
var m = /^(.+)(?::(\d+))?$/.exec(proxy);
|
||||||
|
if (!m)
|
||||||
|
return;
|
||||||
|
var proxyHost = m[1];
|
||||||
|
var proxyPort = parseInt(m[2], 10) || 22;
|
||||||
|
args.push(
|
||||||
|
"-o", "ProxyCommand=ssh -W %h:%p -p " + proxyPort + " " + quote(proxyHost)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.buildArgs = function(prvkeyFile, host, proxy) {
|
||||||
var args = [
|
var args = [
|
||||||
"-o", "PasswordAuthentication=no",
|
"-o", "PasswordAuthentication=no",
|
||||||
"-o", "IdentityFile=" + prvkeyFile,
|
"-o", "IdentityFile=" + prvkeyFile,
|
||||||
|
@ -27,17 +42,21 @@ exports.buildArgs = function(prvkeyFile, host) {
|
||||||
"-o", "ConnectTimeout=10" // default timeout is 2 minutes, which is quite long
|
"-o", "ConnectTimeout=10" // default timeout is 2 minutes, which is quite long
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if (proxy)
|
||||||
|
addProxyCommand(args, proxy);
|
||||||
|
|
||||||
if (host) {
|
if (host) {
|
||||||
host = host.split(":");
|
host = host.split(":");
|
||||||
args.push("-p", host[1] || 22);
|
args.push("-p", host[1] || 22);
|
||||||
args.push(host[0]);
|
args.push(host[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return args;
|
return args;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.spawnWithKeyFile = function(prvkeyFile, host, command, args) {
|
exports.spawnWithKeyFile = function(prvkeyFile, host, proxy, command, args) {
|
||||||
var sshArgs = exports.buildArgs(prvkeyFile, host);
|
var sshArgs = exports.buildArgs(prvkeyFile, host, proxy);
|
||||||
|
|
||||||
args = sshArgs.concat(command ? [command] : []).concat(args || []);
|
args = sshArgs.concat(command ? [command] : []).concat(args || []);
|
||||||
debug("executing: ssh " + args.join(" "));
|
debug("executing: ssh " + args.join(" "));
|
||||||
|
@ -81,11 +100,11 @@ exports.writeKeyFiles = function(prvkey, pubkey, callback) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.spawn = function(prvkey, host, command, args, callback) {
|
exports.spawn = function(prvkey, host, proxy, command, args, callback) {
|
||||||
exports.writeKeyFile(prvkey, function(err, filename) {
|
exports.writeKeyFile(prvkey, function(err, filename) {
|
||||||
if (err) return callback(err);
|
if (err) return callback(err);
|
||||||
|
|
||||||
var child = exports.spawnWithKeyFile(filename, host, command, args);
|
var child = exports.spawnWithKeyFile(filename, host, proxy, command, args);
|
||||||
|
|
||||||
child.on("exit", function(code) {
|
child.on("exit", function(code) {
|
||||||
fs.unlink(filename, function() {});
|
fs.unlink(filename, function() {});
|
||||||
|
@ -95,8 +114,8 @@ exports.spawn = function(prvkey, host, command, args, callback) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.exec = function(prvkey, host, command, args, callback) {
|
exports.exec = function(prvkey, host, proxy, command, args, callback) {
|
||||||
exports.spawn(prvkey, host, command, args, function(err, child) {
|
exports.spawn(prvkey, host, proxy, command, args, function(err, child) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
|
@ -148,8 +167,8 @@ exports.generateKeyPair = function(email, callback) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.validateSSHKey = function(prvkey, host, callback) {
|
exports.validateSSHKey = function(prvkey, host, proxy, callback) {
|
||||||
exports.exec(prvkey, host, "", [], function(err, stdout, stderr) {
|
exports.exec(prvkey, host, proxy, "", [], function(err, stdout, stderr) {
|
||||||
debug("out >> " + stdout);
|
debug("out >> " + stdout);
|
||||||
debug("err >> " + stderr);
|
debug("err >> " + stderr);
|
||||||
debug(err);
|
debug(err);
|
||||||
|
|
Ładowanie…
Reference in New Issue