GithubHelp home page GithubHelp logo

insightfuls / better-https-proxy-agent Goto Github PK

View Code? Open in Web Editor NEW
11.0 1.0 7.0 192 KB

An agent for HTTPS through an HTTP(S) proxy server using the CONNECT method

License: MIT License

JavaScript 100.00%

better-https-proxy-agent's Introduction

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');

/*
 * Options suitable for `https.Agent`. These are applied to the HTTPS agent which
 * manages the proxied connections between the client and the final endpoint.
 */
const httpsAgentOptions = {
    keepAlive: true,
    timeout: 55000,
    maxSockets: 20,
    maxFreeSockets: 5,
    maxCachedSessions: 500
};

/*
 * Options suitable for `http(s).request`. These are used to make the request to
 * the proxy server.
 *
 * You should provide `host` here (unless you want the default `localhost`).
 *
 * `protocol` will default to `http:`. If you need to connect to the proxy over
 * HTTPS, set `protocol` to `https:`. `http.request` or `https.request` will be
 * used accordingly.
 *
 * You may include an `agent` here; if you do not, a default agent will be
 * constructed which will be reused for connections to the proxy. If you wish
 * to use the global agent, explicitly provide `http(s).globalAgent`.
 *
 * Using an agent for connection pooling will not work, because connections use
 * the HTTP CONNECT method; such connection cannot be reused, and furthermore
 * are removed from any agent as soon as a response is received. However, an
 * additional `maxSockets` option is provided directly in this options object
 * for the purpose of limiting the number of concurrent connections to a proxy.
 */
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.

better-https-proxy-agent's People

Contributors

insightfuls avatar kierans avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

better-https-proxy-agent's Issues

PFX Support

Can I use it with pfx file instead of cert and key?

hi bro ,i use this lib throw a error

with superagent 5.x version

TypeError: Cannot read property 'apply' of undefined
    at callSocketMethod (_http_client.js:737:27)
    at ClientRequest.onSocket (_http_client.js:745:7)
    at Object.onceWrapper (events.js:300:26)
    at ClientRequest.emit (events.js:215:7)
    at tickOnSocket (_http_client.js:684:7)
    at onSocketNT (_http_client.js:723:5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.