Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require("fs");
- var pty = require("pty.js");
- var path = require("path");
- var ssh2 = require("ssh2");
- function noop(_v) {}
- new ssh2.Server(
- {
- hostKeys: [
- fs.readFileSync(
- path.resolve(__dirname, "../data/ssh/ssh_host_rsa_key")
- )
- ]
- },
- function(client) {
- console.log("Client connected!");
- client
- .on("authentication", function(ctx) {
- if (
- ctx.method === "password" &&
- ctx.username === "root" &&
- ctx.password === "root"
- ) {
- ctx.accept();
- } else {
- ctx.reject();
- }
- })
- .on("ready", function() {
- console.log("Client authenticated!");
- client.on("session", function(accept, _reject) {
- var session = accept();
- var ptyInfo;
- session.once("pty", function(accept, _reject, info) {
- ptyInfo = info;
- accept && accept();
- });
- session.once("shell", function(accept, _reject, _info) {
- var stream = accept();
- stream.rows = ptyInfo.rows;
- stream.columns = ptyInfo.columns;
- stream.isTTY = true;
- stream.setRawMode = noop;
- stream.on("error", noop);
- var term = pty.spawn("node", ["src/app.js"], {
- name: "xterm-color",
- cols: ptyInfo.columns,
- rows: ptyInfo.rows
- });
- term.on("close", () => {
- stream.end();
- });
- stream.stdout.pipe(term.stdin);
- term.stdout.pipe(
- stream.stdin,
- { end: false }
- );
- });
- });
- })
- .on("end", function() {
- console.log("Client disconnected");
- });
- }
- ).listen(2222, "127.0.0.1", function() {
- console.log("Listening on port " + this.address().port);
- });
Add Comment
Please, Sign In to add comment