UNPKG

allproxy

Version:

AllProxy: MITM HTTP Debugging Tool.

197 lines 9.05 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 }); const path_1 = __importDefault(require("path")); const Paths_1 = __importDefault(require("./Paths")); const fs_1 = __importDefault(require("fs")); const Global_1 = __importDefault(require("./Global")); const GrpcProxy_1 = __importDefault(require("./GrpcProxy")); const ProxyConfig_1 = __importStar(require("../../common/ProxyConfig")); const ConsoleLog_1 = __importDefault(require("./ConsoleLog")); const portToGrpcProxyMap = new Map(); const AllProxyApp = (clientReq, clientRes, reqUrl) => { var _a; let responseSent = true; const clientBuildDir = Paths_1.default.clientDir() + Paths_1.default.sep() + 'build'; switch (clientReq.method) { case 'GET': if (reqUrl.pathname === '/' + 'allproxy' || reqUrl.pathname === '/' + 'mitmproxy') { clientRes.writeHead(200, { 'content-type': 'text/html' }); clientRes.end(fs_1.default.readFileSync(clientBuildDir + Paths_1.default.sep() + 'index.html')); } else if (reqUrl.pathname === '/logviewer' || reqUrl.pathname === '/jlogviewer') { clientRes.writeHead(200, { 'content-type': 'text/html' }); clientRes.end(fs_1.default.readFileSync(clientBuildDir + Paths_1.default.sep() + 'index.html')); Global_1.default.renderLogView = true; } else { const dir = clientBuildDir + ((_a = reqUrl.pathname) === null || _a === void 0 ? void 0 : _a.replace(/\//g, Paths_1.default.sep())); // File exists locally? if (reqUrl.protocol === null && fs_1.default.existsSync(dir) && fs_1.default.lstatSync(dir).isFile()) { const extname = path_1.default.extname(reqUrl.pathname); let contentType = 'text/html'; switch (extname) { case '.js': contentType = 'text/javascript'; break; case '.css': contentType = 'text/css'; break; case '.json': contentType = 'application/json'; break; case '.png': contentType = 'image/png'; break; case '.jpg': case '.ico': contentType = 'image/jpg'; break; case '.wav': contentType = 'audio/wav'; break; } // Read local file and return to client clientRes.writeHead(200, { 'content-type': contentType }); clientRes.end(fs_1.default.readFileSync(clientBuildDir + reqUrl.pathname)); } else if (reqUrl.protocol === null && reqUrl.pathname === '/api/allproxy/config') { Global_1.default.socketIoManager.updateHostReachable() .then((configs) => { clientRes.writeHead(200, { 'content-type': 'application/json' }); clientRes.end(JSON.stringify(configs, null, 2)); }); } else { responseSent = false; } } break; case 'POST': if (reqUrl.protocol === null && reqUrl.pathname === '/api/allproxy/grpc-proxy') { ConsoleLog_1.default.info('POST' + reqUrl.pathname); getReqBody(clientReq) .then((reqBody) => { ConsoleLog_1.default.info(reqBody); if (!reqBody.server || reqBody.server.indexOf(':') === -1) { clientRes.writeHead(400, { "content-type": "application/json; charset=utf-8" }); clientRes.write(JSON.stringify({ error: "server field is invalid or missing" })); clientRes.end(); return; } const config = new ProxyConfig_1.default(); config.protocol = 'grpc:'; config.comment = ProxyConfig_1.DYNAMICALLY_ADDED; config.recording = true; config.path = '0'; // dynamic port const [host, port] = reqBody.server.split(':'); config.hostname = host; config.port = parseInt(port); GrpcProxy_1.default.reverseProxy(config) .then(port => { portToGrpcProxyMap.set(port + '', config); clientRes.writeHead(201, { "content-type": "application/json; charset=utf-8" }); clientRes.write(Buffer.from(JSON.stringify({ id: port + "" }), 'utf-8')); clientRes.end(); }); }); } else { responseSent = false; } break; case 'DELETE': if (reqUrl.protocol === null && reqUrl.pathname.startsWith('/api/allproxy/grpc-proxy/')) { ConsoleLog_1.default.info('DELETE' + reqUrl.pathname); const tokens = reqUrl.pathname.split('/'); const id = tokens[tokens.length - 1]; const config = portToGrpcProxyMap.get(id); if (config) { GrpcProxy_1.default.destructor(config); portToGrpcProxyMap.delete(id); clientRes.writeHead(204); clientRes.end(); } else { clientRes.writeHead(404, { "content-type": "application/json; charset=utf-8" }); clientRes.write(JSON.stringify({ error: "No GRPC proxy found for port " + id })); clientRes.end(); } } else { responseSent = false; } break; default: responseSent = false; } return responseSent; }; function getReqBody(clientReq) { return new Promise(resolve => { let requestBody = {}; clientReq.setEncoding('utf8'); let rawData = ''; clientReq.on('data', function (chunk) { rawData += chunk; }); clientReq.on('end', function () { return __awaiter(this, void 0, void 0, function* () { try { requestBody = JSON.parse(rawData); } catch (e) { } resolve(requestBody); }); }); }); } exports.default = AllProxyApp; //# sourceMappingURL=AllProxyApp.js.map