aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/protocol/primitives.py
diff options
context:
space:
mode:
Diffstat (limited to 'libmproxy/protocol/primitives.py')
-rw-r--r--libmproxy/protocol/primitives.py130
1 files changed, 1 insertions, 129 deletions
diff --git a/libmproxy/protocol/primitives.py b/libmproxy/protocol/primitives.py
index 92fc95e5..c663f0c5 100644
--- a/libmproxy/protocol/primitives.py
+++ b/libmproxy/protocol/primitives.py
@@ -1,11 +1,10 @@
from __future__ import absolute_import
import copy
import uuid
-import netlib.tcp
+
from .. import stateobject, utils, version
from ..proxy.connection import ClientConnection, ServerConnection
-
KILL = 0 # const for killed requests
@@ -165,130 +164,3 @@ class Flow(stateobject.StateObject):
self.intercepted = False
self.reply()
master.handle_accept_intercept(self)
-
-
-
-class ProtocolHandler(object):
- """
- A ProtocolHandler implements an application-layer protocol, e.g. HTTP.
- See: libmproxy.protocol.http.HTTPHandler
- """
-
- def __init__(self, c):
- self.c = c
- """@type: libmproxy.proxy.server.ConnectionHandler"""
- self.live = LiveConnection(c)
- """@type: LiveConnection"""
-
- def handle_messages(self):
- """
- This method gets called if a client connection has been made. Depending
- on the proxy settings, a server connection might already exist as well.
- """
- raise NotImplementedError # pragma: nocover
-
- def handle_server_reconnect(self, state):
- """
- This method gets called if a server connection needs to reconnect and
- there's a state associated with the server connection (e.g. a
- previously-sent CONNECT request or a SOCKS proxy request). This method
- gets called after the connection has been restablished but before SSL is
- established.
- """
- raise NotImplementedError # pragma: nocover
-
- def handle_error(self, error):
- """
- This method gets called should there be an uncaught exception during the
- connection. This might happen outside of handle_messages, e.g. if the
- initial SSL handshake fails in transparent mode.
- """
- raise error # pragma: nocover
-
-
-class LiveConnection(object):
- """
- This facade allows interested parties (FlowMaster, inline scripts) to
- interface with a live connection, without exposing the internals
- of the ConnectionHandler.
- """
-
- def __init__(self, c):
- self.c = c
- """@type: libmproxy.proxy.server.ConnectionHandler"""
- self._backup_server_conn = None
- """@type: libmproxy.proxy.connection.ServerConnection"""
-
- def change_server(
- self,
- address,
- ssl=None,
- sni=None,
- force=False,
- persistent_change=False):
- """
- Change the server connection to the specified address.
- @returns:
- True, if a new connection has been established,
- False, if an existing connection has been used
- """
- address = netlib.tcp.Address.wrap(address)
-
- ssl_mismatch = (
- ssl is not None and
- (
- (self.c.server_conn.connection and ssl != self.c.server_conn.ssl_established)
- or
- (sni is not None and sni != self.c.server_conn.sni)
- )
- )
- address_mismatch = (address != self.c.server_conn.address)
-
- if persistent_change:
- self._backup_server_conn = None
-
- if ssl_mismatch or address_mismatch or force:
-
- self.c.log(
- "Change server connection: %s:%s -> %s:%s [persistent: %s]" % (
- self.c.server_conn.address.host,
- self.c.server_conn.address.port,
- address.host,
- address.port,
- persistent_change
- ),
- "debug"
- )
-
- if not self._backup_server_conn and not persistent_change:
- self._backup_server_conn = self.c.server_conn
- self.c.server_conn = None
- else:
- # This is at least the second temporary change. We can kill the
- # current connection.
- self.c.del_server_connection()
-
- self.c.set_server_address(address)
- self.c.establish_server_connection(ask=False)
- if ssl:
- self.c.establish_ssl(server=True, sni=sni)
- return True
- return False
-
- def restore_server(self):
- # TODO: Similar to _backup_server_conn, introduce _cache_server_conn,
- # which keeps the changed connection open This may be beneficial if a
- # user is rewriting all requests from http to https or similar.
- if not self._backup_server_conn:
- return
-
- self.c.log("Restore original server connection: %s:%s -> %s:%s" % (
- self.c.server_conn.address.host,
- self.c.server_conn.address.port,
- self._backup_server_conn.address.host,
- self._backup_server_conn.address.port
- ), "debug")
-
- self.c.del_server_connection()
- self.c.server_conn = self._backup_server_conn
- self._backup_server_conn = None