Skip to content

Commit

Permalink
Merge branch 'main' of ../private
Browse files Browse the repository at this point in the history
  • Loading branch information
lunar-devops committed Sep 29, 2024
2 parents 754f3fc + b02eb31 commit e858f6d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion interceptors/lunar-ts-interceptor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lunar-interceptor",
"version": "2.1.4",
"version": "2.1.5",
"description": "The Lunar interceptor. Used to manage outgoing HTTP/S traffic.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion interceptors/lunar-ts-interceptor/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const PROXY_HOST_KEY = "LUNAR_PROXY_HOST"
export const HANDSHAKE_PORT_KEY = "LUNAR_HANDSHAKE_PORT"
export const SUPPORT_TLS_KEY = "LUNAR_PROXY_SUPPORT_TLS"

export const INTERCEPTOR_VERSION = "2.1.4"
export const INTERCEPTOR_VERSION = "2.1.5"
export const PROXY_DEFAULT_HANDSHAKE_PORT = 8081
export const INTERCEPTOR_ID = `lunar-ts-interceptor/${INTERCEPTOR_VERSION}`

Expand Down
8 changes: 6 additions & 2 deletions interceptors/lunar-ts-interceptor/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ export function translateProxyError(code: string): string {
}
}

export function generateUrl(options: RequestOptions, scheme: string): URL {
export function generateUrl(options: RequestOptions, scheme: string): URL | null {
let host = options.host
let port = options.port
let path = options.path
if (host == null || host === undefined || host === "") {
if (options.hostname == null || options.hostname === undefined) {
host = ""
logger.debug("Could not determine the host")
Object.entries(options).forEach(([key, value]) => {
logger.debug(`${key}: ${value}`);
});
return null
} else {
host = options.hostname
}
Expand Down
55 changes: 33 additions & 22 deletions interceptors/lunar-ts-interceptor/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class LunarInterceptor {

// https://github.com/nodejs/node/blob/717e233cd95602f79256c5b70c49703fa699174b/lib/_http_client.js#L130
private httpHookRequestFunc(scheme: string, functionName: string, arg0: unknown, arg1: unknown, arg2: unknown, ...args: unknown[]): ClientRequest {
let url: URL;
let url: URL | null;
let options: LunarOptions;
let modifiedOptions: LunarOptions | null = null;

Expand Down Expand Up @@ -214,29 +214,33 @@ class LunarInterceptor {
// @ts-expect-error: TS2345
return this.getFunctionFromMap(scheme, functionName)(arg0, arg1, arg2, ...args)
}

if (this.lunarConnect.isProxyListening() === undefined || this.lunarConnect.isProxyListening() === true) {
if (this._failSafe.stateOk() && this._trafficFilter.isAllowed(url.host, options.headers)) {
logger.debug(`Forwarding the request to ${url.href} using Lunar Proxy`);

modifiedOptions = this.generateModifiedOptions(options, url);
if (options.agent !== undefined && typeof options.agent === "object") {
modifiedOptions.agent = new http.Agent({ keepAlive: true });
copyAgentData(options.agent, modifiedOptions.agent);

if (url !== null && url !== undefined) {
if (this.lunarConnect.isProxyListening() === undefined || this.lunarConnect.isProxyListening() === true) {
if (this._failSafe.stateOk() && this._trafficFilter.isAllowed(url.host, options.headers)) {
logger.debug(`Forwarding the request to ${url.href} using Lunar Proxy`);

modifiedOptions = this.generateModifiedOptions(options, url);
if (modifiedOptions.isURLValid === true) {
if (options.agent !== undefined && typeof options.agent === "object") {
modifiedOptions.agent = new http.Agent({ keepAlive: true });
copyAgentData(options.agent, modifiedOptions.agent);
}

const lunarRequest = new LunarRequest({ scheme, functionName, arg0, arg1, arg2, args },
modifiedOptions, callback, this._failSafe, this.originalFunctions);

lunarRequest.startRequest();
return lunarRequest.getFacade();
}
}

const lunarRequest = new LunarRequest({ scheme, functionName, arg0, arg1, arg2, args },
modifiedOptions, callback, this._failSafe, this.originalFunctions);

lunarRequest.startRequest();
return lunarRequest.getFacade();
} else if (this.lunarConnect.isProxyListening() === false) {
logger.debug('HTTP(S) request is being processed without Lunar Proxy as Lunar Proxy is not listening');
this.removeHooks();
}
} else if (this.lunarConnect.isProxyListening() === false) {
logger.debug('HTTP(S) request is being processed without Lunar Proxy as Lunar Proxy is not listening');
this.removeHooks();
}

logger.debug(`Forwarding the request to ${url.href} using the original function`);
}
logger.debug(`Forwarding the request using the original function`);
const originalFunction = this.getFunctionFromMap(scheme, functionName);
// @ts-expect-error: TS2345
return originalFunction(arg0, arg1, arg2, ...args);
Expand Down Expand Up @@ -277,7 +281,14 @@ class LunarInterceptor {
}
modifiedOptions.ID = generateUUID();
const modifiedURL = generateUrl(modifiedOptions, modifiedOptions.protocol)
logger.debug(`Modified request URL to: ${modifiedURL.href}`)
if (modifiedURL == null) {
logger.debug("Could not determine the host")
modifiedOptions.isURLValid = false
} else {
logger.debug(`Modified request URL to: ${modifiedURL.href}`)
modifiedOptions.isURLValid = true
}

modifiedOptions.href = modifiedURL
this.manipulateHeaders(modifiedOptions, url)

Expand Down
1 change: 1 addition & 0 deletions interceptors/lunar-ts-interceptor/src/lunarObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface LunarOptions extends RequestOptions {
href?: URL | null | undefined
pathname?: string | null | undefined
ID?: string
isURLValid?: boolean
}

export enum LunarType {
Expand Down

0 comments on commit e858f6d

Please # to comment.