简体   繁体   English

如何通过 SOCKS4 代理连接到 HTTPS 套接字服务器

[英]How to connect to HTTPS socket server via SOCKS4 proxy

I am trying to connect to my HTTPS server via SOCKS4 proxy, but it seems like it is not working.我正在尝试通过 SOCKS4 代理连接到我的 HTTPS 服务器,但它似乎无法正常工作。

Here is my code:这是我的代码:

headers = "GET / HTTP/1.1\r\nHost: domain.com\r\n\r\n"
import socks
import ssl
conn = socks.socksocket()
conn.set_proxy(socks.SOCKS4, host, port)
conn.connect(("domain.com", 443))
conn = ssl.create_default_context().wrap_socket(conn, server_hostname="domain.com")
conn.send(headers.encode())

After I run that code, I checked my website log and nothing happened.运行该代码后,我检查了我的网站日志,但没有任何反应。 But if I change the connect() port to 80, it worked.但是,如果我将connect()端口更改为 80,它就可以工作。

You are connect() 'ing the socks connection to the HTTPS server's TLS port before creating the ssl context.在创建ssl上下文之前,您正在connect()socks连接到 HTTPS 服务器的 TLS 端口。 Which is fine, however do_handshake() will not automatically be called on the returned SSLSocket to negotiate a TLS session with the HTTPS server.这很好,但是在返回的SSLSocket上不会自动调用do_handshake()来与 HTTPS 服务器协商 TLS session。 So, you will need to manually call do_handshake() after wrap_socket() returns and before calling send() , eg:因此,您需要在wrap_socket()返回之后和调用send()之前手动调用do_handshake() ) ,例如:

headers = "GET / HTTP/1.1\r\nHost: domain.com\r\n\r\n"
import socks
import ssl
conn = socks.socksocket()
conn.set_proxy(socks.SOCKS4, host, port)
conn.connect(("domain.com", 443))
conn = ssl.create_default_context().wrap_socket(conn, server_hostname="domain.com")
conn.do_handshake()
conn.send(headers.encode())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM