无法连接到Docker化的Tor代理

For*_*vin 5 tor docker alpine-linux

在Lubuntu上,我可以先安装Tor然后再连接到其袜子代理来使用tor,但是在带alpine的docker上,似乎并不那么容易。由于我按原样离开了/ etc / tor / torrc,所以它仅由注释掉的行组成。因此,对于高山用户,我只使用了torrc.sample仅包含注释掉的行的文件。

这是我的Dockerfile

FROM alpine:latest
RUN apk update && apk upgrade && apk add tor curl && rm /var/cache/apk/* && cp /etc/tor/torrc.sample /etc/tor/torrc 
EXPOSE 9050
USER tor
CMD /usr/bin/tor -f /etc/tor/torrc
Run Code Online (Sandbox Code Playgroud)

然后我就跑了:

$ sudo docker build -t tor .
$ sudo docker run --name tor -p 9050:9050 tor
$ curl -x socks5://localhost:9050 -k https://23.128.64.134/ip
curl: (7) Unable to receive initial SOCKS5 response.
$ curl -x socks4://localhost:9050 -k https://23.128.64.134/ip
curl: (7) Failed to receive SOCKS4 connect request ack.
Run Code Online (Sandbox Code Playgroud)

但是如您所见,我无法连接。既不通过袜子4也不通过袜子5。

我似乎无法弄清楚为什么这行不通。我已经尝试使用不同的端口和主机名(127.0.0.1代替localhost),但是没有任何作用。

我究竟做错了什么?

编辑:

有趣的是,这似乎起作用:

$ sudo docker exec -ti tor curl -x socks5://localhost:9050 -k https://23.128.64.134/ip
185.220.101.69
Run Code Online (Sandbox Code Playgroud)

(185.220.101.69实际上是Tor出口节点IP地址)

那么,这里可能出什么问题了?为什么我不能从外面访问它?甚至nmap报告它可以看到端口(在容器外部运行时):

9050/tcp open  tor-socks
Run Code Online (Sandbox Code Playgroud)

Edit2: 我将-v-flag 添加到curl,并通过启用了更多详细的登录信息echo "Log info stdout" > /etc/tor/torrc

当我从容器外部运行curl命令时,tor日志完全没有改变。curl输出也没有显示任何帮助:

$ curl -v -x socks5://localhost:9050 -k https://23.128.64.134/ip
*   Trying ::1:9050...
* TCP_NODELAY set
* SOCKS5 communication to 23.128.64.134:443
* Unable to receive initial SOCKS5 response.
* Closing connection 0
curl: (7) Unable to receive initial SOCKS5 response.

$ curl -v -x socks5://127.0.0.1:9050 -k https://23.128.64.134/ip
*   Trying 127.0.0.1:9050...
* TCP_NODELAY set
* SOCKS5 communication to 23.128.64.134:443
* Unable to receive initial SOCKS5 response.
* Closing connection 0
curl: (7) Unable to receive initial SOCKS5 response.
Run Code Online (Sandbox Code Playgroud)

For*_*vin 6

我设法弄清楚了。问题在于,Tor默认情况下不会绑定到所有接口(如中所述0.0.0.0),而该接口在Docker上无法很好地发挥作用。

可以通过添加到来SocksPort 0.0.0.0:9050解决此问题/etc/tor/torrc

因此解决方案是:

FROM alpine:latest
RUN apk update && apk upgrade && \
    apk add tor curl && \
    rm /var/cache/apk/* && \
    cp /etc/tor/torrc.sample /etc/tor/torrc && \
    echo "SocksPort 0.0.0.0:9050" > /etc/tor/torrc
EXPOSE 9050
USER tor
CMD /usr/bin/tor -f /etc/tor/torrc
Run Code Online (Sandbox Code Playgroud)

然后一切都按预期工作:

$ sudo docker build -t tor .
$ sudo docker run --name tor -p 9050:9050 tor
$ curl -x socks5://localhost:9050 https://ifconfig.io/ip
190.216.2.136
Run Code Online (Sandbox Code Playgroud)