Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Connection, ClientInfo, SFTP_STATUS_CODE } from "ssh2";
  2. import { authenticate } from "../api/auth";
  3. import { FileEntry } from "ssh2-streams";
  4. import { Status } from "../utils";
  5. import * as fs from "fs";
  6. import * as path from "path";
  7.  
  8. interface Handle {
  9.     path: string,
  10.     done: boolean
  11. }
  12.  
  13. const onConnect = (client: Connection, info: ClientInfo) => {
  14.     console.log("Client connected");
  15.     let uuid = null;
  16.  
  17.     client.on("authentication", ctx => {
  18.         if (ctx.method !== "password")
  19.             return ctx.reject(["password"]);
  20.  
  21.         const auth = authenticate(ctx.username, ctx.password);
  22.  
  23.         uuid = auth.uuid;
  24.         (auth.status === Status.SUCCESS) ? ctx.accept() : ctx.reject();
  25.     }).on("ready", () => { onAuthenticated(client, uuid)() });
  26. }
  27.  
  28. const onAuthenticated = (client: Connection, uuid: string) => {
  29.     return () => {
  30.         console.log(`Client '${uuid}' authenticated`);
  31.         client.on("session", (accept, reject) => {
  32.             const session = accept();
  33.  
  34.             session.on("sftp", (accept, reject) => {
  35.                 const stream = accept();
  36.                 let handles = {};
  37.                 let handlesCount = 0;
  38.  
  39.                 stream.on("LSTAT", () => console.log("LSTAT"));
  40.                 stream.on("FSETSTAT", () => console.log("FSETSTAT"));
  41.                 stream.on("SETSTAT", () => console.log("SETSTAT"));
  42.                 stream.on("FSTAT", () => console.log("FSTAT"));
  43.                 stream.on("STAT", () => console.log("STAT"));
  44.                 stream.on("OPEN", () => console.log("OPEN"));
  45.  
  46.                 const readdir = (reqID: number, handle: Buffer) => {
  47.                     console.log(`READDIR, ${reqID}`);
  48.                     const selectedHandle = <Handle>handles[handle.toString()];
  49.  
  50.                     if (selectedHandle.done) {
  51.                         stream.status(reqID, SFTP_STATUS_CODE.EOF);
  52.                         return;
  53.                     }
  54.  
  55.                     handles[handle.toString()].done = true;
  56.                     const files = fs.readdirSync("test").map(fileName => <FileEntry>{
  57.                         filename: fileName,
  58.                         longname: fileName,
  59.                         attrs: {
  60.                             mode: fs.constants.S_IFDIR  
  61.                         }
  62.                     });
  63.  
  64.                     stream.name(reqID, files);
  65.                 }
  66.  
  67.                 // READDIR
  68.                 stream.on("READDIR", readdir);
  69.  
  70.                 const realpath = (reqID: number, customPath: string) => {
  71.                     console.log(`REALPATH, ${customPath}, ${reqID}`)
  72.                     stream.name(reqID, [
  73.                         {
  74.                             filename: "/",
  75.                             longname: "/",
  76.                             attrs: {
  77.                                 mode: fs.constants.S_IFDIR,
  78.                                 gid: 0,
  79.                                 uid: 0,
  80.                                 size: 0,
  81.                                 atime: 0,
  82.                                 mtime: 0
  83.                             }
  84.                         }
  85.                     ]);
  86.                 }
  87.  
  88.                 // REALPATH >> OPENDIR
  89.                 stream.on("REALPATH", realpath);
  90.  
  91.                 const opendir = (reqID: number, customPath: string) => {
  92.                     console.log(`OPENDIR, ${customPath}, ${reqID}`);
  93.  
  94.                     const buffer = Buffer.from(handlesCount.toString());
  95.                     handles[handlesCount] = <Handle>{
  96.                         path: customPath,
  97.                         done: false
  98.                     }
  99.  
  100.                     handlesCount += 1;
  101.                     stream.handle(reqID, buffer);
  102.                 }
  103.  
  104.                 // OPENDIR >> READDIR
  105.                 stream.on("OPENDIR", opendir);
  106.             });
  107.         });
  108.     }
  109. }
  110.  
  111. export {
  112.     onConnect,
  113.     onAuthenticated
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement