如何查看、设置 Mac Terminal Proxy 代理。通过查询当前代理信息,并在命令行导出 HTTP_Proxy 变量即可。

查看代理信息

运行相关代理软件之后,一般会在系统层面直接替换当前的代理配置,如下:

命令行获取

参考:https://superuser.com/questions/48480/how-can-i-get-mac-os-xs-proxy-information-in-a-bash-script

我们能通过以下命令获取

~ system_profiler SPNetworkDataType | grep "SOCKS Proxy"
          SOCKS Proxy Enabled: No
          SOCKS Proxy Enabled: No
          SOCKS Proxy Enabled: Yes
          SOCKS Proxy Port: 13659
          SOCKS Proxy Server: 127.0.0.1
          SOCKS Proxy Enabled: No
          SOCKS Proxy Enabled: No
~

Terminal 设置代理

参考:https://unix.stackexchange.com/questions/71481/how-to-use-socks-proxy-for-commands-in-terminal-such-as-youtube-dl

  • 设置 http_proxy 和 https_proxy 为 socks 地址
    • 通过 export 设置,仅对当前 terminal session 有效
  • 查看设置变量
  • 测试 google 连通性

(base) ➜  ~ export http_proxy=socks5://127.0.0.1:13659 https_proxy=socks5://127.0.0.1:13659
(base) ➜  ~ echo $http_proxy
socks5://127.0.0.1:13659
(base) ➜  ~ curl https://www.google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many spe...

Sockets 代理 HTTP/HTTPS

  • npm i http-proxy-to-socks

  • npx hpts -s 127.0.0.1:13659 -p 8080

代码中的代理

const fetch = require('node-fetch-with-proxy');
fetch('https://www.google.com', { timeout: 5000 } )
  .then(d => {
    console.log('sucess')
  })
  .catch(err => {
    console.log('network error');
  })

运行:

$ node test/network.js
network error
$ HTTPS_PROXY=http://127.0.0.1:8080 HTTP_PROXY=http://127.0.0.1:8080 node test/network.js
sucess