better-https-proxy-agent
An agent for HTTPS through an HTTP(S) proxy server using the CONNECT method.
This is similar to https-proxy-agent
but it leverages functionality available in the NodeJS http.Agent,
https.Agent, http.request
and/or https.request
to provide:
- connection pooling
- timeout
- TLS session resumption
- authentication (basic or client certificate)
- TLS options
- potentially anything else the NodeJS modules support
All of the above apply both to connections to the proxy, as well as
connections through the proxy.
It was partly inspired by this
blog post
and related gist.
Basic usage
npm install better-https-proxy-agent
const { Agent } = require('better-https-proxy-agent');
const fs = require('fs');
const https = require('https');
const httpsAgentOptions = {
keepAlive: true,
timeout: 55000,
maxSockets: 20,
maxFreeSockets: 5,
maxCachedSessions: 500
};
const proxyRequestOptions = {
protocol: "https:",
host: "proxy.example.com",
port: 3128,
timeout: 123000,
maxSockets: 100,
cert: fs.readFileSync("proxy_auth_cert.pem"),
key: fs.readFileSync("proxy_auth_key.pem"),
passphrase: "secret"
};
const agent = new Agent(httpsAgentOptions, proxyRequestOptions);
https.request("https://api.example.com", {
agent,
cert: fs.readFileSync("api_auth_cert.pem"),
key: fs.readFileSync("api_auth_key.pem"),
passphrase: "secret"
});
Caveats
You can get yourself into a bit of trouble if you use maxSockets
to limit the
connections to the proxy, and also pool connections through the proxy. You
can tie up all your proxy connections with connections through to a particular
host, or few hosts (say api.example.com
): the connections through the proxy
will be pooled and remain open, holding the corresponding connections to the
proxy open also. When you go to make a new connection to a different host (say
to www.example.com
) no connection through the proxy can be reused, and no
new connection to the proxy can be opened either. This module isn't smart
enough to close pooled connections through the proxy so that you can open a
new connection to the proxy.
The timeout
that is set on an HTTPS request that uses the proxy agent will be
used to set the 'request timeout' (for requests through the proxy), including
how long to wait for the proxy to connect to the target server, after the
connection to the proxy has already been made. The timeout
in the
proxyRequestOptions
(or agent
provided in the proxyRequestOptions
)
controls how long to wait for a connection to be made to the proxy itself.
Since it is a two-step process to connect to the proxy and then connect
through the proxy, these two timeouts are cumulative, which may not be what
the caller of https.request
expects.
Furthermore, the timeout
in the proxyRequestOptions
(or agent
provided in
proxyRequestOptions
) applies to inactivity on the proxy connection. This can
occur during requests through the proxy, or between them if keepAlive
is
true
in the httpsAgentOptions
. More directly, the timeout for in-flight
requests through the proxy is governed by any timeout
option set on them,
and the timeout for kept-alive connections is governed by timeout
in the
httpsAgentOptions
. Consequently, timeout
in proxyRequestOptions
should be
set higher than both of these other timeouts or it will gazump them, cutting
them short.
Licence
MIT.