我有一个使用JSCH lib用Java编写的客户端SSH服务器,它将端口从客户端转发到ssh服务器,如This JSCH client ,现在我想要一个ssh服务器,它将接受从NODEJS客户端转发的端口!(我已经阅读了有关SSH2和SSH模块的文档,但是没有任何关于接受端口的服务器),我能够创建一个服务器(使用ssh2 module Nodejs)和客户端也连接但不接受转发的Port.Below是服务器代码。
var webSocketPort=20;
var fs = require('fs'),
crypto = require('crypto'),
inspect = require('util').inspect;
var buffersEqual = require('buffer-equal-constant-time'),
ssh2 = require('ssh2'),
utils = ssh2.utils;
var pubKey = utils.genPublicKey(utils.parseKey(fs.readFileSync('C:\\Program Files\\OpenSSH\\etc\\ssh_host_rsa_key.pub')));
new ssh2.Server({
hostKeys: [fs.readFileSync('C:\\Program Files\\OpenSSH\\etc\\ssh_host_rsa_key')]
}, function(client) {
console.log('Client connected!',client);
client.on('authentication', function(ctx) {
if (ctx.method === 'password'
|| ctx.username === '418374'
|| ctx.password === 'hiandroid8@3') {
ctx.accept();
console.log("inside userpwd")
}
else if (ctx.method === 'publickey'
&& ctx.key.algo === pubKey.fulltype
&& buffersEqual(ctx.key.data, pubKey.public)) {
console.log("inside publicKey")
if (ctx.signature) {
console.log("inside signature")
var verifier = crypto.createVerify(ctx.sigAlgo);
verifier.update(ctx.blob);
if (verifier.verify(pubKey.publicOrig, ctx.signature))
ctx.accept();
else
ctx.reject();
} else {
console.log("inside nthing")
// 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) {
console.log('Client Sssio!');
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();
});
});
client.on('request', function(accept, reject, name,info,a) {
console.log('accept',accept)
console.log('reject',reject)
console.log('info',info)
console.log('name',name)
if(name==="tcpip-forward"){
//info.bindAddr='localhost';
}
console.log('infoafgter',info)
var session = accept();
console.log('tcpIp');
})
function reExec(i) {
if (i === 3)
return;
client.forwardOut('0.0.0.0', 3000, 'localhost', 8080, function(err, stream) {
if (err)
console.log(err);
else
stream.end();
reExec(++i);
});
}
reExec(0);
}).on('error',function(e){
console.log("error occcured",e)
}).on('end', function() {
console.log('Client disconnected');
});
}).listen(webSocketPort, '0.0.0.0', function() {
console.log('Listening on port ' + webSocketPort);
});
答案 0 :(得分:2)
在这里回答:
Create a Node SSH2 Server with ability to treat Remote Forwarding
let fs = require('fs'),
inspect = require('util').inspect,
ssh2 = require('ssh2'),
net = require('net');
new ssh2.Server({
hostKeys: [fs.readFileSync('/etc/ssh/ssh_host_rsa_key')]
}, client => {
console.log('Client connected!');
client
.on('authentication', ctx => {
if (
ctx.method === 'password'
&& ctx.username === 'foo'
&& ctx.password === 'bar'
) {
ctx.accept();
} else {
ctx.reject();
}
})
.on('ready', () => {
console.log('Client authenticated!');
client
.on('session', (accept, reject) => {
let session = accept();
session.on('shell', function(accept, reject) {
let stream = accept();
});
})
.on('request', (accept, reject, name, info) => {
if (name === 'tcpip-forward') {
accept();
net.createServer(function(socket) {
socket.setEncoding('utf8');
client.forwardOut(
info.bindAddr, info.bindPort,
socket.remoteAddress, socket.remotePort,
(err, upstream) => {
if (err) {
socket.end();
return console.error('not working: ' + err);
}
upstream.pipe(socket).pipe(upstream);
});
}).listen(info.bindPort);
} else {
reject();
}
});
});
}).listen(21, '0.0.0.0', function() {
console.log('Listening on port ' + this.address().port);
});