mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
Stub implementation of a server connection pool.
This commit is contained in:
parent
53792a5a28
commit
2aa175a6ca
@ -107,12 +107,30 @@ class ServerConnection(tcp.TCPClient):
|
|||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class ServerConnectionPool:
|
||||||
|
def __init__(self, config):
|
||||||
|
self.config = config
|
||||||
|
self.conn = None
|
||||||
|
|
||||||
|
def get_connection(self, scheme, host, port):
|
||||||
|
sc = self.conn
|
||||||
|
if self.conn and (host, port) != (sc.host, sc.port):
|
||||||
|
sc.terminate()
|
||||||
|
self.conn = None
|
||||||
|
if not self.conn:
|
||||||
|
try:
|
||||||
|
self.conn = ServerConnection(self.config, host, port)
|
||||||
|
self.conn.connect(scheme)
|
||||||
|
except tcp.NetLibError, v:
|
||||||
|
raise ProxyError(502, v)
|
||||||
|
return self.conn
|
||||||
|
|
||||||
|
|
||||||
class ProxyHandler(tcp.BaseHandler):
|
class ProxyHandler(tcp.BaseHandler):
|
||||||
def __init__(self, config, connection, client_address, server, mqueue, server_version):
|
def __init__(self, config, connection, client_address, server, mqueue, server_version):
|
||||||
self.mqueue, self.server_version = mqueue, server_version
|
self.mqueue, self.server_version = mqueue, server_version
|
||||||
self.config = config
|
self.config = config
|
||||||
self.server_conn = None
|
self.server_conn_pool = ServerConnectionPool(config)
|
||||||
self.proxy_connect_state = None
|
self.proxy_connect_state = None
|
||||||
self.sni = None
|
self.sni = None
|
||||||
tcp.BaseHandler.__init__(self, connection, client_address, server)
|
tcp.BaseHandler.__init__(self, connection, client_address, server)
|
||||||
@ -133,18 +151,6 @@ class ProxyHandler(tcp.BaseHandler):
|
|||||||
)
|
)
|
||||||
cd._send(self.mqueue)
|
cd._send(self.mqueue)
|
||||||
|
|
||||||
def server_connect(self, scheme, host, port):
|
|
||||||
sc = self.server_conn
|
|
||||||
if sc and (host, port) != (sc.host, sc.port):
|
|
||||||
sc.terminate()
|
|
||||||
self.server_conn = None
|
|
||||||
if not self.server_conn:
|
|
||||||
try:
|
|
||||||
self.server_conn = ServerConnection(self.config, host, port)
|
|
||||||
self.server_conn.connect(scheme)
|
|
||||||
except tcp.NetLibError, v:
|
|
||||||
raise ProxyError(502, v)
|
|
||||||
|
|
||||||
def handle_request(self, cc):
|
def handle_request(self, cc):
|
||||||
try:
|
try:
|
||||||
request, err = None, None
|
request, err = None, None
|
||||||
@ -173,21 +179,21 @@ class ProxyHandler(tcp.BaseHandler):
|
|||||||
scheme, host, port = self.config.reverse_proxy
|
scheme, host, port = self.config.reverse_proxy
|
||||||
else:
|
else:
|
||||||
scheme, host, port = request.scheme, request.host, request.port
|
scheme, host, port = request.scheme, request.host, request.port
|
||||||
self.server_connect(scheme, host, port)
|
sc = self.server_conn_pool.get_connection(scheme, host, port)
|
||||||
self.server_conn.send(request)
|
sc.send(request)
|
||||||
self.server_conn.rfile.reset_timestamps()
|
sc.rfile.reset_timestamps()
|
||||||
httpversion, code, msg, headers, content = http.read_response(
|
httpversion, code, msg, headers, content = http.read_response(
|
||||||
self.server_conn.rfile,
|
sc.rfile,
|
||||||
request.method,
|
request.method,
|
||||||
self.config.body_size_limit
|
self.config.body_size_limit
|
||||||
)
|
)
|
||||||
response = flow.Response(
|
response = flow.Response(
|
||||||
request, httpversion, code, msg, headers, content, self.server_conn.cert, self.server_conn.rfile.first_byte_timestamp, utils.timestamp()
|
request, httpversion, code, msg, headers, content, sc.cert,
|
||||||
|
sc.rfile.first_byte_timestamp, utils.timestamp()
|
||||||
)
|
)
|
||||||
|
|
||||||
response = response._send(self.mqueue)
|
response = response._send(self.mqueue)
|
||||||
if response is None:
|
if response is None:
|
||||||
self.server_conn.terminate()
|
sc.terminate()
|
||||||
if response is None:
|
if response is None:
|
||||||
return
|
return
|
||||||
self.send_response(response)
|
self.send_response(response)
|
||||||
|
Loading…
Reference in New Issue
Block a user