gpt4 book ai didi

javascript - ssh2 : can't run the server on node js

转载 作者:行者123 更新时间:2023-12-03 01:20:57 24 4
gpt4 key购买 nike

如果这个问题重复,我很抱歉,但我找不到解决方案。

看来我的问题很简单,但我不明白我做错了什么。我想设置 ssh 客户端来连接它并在 shell 中执行一些命令。所以第一件事 - 我选择了 ssh2 库并尝试使用 following code .

var fs = require("fs");
var crypto = require("crypto");
var inspect = require("util").inspect;

var buffersEqual = require("buffer-equal-constant-time");
var ssh2 = require("ssh2");
var utils = ssh2.utils;

var pubKey = utils.genPublicKey(
utils.parseKey(fs.readFileSync("user.pub")),
);

new ssh2.Server(
{
hostKeys: [fs.readFileSync("host.key")],
},
function(client) {
console.log("Client connected!");

client
.on("authentication", function(ctx) {
if (
ctx.method === "password" &&
// Note: Don't do this in production code, see
// https://www.brendanlong.com/timing-attacks-and-usernames.html
// In node v6.0.0+, you can use `crypto.timingSafeEqual()` to safely
// compare two values.
ctx.username === "foo" &&
ctx.password === "bar"
)
ctx.accept();
else if (
ctx.method === "publickey" &&
ctx.key.algo === pubKey.fulltype &&
buffersEqual(ctx.key.data, pubKey.public)
) {
if (ctx.signature) {
var verifier = crypto.createVerify(ctx.sigAlgo);
verifier.update(ctx.blob);
if (verifier.verify(pubKey.publicOrig, ctx.signature))
ctx.accept();
else ctx.reject();
} else {
// if no signature present, that means the client is just checking
// the validity of the given public key
ctx.accept();
}
} else ctx.reject();
})
.on("ready", function() {
console.log("Client authenticated!");

client.on("session", function(accept, reject) {
var session = accept();
session.once("exec", function(accept, reject, info) {
console.log(
"Client wants to execute: " + inspect(info.command),
);
var stream = accept();
stream.stderr.write("Oh no, the dreaded errors!\n");
stream.write("Just kidding about the errors!\n");
stream.exit(0);
stream.end();
});
});
})
.on("end", function() {
console.log("Client disconnected");
});
},
).listen(0, "127.0.0.1", function() {
console.log("Listening on port " + this.address().port);
});

我将其放入index.js 文件中。使用此代码之前:

  1. 我通过命令 ssh-keygen -t rsa -b 4096 设置 key 对,得到 2 个 key ssh.pubssh
  2. 然后重命名为user.pubhost.key
  3. 之后我制作了ssh-add host.key。响应是已添加身份:
    主机 key (主机 key )
    。看起来一切都很好。

我运行 node index.js 并出现以下错误:

抛出新错误('缺少加密私钥的密码')

我做错了什么?也许创建 key 对的命令错误?如果您能帮助我,我将不胜感激。

最佳答案

所以问题源于这样一个事实:您(正如您在评论中承认的那样)正在使用受密码保护的主机 key 。 ssh2自然也需要密码。

文档说

hostKeys - array - An array of either Buffers/strings that contain host private keys or objects in the format of { key: <Buffer/string>, passphrase: <string> } for encrypted private keys. (Required)

所以尝试一下

{
hostKeys: [
{
key: fs.readFileSync('...'),
passphrase: 'the-passphrase-you-used',
},
],
}
<小时/>

没有密码

我无法重现此问题(使用更简单的代码):

$ yarn add ssh2
$ cat > index.js

var fs = require("fs");
var crypto = require("crypto");
var inspect = require("util").inspect;

var ssh2 = require("ssh2");
var utils = ssh2.utils;

new ssh2.Server(
{
hostKeys: [fs.readFileSync("host.key")],
},
function(client) {
console.log("Client connected!");

client
.on("authentication", function(ctx) {
ctx.accept();
})
.on("ready", function() {
console.log("Client authenticated!");
client.on("session", function(accept, reject) {
var session = reject();
console.log("Client session rejected.");
});
})
.on("end", function() {
console.log("Client disconnected");
});
},
).listen(0, "127.0.0.1", function() {
console.log("Listening on port " + this.address().port);
});

$ ssh-keygen -t rsa -b 1024
Generating public/private rsa key pair.
Enter file in which to save the key: ./host
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ./host.
Your public key has been saved in ./host.pub.
$ mv host host.key
$ node index.js
Listening on port 52182
Client connected!
Client authenticated!
Client session rejected.
Client disconnected

(我的 ssh 连接尝试是:)

$ ssh foo@127.0.0.1 -p 52182
channel 0: open failed: administratively prohibited:
Connection to 127.0.0.1 closed.
~ $

关于javascript - ssh2 : can't run the server on node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51768342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com