aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/proxy
diff options
context:
space:
mode:
Diffstat (limited to 'libmproxy/proxy')
-rw-r--r--libmproxy/proxy/config.py8
-rw-r--r--libmproxy/proxy/modes/socks_proxy.py2
-rw-r--r--libmproxy/proxy/modes/transparent_proxy.py2
-rw-r--r--libmproxy/proxy/root_context.py36
-rw-r--r--libmproxy/proxy/server.py22
5 files changed, 48 insertions, 22 deletions
diff --git a/libmproxy/proxy/config.py b/libmproxy/proxy/config.py
index 2a1b84cb..cd9eda5a 100644
--- a/libmproxy/proxy/config.py
+++ b/libmproxy/proxy/config.py
@@ -54,6 +54,8 @@ class ProxyConfig:
authenticator=None,
ignore_hosts=tuple(),
tcp_hosts=tuple(),
+ http2=False,
+ rawtcp=False,
ciphers_client=None,
ciphers_server=None,
certs=tuple(),
@@ -78,6 +80,8 @@ class ProxyConfig:
self.check_ignore = HostMatcher(ignore_hosts)
self.check_tcp = HostMatcher(tcp_hosts)
+ self.http2 = http2
+ self.rawtcp = rawtcp
self.authenticator = authenticator
self.cadir = os.path.expanduser(cadir)
self.certstore = certutils.CertStore.from_store(
@@ -183,6 +187,8 @@ def process_proxy_options(parser, options):
upstream_server=upstream_server,
ignore_hosts=options.ignore_hosts,
tcp_hosts=options.tcp_hosts,
+ http2=options.http2,
+ rawtcp=options.rawtcp,
authenticator=authenticator,
ciphers_client=options.ciphers_client,
ciphers_server=options.ciphers_server,
@@ -192,4 +198,4 @@ def process_proxy_options(parser, options):
ssl_verify_upstream_cert=options.ssl_verify_upstream_cert,
ssl_verify_upstream_trusted_cadir=options.ssl_verify_upstream_trusted_cadir,
ssl_verify_upstream_trusted_ca=options.ssl_verify_upstream_trusted_ca
- ) \ No newline at end of file
+ )
diff --git a/libmproxy/proxy/modes/socks_proxy.py b/libmproxy/proxy/modes/socks_proxy.py
index 0efeab67..545c38d6 100644
--- a/libmproxy/proxy/modes/socks_proxy.py
+++ b/libmproxy/proxy/modes/socks_proxy.py
@@ -48,7 +48,7 @@ class Socks5Proxy(Layer, ServerConnectionMixin):
self.client_conn.wfile.flush()
except (socks.SocksError, NetLibError) as e:
- raise Socks5Exception("SOCKS5 mode failure: %s" % repr(e), e)
+ raise Socks5Exception("SOCKS5 mode failure: %s" % repr(e))
self.server_conn.address = connect_request.addr
diff --git a/libmproxy/proxy/modes/transparent_proxy.py b/libmproxy/proxy/modes/transparent_proxy.py
index d99485c9..da1d4632 100644
--- a/libmproxy/proxy/modes/transparent_proxy.py
+++ b/libmproxy/proxy/modes/transparent_proxy.py
@@ -14,7 +14,7 @@ class TransparentProxy(Layer, ServerConnectionMixin):
try:
self.server_conn.address = self.resolver.original_addr(self.client_conn.connection)
except Exception as e:
- raise ProtocolException("Transparent mode failure: %s" % repr(e), e)
+ raise ProtocolException("Transparent mode failure: %s" % repr(e))
layer = self.ctx.next_layer(self)
try:
diff --git a/libmproxy/proxy/root_context.py b/libmproxy/proxy/root_context.py
index dccdf023..54bea1db 100644
--- a/libmproxy/proxy/root_context.py
+++ b/libmproxy/proxy/root_context.py
@@ -1,8 +1,13 @@
from __future__ import (absolute_import, print_function, division)
+import string
+import sys
+import six
+
+from libmproxy.exceptions import ProtocolException
from netlib.http.http1 import HTTP1Protocol
from netlib.http.http2 import HTTP2Protocol
-
+from netlib.tcp import NetLibError
from ..protocol import (
RawTCPLayer, TlsLayer, Http1Layer, Http2Layer, is_tls_record_magic, ServerConnectionMixin
)
@@ -48,7 +53,10 @@ class RootContext(object):
if self.config.check_ignore(top_layer.server_conn.address):
return RawTCPLayer(top_layer, logging=False)
- d = top_layer.client_conn.rfile.peek(3)
+ try:
+ d = top_layer.client_conn.rfile.peek(3)
+ except NetLibError as e:
+ six.reraise(ProtocolException, ProtocolException(str(e)), sys.exc_info()[2])
client_tls = is_tls_record_magic(d)
# 2. Always insert a TLS layer, even if there's neither client nor server tls.
@@ -82,21 +90,17 @@ class RootContext(object):
if alpn == HTTP1Protocol.ALPN_PROTO_HTTP1:
return Http1Layer(top_layer, 'transparent')
- # 6. Assume HTTP1 by default
- return Http1Layer(top_layer, 'transparent')
+ # 6. Check for raw tcp mode
+ is_ascii = (
+ len(d) == 3 and
+ # better be safe here and don't expect uppercase...
+ all(x in string.ascii_letters for x in d)
+ )
+ if self.config.rawtcp and not is_ascii:
+ return RawTCPLayer(top_layer)
- # In a future version, we want to implement TCP passthrough as the last fallback,
- # but we don't have the UI part ready for that.
- #
- # d = top_layer.client_conn.rfile.peek(3)
- # is_ascii = (
- # len(d) == 3 and
- # # better be safe here and don't expect uppercase...
- # all(x in string.ascii_letters for x in d)
- # )
- # # TODO: This could block if there are not enough bytes available?
- # d = top_layer.client_conn.rfile.peek(len(HTTP2Protocol.CLIENT_CONNECTION_PREFACE))
- # is_http2_magic = (d == HTTP2Protocol.CLIENT_CONNECTION_PREFACE)
+ # 7. Assume HTTP1 by default
+ return Http1Layer(top_layer, 'transparent')
def log(self, msg, level, subs=()):
"""
diff --git a/libmproxy/proxy/server.py b/libmproxy/proxy/server.py
index c12bbbfa..88448172 100644
--- a/libmproxy/proxy/server.py
+++ b/libmproxy/proxy/server.py
@@ -3,11 +3,12 @@ from __future__ import (absolute_import, print_function, division)
import traceback
import sys
import socket
+import six
from netlib import tcp
from netlib.http.http1 import HTTP1Protocol
from netlib.tcp import NetLibError
-from ..exceptions import ProtocolException, ServerException
+from ..exceptions import ProtocolException, ServerException, ClientHandshakeException
from ..protocol import Kill
from ..models import ClientConnection, make_error_response
from .modes import HttpUpstreamProxy, HttpProxy, ReverseProxy, TransparentProxy, Socks5Proxy
@@ -39,7 +40,11 @@ class ProxyServer(tcp.TCPServer):
try:
super(ProxyServer, self).__init__((config.host, config.port))
except socket.error as e:
- raise ServerException('Error starting proxy server: ' + repr(e), e)
+ six.reraise(
+ ServerException,
+ ServerException('Error starting proxy server: ' + repr(e)),
+ sys.exc_info()[2]
+ )
self.channel = None
def start_slave(self, klass, channel):
@@ -116,7 +121,18 @@ class ConnectionHandler(object):
except Kill:
self.log("Connection killed", "info")
except ProtocolException as e:
- self.log(e, "info")
+
+ if isinstance(e, ClientHandshakeException):
+ self.log(
+ "Client Handshake failed. "
+ "The client may not trust the proxy's certificate for {}.".format(e.server),
+ "error"
+ )
+ self.log(repr(e), "debug")
+ else:
+ self.log(repr(e), "error")
+
+ self.log(traceback.format_exc(), "debug")
# If an error propagates to the topmost level,
# we send an HTTP error response, which is both
# understandable by HTTP clients and humans.