Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Connection, ClientInfo, SFTP_STATUS_CODE } from "ssh2";
- import { authenticate } from "../api/auth";
- import { FileEntry } from "ssh2-streams";
- import { Status } from "../utils";
- import * as fs from "fs";
- import * as path from "path";
- interface Handle {
- path: string,
- done: boolean
- }
- const onConnect = (client: Connection, info: ClientInfo) => {
- console.log("Client connected");
- let uuid = null;
- client.on("authentication", ctx => {
- if (ctx.method !== "password")
- return ctx.reject(["password"]);
- const auth = authenticate(ctx.username, ctx.password);
- uuid = auth.uuid;
- (auth.status === Status.SUCCESS) ? ctx.accept() : ctx.reject();
- }).on("ready", () => { onAuthenticated(client, uuid)() });
- }
- const onAuthenticated = (client: Connection, uuid: string) => {
- return () => {
- console.log(`Client '${uuid}' authenticated`);
- client.on("session", (accept, reject) => {
- const session = accept();
- session.on("sftp", (accept, reject) => {
- const stream = accept();
- let handles = {};
- let handlesCount = 0;
- stream.on("LSTAT", () => console.log("LSTAT"));
- stream.on("FSETSTAT", () => console.log("FSETSTAT"));
- stream.on("SETSTAT", () => console.log("SETSTAT"));
- stream.on("FSTAT", () => console.log("FSTAT"));
- stream.on("STAT", () => console.log("STAT"));
- stream.on("OPEN", () => console.log("OPEN"));
- const readdir = (reqID: number, handle: Buffer) => {
- console.log(`READDIR, ${reqID}`);
- const selectedHandle = <Handle>handles[handle.toString()];
- if (selectedHandle.done) {
- stream.status(reqID, SFTP_STATUS_CODE.EOF);
- return;
- }
- handles[handle.toString()].done = true;
- const files = fs.readdirSync("test").map(fileName => <FileEntry>{
- filename: fileName,
- longname: fileName,
- attrs: {
- mode: fs.constants.S_IFDIR
- }
- });
- stream.name(reqID, files);
- }
- // READDIR
- stream.on("READDIR", readdir);
- const realpath = (reqID: number, customPath: string) => {
- console.log(`REALPATH, ${customPath}, ${reqID}`)
- stream.name(reqID, [
- {
- filename: "/",
- longname: "/",
- attrs: {
- mode: fs.constants.S_IFDIR,
- gid: 0,
- uid: 0,
- size: 0,
- atime: 0,
- mtime: 0
- }
- }
- ]);
- }
- // REALPATH >> OPENDIR
- stream.on("REALPATH", realpath);
- const opendir = (reqID: number, customPath: string) => {
- console.log(`OPENDIR, ${customPath}, ${reqID}`);
- const buffer = Buffer.from(handlesCount.toString());
- handles[handlesCount] = <Handle>{
- path: customPath,
- done: false
- }
- handlesCount += 1;
- stream.handle(reqID, buffer);
- }
- // OPENDIR >> READDIR
- stream.on("OPENDIR", opendir);
- });
- });
- }
- }
- export {
- onConnect,
- onAuthenticated
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement