Curl 和系统代理

Curl 和系统代理

我目前正在通过 ssh 隧道转发所有流量:

ssh -ND 8080 user@remote-machine

并将系统 socks5 代理(在系统设置中的网络部分)设置为127.0.0.1:8080。一切正常,Google Chrome 和其他程序都按预期使用 socks 代理。

当我尝试使用curl(在终端中或在 中使用它PHP)时出现问题。我得到的是:

user@laptop:~$ curl -v google.com
* About to connect() to proxy 127.0.0.1 port 8080 (#0)
*   Trying 127.0.0.1... connected
> GET HTTP://google.com HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: google.com
> Accept: */*
> Proxy-Connection: Keep-Alive
> 
* Empty reply from server
* Connection #0 to host 127.0.0.1 left intact
curl: (52) Empty reply from server
* Closing connection #0

我尝试在 Google 上搜索,但找不到任何相关信息。那么,如何让 curl 与系统代理(如果存在)配合使用?我希望能够设置一个适用于两种情况(代理和无代理)的别名,因为我并不总是愿意使用代理。

答案1

对于 HTTP 代理,使用-x

$HOME/.curlrc一个

-x 127.0.0.1:8080

或者使用别名来curl -x 127.0.0.1:8080 ...作为代理使用。

对于 SOCKS 代理,查看man curl以下内容:

   --socks5 <host[:port]>
          Use the specified SOCKS5 proxy - but resolve the host name locally. 
          If the port number is not specified, it is assumed at port 1080.

          This option overrides any previous use of -x, --proxy, as they are mutually exclusive.

因此,使用curl --socks5 127.0.0.1:8080,即。curl --socks5 $socks_proxy

相关内容