aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/proxy/server.py
diff options
context:
space:
mode:
Diffstat (limited to 'libmproxy/proxy/server.py')
-rw-r--r--libmproxy/proxy/server.py69
1 files changed, 61 insertions, 8 deletions
diff --git a/libmproxy/proxy/server.py b/libmproxy/proxy/server.py
index 2f6ee061..a45276d4 100644
--- a/libmproxy/proxy/server.py
+++ b/libmproxy/proxy/server.py
@@ -1,13 +1,14 @@
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
+import traceback
+import sys
import socket
-from OpenSSL import SSL
from netlib import tcp
+from . import layer
from .primitives import ProxyServerError, Log, ProxyError
from .connection import ClientConnection, ServerConnection
from ..protocol.handle import protocol_handler
-from .. import version
class DummyServer:
@@ -46,7 +47,7 @@ class ProxyServer(tcp.TCPServer):
self.channel = channel
def handle_client_connection(self, conn, client_address):
- h = ConnectionHandler(
+ h = ConnectionHandler2(
self.config,
conn,
client_address,
@@ -56,6 +57,57 @@ class ProxyServer(tcp.TCPServer):
h.finish()
+class ConnectionHandler2:
+ # FIXME: parameter ordering
+ # FIXME: remove server attribute
+ def __init__(self, config, client_conn, client_address, server, channel):
+ self.config = config
+ """@type: libmproxy.proxy.config.ProxyConfig"""
+ self.client_conn = ClientConnection(
+ client_conn,
+ client_address,
+ server)
+ """@type: libmproxy.proxy.connection.ClientConnection"""
+ self.channel = channel
+ """@type: libmproxy.controller.Channel"""
+
+ def handle(self):
+ self.log("clientconnect", "info")
+
+ root_context = layer.RootContext(
+ self.client_conn,
+ self.config,
+ self.channel
+ )
+ root_layer = layer.Socks5IncomingLayer(root_context)
+
+ try:
+ for message in root_layer():
+ print("Root layer receveived: %s" % message)
+ except layer.ProxyError2 as e:
+ self.log(e, "info")
+ except Exception:
+ self.log(traceback.format_exc(), "error")
+ print(traceback.format_exc(), file=sys.stderr)
+ print("mitmproxy has crashed!", file=sys.stderr)
+ print("Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy", file=sys.stderr)
+
+ def finish(self):
+ self.client_conn.finish()
+
+ def log(self, msg, level, subs=()):
+ # FIXME: Duplicate code
+ full_msg = [
+ "%s:%s: %s" %
+ (self.client_conn.address.host,
+ self.client_conn.address.port,
+ msg)]
+ for i in subs:
+ full_msg.append(" -> " + i)
+ full_msg = "\n".join(full_msg)
+ self.channel.tell("log", Log(full_msg, level))
+
+
class ConnectionHandler:
def __init__(
self,
@@ -74,6 +126,7 @@ class ConnectionHandler:
self.server_conn = None
"""@type: libmproxy.proxy.connection.ServerConnection"""
self.channel = channel
+ """@type: libmproxy.controller.Channel"""
self.conntype = "http"
@@ -144,9 +197,9 @@ class ConnectionHandler:
import sys
self.log(traceback.format_exc(), "error")
- print >> sys.stderr, traceback.format_exc()
- print >> sys.stderr, "mitmproxy has crashed!"
- print >> sys.stderr, "Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy"
+ print(traceback.format_exc(), file=sys.stderr)
+ print("mitmproxy has crashed!", file=sys.stderr)
+ print("Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy", file=sys.stderr)
finally:
# Make sure that we close the server connection in any case.
# The client connection is closed by the ProxyServer and does not
@@ -201,7 +254,7 @@ class ConnectionHandler:
"serverconnect", "debug", [
"%s:%s" %
self.server_conn.address()[
- :2]])
+ :2]])
if ask:
self.channel.ask("serverconnect", self)
try: