Skip to content

fix issue to download CLI due to PROXY #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"async-mutex": "^0.5.0",
"axios": "^1.7.7",
"https-proxy-agent": "^7.0.6",
"log4js": "^6.9.1",
"node-fetch": "^3.3.2",
"tar": "^7.4.3",
Expand Down
35 changes: 12 additions & 23 deletions src/main/client/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, {AxiosRequestConfig, AxiosResponse} from 'axios';
import {logger} from '../wrapper/loggerConfig';
import {Client} from "./Client";
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { logger } from '../wrapper/loggerConfig';
import { Client } from "./Client";
import { HttpsProxyAgent } from 'https-proxy-agent'; // Import as a function

export class HttpClient implements Client {
private readonly axiosConfig: AxiosRequestConfig;
Expand All @@ -9,47 +10,35 @@ export class HttpClient implements Client {
constructor() {
this.axiosConfig = {
responseType: 'stream',
proxy: this.getProxyConfig(),
httpsAgent: this.getProxyConfig(), // Use httpsAgent instead of proxy
};
}

public getProxyConfig() {
const proxyUrl = process.env.HTTP_PROXY;
if (proxyUrl) {
logger.info(`Detected proxy configuration in HTTP_PROXY`);
const parsedProxy = new URL(proxyUrl);

return {
host: parsedProxy.hostname,
port: parseInt(parsedProxy.port, 10),
protocol: parsedProxy.protocol.replace(':', ''), // remove the colon
auth: parsedProxy.username && parsedProxy.password
? {username: parsedProxy.username, password: parsedProxy.password}
: undefined,
};
return new HttpsProxyAgent(proxyUrl);
}
logger.info('No proxy configuration detected.');
return undefined;
}

public async request(url: string, method: string, data: any): Promise<AxiosResponse<any, any>> {
logger.info(`Sending ${method} request to URL: ${url}`);
if (this.axiosConfig.proxy) {
logger.info(
`Using proxy - Host: ${this.axiosConfig.proxy.host}, Port: ${this.axiosConfig.proxy.port},` +
`Protocol: ${this.axiosConfig.proxy.protocol}, Auth: ${this.axiosConfig.proxy.auth ? 'Yes' : 'No'}`
);
if (this.axiosConfig.httpsAgent) {
logger.info(`Using proxy - URL: ${process.env.HTTP_PROXY}`);
}
try {
const response = await axios({...this.axiosConfig, url, method, data});
const response = await axios({ ...this.axiosConfig, url, method, data });
logger.info(`Request completed successfully.`);
return response;
} catch (error) {
logger.error(`Error sending ${method} request to ${url}: ${error.message || error}`);
if (this.axiosConfig.proxy!==undefined) {
if (this.axiosConfig.httpsAgent !== undefined) {
throw new Error(`${this.domainErrMsg} \nError: ${error.message || error}`);
}
throw error;
}
}
}
}
Loading