Proxy not working over SSL connection

北城余情 提交于 2019-12-04 06:23:51

问题


I'm trying to use tor, socksipy and ssl to proxy a ssl connection. My client looks like this:

import socks, ssl
s = socks.socksocket()
s.setproxy(socks.PROXY_TYPE_SOCKS5,"127.0.0.1", 9050)
ssl_sock = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1)
ssl_sock.connect(('127.0.0.1', 443))

The server just accepts connections and prints getpeername.

The peer name is always 127.0.0.1. It doesn't even matter if I give it a non-valid proxy. The client won't complain, it will connect anyway.

How do I make it connect through the proxy?


回答1:


I managed to figure it out so I will leave the answer here for future reference.

The first problem was that I tried to connect to 127.0.0.1. As the request was proxied, the proxy would try to connect to 127.0.0.1, so it would try to connect to itself, not to me. I had to configure my router to forward requests on port 443 to my laptop and then I replaced 127.0.0.1 with my routers IP.

After that was out of the way, I found out that socksipy doesn't play very well with ssl. I had to call connect on the socket before wrapping it, otherwise I'd get a handshake failure. The code became:

import socks, ssl
s = socks.socksocket()
s.setproxy(socks.PROXY_TYPE_SOCKS5,"127.0.0.1", 9050)
s.connect(('127.0.0.1', 443))
ssl_sock = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1)

After that, everything was working fine.



来源:https://stackoverflow.com/questions/25190320/proxy-not-working-over-ssl-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!