Skip to content

Commit 2c8dfae

Browse files
authored
feat: parse port from onConnect handler (#45)
1 parent ad0b99e commit 2c8dfae

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/components/proxy-middleware/middlewares/logger_middleware.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
12
// import { cloneDeep } from "lodash";
23
// import HTTPSnippet from "httpsnippet";
3-
import { get_success_actions_from_action_results } from "../rule_action_processor/utils";
4+
import { get_success_actions_from_action_results, getHost } from "../rule_action_processor/utils";
45
import { createHar } from "../helpers/harObectCreator";
56
// const url = require("url");
67

@@ -15,6 +16,7 @@ class LoggerMiddleware {
1516
return "";
1617
}
1718
let requestCurl = "";
19+
1820
// try {
1921
// const harObject = cloneDeep(requestHarObject);
2022
// requestCurl = new HTTPSnippet(harObject).convert("shell", "curl", {
@@ -79,7 +81,7 @@ class LoggerMiddleware {
7981
ctx.rq.final_request.headers,
8082
ctx.rq.final_request.method,
8183
protocol,
82-
ctx.rq.final_request.host,
84+
getHost(ctx),
8385
ctx.rq.final_request.path,
8486
ctx.rq.final_request.body,
8587
ctx.rq.final_response.status_code,

src/components/proxy-middleware/rule_action_processor/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ export const get_success_actions_from_action_results = (
4343
return success_action_results_objs.map((obj) => obj.action);
4444
};
4545

46+
export const getHost = (ctx) => {
47+
const finalHost = ctx.rq.final_request.host;
48+
const port = ctx.rq.final_request.port;
49+
const protocol = ctx.isSSL ? "https" : "http";
50+
const standardPort = protocol === "https" ? 443 : 80;
51+
52+
// Only append port if it's non-standard and not already in the host
53+
if (port && port !== standardPort && !finalHost.includes(':')) {
54+
return `${finalHost}:${port}`;
55+
}
56+
57+
return finalHost;
58+
}
59+
4660
export const get_file_contents = (file_path) => {
4761
return fs.readFileSync(file_path, "utf-8");
4862
}

0 commit comments

Comments
 (0)