node-apn / node-apn

:calling: Apple Push Notification module for Node.js
MIT License
4.37k stars 681 forks source link

Improve proxy support #717

Open MatsAnd opened 1 year ago

MatsAnd commented 1 year ago

Hi, I ran into some problems when using this package in Kubernetes using a HTTP proxy that requires authentication, so I made some improvements to the HTTP proxy implementation.

Improvements:

  1. Added optional username and password proxy options. When specified this will set the Proxy-Authorization header with basic authentication.

    var options = {
      token: {
        key: "path/to/APNsAuthKey_XXXXXXXXXX.p8",
        keyId: "key-id",
        teamId: "developer-team-id"
      },
      proxy: {
        host: "192.168.10.92",
        port: 8080,
        username: "secretUsername",
        password: "secretPassword"
      },
      production: false
    };
    
    var apnProvider = new apn.Provider(options);
  2. Added support for retrieving HTTP proxy config from environment variables, instead of needing to specify it in options. This is useful in environments where HTTP proxy is configured automatically through Kubernetes for example. This also follows a normal convention across tools and other request libraries, and is normally the expected behaviour (reference). When one of the following environment variables is set we're using this value to connect (in this order):

    1. apn_proxy
    2. npm_config_[http/https]_proxy (npm config variable for http or https proxy)
    3. [http/https]_proxy
    4. all_proxy
    5. npm_config_proxy (npm config variable for general proxy)
    6. proxy

    To completely disable proxies and ignore environment variables set { proxy: false }. This is similar to how axios is handling proxy config.

  3. Added support for the no_proxy environment variable that makes sure urls that shouldn't be proxied isn't. This is also a commonly supported variable across tools, and is become a convention. (Reference).

MatsAnd commented 1 year ago

@florianreinhart I'm hoping you could review my pull request when you have a moment. If possible, I'd love to see this included in an upcoming release soon. :)