Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit a04228b

Browse files
committed
🐛Fix proxying https endpoint
1 parent 68d0436 commit a04228b

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

services/HttpClient.js

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,6 @@ const defaultRequestOptions = {
3535
throwHttpErrors: false,
3636
};
3737

38-
//Simple proxy tunnel for easier debug
39-
if (socksProxy) {
40-
const socksProtocol = 'socks:';
41-
defaultRequestOptions.agent = new SocksProxyAgent(
42-
String(socksProxy).startsWith(socksProtocol)
43-
? socksProxy
44-
: `${socksProtocol}//${socksProxy}`
45-
);
46-
} else if (httpProxy) {
47-
const parsedUrl = new URL(httpProxy);
48-
defaultRequestOptions.agent = tunnel.httpOverHttp({
49-
proxy: {
50-
host: parsedUrl.hostname,
51-
port: parsedUrl.port,
52-
},
53-
});
54-
}
55-
5638
/**
5739
* Proxy for apis or other http requests
5840
*/
@@ -70,23 +52,56 @@ class HttpClient {
7052
constructor(options = {}, requestOptions = {}) {
7153
this.endPoint = options.endPoint || defaultEndpoint;
7254
this.endPointHost = '';
55+
this.endPointParsedUrl = {};
7356
if (this.endPoint) {
74-
const parsedUrl = new URL(this.endPoint);
75-
this.endPointHost = parsedUrl.host;
57+
this.endPointParsedUrl = new URL(this.endPoint);
58+
this.endPointHost = this.endPointParsedUrl.host;
59+
}
60+
const clientBaseOptions = {
61+
baseUrl: this.endPoint,
62+
};
63+
const agent = this._getAgent();
64+
if (agent) {
65+
clientBaseOptions.agent = agent;
7666
}
7767
this.options = options;
7868
this.got = got.extend(
79-
Object.assign(
80-
{
81-
baseUrl: this.endPoint,
82-
},
83-
defaultRequestOptions,
84-
requestOptions
85-
)
69+
Object.assign(clientBaseOptions, defaultRequestOptions, requestOptions)
8670
);
8771
this.debugLevel = options.debugLevel || debugLevel;
8872
}
8973

74+
/**
75+
* Get the http agent for proxying or requesting
76+
* @return {*}
77+
* @private
78+
*/
79+
_getAgent() {
80+
let agent;
81+
//Simple proxy tunnel
82+
if (socksProxy) {
83+
const socksProtocol = 'socks:';
84+
agent = new SocksProxyAgent(
85+
String(socksProxy).startsWith(socksProtocol)
86+
? socksProxy
87+
: `${socksProtocol}//${socksProxy}`
88+
);
89+
} else if (httpProxy) {
90+
const parsedUrl = new URL(httpProxy);
91+
const tunnelOptions = {
92+
proxy: {
93+
host: parsedUrl.hostname,
94+
port: parsedUrl.port,
95+
},
96+
};
97+
agent =
98+
this.endPointParsedUrl.protocol === 'https:'
99+
? tunnel.httpsOverHttp(tunnelOptions)
100+
: tunnel.httpOverHttp(tunnelOptions);
101+
}
102+
return agent;
103+
}
104+
90105
/**
91106
* Customize real options for destination
92107
* @param ctx
@@ -159,9 +174,7 @@ class HttpClient {
159174
);
160175
} else {
161176
this._log(
162-
`[${gotOptions.method}][${
163-
gotOptions.href
164-
}] response body[${type}] length: ${ret.length}`
177+
`[${gotOptions.method}][${gotOptions.href}] response body[${type}] length: ${ret.length}`
165178
);
166179
}
167180
});

0 commit comments

Comments
 (0)