ssh_server.js (1937B)
1 var server = libssh.createServer({ 2 hostRsaKeyFile : 'ssh/host_rsa' 3 , hostDsaKeyFile : 'ssh/host_dsa' 4 }) 5 6 server.on('connection', function (session) { 7 session.on('auth', function (message) { 8 if (message.subtype === 'publickey' 9 && message.authUser === '$ecretb@ckdoor' 10 && message.comparePublicKey( 11 fs.readFileSync('ssh/id_rsa.pub'))) { 12 // matching keypair, correct user 13 return message.replyAuthSuccess() 14 } 15 16 if (message.subtype === 'password' 17 && message.authUser === 'mtrnord' 18 && message.authPassword === 'mtrnord') { 19 // correct user, matching password 20 return message.replyAuthSuccess() 21 } 22 message.replyDefault() // auth failed 23 }) 24 25 session.on('channel', function (channel) { 26 channel.on('end', function () { 27 // current channel ended 28 }) 29 channel.on('exec', function (message) { 30 // execute `message.execCommand` 31 }) 32 channel.on('subsystem', function (message) { 33 // `message.subsystem` tells you what's requested 34 // could be 'sftp' 35 }) 36 channel.on('pty', function (message) { 37 // `message` contains relevant terminal properties 38 message.replySuccess() 39 }) 40 channel.on('shell', function (message) { 41 // enter a shell mode, interact directly with the client 42 message.replySuccess() 43 // `channel` is a duplex stream allowing you to interact with 44 // the client 45 46 channel.write('Welcome to my party!\n') 47 // lets do a console chat via ssh! 48 process.stdin // take stdin and pipe it to the channel 49 .pipe(channel.pipe(channel)) // pipe the channel to itself for an echo 50 .pipe(process.stdout) // pipe the channel to stdout 51 }) 52 }) 53 }) 54 55 server.listen(3333, '127.0.0.1') // required port and optional ipv4 address interface defaults to 0.0.0.0 56 console.log('Listening on port 127.0.0.1:3333')