Skip to content

Commit

Permalink
remove ServerConnectionMixin.reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Sep 3, 2015
1 parent 1f6d05f commit f4272de
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 62 deletions.
4 changes: 2 additions & 2 deletions libmproxy/protocol/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import (absolute_import, print_function, division)
from .base import Layer, ServerConnectionMixin, Log, Kill
from .base import Layer, ServerConnectionMixin, Kill
from .http import Http1Layer, Http2Layer
from .tls import TlsLayer, is_tls_record_magic
from .rawtcp import RawTCPLayer

__all__ = [
"Layer", "ServerConnectionMixin", "Log", "Kill",
"Layer", "ServerConnectionMixin", "Kill",
"Http1Layer", "Http2Layer",
"TlsLayer", "is_tls_record_magic",
"RawTCPLayer"
Expand Down
34 changes: 7 additions & 27 deletions libmproxy/protocol/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@ def __getattr__(self, name):
"""
return getattr(self.ctx, name)

def log(self, msg, level, subs=()):
full_msg = [
"{}: {}".format(repr(self.client_conn.address), msg)
]
for i in subs:
full_msg.append(" -> " + i)
full_msg = "\n".join(full_msg)
self.channel.tell("log", Log(full_msg, level))

@property
def layers(self):
return [self] + self.ctx.layers
Expand All @@ -106,15 +97,9 @@ class ServerConnectionMixin(object):
def __init__(self, server_address=None):
super(ServerConnectionMixin, self).__init__()
self.server_conn = ServerConnection(server_address)
self._check_self_connect()

def reconnect(self):
address = self.server_conn.address
self._disconnect()
self.server_conn.address = address
self.connect()
self.__check_self_connect()

def _check_self_connect(self):
def __check_self_connect(self):
"""
We try to protect the proxy from _accidentally_ connecting to itself,
e.g. because of a failed transparent lookup or an invalid configuration.
Expand All @@ -134,26 +119,27 @@ def _check_self_connect(self):
def set_server(self, address, server_tls=None, sni=None, depth=1):
if depth == 1:
if self.server_conn:
self._disconnect()
self.disconnect()
self.log("Set new server address: " + repr(address), "debug")
self.server_conn.address = address
self._check_self_connect()
self.__check_self_connect()
if server_tls:
raise ProtocolException(
"Cannot upgrade to TLS, no TLS layer on the protocol stack."
)
else:
self.ctx.set_server(address, server_tls, sni, depth - 1)

def _disconnect(self):
def disconnect(self):
"""
Deletes (and closes) an existing server connection.
"""
self.log("serverdisconnect", "debug", [repr(self.server_conn.address)])
address = self.server_conn.address
self.server_conn.finish()
self.server_conn.close()
self.channel.tell("serverdisconnect", self.server_conn)
self.server_conn = ServerConnection(None)
self.server_conn = ServerConnection(address)

def connect(self):
if not self.server_conn.address:
Expand All @@ -167,12 +153,6 @@ def connect(self):
"Server connection to %s failed: %s" % (repr(self.server_conn.address), e), e)


class Log(object):
def __init__(self, msg, level="info"):
self.msg = msg
self.level = level


class Kill(Exception):
"""
Kill a connection.
Expand Down
25 changes: 7 additions & 18 deletions libmproxy/protocol/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ def connect(self):
self.ctx.connect()
self.server_protocol = HTTP1Protocol(self.server_conn)

def reconnect(self):
self.ctx.reconnect()
self.server_protocol = HTTP1Protocol(self.server_conn)

def set_server(self, *args, **kwargs):
self.ctx.set_server(*args, **kwargs)
self.server_protocol = HTTP1Protocol(self.server_conn)
Expand Down Expand Up @@ -202,12 +198,6 @@ def connect(self):
unhandled_frame_cb=self.handle_unexpected_frame_from_server)
self.server_protocol.perform_connection_preface()

def reconnect(self):
self.ctx.reconnect()
self.server_protocol = HTTP2Protocol(self.server_conn, is_server=False,
unhandled_frame_cb=self.handle_unexpected_frame_from_server)
self.server_protocol.perform_connection_preface()

def set_server(self, *args, **kwargs):
self.ctx.set_server(*args, **kwargs)
self.server_protocol = HTTP2Protocol(self.server_conn, is_server=False,
Expand Down Expand Up @@ -314,14 +304,10 @@ def connect(self):
else:
pass # swallow the message

def reconnect(self):
self.ctx.reconnect()
self._send_connect_request()

def set_server(self, address, server_tls=None, sni=None, depth=1):
if depth == 1:
if self.ctx.server_conn:
self.ctx.reconnect()
self.ctx.disconnect()
address = Address.wrap(address)
self.connect_request.host = address.host
self.connect_request.port = address.port
Expand Down Expand Up @@ -459,7 +445,8 @@ def get_response():
# > server detects timeout, disconnects
# > read (100-n)% of large request
# > send large request upstream
self.reconnect()
self.disconnect()
self.connect()
get_response()

# call the appropriate script hook - this is an opportunity for an
Expand Down Expand Up @@ -534,10 +521,12 @@ def establish_server_connection(self, flow):
"""
# This is a very ugly (untested) workaround to solve a very ugly problem.
if self.server_conn and self.server_conn.tls_established and not ssl:
self.reconnect()
self.disconnect()
self.connect()
elif ssl and not hasattr(self, "connected_to") or self.connected_to != address:
if self.server_conn.tls_established:
self.reconnect()
self.disconnect()
self.connect()
self.send_request(make_connect_request(address))
tls_layer = TlsLayer(self, False, True)
Expand Down
5 changes: 3 additions & 2 deletions libmproxy/protocol/http_replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from netlib.tcp import NetLibError
from ..controller import Channel
from ..models import Error, HTTPResponse, ServerConnection, make_connect_request
from .base import Log, Kill
from .base import Kill


# TODO: Doesn't really belong into libmproxy.protocol...
Expand Down Expand Up @@ -89,8 +89,9 @@ def run(self):
if self.channel:
self.channel.ask("error", self.flow)
except Kill:
# KillSignal should only be raised if there's a channel in the
# Kill should only be raised if there's a channel in the
# first place.
from ..proxy.root_context import Log
self.channel.tell("log", Log("Connection killed", "info"))
finally:
r.form_out = form_out_backup
5 changes: 0 additions & 5 deletions libmproxy/protocol/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,6 @@ def connect(self):
if self._server_tls and not self.server_conn.tls_established:
self._establish_tls_with_server()

def reconnect(self):
self.ctx.reconnect()
if self._server_tls and not self.server_conn.tls_established:
self._establish_tls_with_server()

def set_server(self, address, server_tls=None, sni=None, depth=1):
if depth == 1 and server_tls is not None:
self.ctx.set_server(address, None, None, 1)
Expand Down
2 changes: 2 additions & 0 deletions libmproxy/proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from .server import ProxyServer, DummyServer
from .config import ProxyConfig
from .root_context import RootContext, Log

__all__ = [
"ProxyServer", "DummyServer",
"ProxyConfig",
"RootContext", "Log",
]
4 changes: 2 additions & 2 deletions libmproxy/proxy/modes/http_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __call__(self):
layer()
finally:
if self.server_conn:
self._disconnect()
self.disconnect()


class HttpUpstreamProxy(Layer, ServerConnectionMixin):
Expand All @@ -23,4 +23,4 @@ def __call__(self):
layer()
finally:
if self.server_conn:
self._disconnect()
self.disconnect()
2 changes: 1 addition & 1 deletion libmproxy/proxy/modes/reverse_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def __call__(self):
layer()
finally:
if self.server_conn:
self._disconnect()
self.disconnect()
2 changes: 1 addition & 1 deletion libmproxy/proxy/modes/socks_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ def __call__(self):
layer()
finally:
if self.server_conn:
self._disconnect()
self.disconnect()
2 changes: 1 addition & 1 deletion libmproxy/proxy/modes/transparent_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def __call__(self):
layer()
finally:
if self.server_conn:
self._disconnect()
self.disconnect()
19 changes: 19 additions & 0 deletions libmproxy/proxy/root_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,28 @@ def next_layer(self, top_layer):
# d = top_layer.client_conn.rfile.peek(len(HTTP2Protocol.CLIENT_CONNECTION_PREFACE))
# is_http2_magic = (d == HTTP2Protocol.CLIENT_CONNECTION_PREFACE)

def log(self, msg, level, subs=()):
"""
Send a log message to the master.
"""

full_msg = [
"{}: {}".format(repr(self.client_conn.address), msg)
]
for i in subs:
full_msg.append(" -> " + i)
full_msg = "\n".join(full_msg)
self.channel.tell("log", Log(full_msg, level))

@property
def layers(self):
return []

def __repr__(self):
return "RootContext"


class Log(object):
def __init__(self, msg, level="info"):
self.msg = msg
self.level = level
4 changes: 2 additions & 2 deletions libmproxy/proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from netlib.http.http1 import HTTP1Protocol
from netlib.tcp import NetLibError
from ..exceptions import ProtocolException, ServerException
from ..protocol import Log, Kill
from ..protocol import Kill
from ..models import ClientConnection, make_error_response
from .modes import HttpUpstreamProxy, HttpProxy, ReverseProxy, TransparentProxy, Socks5Proxy
from .root_context import RootContext
from .root_context import RootContext, Log


class DummyServer:
Expand Down
2 changes: 1 addition & 1 deletion test/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from netlib.http.semantics import CONTENT_MISSING

from libmproxy import dump, flow
from libmproxy.protocol import Log
from libmproxy.proxy import Log
import tutils
import mock

Expand Down

0 comments on commit f4272de

Please sign in to comment.