Skip to content

Commit be817cb

Browse files
authored
Merge pull request TooTallNate#1 from bonald/2022
2022
2 parents 78ea6e2 + 73185fd commit be817cb

4 files changed

Lines changed: 69 additions & 40 deletions

File tree

package-lock.json

Lines changed: 22 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "https-proxy-agent",
33
"version": "5.0.1",
44
"description": "An HTTP(s) proxy `http.Agent` implementation for HTTPS",
5-
"main": "dist/index",
6-
"types": "dist/index",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
77
"files": [
88
"dist"
99
],
@@ -30,24 +30,24 @@
3030
"url": "https://github.com/TooTallNate/node-https-proxy-agent/issues"
3131
},
3232
"dependencies": {
33-
"agent-base": "6",
33+
"agent-base": "6.0.2",
3434
"debug": "4"
3535
},
3636
"devDependencies": {
3737
"@types/debug": "4",
38-
"@types/node": "^12.20.48",
38+
"@types/node": "^14.0.20",
3939
"@typescript-eslint/eslint-plugin": "1.6.0",
4040
"@typescript-eslint/parser": "1.1.0",
4141
"eslint": "5.16.0",
4242
"eslint-config-airbnb": "17.1.0",
4343
"eslint-config-prettier": "4.1.0",
4444
"eslint-import-resolver-typescript": "1.1.1",
45-
"eslint-plugin-import": "2.16.0",
45+
"eslint-plugin-import": "2.17.2",
4646
"eslint-plugin-jsx-a11y": "6.2.1",
4747
"eslint-plugin-react": "7.12.4",
4848
"mocha": "^6.2.2",
4949
"proxy": "1",
50-
"rimraf": "^3.0.0",
50+
"rimraf": "^3.0.2",
5151
"typescript": "^3.5.3"
5252
},
5353
"engines": {

src/agent.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import net from 'net';
2-
import tls from 'tls';
1+
import * as net from 'net';
2+
import * as tls from 'tls';
33
import url from 'url';
44
import assert from 'assert';
55
import createDebug from 'debug';
@@ -28,6 +28,8 @@ export default class HttpsProxyAgent extends Agent {
2828
private secureProxy: boolean;
2929
private secureContext: tls.SecureContext | undefined;
3030
private proxy: HttpsProxyAgentOptions;
31+
public timeout: number | null;
32+
3133

3234
constructor(_opts: string | HttpsProxyAgentOptions) {
3335
let opts: HttpsProxyAgentOptions;
@@ -44,6 +46,8 @@ export default class HttpsProxyAgent extends Agent {
4446
debug('creating new HttpsProxyAgent instance: %o', opts);
4547
super(opts);
4648

49+
this.timeout = opts.timeout || null;
50+
4751
const proxy: HttpsProxyAgentOptions = { ...opts };
4852

4953
// If `true`, then connect to the proxy server over TLS.
@@ -99,6 +103,14 @@ export default class HttpsProxyAgent extends Agent {
99103
socket = net.connect(proxy as net.NetConnectOpts);
100104
}
101105

106+
if (this.timeout) {
107+
socket.setTimeout(this.timeout);
108+
109+
socket.on('timeout', () => {
110+
socket.end();
111+
});
112+
}
113+
102114
const headers: OutgoingHttpHeaders = { ...proxy.headers };
103115
const hostname = `${opts.host}:${opts.port}`;
104116
let payload = `CONNECT ${hostname} HTTP/1.1\r\n`;
@@ -112,13 +124,15 @@ export default class HttpsProxyAgent extends Agent {
112124

113125
// The `Host` header should only include the port
114126
// number when it is not the default port.
115-
let { host, port, secureEndpoint } = opts;
116-
if (!isDefaultPort(port, secureEndpoint)) {
117-
host += `:${port}`;
118-
}
119-
headers.Host = host;
127+
//let { host, port, secureEndpoint } = opts;
128+
//if (!isDefaultPort(port, secureEndpoint)) {
129+
// host += `:${port}`;
130+
//}
131+
//headers.Host = host;
120132

133+
headers.Host = hostname;
121134
headers.Connection = 'close';
135+
122136
for (const name of Object.keys(headers)) {
123137
payload += `${name}: ${headers[name]}\r\n`;
124138
}
@@ -140,12 +154,23 @@ export default class HttpsProxyAgent extends Agent {
140154
// this socket connection to a TLS connection.
141155
debug('Upgrading socket connection to TLS');
142156
const servername = opts.servername || opts.host;
143-
return tls.connect({
157+
const tlsSocket = tls.connect({
144158
...omit(opts, 'host', 'hostname', 'path', 'port'),
145159
socket,
146160
servername,
147161
secureContext: this.secureContext
148162
});
163+
return new Promise((resolve, reject) => {
164+
const errCb = (err: Error) => {
165+
reject(err);
166+
};
167+
tlsSocket.once('error', errCb);
168+
169+
tlsSocket.once('secureConnect', () => {
170+
resolve(tlsSocket);
171+
tlsSocket.off('error', errCb);
172+
});
173+
});
149174
}
150175

151176
return socket;
@@ -199,10 +224,11 @@ function omit<T extends object, K extends [...(keyof T)[]]>(
199224
obj: T,
200225
...keys: K
201226
): {
202-
[K2 in Exclude<keyof T, K[number]>]: T[K2];
203-
} {
204-
const ret = {} as {
205-
[K in keyof typeof obj]: (typeof obj)[K];
227+
[K2 in Exclude<keyof T, K[number]>]: T[K2];
228+
} {
229+
const ret = {} as {
230+
[K in keyof typeof obj]: typeof obj[K];
231+
206232
};
207233
let key: keyof typeof obj;
208234
for (key in obj) {

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import net from 'net';
2-
import tls from 'tls';
1+
import * as net from 'net';
2+
import * as tls from 'tls';
33
import { Url } from 'url';
44
import { AgentOptions } from 'agent-base';
55
import { OutgoingHttpHeaders } from 'http';
@@ -19,6 +19,7 @@ namespace createHttpsProxyAgent {
1919
path?: string | null;
2020
port?: string | number | null;
2121
secureContext?: tls.SecureContext | null;
22+
timeout?: number;
2223
}
2324

2425
export interface HttpsProxyAgentOptions

0 commit comments

Comments
 (0)