UNPKG

allproxy

Version:

AllProxy: MITM HTTP Debugging Tool.

129 lines 6.16 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 }); const Message_1 = require("../../common/Message"); const SocketIoMessage_1 = __importDefault(require("./SocketIoMessage")); const Global_1 = __importDefault(require("./Global")); class HttpMessage { constructor(messageProtocol, proxyConfig, sequenceNumber, remoteAddress, method, url, reqHeaders) { this.emitCount = 0; this.sequenceNumber = 0; this.getHttpEndpoint = (method, url, requestBody) => { let endpoint = url.split('?')[0]; const tokens = endpoint.split('/'); endpoint = tokens ? tokens[tokens.length - 1] : ''; // Include the last 2 path segments, if this looks like and id (e.g., '/items/111222') if (tokens && tokens.length > 1 /*&& tokens[tokens.length - 2].endsWith('s')*/) { endpoint = tokens[tokens.length - 2] + '/' + tokens[tokens.length - 1]; } // GraphQL? if (method !== 'OPTIONS' && (url.endsWith('/graphql') || url.endsWith('/graphql-public'))) { endpoint = ''; if (typeof requestBody === 'object') { if (Array.isArray(requestBody)) { requestBody.forEach((entry) => { if (entry.operationName) { if (endpoint.length > 0) endpoint += ','; endpoint += ' ' + entry.operationName; } }); } else { for (const key in requestBody) { if (key === 'operationName') { if (endpoint.length > 0) endpoint += ','; endpoint += ' ' + requestBody[key]; } } } } const tag = url.endsWith('/graphql-public') ? 'GQLP' : 'GQL'; endpoint = ' ' + tag + endpoint; } if (url === '/') endpoint = ''; return endpoint; }; this.startTime = Date.now(); this.messageProtocol = messageProtocol; this.proxyConfig = proxyConfig; this.sequenceNumber = sequenceNumber; this.remoteAddress = remoteAddress; this.method = method; this.url = url; this.reqHeaders = reqHeaders; this.reqHeaders = this.sortObjectKeys(this.reqHeaders); } emitMessageToBrowser(reqBody = '', resStatus = 0, resHeaders = {}, resBody = Message_1.NO_RESPONSE) { return __awaiter(this, void 0, void 0, function* () { const message = yield this.buildMessage(reqBody, resStatus, resHeaders, resBody); this.emitMessageToBrowser2(message); }); } emitMessageToBrowser2(message) { return __awaiter(this, void 0, void 0, function* () { Global_1.default.socketIoManager.emitMessageToBrowser(message.responseBody === Message_1.NO_RESPONSE ? Message_1.MessageType.REQUEST : this.emitCount === 0 ? Message_1.MessageType.REQUEST_AND_RESPONSE : Message_1.MessageType.RESPONSE, message, this.proxyConfig); ++this.emitCount; }); } buildMessage(reqBody = '', resStatus = 0, resHeaders = {}, resBody = Message_1.NO_RESPONSE) { return __awaiter(this, void 0, void 0, function* () { const reqBodyJson = typeof reqBody === 'object' ? reqBody : this.toJSON(reqBody); const resBodyJson = resBody === Message_1.NO_RESPONSE || typeof resBody === 'object' ? resBody : this.toJSON(resBody); const host = this.proxyConfig ? this.getHostPort(this.proxyConfig, this.reqHeaders) : 'Unknown'; const message = yield SocketIoMessage_1.default.buildRequest(Date.now(), this.sequenceNumber, this.reqHeaders, this.method, this.url, this.getHttpEndpoint(this.method, this.url, reqBodyJson), reqBodyJson, this.remoteAddress, host, // server host this.proxyConfig ? this.proxyConfig.path : '', Date.now() - this.startTime); message.protocol = this.messageProtocol; resHeaders = this.sortObjectKeys(resHeaders); SocketIoMessage_1.default.appendResponse(message, resHeaders, resBodyJson, resStatus, Date.now() - this.startTime); return message; }); } toJSON(s) { try { return JSON.parse(s); } catch (e) { return s; } } getHostPort(proxyConfig, reqHeaders) { if (proxyConfig.hostname && proxyConfig.hostname.length > 0) { let host = proxyConfig.hostname; if (proxyConfig.port) host += ':' + proxyConfig.port; return host; } else { return reqHeaders.host; } } sortObjectKeys(o) { const out = {}; const keys = Object.keys(o).sort(); for (const key of keys) { out[key] = o[key]; } return out; } } exports.default = HttpMessage; //# sourceMappingURL=HttpMessage.js.map