mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-02-02 00:05:27 +00:00
improve alpn handling
This commit is contained in:
parent
1093d185ec
commit
aebe342025
@ -78,13 +78,14 @@ class Http2Layer(Layer):
|
|||||||
body_size_limit=self.config.body_size_limit
|
body_size_limit=self.config.body_size_limit
|
||||||
)
|
)
|
||||||
self._stream_id = request.stream_id
|
self._stream_id = request.stream_id
|
||||||
|
return request
|
||||||
|
|
||||||
def read_from_server(self, request_method):
|
def read_from_server(self, request_method):
|
||||||
return HTTPResponse.from_protocol(
|
return HTTPResponse.from_protocol(
|
||||||
self.server_protocol,
|
self.server_protocol,
|
||||||
request_method,
|
request_method,
|
||||||
body_size_limit=self.config.body_size_limit,
|
body_size_limit=self.config.body_size_limit,
|
||||||
include_body=False,
|
include_body=True,
|
||||||
stream_id=self._stream_id
|
stream_id=self._stream_id
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -389,9 +390,11 @@ class HttpLayer(Layer):
|
|||||||
if flow is None or flow == KILL:
|
if flow is None or flow == KILL:
|
||||||
raise Kill()
|
raise Kill()
|
||||||
|
|
||||||
if flow.response.stream:
|
if isinstance(self.ctx, Http2Layer):
|
||||||
|
pass # streaming is not implemented for http2 yet.
|
||||||
|
elif flow.response.stream:
|
||||||
flow.response.content = CONTENT_MISSING
|
flow.response.content = CONTENT_MISSING
|
||||||
elif isinstance(self.server_protocol, http1.HTTP1Protocol):
|
else:
|
||||||
flow.response.content = self.server_protocol.read_http_body(
|
flow.response.content = self.server_protocol.read_http_body(
|
||||||
flow.response.headers,
|
flow.response.headers,
|
||||||
self.config.body_size_limit,
|
self.config.body_size_limit,
|
||||||
|
@ -87,10 +87,9 @@ class TlsLayer(Layer):
|
|||||||
self.log("Unknown Server Name Indication: %s" % extension.server_names, "error")
|
self.log("Unknown Server Name Indication: %s" % extension.server_names, "error")
|
||||||
self.client_sni = extension.server_names[0].name
|
self.client_sni = extension.server_names[0].name
|
||||||
elif extension.type == 0x10:
|
elif extension.type == 0x10:
|
||||||
self.client_alpn_protocols = extension.alpn_protocols
|
self.client_alpn_protocols = list(extension.alpn_protocols)
|
||||||
|
|
||||||
print("sni: %s" % self.client_sni)
|
self.log("Parsed Client Hello: sni=%s, alpn=%s" % (self.client_sni, self.client_alpn_protocols), "debug")
|
||||||
print("alpn: %s" % self.client_alpn_protocols)
|
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
if not self.server_conn:
|
if not self.server_conn:
|
||||||
@ -131,10 +130,13 @@ class TlsLayer(Layer):
|
|||||||
# alpn_preference = netlib.http.http2.HTTP2Protocol.ALPN_PROTO_H2
|
# alpn_preference = netlib.http.http2.HTTP2Protocol.ALPN_PROTO_H2
|
||||||
|
|
||||||
if self.alpn_for_client_connection in options:
|
if self.alpn_for_client_connection in options:
|
||||||
return bytes(self.alpn_for_client_connection)
|
choice = bytes(self.alpn_for_client_connection)
|
||||||
if default_alpn in options:
|
elif default_alpn in options:
|
||||||
return bytes(default_alpn)
|
choice = bytes(default_alpn)
|
||||||
return options[0]
|
else:
|
||||||
|
choice = options[0]
|
||||||
|
self.log("ALPN for client: %s" % choice, "debug")
|
||||||
|
return choice
|
||||||
|
|
||||||
def _establish_tls_with_client(self):
|
def _establish_tls_with_client(self):
|
||||||
self.log("Establish TLS with client", "debug")
|
self.log("Establish TLS with client", "debug")
|
||||||
@ -156,6 +158,12 @@ class TlsLayer(Layer):
|
|||||||
def _establish_tls_with_server(self):
|
def _establish_tls_with_server(self):
|
||||||
self.log("Establish TLS with server", "debug")
|
self.log("Establish TLS with server", "debug")
|
||||||
try:
|
try:
|
||||||
|
# We only support http/1.1 and h2.
|
||||||
|
# If the server only supports spdy (next to http/1.1), it may select that
|
||||||
|
# and mitmproxy would enter TCP passthrough mode, which we want to avoid.
|
||||||
|
deprecated_http2_variant = lambda x: x.startswith("h2-") or x.startswith("spdy")
|
||||||
|
alpn = filter(lambda x: not deprecated_http2_variant(x), self.client_alpn_protocols)
|
||||||
|
|
||||||
self.server_conn.establish_ssl(
|
self.server_conn.establish_ssl(
|
||||||
self.config.clientcerts,
|
self.config.clientcerts,
|
||||||
self.sni_for_server_connection,
|
self.sni_for_server_connection,
|
||||||
@ -165,7 +173,7 @@ class TlsLayer(Layer):
|
|||||||
ca_path=self.config.openssl_trusted_cadir_server,
|
ca_path=self.config.openssl_trusted_cadir_server,
|
||||||
ca_pemfile=self.config.openssl_trusted_ca_server,
|
ca_pemfile=self.config.openssl_trusted_ca_server,
|
||||||
cipher_list=self.config.ciphers_server,
|
cipher_list=self.config.ciphers_server,
|
||||||
alpn_protos=self.client_alpn_protocols,
|
alpn_protos=alpn,
|
||||||
)
|
)
|
||||||
tls_cert_err = self.server_conn.ssl_verification_error
|
tls_cert_err = self.server_conn.ssl_verification_error
|
||||||
if tls_cert_err is not None:
|
if tls_cert_err is not None:
|
||||||
@ -185,6 +193,8 @@ class TlsLayer(Layer):
|
|||||||
except tcp.NetLibError as e:
|
except tcp.NetLibError as e:
|
||||||
raise ProtocolException(repr(e), e)
|
raise ProtocolException(repr(e), e)
|
||||||
|
|
||||||
|
self.log("ALPN selected by server: %s" % self.alpn_for_client_connection, "debug")
|
||||||
|
|
||||||
def _find_cert(self):
|
def _find_cert(self):
|
||||||
host = self.server_conn.address.host
|
host = self.server_conn.address.host
|
||||||
sans = set()
|
sans = set()
|
||||||
|
Loading…
Reference in New Issue
Block a user