@requestly/requestly-proxy 1.3.8 → 1.3.10

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.
@@ -0,0 +1,6 @@
1
+ export function isAddressUnreachableError(host: any): Promise<any>;
2
+ export function dataToServeUnreachablePage(host: any): {
3
+ status: number;
4
+ contentType: string;
5
+ body: string;
6
+ };
@@ -0,0 +1,86 @@
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
+ exports.isAddressUnreachableError = isAddressUnreachableError;
7
+ exports.dataToServeUnreachablePage = dataToServeUnreachablePage;
8
+ const dns_1 = __importDefault(require("dns"));
9
+ function isAddressUnreachableError(host) {
10
+ return new Promise((resolve, reject) => {
11
+ dns_1.default.lookup(host, (err, address) => {
12
+ if (err) {
13
+ if (err.code === 'ENOTFOUND') {
14
+ resolve(true);
15
+ }
16
+ else {
17
+ reject(err);
18
+ }
19
+ }
20
+ else {
21
+ resolve(false);
22
+ }
23
+ });
24
+ });
25
+ }
26
+ function dataToServeUnreachablePage(host) {
27
+ return {
28
+ status: 502,
29
+ contentType: 'text/html',
30
+ body: `
31
+ <!DOCTYPE html>
32
+ <html lang="en">
33
+ <head>
34
+ <meta charset="UTF-8">
35
+ <title>ERR_NAME_NOT_RESOLVED</title>
36
+ <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
37
+ <style>
38
+ body {
39
+ background-color: #f2f2f2;
40
+ color: #333;
41
+ font-family: 'Roboto', sans-serif;
42
+ margin: 0;
43
+ padding: 0;
44
+ }
45
+ .container {
46
+ max-width: 600px;
47
+ margin: 100px auto;
48
+ padding: 20px;
49
+ text-align: center;
50
+ }
51
+ .sad-face {
52
+ font-size: 80px;
53
+ margin-bottom: 20px;
54
+ }
55
+ h1 {
56
+ font-size: 24px;
57
+ font-weight: normal;
58
+ margin-bottom: 10px;
59
+ }
60
+ p {
61
+ font-size: 16px;
62
+ color: #666;
63
+ }
64
+ @media (max-width: 600px) {
65
+ .container {
66
+ margin-top: 50px;
67
+ padding: 10px;
68
+ }
69
+ .sad-face {
70
+ font-size: 60px;
71
+ }
72
+ }
73
+ </style>
74
+ </head>
75
+ <body>
76
+ <div class="container">
77
+ <div class="sad-face">:(</div>
78
+ <h1>This site can’t be reached</h1>
79
+ <p>The webpage at <strong>${host}/</strong> might be temporarily down or it may have moved permanently to a new web address.</p>
80
+ <p><strong>ERR_NAME_NOT_RESOLVED</strong></p>
81
+ </div>
82
+ </body>
83
+ </html>
84
+ `.trim()
85
+ };
86
+ }
@@ -17,6 +17,7 @@ const ctx_rq_namespace_1 = __importDefault(require("./helpers/ctx_rq_namespace")
17
17
  const http_helpers_1 = require("./helpers/http_helpers");
18
18
  const constants_1 = require("./constants");
19
19
  const requestly_core_1 = require("@requestly/requestly-core");
20
+ const handleUnreachableAddress_1 = require("./helpers/handleUnreachableAddress");
20
21
  // import SSLProxyingConfigFetcher from "renderer/lib/fetcher/ssl-proxying-config-fetcher";
21
22
  // import SSLProxyingManager from "../ssl-proxying/ssl-proxying-manager";
22
23
  exports.MIDDLEWARE_TYPE = {
@@ -96,7 +97,36 @@ class ProxyMiddlewareManager {
96
97
  headers: responseHeaders,
97
98
  body: ctx.rq_response_body,
98
99
  });
99
- logger_middleware.send_network_log(ctx, rules_middleware.action_result_objs, requestly_core_1.CONSTANTS.REQUEST_STATE.COMPLETE);
100
+ if (!ctx.rq.request_finished) {
101
+ logger_middleware.send_network_log(ctx, rules_middleware.action_result_objs, requestly_core_1.CONSTANTS.REQUEST_STATE.COMPLETE);
102
+ }
103
+ }
104
+ else if (kind === "PROXY_TO_SERVER_REQUEST_ERROR") {
105
+ try {
106
+ const host = (0, proxy_ctx_helper_1.get_request_url)(ctx);
107
+ const isAddressUnreachable = await (0, handleUnreachableAddress_1.isAddressUnreachableError)(host);
108
+ if (isAddressUnreachable) {
109
+ const { status, contentType, body } = (0, handleUnreachableAddress_1.dataToServeUnreachablePage)(host);
110
+ ctx.proxyToClientResponse.writeHead(status, http_1.default.STATUS_CODES[status], {
111
+ "Content-Type": contentType,
112
+ "x-rq-error": "ERR_NAME_NOT_RESOLVED"
113
+ });
114
+ ctx.proxyToClientResponse.end(body);
115
+ ctx.rq.set_final_response({
116
+ status_code: status,
117
+ headers: { "Content-Type": contentType },
118
+ body: body,
119
+ });
120
+ logger_middleware.send_network_log(ctx, rules_middleware.action_result_objs, requestly_core_1.CONSTANTS.REQUEST_STATE.COMPLETE);
121
+ return;
122
+ }
123
+ }
124
+ catch (error) {
125
+ console.error("Error checking address:", error);
126
+ }
127
+ }
128
+ else {
129
+ console.log("Expected Error after early termination of request: ", err);
100
130
  }
101
131
  return callback();
102
132
  });
@@ -119,6 +149,10 @@ class ProxyMiddlewareManager {
119
149
  if (parsedBody && constants_1.RQ_INTERCEPTED_CONTENT_TYPES.includes(contentType)) {
120
150
  // Do modifications, if any
121
151
  const { action_result_objs, continue_request } = await rules_middleware.on_request_end(ctx);
152
+ if (!continue_request) {
153
+ logger_middleware.send_network_log(ctx, rules_middleware.action_result_objs, requestly_core_1.CONSTANTS.REQUEST_STATE.COMPLETE);
154
+ return;
155
+ }
122
156
  }
123
157
  // Use the updated request
124
158
  ctx.proxyToServerRequest.write(ctx.rq_request_body);
@@ -78,6 +78,7 @@ class RulesMiddleware {
78
78
  return [];
79
79
  }
80
80
  this._update_request_data({ request_body: ctx.rq.get_json_request_body() });
81
+ this.on_request_actions = this._process_rules();
81
82
  const { action_result_objs, continue_request } = await this.rule_action_processor.process_actions(this.on_request_actions, ctx);
82
83
  this._update_action_result_objs(action_result_objs);
83
84
  return { action_result_objs, continue_request };
@@ -12,12 +12,12 @@ declare function handleMixedResponse(ctx: any, destinationUrl: any): Promise<{
12
12
  status: boolean;
13
13
  response_data: {
14
14
  headers: {
15
- "Content-Type": any;
15
+ "content-type": any;
16
16
  "Content-Length": number;
17
17
  "Cache-Control": string;
18
18
  } | {
19
19
  "Cache-Control": string;
20
- "Content-Type"?: undefined;
20
+ "content-type"?: undefined;
21
21
  "Content-Length"?: undefined;
22
22
  };
23
23
  status_code: number;
@@ -76,12 +76,16 @@ const handleMixedResponse = async (ctx, destinationUrl) => {
76
76
  const mimeType = mime.lookup(path);
77
77
  const bodyContent = buffers.toString("utf-8"); // assuming utf-8 encoding
78
78
  const headers = mimeType ? {
79
- "Content-Type": mimeType,
79
+ "content-type": mimeType,
80
80
  "Content-Length": Buffer.byteLength(bodyContent),
81
81
  "Cache-Control": "no-cache"
82
82
  } : {
83
83
  "Cache-Control": "no-cache"
84
84
  };
85
+ headers["access-control-allow-origin"] = "*";
86
+ headers["access-control-allow-credentials"] = "true";
87
+ headers["access-control-allow-methods"] = "*";
88
+ headers["access-control-allow-headers"] = "*";
85
89
  return {
86
90
  status: true,
87
91
  response_data: {
@@ -45,6 +45,7 @@ class RuleActionProcessor {
45
45
  body = JSON.stringify(body);
46
46
  }
47
47
  ctx.proxyToClientResponse.writeHead(status_code, headers).end(body);
48
+ ctx.rq.request_finished = true;
48
49
  ctx.rq.set_final_response({
49
50
  status_code: status_code,
50
51
  headers: headers,
@@ -8,11 +8,17 @@ const http_helpers_1 = require("../../helpers/http_helpers");
8
8
  const utils_2 = require("../../../../utils");
9
9
  const constants_1 = require("../../constants");
10
10
  const process_modify_response_action = async (action, ctx) => {
11
- const allowed_handlers = [proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST, proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE_END, proxy_1.PROXY_HANDLER_TYPE.ON_ERROR];
11
+ const allowed_handlers = [
12
+ proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST,
13
+ proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST_END,
14
+ proxy_1.PROXY_HANDLER_TYPE.ON_RESPONSE_END,
15
+ proxy_1.PROXY_HANDLER_TYPE.ON_ERROR
16
+ ];
12
17
  if (!allowed_handlers.includes(ctx.currentHandler)) {
13
18
  return (0, utils_1.build_action_processor_response)(action, false);
14
19
  }
15
- if (ctx.currentHandler === proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST) {
20
+ if (ctx.currentHandler === proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST ||
21
+ ctx.currentHandler === proxy_1.PROXY_HANDLER_TYPE.ON_REQUEST_END) {
16
22
  if (action.serveWithoutRequest) {
17
23
  let contentType, finalBody;
18
24
  if (action.responseType === requestly_core_1.CONSTANTS.RESPONSE_BODY_TYPES.LOCAL_FILE) {
@@ -41,7 +47,13 @@ const process_modify_response_action = async (action, ctx) => {
41
47
  contentType = "text/plain";
42
48
  }
43
49
  const status = action.statusCode || 200;
44
- const finalHeaders = { "Content-Type": contentType };
50
+ const finalHeaders = {
51
+ "content-type": contentType,
52
+ "access-control-allow-origin": "*",
53
+ "access-control-allow-methods": "*",
54
+ "access-control-allow-headers": "*",
55
+ "access-control-allow-credentials": "true",
56
+ };
45
57
  modify_response(ctx, finalBody, status);
46
58
  return (0, utils_1.build_action_processor_response)(action, true, (0, utils_1.build_post_process_data)(status, finalHeaders, finalBody));
47
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@requestly/requestly-proxy",
3
- "version": "1.3.8",
3
+ "version": "1.3.10",
4
4
  "description": "Proxy that gives superpowers to all the Requestly clients",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",