Guest User

Untitled

a guest
Aug 27th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. var fs = require("fs");
  2. var pty = require("pty.js");
  3. var path = require("path");
  4.  
  5. var ssh2 = require("ssh2");
  6. function noop(_v) {}
  7.  
  8. new ssh2.Server(
  9. {
  10. hostKeys: [
  11. fs.readFileSync(
  12. path.resolve(__dirname, "../data/ssh/ssh_host_rsa_key")
  13. )
  14. ]
  15. },
  16. function(client) {
  17. console.log("Client connected!");
  18.  
  19. client
  20. .on("authentication", function(ctx) {
  21. if (
  22. ctx.method === "password" &&
  23. ctx.username === "root" &&
  24. ctx.password === "root"
  25. ) {
  26. ctx.accept();
  27. } else {
  28. ctx.reject();
  29. }
  30. })
  31. .on("ready", function() {
  32. console.log("Client authenticated!");
  33.  
  34. client.on("session", function(accept, _reject) {
  35. var session = accept();
  36. var ptyInfo;
  37.  
  38. session.once("pty", function(accept, _reject, info) {
  39. ptyInfo = info;
  40. accept && accept();
  41. });
  42.  
  43. session.once("shell", function(accept, _reject, _info) {
  44. var stream = accept();
  45. stream.rows = ptyInfo.rows;
  46. stream.columns = ptyInfo.columns;
  47. stream.isTTY = true;
  48. stream.setRawMode = noop;
  49. stream.on("error", noop);
  50.  
  51. var term = pty.spawn("node", ["src/app.js"], {
  52. name: "xterm-color",
  53. cols: ptyInfo.columns,
  54. rows: ptyInfo.rows
  55. });
  56.  
  57. term.on("close", () => {
  58. stream.end();
  59. });
  60.  
  61. stream.stdout.pipe(term.stdin);
  62. term.stdout.pipe(
  63. stream.stdin,
  64. { end: false }
  65. );
  66. });
  67. });
  68. })
  69. .on("end", function() {
  70. console.log("Client disconnected");
  71. });
  72. }
  73. ).listen(2222, "127.0.0.1", function() {
  74. console.log("Listening on port " + this.address().port);
  75. });
Add Comment
Please, Sign In to add comment