Skip to content

Instantly share code, notes, and snippets.

@niradler
Last active April 16, 2024 11:44
Show Gist options
  • Save niradler/4e1eab3c5741c55620c033807e3f2212 to your computer and use it in GitHub Desktop.
Save niradler/4e1eab3c5741c55620c033807e3f2212 to your computer and use it in GitHub Desktop.
how to validate axios is compatible with my proxy
import { HttpsProxyAgent } from "https-proxy-agent";
import * as https from "https";
const { CAS_HTTPS_PROXY } = process.env;
const agent = new HttpsProxyAgent(CAS_HTTPS_PROXY);
const headers = {
"User-Agent": "axios/1.6.8",
"Accept-Encoding": "gzip, compress, deflate, br",
Accept: "application/json, text/plain, */*",
};
https.get("https://www.google.com", { agent, headers }, (res) => {
console.log("headers test", res.statusCode);
});
for (let header of Object.keys(headers)) {
const testHeader = {
[header]: headers[header],
};
console.log(testHeader);
https.get("https://www.google.com", { agent, headers: testHeader }, (res) => {
console.log("single header", header, res.statusCode);
});
}
https.get("https://www.google.com", { agent }, (res) => {
console.log("no headers test", res.statusCode);
});
import { HttpsProxyAgent } from "https-proxy-agent";
import axios from "axios";
const { CAS_HTTPS_PROXY } = process.env;
const agent = new HttpsProxyAgent(CAS_HTTPS_PROXY);
const api = axios.create({
httpsAgent: agent,
// remove hedaers
// headers:{
// "User-Agent": null,
// "Accept-Encoding": null,
// Accept: null
// }
});
api
.get("https://google.com")
.then((r) => {
console.log(r.status);
})
.catch((e) => {
console.error(e);
});
import requests
import os
session = requests.Session()
proxy_url = os.environ.get("CAS_HTTPS_PROXY")
https_proxy = os.environ.get("HTTPS_PROXY")
if proxy_url:
session.proxies = {"https": proxy_url}
if proxy_url or https_proxy:
print("using proxy")
res = session.get("https://www.google.com")
print(res.status_code)
import axios from "axios";
import https from "https";
import URL from "url";
import fs from "fs";
const { CAS_HTTPS_PROXY, CAS_HTTPS_CA } = process.env;
const url = URL.parse(CAS_HTTPS_PROXY);
const api = axios.create({
httpsAgent: new https.Agent({
ca: fs.readFileSync(CAS_HTTPS_CA),
}),
proxy: {
host: url.hostname,
port: parseInt(url.port, 10),
protocol: url.protocol,
},
});
api
.get("https://google.com")
.then((r) => {
console.log(r.status);
})
.catch((e) => {
console.error(e);
});
@niradler
Copy link
Author

niradler commented Apr 1, 2024

for general proxy with ssl termination (proxy-test.mjs)
How to run:

mkdir proxy-test
cd proxy-test
npm init -y
npm i axios

Now create a file with the content above proxy-test.mjs and run it with proxy details.

Example:
CAS_HTTPS_PROXY=http://localhost:8080 CAS_HTTPS_CA=/tls/proxy/ca.pem node proxy-test.mjs

@niradler
Copy link
Author

niradler commented Apr 2, 2024

for passthrough proxy without ssl termination (passthrough-proxy.mjs)
How to run:

mkdir proxy-test
cd proxy-test
npm init -y
npm i axios https-proxy-agent

Now create a file with the content above passthrough-proxy.mjs and run it with proxy details.

Example:
CAS_HTTPS_PROXY=http://localhost:8080 node proxy-test.mjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment