@requestly/requestly-proxy 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +5 -0
  3. package/dist/components/interfaces/logger-service.d.ts +4 -0
  4. package/dist/components/interfaces/logger-service.js +2 -0
  5. package/dist/components/interfaces/rules-data-source.d.ts +6 -0
  6. package/dist/components/interfaces/rules-data-source.js +2 -0
  7. package/dist/components/proxy-middleware/constants.d.ts +11 -0
  8. package/dist/components/proxy-middleware/constants.js +21 -0
  9. package/dist/components/proxy-middleware/helpers/ctx_rq_namespace.d.ts +35 -0
  10. package/dist/components/proxy-middleware/helpers/ctx_rq_namespace.js +96 -0
  11. package/dist/components/proxy-middleware/helpers/harObectCreator.d.ts +140 -0
  12. package/dist/components/proxy-middleware/helpers/harObectCreator.js +158 -0
  13. package/dist/components/proxy-middleware/helpers/http_helpers.d.ts +2 -0
  14. package/dist/components/proxy-middleware/helpers/http_helpers.js +47 -0
  15. package/dist/components/proxy-middleware/helpers/proxy_ctx_helper.d.ts +14 -0
  16. package/dist/components/proxy-middleware/helpers/proxy_ctx_helper.js +86 -0
  17. package/dist/components/proxy-middleware/helpers/response_helper.d.ts +3 -0
  18. package/dist/components/proxy-middleware/helpers/response_helper.js +17 -0
  19. package/dist/components/proxy-middleware/helpers/rule_processor_helper.d.ts +17 -0
  20. package/dist/components/proxy-middleware/helpers/rule_processor_helper.js +192 -0
  21. package/dist/components/proxy-middleware/index.d.ts +25 -0
  22. package/dist/components/proxy-middleware/index.js +257 -0
  23. package/dist/components/proxy-middleware/middlewares/amiusing_middleware.d.ts +6 -0
  24. package/dist/components/proxy-middleware/middlewares/amiusing_middleware.js +25 -0
  25. package/dist/components/proxy-middleware/middlewares/logger_middleware.d.ts +66 -0
  26. package/dist/components/proxy-middleware/middlewares/logger_middleware.js +89 -0
  27. package/dist/components/proxy-middleware/middlewares/rules_middleware.d.ts +40 -0
  28. package/dist/components/proxy-middleware/middlewares/rules_middleware.js +117 -0
  29. package/dist/components/proxy-middleware/middlewares/ssl_cert_middleware.d.ts +7 -0
  30. package/dist/components/proxy-middleware/middlewares/ssl_cert_middleware.js +33 -0
  31. package/dist/components/proxy-middleware/rule_action_processor/handle_mixed_response.d.ts +14 -0
  32. package/dist/components/proxy-middleware/rule_action_processor/handle_mixed_response.js +78 -0
  33. package/dist/components/proxy-middleware/rule_action_processor/index.d.ts +13 -0
  34. package/dist/components/proxy-middleware/rule_action_processor/index.js +101 -0
  35. package/dist/components/proxy-middleware/rule_action_processor/modified_requests_pool.d.ts +7 -0
  36. package/dist/components/proxy-middleware/rule_action_processor/modified_requests_pool.js +34 -0
  37. package/dist/components/proxy-middleware/rule_action_processor/processors/block_processor.d.ts +6 -0
  38. package/dist/components/proxy-middleware/rule_action_processor/processors/block_processor.js +21 -0
  39. package/dist/components/proxy-middleware/rule_action_processor/processors/delay_processor.d.ts +6 -0
  40. package/dist/components/proxy-middleware/rule_action_processor/processors/delay_processor.js +29 -0
  41. package/dist/components/proxy-middleware/rule_action_processor/processors/insert_processor.d.ts +6 -0
  42. package/dist/components/proxy-middleware/rule_action_processor/processors/insert_processor.js +154 -0
  43. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_header_processor.d.ts +6 -0
  44. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_header_processor.js +69 -0
  45. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_request_processor.d.ts +6 -0
  46. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_request_processor.js +89 -0
  47. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_response_processor.d.ts +6 -0
  48. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_response_processor.js +109 -0
  49. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_user_agent_processor.d.ts +6 -0
  50. package/dist/components/proxy-middleware/rule_action_processor/processors/modify_user_agent_processor.js +25 -0
  51. package/dist/components/proxy-middleware/rule_action_processor/processors/redirect_processor.d.ts +6 -0
  52. package/dist/components/proxy-middleware/rule_action_processor/processors/redirect_processor.js +58 -0
  53. package/dist/components/proxy-middleware/rule_action_processor/utils.d.ts +11 -0
  54. package/dist/components/proxy-middleware/rule_action_processor/utils.js +33 -0
  55. package/dist/components/ssl-proxying/ssl-proxying-manager.d.ts +3 -0
  56. package/dist/components/ssl-proxying/ssl-proxying-manager.js +6 -0
  57. package/dist/constants/cert.d.ts +7 -0
  58. package/dist/constants/cert.js +13 -0
  59. package/dist/index.d.ts +4 -0
  60. package/dist/index.js +11 -0
  61. package/dist/lib/proxy/bin/mitm-proxy.d.ts +2 -0
  62. package/dist/lib/proxy/bin/mitm-proxy.js +30 -0
  63. package/dist/lib/proxy/custom/utils/checkInvalidHeaderChar.d.ts +8 -0
  64. package/dist/lib/proxy/custom/utils/checkInvalidHeaderChar.js +14 -0
  65. package/dist/lib/proxy/index.d.ts +2 -0
  66. package/dist/lib/proxy/index.js +2 -0
  67. package/dist/lib/proxy/lib/ca.d.ts +8 -0
  68. package/dist/lib/proxy/lib/ca.js +277 -0
  69. package/dist/lib/proxy/lib/middleware/gunzip.d.ts +2 -0
  70. package/dist/lib/proxy/lib/middleware/gunzip.js +17 -0
  71. package/dist/lib/proxy/lib/middleware/wildcard.d.ts +1 -0
  72. package/dist/lib/proxy/lib/middleware/wildcard.js +20 -0
  73. package/dist/lib/proxy/lib/proxy.d.ts +36 -0
  74. package/dist/lib/proxy/lib/proxy.js +1217 -0
  75. package/dist/rq-proxy-provider.d.ts +10 -0
  76. package/dist/rq-proxy-provider.js +20 -0
  77. package/dist/rq-proxy.d.ts +15 -0
  78. package/dist/rq-proxy.js +53 -0
  79. package/dist/test.d.ts +1 -0
  80. package/dist/test.js +111 -0
  81. package/dist/types/index.d.ts +12 -0
  82. package/dist/types/index.js +2 -0
  83. package/dist/utils/circularQueue.d.ts +2 -0
  84. package/dist/utils/circularQueue.js +31 -0
  85. package/dist/utils/helpers/rules-helper.d.ts +11 -0
  86. package/dist/utils/helpers/rules-helper.js +59 -0
  87. package/dist/utils/index.d.ts +0 -0
  88. package/dist/utils/index.js +0 -0
  89. package/package.json +43 -0
@@ -0,0 +1,257 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.MIDDLEWARE_TYPE = void 0;
16
+ // Move Proxy code to this package
17
+ var net = require("net");
18
+ const proxy_1 = __importDefault(require("../../lib/proxy"));
19
+ const proxy_ctx_helper_1 = require("./helpers/proxy_ctx_helper");
20
+ const rules_middleware_1 = __importDefault(require("./middlewares/rules_middleware"));
21
+ const amiusing_middleware_1 = __importDefault(require("./middlewares/amiusing_middleware"));
22
+ const logger_middleware_1 = __importDefault(require("./middlewares/logger_middleware"));
23
+ const ssl_cert_middleware_1 = __importDefault(require("./middlewares/ssl_cert_middleware"));
24
+ const ctx_rq_namespace_1 = __importDefault(require("./helpers/ctx_rq_namespace"));
25
+ const http_helpers_1 = require("./helpers/http_helpers");
26
+ const constants_1 = require("./constants");
27
+ const requestly_core_1 = require("@requestly/requestly-core");
28
+ // import SSLProxyingConfigFetcher from "renderer/lib/fetcher/ssl-proxying-config-fetcher";
29
+ // import SSLProxyingManager from "../ssl-proxying/ssl-proxying-manager";
30
+ exports.MIDDLEWARE_TYPE = {
31
+ AMIUSING: "AMIUSING",
32
+ RULES: "RULES",
33
+ LOGGER: "LOGGER",
34
+ SSL_CERT: "SSL_CERT",
35
+ };
36
+ class ProxyMiddlewareManager {
37
+ constructor(proxy, proxyConfig, rulesHelper, loggerService, sslConfigFetcher) {
38
+ this.init_config = (config = {}) => {
39
+ Object.keys(exports.MIDDLEWARE_TYPE).map((middleware_key) => {
40
+ this.config[middleware_key] =
41
+ config[middleware_key] !== undefined ? !!config[middleware_key] : true;
42
+ });
43
+ };
44
+ this.init = (config = {}) => {
45
+ this.init_config(config);
46
+ this.request_handler_idx = 42;
47
+ if (!this.proxy) {
48
+ return;
49
+ }
50
+ this.init_handlers();
51
+ };
52
+ /*
53
+ This is used to init a sequential handler
54
+ */
55
+ this.init_request_handler = (fn, is_detachable = false) => {
56
+ if (is_detachable) {
57
+ const idx = this.request_handler_idx;
58
+ this.proxy.onRequestSetArray(idx, fn);
59
+ this.request_handler_idx++;
60
+ return idx;
61
+ }
62
+ this.proxy.onRequest(fn);
63
+ };
64
+ this.init_amiusing_handler = () => {
65
+ const amiusing_middleware = new amiusing_middleware_1.default(this.config[exports.MIDDLEWARE_TYPE.AMIUSING]);
66
+ this.init_request_handler((ctx, callback) => {
67
+ amiusing_middleware.on_request(ctx);
68
+ callback();
69
+ }, true);
70
+ };
71
+ this.init_ssl_cert_handler = () => {
72
+ const ssl_cert_middleware = new ssl_cert_middleware_1.default(this.config[exports.MIDDLEWARE_TYPE.SSL_CERT], this.proxyConfig.rootCertPath);
73
+ this.init_request_handler((ctx, callback) => {
74
+ ssl_cert_middleware.on_request(ctx);
75
+ callback();
76
+ }, true);
77
+ };
78
+ this.init_main_handler = () => {
79
+ const self = this;
80
+ const is_detachable = true;
81
+ const logger_middleware = new logger_middleware_1.default(this.config[exports.MIDDLEWARE_TYPE.LOGGER], this.loggerService);
82
+ const idx = this.init_request_handler((ctx, callback) => __awaiter(this, void 0, void 0, function* () {
83
+ ctx.rq = new ctx_rq_namespace_1.default();
84
+ ctx.rq.set_original_request((0, proxy_ctx_helper_1.get_request_options)(ctx));
85
+ ctx.proxyToServerRequestOptions.rejectUnauthorized = false;
86
+ // Figure out a way to enable/disable middleware dynamically
87
+ // instead of re-initing this again
88
+ const rules_middleware = new rules_middleware_1.default(this.config[exports.MIDDLEWARE_TYPE.RULES], ctx, this.rulesHelper);
89
+ let request_body_chunks = [];
90
+ ctx.onRequestData(function (ctx, chunk, callback) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ if (chunk) {
93
+ request_body_chunks.push(chunk);
94
+ }
95
+ return callback(null, null); // don't write chunks to client Request
96
+ });
97
+ });
98
+ ctx.onRequestEnd(function (ctx, callback) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ let body = new Buffer.concat(request_body_chunks);
101
+ const contentTypeHeader = (0, proxy_ctx_helper_1.getRequestContentTypeHeader)(ctx);
102
+ const contentType = (0, http_helpers_1.getContentType)(contentTypeHeader);
103
+ const parsedBody = (0, http_helpers_1.bodyParser)(contentTypeHeader, body);
104
+ // Request body before any modifications
105
+ let pre_final_body = parsedBody || body.toString("utf8");
106
+ ctx.rq.set_original_request({ body: pre_final_body });
107
+ ctx.rq_request_body = pre_final_body;
108
+ if (parsedBody && constants_1.RQ_INTERCEPTED_CONTENT_TYPES.includes(contentType)) {
109
+ // Do modifications, if any
110
+ const { action_result_objs, continue_request } = yield rules_middleware.on_request_end(ctx);
111
+ }
112
+ // Use the updated request
113
+ ctx.proxyToServerRequest.write(ctx.rq_request_body);
114
+ ctx.rq.set_final_request({ body: ctx.rq_request_body });
115
+ return callback();
116
+ });
117
+ });
118
+ ctx.onResponse((ctx, callback) => __awaiter(this, void 0, void 0, function* () {
119
+ ctx.rq.set_original_response((0, proxy_ctx_helper_1.get_response_options)(ctx));
120
+ const { action_result_objs, continue_request: continue_response } = yield rules_middleware.on_response(ctx);
121
+ if (continue_response) {
122
+ return callback();
123
+ }
124
+ }));
125
+ let response_body_chunks = [];
126
+ ctx.onResponseData(function (ctx, chunk, callback) {
127
+ return __awaiter(this, void 0, void 0, function* () {
128
+ if (chunk) {
129
+ response_body_chunks.push(chunk);
130
+ }
131
+ return callback(null, null); // don't write chunks to client response
132
+ });
133
+ });
134
+ ctx.onResponseEnd(function (ctx, callback) {
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ let body = new Buffer.concat(response_body_chunks);
137
+ const contentTypeHeader = (0, proxy_ctx_helper_1.getResponseContentTypeHeader)(ctx);
138
+ const contentType = (0, http_helpers_1.getContentType)(contentTypeHeader);
139
+ const parsedBody = (0, http_helpers_1.bodyParser)(contentTypeHeader, body);
140
+ ctx.rq_response_body = body;
141
+ ctx.rq_response_status_code = (0, proxy_ctx_helper_1.getResponseStatusCode)(ctx);
142
+ if (constants_1.RQ_INTERCEPTED_CONTENT_TYPES.includes(contentType) && parsedBody) {
143
+ ctx.rq.set_original_response({ body: parsedBody });
144
+ // Body and status code before any modifications
145
+ ctx.rq_response_body = parsedBody;
146
+ const { action_result_objs, continue_request } = yield rules_middleware.on_response_end(ctx);
147
+ // ctx.rq_response_body, ctx.rq_response_status_code after modifications
148
+ // TODO: @sahil to investigate why this is need
149
+ // Remove some conflicting headers like content-length, if any
150
+ delete (0, proxy_ctx_helper_1.getResponseHeaders)(ctx)["content-length"];
151
+ }
152
+ ctx.proxyToClientResponse.writeHead(ctx.rq_response_status_code || (0, proxy_ctx_helper_1.getResponseStatusCode)(ctx), (0, proxy_ctx_helper_1.getResponseHeaders)(ctx));
153
+ ctx.proxyToClientResponse.write(ctx.rq_response_body);
154
+ ctx.rq.set_final_response(Object.assign(Object.assign({}, (0, proxy_ctx_helper_1.get_response_options)(ctx)), { status_code: ctx.rq_response_status_code || (0, proxy_ctx_helper_1.getResponseStatusCode)(ctx), body: ctx.rq_response_body }));
155
+ logger_middleware.send_network_log(ctx, rules_middleware.action_result_objs, requestly_core_1.CONSTANTS.REQUEST_STATE.COMPLETE);
156
+ return callback();
157
+ });
158
+ });
159
+ // Remove headers that may conflict
160
+ delete (0, proxy_ctx_helper_1.getRequestHeaders)(ctx)["content-length"];
161
+ const { action_result_objs, continue_request } = yield rules_middleware.on_request(ctx);
162
+ ctx.rq.set_final_request((0, proxy_ctx_helper_1.get_request_options)(ctx));
163
+ // TODO: Removing this log for now. Will add this when support is added for upsert in firebase logs.
164
+ logger_middleware.send_network_log(ctx, rules_middleware.action_result_objs, requestly_core_1.CONSTANTS.REQUEST_STATE.LOADING);
165
+ //logger
166
+ if (continue_request) {
167
+ return callback();
168
+ }
169
+ }), is_detachable);
170
+ };
171
+ this.init_ssl_tunneling_handler = () => {
172
+ this.proxy.onConnect((req, socket, head, callback) => __awaiter(this, void 0, void 0, function* () {
173
+ const host = req.url.split(":")[0];
174
+ const port = req.url.split(":")[1];
175
+ const origin = `https://${host}`;
176
+ const isSSLProxyingActive = this.sslProxyingManager.isSslProxyingActive(origin);
177
+ if (isSSLProxyingActive) {
178
+ return callback();
179
+ }
180
+ // TODO: @sahil add logger here for CONNECT request
181
+ console.log("Tunnel to", req.url);
182
+ // Hack: For timing out tunnel in case of inclusionList/ExclusionList Change
183
+ global.rq.sslTunnelingSocketsMap[req.url] = socket;
184
+ var conn = net.connect({
185
+ port: port,
186
+ host: host,
187
+ allowHalfOpen: false,
188
+ }, function () {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ conn.on("finish", () => {
191
+ console.log(`Finish ${host}`);
192
+ socket.destroy();
193
+ delete global.rq.sslTunnelingSocketsMap[req.url];
194
+ });
195
+ socket.on("close", () => {
196
+ console.log("Close");
197
+ conn.end();
198
+ });
199
+ // socket.on('data', (data) => {
200
+ // // console.log("FROM SOCKET: " + data.toString("ascii") + "\n");
201
+ // console.log("FROM SOCKET: data");
202
+ // });
203
+ // conn.on('data', (data) => {
204
+ // // console.log("FROM CONN: " + data.toString("ascii") + "\n");
205
+ // console.log("FROM CONN: data");
206
+ // });
207
+ socket.write("HTTP/1.1 200 OK\r\n\r\n", "UTF-8", function () {
208
+ conn.pipe(socket);
209
+ socket.pipe(conn);
210
+ });
211
+ });
212
+ });
213
+ conn.on("error", function (err) {
214
+ filterSocketConnReset(err, "PROXY_TO_SERVER_SOCKET");
215
+ });
216
+ socket.on("error", function (err) {
217
+ filterSocketConnReset(err, "CLIENT_TO_PROXY_SOCKET");
218
+ });
219
+ }));
220
+ // Since node 0.9.9, ECONNRESET on sockets are no longer hidden
221
+ function filterSocketConnReset(err, socketDescription) {
222
+ if (err.errno === "ECONNRESET") {
223
+ console.log("Got ECONNRESET on " + socketDescription + ", ignoring.");
224
+ }
225
+ else {
226
+ console.log("Got unexpected error on " + socketDescription, err);
227
+ }
228
+ }
229
+ };
230
+ this.init_handlers = () => {
231
+ this.proxy.onRequestHandlers = [];
232
+ this.proxy.onConnectHandlers = [];
233
+ this.proxy.use(proxy_1.default.gunzip);
234
+ // this.init_ssl_tunneling_handler();
235
+ this.init_amiusing_handler();
236
+ this.init_ssl_cert_handler();
237
+ this.init_main_handler();
238
+ };
239
+ /*
240
+ {
241
+ AMIUSING: true,
242
+ PROXY: true,
243
+ LOGGER: true,
244
+ SSL_CERT: true
245
+ }
246
+ */
247
+ this.config = {};
248
+ this.init_config();
249
+ this.proxy = proxy;
250
+ this.proxyConfig = proxyConfig;
251
+ this.rulesHelper = rulesHelper;
252
+ this.loggerService = loggerService;
253
+ this.sslConfigFetcher = sslConfigFetcher;
254
+ // this.sslProxyingManager = new SSLProxyingManager(sslConfigFetcher);
255
+ }
256
+ }
257
+ exports.default = ProxyMiddlewareManager;
@@ -0,0 +1,6 @@
1
+ export default AmisuingMiddleware;
2
+ declare class AmisuingMiddleware {
3
+ constructor(is_active: any);
4
+ is_active: any;
5
+ on_request: (ctx: any) => Promise<boolean>;
6
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ class AmisuingMiddleware {
13
+ constructor(is_active) {
14
+ this.on_request = (ctx) => __awaiter(this, void 0, void 0, function* () {
15
+ if (!this.is_active) {
16
+ return true;
17
+ }
18
+ Object.assign(ctx.proxyToServerRequestOptions.headers, {
19
+ ["amiusingrequestly"]: "true",
20
+ });
21
+ });
22
+ this.is_active = is_active;
23
+ }
24
+ }
25
+ exports.default = AmisuingMiddleware;
@@ -0,0 +1,66 @@
1
+ export default LoggerMiddleware;
2
+ declare class LoggerMiddleware {
3
+ constructor(is_active: any, loggerService: any);
4
+ is_active: any;
5
+ loggerService: any;
6
+ generate_curl_from_har: (requestHarObject: any) => string;
7
+ send_network_log: (ctx: any, action_result_objs?: any[], requestState?: string) => void;
8
+ createLog: (ctx: any, action_result_objs?: any[], requestState?: string) => {
9
+ id: any;
10
+ timestamp: number;
11
+ finalHar: {
12
+ log: {
13
+ version: string;
14
+ creator: {};
15
+ browser: {};
16
+ pages: any[];
17
+ entries: {
18
+ startedDateTime: string;
19
+ request: {
20
+ bodySize: number;
21
+ headersSize: number;
22
+ httpVersion: string;
23
+ cookies: any[];
24
+ headers: {
25
+ name: string;
26
+ value: any;
27
+ }[];
28
+ method: any;
29
+ queryString: any[];
30
+ url: string;
31
+ postData: {
32
+ mimeType: any;
33
+ text: any;
34
+ };
35
+ };
36
+ response: {
37
+ status: any;
38
+ httpVersion: string;
39
+ cookies: any[];
40
+ headers: {
41
+ name: string;
42
+ value: any;
43
+ }[];
44
+ content: {
45
+ size: number;
46
+ compression: number;
47
+ mimeType: any;
48
+ text: any;
49
+ comment: string;
50
+ };
51
+ headersSize: number;
52
+ bodySize: number;
53
+ comment: string;
54
+ };
55
+ cache: {};
56
+ timings: {};
57
+ comment: string;
58
+ }[];
59
+ comment: string;
60
+ };
61
+ };
62
+ requestShellCurl: string;
63
+ actions: any[];
64
+ requestState: string;
65
+ };
66
+ }
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const lodash_1 = require("lodash");
7
+ const httpsnippet_1 = __importDefault(require("httpsnippet"));
8
+ const utils_1 = require("../rule_action_processor/utils");
9
+ const harObectCreator_1 = require("../helpers/harObectCreator");
10
+ const url = require("url");
11
+ class LoggerMiddleware {
12
+ constructor(is_active, loggerService) {
13
+ this.generate_curl_from_har = (requestHarObject) => {
14
+ if (!requestHarObject) {
15
+ return "";
16
+ }
17
+ let requestCurl = "";
18
+ try {
19
+ const harObject = (0, lodash_1.cloneDeep)(requestHarObject);
20
+ requestCurl = new httpsnippet_1.default(harObject).convert("shell", "curl", {
21
+ indent: " ",
22
+ });
23
+ }
24
+ catch (err) {
25
+ console.error(`LoggerMiddleware.generate_curl_from_har Error: ${err}`);
26
+ }
27
+ return requestCurl;
28
+ };
29
+ this.send_network_log = (ctx, action_result_objs = [], requestState = "") => {
30
+ // let query_params_string;
31
+ // if (ctx.rq.final_request.query_params) {
32
+ // query_params_string = JSON.stringify(ctx.rq.final_request.query_params);
33
+ // }
34
+ // const log = {
35
+ // id: ctx.uuid,
36
+ // timestamp: Math.floor(Date.now() / 1000),
37
+ // url: url.parse(
38
+ // (ctx.isSSL ? "https://" : "http://") +
39
+ // ctx.clientToProxyRequest.headers.host +
40
+ // ctx.clientToProxyRequest.url
41
+ // ).href,
42
+ // request: {
43
+ // method: ctx.rq.final_request.method,
44
+ // path: ctx.rq.final_request.host,
45
+ // host: ctx.rq.final_request.host,
46
+ // port: ctx.rq.final_request.port,
47
+ // headers: ctx.rq.final_request.headers,
48
+ // body: ctx.rq.final_request.body,
49
+ // query_params: query_params_string,
50
+ // },
51
+ // requestShellCurl: this.generate_curl_from_har(
52
+ // ctx.rq.final_request.requestHarObject
53
+ // ),
54
+ // response: {
55
+ // statusCode: ctx.rq.final_response.status_code,
56
+ // headers: ctx.rq.final_response.headers || {},
57
+ // contentType:
58
+ // (ctx.rq.final_response.headers &&
59
+ // ctx.rq.final_response.headers["content-type"] &&
60
+ // ctx.rq.final_response.headers["content-type"].includes(";") &&
61
+ // ctx.rq.final_response.headers["content-type"].split(";")[0]) ||
62
+ // (ctx.rq.final_response.headers &&
63
+ // ctx.rq.final_response.headers["content-type"]),
64
+ // body: ctx.rq.final_response.body || null,
65
+ // },
66
+ // actions: get_success_actions_from_action_results(action_result_objs),
67
+ // };
68
+ // ipcRenderer.send("log-network-request", log);
69
+ // TODO: Sending log for now. Ideally this should be har object
70
+ this.loggerService.addLog(this.createLog(ctx, action_result_objs, requestState), ctx.rq.final_request.headers || {});
71
+ };
72
+ this.createLog = (ctx, action_result_objs = [], requestState = "") => {
73
+ var _a, _b;
74
+ const protocol = ctx.isSSL ? "https" : "http";
75
+ const rqLog = {
76
+ id: ctx.uuid,
77
+ timestamp: Math.floor(Date.now() / 1000),
78
+ finalHar: (0, harObectCreator_1.createHar)(ctx.rq.final_request.headers, ctx.rq.final_request.method, protocol, ctx.rq.final_request.host, ctx.rq.final_request.path, ctx.rq.final_request.body, ctx.rq.final_response.status_code, ctx.rq.final_response.body, ctx.rq.final_response.headers || {}),
79
+ requestShellCurl: this.generate_curl_from_har((_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.rq) === null || _a === void 0 ? void 0 : _a.final_request) === null || _b === void 0 ? void 0 : _b.requestHarObject),
80
+ actions: (0, utils_1.get_success_actions_from_action_results)(action_result_objs),
81
+ requestState
82
+ };
83
+ return rqLog;
84
+ };
85
+ this.is_active = is_active;
86
+ this.loggerService = loggerService;
87
+ }
88
+ }
89
+ exports.default = LoggerMiddleware;
@@ -0,0 +1,40 @@
1
+ export default RulesMiddleware;
2
+ declare class RulesMiddleware {
3
+ constructor(is_active: any, ctx: any, rulesHelper: any);
4
+ is_active: any;
5
+ rule_processor_helper: RuleProcessorHelper;
6
+ rule_action_processor: RuleActionProcessor;
7
+ rulesHelper: any;
8
+ active_rules: any[];
9
+ on_request_actions: any[];
10
+ on_response_actions: any[];
11
+ action_result_objs: any[];
12
+ _init_request_data: (ctx: any) => void;
13
+ request_data: any;
14
+ _init_response_data: (ctx: any) => void;
15
+ response_data: {
16
+ response_headers: any;
17
+ };
18
+ _update_request_data: (data: any) => void;
19
+ _fetch_rules: () => Promise<void>;
20
+ _process_rules: (is_response?: boolean) => any;
21
+ _update_action_result_objs: (action_result_objs?: any[]) => void;
22
+ on_request: (ctx: any) => Promise<any[] | {
23
+ action_result_objs: any[];
24
+ continue_request: boolean;
25
+ }>;
26
+ on_response: (ctx: any) => Promise<any[] | {
27
+ action_result_objs: any[];
28
+ continue_request: boolean;
29
+ }>;
30
+ on_request_end: (ctx: any) => Promise<any[] | {
31
+ action_result_objs: any[];
32
+ continue_request: boolean;
33
+ }>;
34
+ on_response_end: (ctx: any) => Promise<any[] | {
35
+ action_result_objs: any[];
36
+ continue_request: boolean;
37
+ }>;
38
+ }
39
+ import RuleProcessorHelper from "../helpers/rule_processor_helper";
40
+ import RuleActionProcessor from "../rule_action_processor";
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const proxy_ctx_helper_1 = require("../helpers/proxy_ctx_helper");
16
+ const rule_processor_helper_1 = __importDefault(require("../helpers/rule_processor_helper"));
17
+ const rule_action_processor_1 = __importDefault(require("../rule_action_processor"));
18
+ class RulesMiddleware {
19
+ constructor(is_active, ctx, rulesHelper) {
20
+ this._init_request_data = (ctx) => {
21
+ var _a, _b, _c, _d;
22
+ this.request_data = {
23
+ request_url: (0, proxy_ctx_helper_1.get_request_url)(ctx),
24
+ request_headers: (0, proxy_ctx_helper_1.get_original_request_headers)(ctx),
25
+ query_params: (_b = (_a = ctx.rq) === null || _a === void 0 ? void 0 : _a.original_request) === null || _b === void 0 ? void 0 : _b.query_params,
26
+ method: (_d = (_c = ctx.rq) === null || _c === void 0 ? void 0 : _c.original_request) === null || _d === void 0 ? void 0 : _d.method,
27
+ };
28
+ this.rule_processor_helper.init_request_data(this.request_data);
29
+ };
30
+ this._init_response_data = (ctx) => {
31
+ this.response_data = {
32
+ response_headers: (0, proxy_ctx_helper_1.get_original_response_headers)(ctx),
33
+ };
34
+ this.rule_processor_helper.init_response_data(this.response_data);
35
+ };
36
+ this._update_request_data = (data) => {
37
+ this.request_data = Object.assign(Object.assign({}, this.request_data), data);
38
+ this.rule_processor_helper.init_request_data(this.request_data);
39
+ };
40
+ this._fetch_rules = () => __awaiter(this, void 0, void 0, function* () {
41
+ var _a;
42
+ this.active_rules = yield this.rulesHelper.get_rules(true, ((_a = this.request_data) === null || _a === void 0 ? void 0 : _a.request_headers) || {});
43
+ });
44
+ /*
45
+ @return: actions[]
46
+ */
47
+ this._process_rules = (is_response = false) => {
48
+ // https://github.com/requestly/requestly-master/issues/686
49
+ // 1 time processing if we fix this issue
50
+ let rule_actions = this.rule_processor_helper.process_rules(this.active_rules, is_response);
51
+ // Filter out all the null actions
52
+ rule_actions = rule_actions.filter((action) => !!action);
53
+ return rule_actions;
54
+ };
55
+ this._update_action_result_objs = (action_result_objs = []) => {
56
+ if (action_result_objs) {
57
+ this.action_result_objs =
58
+ this.action_result_objs.concat(action_result_objs);
59
+ }
60
+ };
61
+ this.on_request = (ctx) => __awaiter(this, void 0, void 0, function* () {
62
+ if (!this.is_active) {
63
+ return [];
64
+ }
65
+ // TODO: Remove this. Hack to fix rule not fetching first time.
66
+ yield this._fetch_rules();
67
+ this.on_request_actions = this._process_rules();
68
+ const { action_result_objs, continue_request } = yield this.rule_action_processor.process_actions(this.on_request_actions, ctx);
69
+ this._update_action_result_objs(action_result_objs);
70
+ return { action_result_objs, continue_request };
71
+ });
72
+ this.on_response = (ctx) => __awaiter(this, void 0, void 0, function* () {
73
+ if (!this.is_active) {
74
+ return [];
75
+ }
76
+ this._init_response_data(ctx);
77
+ this.on_response_actions = this._process_rules(true);
78
+ const { action_result_objs, continue_request } = yield this.rule_action_processor.process_actions(this.on_response_actions, ctx);
79
+ this._update_action_result_objs(action_result_objs);
80
+ return { action_result_objs, continue_request };
81
+ });
82
+ this.on_request_end = (ctx) => __awaiter(this, void 0, void 0, function* () {
83
+ if (!this.is_active) {
84
+ return [];
85
+ }
86
+ this._update_request_data({ request_body: ctx.rq.get_json_request_body() });
87
+ const { action_result_objs, continue_request } = yield this.rule_action_processor.process_actions(this.on_request_actions, ctx);
88
+ this._update_action_result_objs(action_result_objs);
89
+ return { action_result_objs, continue_request };
90
+ });
91
+ this.on_response_end = (ctx) => __awaiter(this, void 0, void 0, function* () {
92
+ if (!this.is_active) {
93
+ return [];
94
+ }
95
+ const { action_result_objs, continue_request } = yield this.rule_action_processor.process_actions(this.on_response_actions, ctx);
96
+ this._update_action_result_objs(action_result_objs);
97
+ return { action_result_objs, continue_request };
98
+ });
99
+ this.is_active = is_active;
100
+ this.rule_processor_helper = new rule_processor_helper_1.default();
101
+ this.rule_action_processor = new rule_action_processor_1.default();
102
+ this.rulesHelper = rulesHelper;
103
+ this._init_request_data(ctx);
104
+ this.active_rules = [];
105
+ this._fetch_rules();
106
+ // Keeping these 2 separate because of request and response headers
107
+ // from rule processor are triggers during different proxy hooks.
108
+ // These can be combined into 1 if we change the actions returned
109
+ // by modify headers rule processor
110
+ // TODO: @sahil865gupta UPGRADE MODIFY HEADER ACTIONS
111
+ // https://github.com/requestly/requestly-master/issues/686
112
+ this.on_request_actions = [];
113
+ this.on_response_actions = [];
114
+ this.action_result_objs = [];
115
+ }
116
+ }
117
+ exports.default = RulesMiddleware;
@@ -0,0 +1,7 @@
1
+ export default SslCertMiddleware;
2
+ declare class SslCertMiddleware {
3
+ constructor(is_active: any, rootCertPath: any);
4
+ is_active: any;
5
+ rootCertPath: any;
6
+ on_request: (ctx: any) => Promise<boolean>;
7
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const fs = require("fs");
13
+ class SslCertMiddleware {
14
+ constructor(is_active, rootCertPath) {
15
+ this.on_request = (ctx) => __awaiter(this, void 0, void 0, function* () {
16
+ if (!this.is_active) {
17
+ return true;
18
+ }
19
+ if (ctx.clientToProxyRequest.headers.host == "requestly.io" &&
20
+ ctx.clientToProxyRequest.url.indexOf("/ssl") == 0) {
21
+ ctx.proxyToClientResponse.writeHead(200, {
22
+ "Content-Type": "text/plain",
23
+ "Content-Disposition": "attachment;filename=RequestlyCA.pem.crt",
24
+ });
25
+ const certificateString = fs.readFileSync(this.rootCertPath);
26
+ ctx.proxyToClientResponse.end(certificateString);
27
+ }
28
+ });
29
+ this.is_active = is_active;
30
+ this.rootCertPath = rootCertPath;
31
+ }
32
+ }
33
+ exports.default = SslCertMiddleware;
@@ -0,0 +1,14 @@
1
+ export default handleMixedResponse;
2
+ declare function handleMixedResponse(ctx: any, destinationUrl: any): Promise<{
3
+ status: boolean;
4
+ response_data: {
5
+ headers: {
6
+ "Cache-Control": string;
7
+ };
8
+ status_code: number;
9
+ body: any;
10
+ };
11
+ } | {
12
+ status: boolean;
13
+ response_data?: undefined;
14
+ }>;