Node httpAgent with proxy and certificates (https-proxy-agent with TLS)

5.2k views Asked by At

So I'm trying to make a call inside my node test (AVA) that uses proxy and TLS authorization. I'm using:

  • typescript: 3.9.3
  • ts-node: 8.10.2
  • axios: 0.21.1
  • https-proxy-agent: 5.0.0

What I've learnt so far:

So just to wrap up with some code, that's what I'm trying to do and I have no idea how to achieve that:

const httpsProxyAgent = new HttpsProxyAgent({
        cert: this.cert,
        key: this.key,
        ca: this.ca,
        host: PROXY_HOST,
        port: PROXY_PORT,
      });
// then later
const config: AxiosRequestConfig = {
  httpsAgent: httpsProxyAgent,
  headers: { ... }
  proxy: false
};

Though HttpsProxyAgent seems to extend Agent those options (certs part) are not used and I get UNABLE_TO_VERIFY_LEAF_SIGNATURE error that indicates that ca is ignored. Does anyone knows how to provide those certs to this agent? I couldn't find any answers. I'm not a node expert so I might have missed something obvious.

Thanks in advance!

PS. I've also tried Objects.assign() like this

// this proxy agent is working for sure (tested)
const httpsProxyAgent = new HttpsProxyAgent('http://proxy:1234');

// trying to assign certs after creating httpsProxyAgent
Object.assign(httpsAgent.options, {
  ca: this.ca,
  key: this.key,
  cert: this.cert
});

// then again passing it to AxiosRequestConfig.httpAgent and making a call

result was once again UNABLE_TO_VERIFY_LEAF_SIGNATURE.

PSS. I've seen this better-https-proxy-agent (git page) that seems to have solution, the only drawback is I cannot see any TS support.

1

There are 1 answers

0
Facundo Diaz Cobos On

You can add the certificates in the agent, and the proxy in the request call, something like this...

// First create the Agent
const agent = new https.Agent({
  ca: certificates...
});

// Then create an instance of Axios with the agent
const axiosInstance = axios.create({
  httpsAgent: agent
});

// And Last set the proxy options
const response: AxiosResponse = await axiosInstance.post(payload, {
  headers: {
    "Content-Type": "text/xml",
  },
  proxy: {
    host,
    port,
    protocol: "http://",
    auth: {
      username,
      password,
    },
  }
});