HTTPS proxy tunneling with the ssl module

后端 未结 5 1120
耶瑟儿~
耶瑟儿~ 2021-01-02 01:51

I\'d like to manually (using the socket and ssl modules) make an HTTPS request through a proxy which itself uses HTTPS.

I can perform the i

5条回答
  •  醉梦人生
    2021-01-02 02:22

    Building on @kravietz answer. Here is a version that works in Python3 through a Squid proxy:

    from OpenSSL import SSL
    import socket
    
    def verify_cb(conn, cert, errun, depth, ok):
            return True
    
    server = 'mail.google.com'
    port = 443
    PROXY_ADDR = ("", 3128)
    CONNECT = "CONNECT %s:%s HTTP/1.0\r\nConnection: close\r\n\r\n" % (server, port)
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(PROXY_ADDR)
    s.send(str.encode(CONNECT))
    s.recv(4096)
    
    ctx = SSL.Context(SSL.SSLv23_METHOD)
    ctx.set_verify(SSL.VERIFY_PEER, verify_cb)
    ss = SSL.Connection(ctx, s)
    
    ss.set_connect_state()
    ss.do_handshake()
    cert = ss.get_peer_certificate()
    print(cert.get_subject())
    ss.shutdown()
    ss.close()
    

    This works in Python 2 also.

提交回复
热议问题