mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
use new netlib exceptions
This commit is contained in:
parent
8fbed971ae
commit
14a5f405fd
@ -26,7 +26,7 @@ import random
|
|||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
from libmproxy.exceptions import TlsException
|
from libmproxy.exceptions import TlsProtocolException
|
||||||
from libmproxy.protocol import TlsLayer, RawTCPLayer
|
from libmproxy.protocol import TlsLayer, RawTCPLayer
|
||||||
|
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ class TlsFeedback(TlsLayer):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
super(TlsFeedback, self)._establish_tls_with_client()
|
super(TlsFeedback, self)._establish_tls_with_client()
|
||||||
except TlsException as e:
|
except TlsProtocolException as e:
|
||||||
tls_strategy.record_failure(server_address)
|
tls_strategy.record_failure(server_address)
|
||||||
raise e
|
raise e
|
||||||
else:
|
else:
|
||||||
|
@ -20,17 +20,17 @@ class ProtocolException(ProxyException):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TlsException(ProtocolException):
|
class TlsProtocolException(ProtocolException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ClientHandshakeException(TlsException):
|
class ClientHandshakeException(TlsProtocolException):
|
||||||
def __init__(self, message, server):
|
def __init__(self, message, server):
|
||||||
super(ClientHandshakeException, self).__init__(message)
|
super(ClientHandshakeException, self).__init__(message)
|
||||||
self.server = server
|
self.server = server
|
||||||
|
|
||||||
|
|
||||||
class Socks5Exception(ProtocolException):
|
class Socks5ProtocolException(ProtocolException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import six
|
|||||||
from netlib import tcp
|
from netlib import tcp
|
||||||
from ..models import ServerConnection
|
from ..models import ServerConnection
|
||||||
from ..exceptions import ProtocolException
|
from ..exceptions import ProtocolException
|
||||||
|
from netlib.exceptions import TcpException
|
||||||
|
|
||||||
|
|
||||||
class _LayerCodeCompletion(object):
|
class _LayerCodeCompletion(object):
|
||||||
@ -175,7 +176,7 @@ class ServerConnectionMixin(object):
|
|||||||
self.channel.ask("serverconnect", self.server_conn)
|
self.channel.ask("serverconnect", self.server_conn)
|
||||||
try:
|
try:
|
||||||
self.server_conn.connect()
|
self.server_conn.connect()
|
||||||
except tcp.NetLibError as e:
|
except TcpException as e:
|
||||||
six.reraise(
|
six.reraise(
|
||||||
ProtocolException,
|
ProtocolException,
|
||||||
ProtocolException(
|
ProtocolException(
|
||||||
|
@ -6,10 +6,10 @@ import traceback
|
|||||||
import six
|
import six
|
||||||
|
|
||||||
from netlib import tcp
|
from netlib import tcp
|
||||||
from netlib.exceptions import HttpException, HttpReadDisconnect
|
from netlib.exceptions import HttpException, HttpReadDisconnect, TcpException
|
||||||
from netlib.http import http1, Headers
|
from netlib.http import http1, Headers
|
||||||
from netlib.http import CONTENT_MISSING
|
from netlib.http import CONTENT_MISSING
|
||||||
from netlib.tcp import NetLibError, Address
|
from netlib.tcp import Address
|
||||||
from netlib.http.http2.connections import HTTP2Protocol
|
from netlib.http.http2.connections import HTTP2Protocol
|
||||||
from netlib.http.http2.frame import GoAwayFrame, PriorityFrame, WindowUpdateFrame
|
from netlib.http.http2.frame import GoAwayFrame, PriorityFrame, WindowUpdateFrame
|
||||||
from .. import utils
|
from .. import utils
|
||||||
@ -321,7 +321,7 @@ class HttpLayer(Layer):
|
|||||||
except HttpReadDisconnect:
|
except HttpReadDisconnect:
|
||||||
# don't throw an error for disconnects that happen before/between requests.
|
# don't throw an error for disconnects that happen before/between requests.
|
||||||
return
|
return
|
||||||
except (HttpException, NetLibError) as e:
|
except (HttpException, TcpException) as e:
|
||||||
self.send_error_response(400, repr(e))
|
self.send_error_response(400, repr(e))
|
||||||
six.reraise(ProtocolException, ProtocolException("Error in HTTP connection: %s" % repr(e)), sys.exc_info()[2])
|
six.reraise(ProtocolException, ProtocolException("Error in HTTP connection: %s" % repr(e)), sys.exc_info()[2])
|
||||||
|
|
||||||
@ -358,7 +358,7 @@ class HttpLayer(Layer):
|
|||||||
self.handle_upstream_mode_connect(flow.request.copy())
|
self.handle_upstream_mode_connect(flow.request.copy())
|
||||||
return
|
return
|
||||||
|
|
||||||
except (HttpException, NetLibError) as e:
|
except (HttpException, TcpException) as e:
|
||||||
self.send_error_response(502, repr(e))
|
self.send_error_response(502, repr(e))
|
||||||
|
|
||||||
if not flow.response:
|
if not flow.response:
|
||||||
@ -375,7 +375,7 @@ class HttpLayer(Layer):
|
|||||||
try:
|
try:
|
||||||
response = make_error_response(code, message)
|
response = make_error_response(code, message)
|
||||||
self.send_response(response)
|
self.send_response(response)
|
||||||
except NetLibError:
|
except TcpException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def change_upstream_proxy_server(self, address):
|
def change_upstream_proxy_server(self, address):
|
||||||
@ -423,7 +423,7 @@ class HttpLayer(Layer):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
get_response()
|
get_response()
|
||||||
except (tcp.NetLibError, HttpException) as v:
|
except (TcpException, HttpException) as v:
|
||||||
self.log(
|
self.log(
|
||||||
"server communication error: %s" % repr(v),
|
"server communication error: %s" % repr(v),
|
||||||
level="debug"
|
level="debug"
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
from __future__ import (absolute_import, print_function, division)
|
from __future__ import (absolute_import, print_function, division)
|
||||||
import threading
|
import threading
|
||||||
from libmproxy.exceptions import ReplayException
|
from libmproxy.exceptions import ReplayException
|
||||||
from netlib.exceptions import HttpException
|
from netlib.exceptions import HttpException, TcpException
|
||||||
from netlib.http import http1
|
from netlib.http import http1
|
||||||
|
|
||||||
from netlib.tcp import NetLibError
|
|
||||||
from ..controller import Channel
|
from ..controller import Channel
|
||||||
from ..models import Error, HTTPResponse, ServerConnection, make_connect_request
|
from ..models import Error, HTTPResponse, ServerConnection, make_connect_request
|
||||||
from .base import Kill
|
from .base import Kill
|
||||||
@ -89,7 +88,7 @@ class RequestReplayThread(threading.Thread):
|
|||||||
response_reply = self.channel.ask("response", self.flow)
|
response_reply = self.channel.ask("response", self.flow)
|
||||||
if response_reply == Kill:
|
if response_reply == Kill:
|
||||||
raise Kill()
|
raise Kill()
|
||||||
except (ReplayException, HttpException, NetLibError) as v:
|
except (ReplayException, HttpException, TcpException) as v:
|
||||||
self.flow.error = Error(repr(v))
|
self.flow.error = Error(repr(v))
|
||||||
if self.channel:
|
if self.channel:
|
||||||
self.channel.ask("error", self.flow)
|
self.channel.ask("error", self.flow)
|
||||||
|
@ -5,8 +5,9 @@ import six
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from OpenSSL import SSL
|
from OpenSSL import SSL
|
||||||
|
from netlib.exceptions import TcpException
|
||||||
|
|
||||||
from netlib.tcp import NetLibError, ssl_read_select
|
from netlib.tcp import ssl_read_select
|
||||||
from netlib.utils import clean_bin
|
from netlib.utils import clean_bin
|
||||||
from ..exceptions import ProtocolException
|
from ..exceptions import ProtocolException
|
||||||
from .base import Layer
|
from .base import Layer
|
||||||
@ -64,7 +65,7 @@ class RawTCPLayer(Layer):
|
|||||||
"info"
|
"info"
|
||||||
)
|
)
|
||||||
|
|
||||||
except (socket.error, NetLibError, SSL.Error) as e:
|
except (socket.error, TcpException, SSL.Error) as e:
|
||||||
six.reraise(
|
six.reraise(
|
||||||
ProtocolException,
|
ProtocolException,
|
||||||
ProtocolException("TCP connection closed unexpectedly: {}".format(repr(e))),
|
ProtocolException("TCP connection closed unexpectedly: {}".format(repr(e))),
|
||||||
|
@ -5,11 +5,11 @@ import sys
|
|||||||
|
|
||||||
from construct import ConstructError
|
from construct import ConstructError
|
||||||
import six
|
import six
|
||||||
|
from netlib.exceptions import InvalidCertificateException, TcpException, TlsException
|
||||||
|
|
||||||
from netlib.tcp import NetLibError, NetLibInvalidCertificateError
|
|
||||||
from netlib.http import ALPN_PROTO_HTTP1
|
from netlib.http import ALPN_PROTO_HTTP1
|
||||||
from ..contrib.tls._constructs import ClientHello
|
from ..contrib.tls._constructs import ClientHello
|
||||||
from ..exceptions import ProtocolException, TlsException, ClientHandshakeException
|
from ..exceptions import ProtocolException, TlsProtocolException, ClientHandshakeException
|
||||||
from .base import Layer
|
from .base import Layer
|
||||||
|
|
||||||
|
|
||||||
@ -295,11 +295,11 @@ class TlsLayer(Layer):
|
|||||||
while len(client_hello) < client_hello_size:
|
while len(client_hello) < client_hello_size:
|
||||||
record_header = self.client_conn.rfile.peek(offset + 5)[offset:]
|
record_header = self.client_conn.rfile.peek(offset + 5)[offset:]
|
||||||
if not is_tls_record_magic(record_header) or len(record_header) != 5:
|
if not is_tls_record_magic(record_header) or len(record_header) != 5:
|
||||||
raise TlsException('Expected TLS record, got "%s" instead.' % record_header)
|
raise TlsProtocolException('Expected TLS record, got "%s" instead.' % record_header)
|
||||||
record_size = struct.unpack("!H", record_header[3:])[0] + 5
|
record_size = struct.unpack("!H", record_header[3:])[0] + 5
|
||||||
record_body = self.client_conn.rfile.peek(offset + record_size)[offset + 5:]
|
record_body = self.client_conn.rfile.peek(offset + record_size)[offset + 5:]
|
||||||
if len(record_body) != record_size - 5:
|
if len(record_body) != record_size - 5:
|
||||||
raise TlsException("Unexpected EOF in TLS handshake: %s" % record_body)
|
raise TlsProtocolException("Unexpected EOF in TLS handshake: %s" % record_body)
|
||||||
client_hello += record_body
|
client_hello += record_body
|
||||||
offset += record_size
|
offset += record_size
|
||||||
client_hello_size = struct.unpack("!I", '\x00' + client_hello[1:4])[0] + 4
|
client_hello_size = struct.unpack("!I", '\x00' + client_hello[1:4])[0] + 4
|
||||||
@ -414,7 +414,7 @@ class TlsLayer(Layer):
|
|||||||
# The reason for this might be difficult to find, so we try to peek here to see if it
|
# The reason for this might be difficult to find, so we try to peek here to see if it
|
||||||
# raises ann error.
|
# raises ann error.
|
||||||
self.client_conn.rfile.peek(1)
|
self.client_conn.rfile.peek(1)
|
||||||
except NetLibError as e:
|
except TlsException as e:
|
||||||
six.reraise(
|
six.reraise(
|
||||||
ClientHandshakeException,
|
ClientHandshakeException,
|
||||||
ClientHandshakeException(
|
ClientHandshakeException(
|
||||||
@ -466,7 +466,7 @@ class TlsLayer(Layer):
|
|||||||
(tls_cert_err['depth'], tls_cert_err['errno']),
|
(tls_cert_err['depth'], tls_cert_err['errno']),
|
||||||
"error")
|
"error")
|
||||||
self.log("Ignoring server verification error, continuing with connection", "error")
|
self.log("Ignoring server verification error, continuing with connection", "error")
|
||||||
except NetLibInvalidCertificateError as e:
|
except InvalidCertificateException as e:
|
||||||
tls_cert_err = self.server_conn.ssl_verification_error
|
tls_cert_err = self.server_conn.ssl_verification_error
|
||||||
self.log(
|
self.log(
|
||||||
"TLS verification failed for upstream server at depth %s with error: %s" %
|
"TLS verification failed for upstream server at depth %s with error: %s" %
|
||||||
@ -474,18 +474,18 @@ class TlsLayer(Layer):
|
|||||||
"error")
|
"error")
|
||||||
self.log("Aborting connection attempt", "error")
|
self.log("Aborting connection attempt", "error")
|
||||||
six.reraise(
|
six.reraise(
|
||||||
TlsException,
|
TlsProtocolException,
|
||||||
TlsException("Cannot establish TLS with {address} (sni: {sni}): {e}".format(
|
TlsProtocolException("Cannot establish TLS with {address} (sni: {sni}): {e}".format(
|
||||||
address=repr(self.server_conn.address),
|
address=repr(self.server_conn.address),
|
||||||
sni=self.sni_for_server_connection,
|
sni=self.sni_for_server_connection,
|
||||||
e=repr(e),
|
e=repr(e),
|
||||||
)),
|
)),
|
||||||
sys.exc_info()[2]
|
sys.exc_info()[2]
|
||||||
)
|
)
|
||||||
except NetLibError as e:
|
except TlsException as e:
|
||||||
six.reraise(
|
six.reraise(
|
||||||
TlsException,
|
TlsProtocolException,
|
||||||
TlsException("Cannot establish TLS with {address} (sni: {sni}): {e}".format(
|
TlsProtocolException("Cannot establish TLS with {address} (sni: {sni}): {e}".format(
|
||||||
address=repr(self.server_conn.address),
|
address=repr(self.server_conn.address),
|
||||||
sni=self.sni_for_server_connection,
|
sni=self.sni_for_server_connection,
|
||||||
e=repr(e),
|
e=repr(e),
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
from __future__ import (absolute_import, print_function, division)
|
from __future__ import (absolute_import, print_function, division)
|
||||||
|
|
||||||
from netlib import socks
|
from netlib import socks
|
||||||
from netlib.tcp import NetLibError
|
from netlib.exceptions import TcpException
|
||||||
|
|
||||||
from ...exceptions import Socks5Exception
|
from ...exceptions import Socks5ProtocolException
|
||||||
from ...protocol import Layer, ServerConnectionMixin
|
from ...protocol import Layer, ServerConnectionMixin
|
||||||
|
|
||||||
|
|
||||||
@ -47,8 +47,8 @@ class Socks5Proxy(Layer, ServerConnectionMixin):
|
|||||||
connect_reply.to_file(self.client_conn.wfile)
|
connect_reply.to_file(self.client_conn.wfile)
|
||||||
self.client_conn.wfile.flush()
|
self.client_conn.wfile.flush()
|
||||||
|
|
||||||
except (socks.SocksError, NetLibError) as e:
|
except (socks.SocksError, TcpException) as e:
|
||||||
raise Socks5Exception("SOCKS5 mode failure: %s" % repr(e))
|
raise Socks5ProtocolException("SOCKS5 mode failure: %s" % repr(e))
|
||||||
|
|
||||||
self.server_conn.address = connect_request.addr
|
self.server_conn.address = connect_request.addr
|
||||||
|
|
||||||
|
@ -5,8 +5,8 @@ import sys
|
|||||||
import six
|
import six
|
||||||
|
|
||||||
from libmproxy.exceptions import ProtocolException
|
from libmproxy.exceptions import ProtocolException
|
||||||
|
from netlib.exceptions import TcpException
|
||||||
from netlib.http import ALPN_PROTO_H2, ALPN_PROTO_HTTP1
|
from netlib.http import ALPN_PROTO_H2, ALPN_PROTO_HTTP1
|
||||||
from netlib.tcp import NetLibError
|
|
||||||
from ..protocol import (
|
from ..protocol import (
|
||||||
RawTCPLayer, TlsLayer, Http1Layer, Http2Layer, is_tls_record_magic, ServerConnectionMixin
|
RawTCPLayer, TlsLayer, Http1Layer, Http2Layer, is_tls_record_magic, ServerConnectionMixin
|
||||||
)
|
)
|
||||||
@ -54,7 +54,7 @@ class RootContext(object):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
d = top_layer.client_conn.rfile.peek(3)
|
d = top_layer.client_conn.rfile.peek(3)
|
||||||
except NetLibError as e:
|
except TcpException as e:
|
||||||
six.reraise(ProtocolException, ProtocolException(str(e)), sys.exc_info()[2])
|
six.reraise(ProtocolException, ProtocolException(str(e)), sys.exc_info()[2])
|
||||||
client_tls = is_tls_record_magic(d)
|
client_tls = is_tls_record_magic(d)
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ import socket
|
|||||||
import six
|
import six
|
||||||
|
|
||||||
from netlib import tcp
|
from netlib import tcp
|
||||||
|
from netlib.exceptions import TcpException
|
||||||
from netlib.http.http1 import assemble_response
|
from netlib.http.http1 import assemble_response
|
||||||
from netlib.tcp import NetLibError
|
|
||||||
from ..exceptions import ProtocolException, ServerException, ClientHandshakeException
|
from ..exceptions import ProtocolException, ServerException, ClientHandshakeException
|
||||||
from ..protocol import Kill
|
from ..protocol import Kill
|
||||||
from ..models import ClientConnection, make_error_response
|
from ..models import ClientConnection, make_error_response
|
||||||
@ -139,7 +139,7 @@ class ConnectionHandler(object):
|
|||||||
try:
|
try:
|
||||||
error_response = make_error_response(502, repr(e))
|
error_response = make_error_response(502, repr(e))
|
||||||
self.client_conn.send(assemble_response(error_response))
|
self.client_conn.send(assemble_response(error_response))
|
||||||
except NetLibError:
|
except TcpException:
|
||||||
pass
|
pass
|
||||||
except Exception:
|
except Exception:
|
||||||
self.log(traceback.format_exc(), "error")
|
self.log(traceback.format_exc(), "error")
|
||||||
|
@ -6,6 +6,7 @@ from libmproxy.proxy import ProxyConfig
|
|||||||
from libmproxy.proxy.config import process_proxy_options
|
from libmproxy.proxy.config import process_proxy_options
|
||||||
from libmproxy.models.connections import ServerConnection
|
from libmproxy.models.connections import ServerConnection
|
||||||
from libmproxy.proxy.server import DummyServer, ProxyServer, ConnectionHandler
|
from libmproxy.proxy.server import DummyServer, ProxyServer, ConnectionHandler
|
||||||
|
from netlib.exceptions import TcpDisconnect
|
||||||
import tutils
|
import tutils
|
||||||
from libpathod import test
|
from libpathod import test
|
||||||
from netlib import http, tcp
|
from netlib import http, tcp
|
||||||
@ -40,7 +41,7 @@ class TestServerConnection:
|
|||||||
sc.connect()
|
sc.connect()
|
||||||
sc.connection = mock.Mock()
|
sc.connection = mock.Mock()
|
||||||
sc.connection.recv = mock.Mock(return_value=False)
|
sc.connection.recv = mock.Mock(return_value=False)
|
||||||
sc.connection.flush = mock.Mock(side_effect=tcp.NetLibDisconnect)
|
sc.connection.flush = mock.Mock(side_effect=TcpDisconnect)
|
||||||
sc.finish()
|
sc.finish()
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
|
@ -260,21 +260,6 @@ class TestHTTP(tservers.HTTPProxTest, CommonMixin, AppMixin):
|
|||||||
resp = p.request("get:'http://foo':h':foo'='bar'")
|
resp = p.request("get:'http://foo':h':foo'='bar'")
|
||||||
assert resp.status_code == 400
|
assert resp.status_code == 400
|
||||||
|
|
||||||
def test_empty_chunked_content(self):
|
|
||||||
"""
|
|
||||||
https://github.com/mitmproxy/mitmproxy/issues/186
|
|
||||||
"""
|
|
||||||
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
connection.connect(("127.0.0.1", self.proxy.port))
|
|
||||||
spec = '301:h"Transfer-Encoding"="chunked":r:b"0\\r\\n\\r\\n"'
|
|
||||||
connection.send(
|
|
||||||
"GET http://localhost:%d/p/%s HTTP/1.1\r\n" %
|
|
||||||
(self.server.port, spec))
|
|
||||||
connection.send("\r\n")
|
|
||||||
resp = connection.recv(50000)
|
|
||||||
connection.close()
|
|
||||||
assert "content-length" in resp.lower()
|
|
||||||
|
|
||||||
def test_stream(self):
|
def test_stream(self):
|
||||||
self.master.set_stream_large_bodies(1024 * 2)
|
self.master.set_stream_large_bodies(1024 * 2)
|
||||||
|
|
||||||
@ -624,7 +609,7 @@ class MasterRedirectRequest(tservers.TestMaster):
|
|||||||
super(MasterRedirectRequest, self).handle_request(f)
|
super(MasterRedirectRequest, self).handle_request(f)
|
||||||
|
|
||||||
def handle_response(self, f):
|
def handle_response(self, f):
|
||||||
f.response.content = str(f.client_conn.address.port)
|
f.response.body = str(f.client_conn.address.port)
|
||||||
f.response.headers["server-conn-id"] = str(f.server_conn.source_address.port)
|
f.response.headers["server-conn-id"] = str(f.server_conn.source_address.port)
|
||||||
super(MasterRedirectRequest, self).handle_response(f)
|
super(MasterRedirectRequest, self).handle_response(f)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user