def test_tls_with_sni(self, sni):
        address = ('127.0.0.1', 0)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(address)
        sock.listen()
        address = sock.getsockname()

        def client_run():
            ctx = ssl.create_default_context()
            ctx.check_hostname = False
            ctx.verify_mode = ssl.CERT_NONE
            s = socket.create_connection(address)
            s = ctx.wrap_socket(s, server_hostname=sni)
            s.send(b'foobar')
            s.close()

        threading.Thread(target=client_run).start()

        connection, client_address = sock.accept()
        c = connections.ClientConnection(connection, client_address, None)

        cert = tutils.test_data.path("mitmproxy/net/data/server.crt")
        with open(tutils.test_data.path("mitmproxy/net/data/server.key")) as f:
            raw_key = f.read()
        key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM,
                                             raw_key)
        c.convert_to_tls(cert, key)
        assert c.connected()
        assert c.sni == sni
        assert c.tls_established
        assert c.rfile.read(6) == b'foobar'
        c.finish()
        sock.close()
Example #2
0
 def __init__(self, client_conn, client_address, config, channel):
     self.config: config.ProxyConfig = config
     self.client_conn = connections.ClientConnection(
         client_conn, client_address, None)
     """@type: mitmproxy.proxy.connection.ClientConnection"""
     self.channel = channel
     """@type: mitmproxy.controller.Channel"""
Example #3
0
    def test_tls_with_sni(self, sni, tdata):
        address = ('127.0.0.1', 0)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(address)
        sock.listen()
        address = sock.getsockname()

        def client_run():
            ctx = ssl.create_default_context()
            ctx.check_hostname = False
            ctx.verify_mode = ssl.CERT_NONE
            s = socket.create_connection(address)
            s = ctx.wrap_socket(s, server_hostname=sni)
            s.send(b'foobar')
            # we need to wait for the test to finish successfully before calling .close() on Windows.
            # The workaround here is to signal completion by sending data the other way around.
            s.recv(3)
            s.close()

        threading.Thread(target=client_run).start()

        connection, client_address = sock.accept()
        c = connections.ClientConnection(connection, client_address, None)

        cert = tdata.path("mitmproxy/net/data/server.crt")
        with open(tdata.path("mitmproxy/net/data/server.key")) as f:
            raw_key = f.read()
        key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM,
                                             raw_key)
        c.convert_to_tls(cert, key)
        assert c.connected()
        assert c.sni == sni
        assert c.tls_established
        assert c.rfile.read(6) == b'foobar'
        c.wfile.send(b"foo")
        c.finish()
        sock.close()