UNPKG

allproxy

Version:

AllProxy: MITM HTTP Debugging Tool.

211 lines 8.88 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.run = exports.dateToHHMMSS = void 0; const fs_1 = __importDefault(require("fs")); const ConsoleLog_1 = __importDefault(require("./ConsoleLog")); const Paths_1 = __importDefault(require("./Paths")); const fs_2 = require("./interceptors/util/fs"); const spawn = require('child_process').spawn; const rmdir = require('rimraf'); const { rgPath } = require('@vscode/ripgrep'); function dateToHHMMSS(d) { if (isNaN(d.getMonth()) || isNaN(d.getDate())) { return "Invalid Date"; } const monthDay = d.getMonth() + 1 + '/' + d.getDate(); return monthDay + ' ' + d.getHours().toString().padStart(2, '0') + ':' + d.getMinutes().toString().padStart(2, '0') + ':' + d.getSeconds().toString().padStart(2, '0'); } exports.dateToHHMMSS = dateToHHMMSS; class APFileSystem { constructor(socket) { this.socket = socket; this.log('constructor'); } log(method, ...args) { if (process.env.FILE_SYSTEM_LOG === '1') { const time = dateToHHMMSS(new Date()); const u = this.socket.handshake.url; const urlPath = u.includes('allproxy') || u.includes('mitmproxy') || u.includes('jlogviewer') ? u : ''; console.log(time, this.socket.handshake.address, urlPath, method, ...args); } } toPlatformPath(path) { if (Paths_1.default.sep() === "\\") { path = path.replace(/:/g, '-'); } return Paths_1.default.platform(Paths_1.default.getDataDir() + path); } listen() { this.log('listen'); ConsoleLog_1.default.debug('APFileSystem.listen'); // mkdir this.socket.on('mkdir', (path) => { this.log('mkdir', path); const dir = this.toPlatformPath(path); ConsoleLog_1.default.debug('ApFileSystem.mkdir', dir); fs_1.default.mkdirSync(dir); }); // rmdir this.socket.on('rmdir', (path) => { this.log('rmdir', path); const platformPath = this.toPlatformPath(path); ConsoleLog_1.default.debug('ApFileSystem.rmdir', platformPath); rmdir(platformPath, () => { }); }); // writeFile this.socket.on('appendFile', (path, data, ack) => { this.log('appendFile', path); try { const file = this.toPlatformPath(path); ConsoleLog_1.default.debug('ApFileSystem.appendFile', file); fs_1.default.writeFileSync(file, data, { flag: 'a' }); } catch (e) { console.log('afFileSystem.appendFile', e); } ack(); }); // writeFile this.socket.on('writeFile', (path, data, ack) => { this.log('writeFile', path); try { const file = this.toPlatformPath(path); ConsoleLog_1.default.debug('ApFileSystem.writeFile', file); fs_1.default.writeFileSync(file, data, { flag: 'w' }); } catch (e) { console.log('afFileSystem.writeFile', e); } ack(); }); // deleteFile this.socket.on('deleteFile', (path, ack) => { this.log('deleteFile', path); try { const file = this.toPlatformPath(path); ConsoleLog_1.default.debug('ApFileSystem.deleteFile', file); fs_1.default.rmSync(file); } catch (e) { console.log('afFileSystem.deleteFile', e); } ack(); }); // renameFile this.socket.on('renameFile', (oldPath, newPath, ack) => { this.log('renameFile', oldPath, newPath); try { const oldFile = this.toPlatformPath(oldPath); const newFile = this.toPlatformPath(newPath); ConsoleLog_1.default.debug('ApFileSystem.renameFile', oldFile, newFile); fs_1.default.renameSync(oldFile, newFile); } catch (e) { console.log('afFileSystem.renameFile', e); } ack(); }); // exists this.socket.on('exists', (path, callback) => { this.log('exists', path); try { const platformPath = this.toPlatformPath(path); ConsoleLog_1.default.debug('ApFileSystem.exists', platformPath); callback(fs_1.default.existsSync(platformPath)); } catch (e) { callback(false); } }); // readDir this.socket.on('readDir', (path, callback) => { this.log('readDir', path); try { const platformPath = this.toPlatformPath(path); const files = fs_1.default.readdirSync(platformPath); ConsoleLog_1.default.debug('ApFileSystem.readDir', platformPath, files); callback(files); } catch (e) { console.log('afFileSystem.readDir', e); } }); // grepDir - find all files in a directory with a matching string this.socket.on('grepDir', (path, match, callback) => __awaiter(this, void 0, void 0, function* () { this.log('grepDir', path, match); try { const subDir = Paths_1.default.platform(path); const dontUseParallel = true; let output; if (!dontUseParallel && (yield (0, fs_2.commandExists)('parallel'))) { output = yield run(`find ${subDir} -type f -print0 | parallel -0 ${rgPath} -l -m 1 "${match}" {}`, Paths_1.default.getDataDir()); } else { output = yield run(`find ${subDir} -type f -exec ${rgPath} -l -m 1 "${match}" {} +`, Paths_1.default.getDataDir()); } const files = output.toString().split('\n'); files.pop(); ConsoleLog_1.default.debug('ApFileSystem.grepDir', subDir, match, files); callback(files); } catch (e) { console.log('afFileSystem.grepDir', e); } })); // readFile this.socket.on('readFile', (path, offset, chunkSize, callback) => { if (offset === 0) this.log('readFile', path); try { const file = this.toPlatformPath(path); const mode = 'win32' ? 'r' : 444; const fd = fs_1.default.openSync(file, mode); const data = Buffer.alloc(chunkSize); const read = fs_1.default.readSync(fd, data, 0, chunkSize, offset); fs_1.default.closeSync(fd); const size = fs_1.default.statSync(file).size; const eof = offset + chunkSize >= size; callback(data.subarray(0, read).toString(), eof); ConsoleLog_1.default.debug('ApFileSystem.readFile', file, offset, chunkSize, eof, data.toString()); } catch (e) { console.log('afFileSystem.readFile', e); } }); } } exports.default = APFileSystem; function run(command, cwd) { return __awaiter(this, void 0, void 0, function* () { //console.log(command); let response = ''; return yield new Promise(resolve => { const tokens = command.split(' '); const p = spawn(tokens[0], tokens.slice(1), { cwd, shell: true }); p.stdout.on('data', (data) => { //console.log(data.toString()); response += data.toString(); }); p.stderr.on('data', (data) => { console.error(data.toString()); }); p.on('exit', () => { resolve(response); }); }); }); } exports.run = run; //# sourceMappingURL=APFileSystem.js.map