This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
stdiopipe.js
63 lines (56 loc) · 2.05 KB
/
stdiopipe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const libssh = require('../')
, fs = require('fs')
// connect with: ssh -p 3333 localhost -l '$ecretb@ckdoor'
// password 'nsa'
// or: ssh -p 3333 localhost -i ../test/keys/id_rsa
var server = libssh.createServer({
hostRsaKeyFile : __dirname + '/../test/keys/host_rsa'
, hostDsaKeyFile : __dirname + '/../test/keys/host_dsa'
})
server.on('connection', function (session) {
session.on('auth', function (message) {
if (message.subtype == 'publickey'
&& message.authUser == '$ecretb@ckdoor'
&& message.comparePublicKey(
fs.readFileSync(__dirname + '/../test/keys/id_rsa.pub'))) {
// matching keypair, correct user
return message.replyAuthSuccess()
}
if (message.subtype == 'password'
&& message.authUser == '$ecretb@ckdoor'
&& message.authPassword == 'nsa') {
// correct user, matching password
return message.replyAuthSuccess()
}
message.replyDefault() // auth failed
})
session.on('channel', function (channel) {
channel.on('end', function () {
// current channel ended
})
channel.on('exec', function (message) {
// execute `message.execCommand`
})
channel.on('subsystem', function (message) {
// `message.subsystem` tells you what's requested
// could be 'sftp'
})
channel.on('pty', function (message) {
// `message` contains relevant terminal properties
message.replySuccess()
})
channel.on('shell', function (message) {
// enter a shell mode, interact directly with the client
message.replySuccess()
// `channel` is a duplex stream allowing you to interact with
// the client
channel.write('Welcome to my party!\n')
// lets do a console chat via ssh!
process.stdin // take stdin and pipe it to the channel
.pipe(channel.pipe(channel)) // pipe the channel to itself for an echo
.pipe(process.stdout) // pipe the channel to stdout
})
})
})
server.listen(3333, '127.0.0.1')
console.log('Listening on port 127.0.0.1:3333')