near / near-cli

General purpose command line tools for interacting with NEAR Protocol
https://docs.near.org
MIT License
193 stars 92 forks source link

feat: Add proxy settings #1067

Open xiangxn opened 9 months ago

xiangxn commented 9 months ago

you only need to set the environment variable: export GLOBAL_AGENT_HTTP_PROXY="http://127.0.0.1:8001"

View in detail: https://github.com/gajus/global-agent

think-in-universe commented 9 months ago

This is great! This fix makes near-cli usable for developers who need http(s) proxies to visit NEAR RPC and helper services.

Can you make some enhancements to support HTTP_PROXY, HTTPS_PROXY and NO_PROXY environment variables?

xiangxn commented 9 months ago

These are all supported, please check for details: https://github.com/gajus/global-agent

think-in-universe commented 9 months ago

I mean after import global agent, you can configure the global agent environment variables, so near-cli acts more like a normal process that respects the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY.

function setProxies() {
  const http_proxy = process.env.http_proxy || process.env.HTTP_PROXY;
  if (http_proxy) {
    global.GLOBAL_AGENT.HTTP_PROXY = http_proxy;
  }

  const https_proxy = process.env.https_proxy || process.env.HTTPS_PROXY;
  if (https_proxy) {
    global.GLOBAL_AGENT.HTTPS_PROXY = https_proxy;
  }

  const no_proxy = process.env.no_proxy || process.env.NO_PROXY;
  if (no_proxy) {
    global.GLOBAL_AGENT.NO_PROXY = no_proxy;
  }
}
xiangxn commented 9 months ago

I mean after import global agent, you can configure the global agent environment variables, so near-cli acts more like a normal process that respects the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY.

function setProxies() {
  const http_proxy = process.env.http_proxy || process.env.HTTP_PROXY;
  if (http_proxy) {
    global.GLOBAL_AGENT.HTTP_PROXY = http_proxy;
  }

  const https_proxy = process.env.https_proxy || process.env.HTTPS_PROXY;
  if (https_proxy) {
    global.GLOBAL_AGENT.HTTPS_PROXY = https_proxy;
  }

  const no_proxy = process.env.no_proxy || process.env.NO_PROXY;
  if (no_proxy) {
    global.GLOBAL_AGENT.NO_PROXY = no_proxy;
  }
}

Updated to support these environment variables by default.