2019-11-30 05:28:31 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
proxy.py
|
|
|
|
~~~~~~~~
|
|
|
|
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
|
|
|
|
Network monitoring, controls & Application development, testing, debugging.
|
|
|
|
|
|
|
|
:copyright: (c) 2013-present by Abhinav Singh and contributors.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
2021-11-05 10:25:56 +00:00
|
|
|
import ssl
|
2019-11-30 05:28:31 +00:00
|
|
|
import random
|
2021-11-05 10:25:56 +00:00
|
|
|
import socket
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from typing import List, Optional, Tuple, Any
|
2019-11-30 05:28:31 +00:00
|
|
|
from urllib import parse as urlparse
|
|
|
|
|
2021-11-05 10:25:56 +00:00
|
|
|
from ..common.utils import text_
|
|
|
|
from ..common.constants import DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT
|
|
|
|
from ..common.types import Readables, Writables
|
|
|
|
from ..core.connection import TcpServerConnection
|
|
|
|
from ..http.exception import HttpProtocolException
|
2019-11-30 05:28:31 +00:00
|
|
|
from ..http.parser import HttpParser
|
|
|
|
from ..http.websocket import WebsocketFrame
|
|
|
|
from ..http.server import HttpWebServerBasePlugin, httpProtocolTypes
|
|
|
|
|
2021-11-05 10:25:56 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: ReverseProxyPlugin and ProxyPoolPlugin are implementing
|
|
|
|
# a similar behavior. Abstract that particular logic out into its
|
|
|
|
# own class.
|
2019-11-30 05:28:31 +00:00
|
|
|
class ReverseProxyPlugin(HttpWebServerBasePlugin):
|
|
|
|
"""Extend in-built Web Server to add Reverse Proxy capabilities.
|
|
|
|
|
|
|
|
This example plugin is equivalent to following Nginx configuration:
|
|
|
|
|
|
|
|
location /get {
|
|
|
|
proxy_pass http://httpbin.org/get
|
|
|
|
}
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
$ curl http://localhost:9000/get
|
|
|
|
{
|
|
|
|
"args": {},
|
|
|
|
"headers": {
|
|
|
|
"Accept": "*/*",
|
|
|
|
"Host": "localhost",
|
|
|
|
"User-Agent": "curl/7.64.1"
|
|
|
|
},
|
|
|
|
"origin": "1.2.3.4, 5.6.7.8",
|
2021-11-05 10:25:56 +00:00
|
|
|
"url": "http://localhost/get"
|
2019-11-30 05:28:31 +00:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2021-11-05 10:25:56 +00:00
|
|
|
# TODO: We must use nginx python parser and
|
|
|
|
# make this plugin nginx.conf complaint.
|
2019-12-02 04:57:05 +00:00
|
|
|
REVERSE_PROXY_LOCATION: str = r'/get$'
|
2021-11-05 10:25:56 +00:00
|
|
|
# Randomly choose either http or https upstream endpoint.
|
|
|
|
#
|
|
|
|
# This is just to demonstrate that both http and https upstream
|
|
|
|
# reverse proxy works.
|
2019-11-30 05:28:31 +00:00
|
|
|
REVERSE_PROXY_PASS = [
|
2021-11-04 11:28:36 +00:00
|
|
|
b'http://httpbin.org/get',
|
2021-11-05 10:25:56 +00:00
|
|
|
b'https://httpbin.org/get',
|
2019-11-30 05:28:31 +00:00
|
|
|
]
|
|
|
|
|
2021-11-05 10:25:56 +00:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.upstream: Optional[TcpServerConnection] = None
|
|
|
|
|
2019-12-02 04:57:05 +00:00
|
|
|
def routes(self) -> List[Tuple[int, str]]:
|
2019-11-30 05:28:31 +00:00
|
|
|
return [
|
|
|
|
(httpProtocolTypes.HTTP, ReverseProxyPlugin.REVERSE_PROXY_LOCATION),
|
2021-11-04 11:28:36 +00:00
|
|
|
(httpProtocolTypes.HTTPS, ReverseProxyPlugin.REVERSE_PROXY_LOCATION),
|
2019-11-30 05:28:31 +00:00
|
|
|
]
|
|
|
|
|
2021-11-05 10:25:56 +00:00
|
|
|
def get_descriptors(self) -> Tuple[List[socket.socket], List[socket.socket]]:
|
|
|
|
if not self.upstream:
|
|
|
|
return [], []
|
|
|
|
return [self.upstream.connection], [self.upstream.connection] if self.upstream.has_buffer() else []
|
|
|
|
|
|
|
|
def read_from_descriptors(self, r: Readables) -> bool:
|
|
|
|
if self.upstream and self.upstream.connection in r:
|
|
|
|
try:
|
|
|
|
raw = self.upstream.recv(self.flags.server_recvbuf_size)
|
|
|
|
if raw is not None:
|
|
|
|
self.client.queue(raw)
|
|
|
|
else:
|
|
|
|
return True # Teardown because upstream server closed the connection
|
|
|
|
except ssl.SSLWantReadError:
|
|
|
|
logger.info('Upstream server SSLWantReadError, will retry')
|
|
|
|
return False
|
|
|
|
except ConnectionResetError:
|
|
|
|
logger.debug('Connection reset by upstream server')
|
|
|
|
return True
|
|
|
|
return super().read_from_descriptors(r)
|
|
|
|
|
|
|
|
def write_to_descriptors(self, w: Writables) -> bool:
|
|
|
|
if self.upstream and self.upstream.connection in w and self.upstream.has_buffer():
|
|
|
|
try:
|
|
|
|
self.upstream.flush()
|
|
|
|
except ssl.SSLWantWriteError:
|
|
|
|
logger.info('Upstream server SSLWantWriteError, will retry')
|
|
|
|
return False
|
|
|
|
except BrokenPipeError:
|
|
|
|
logger.debug(
|
|
|
|
'BrokenPipeError when flushing to upstream server',
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
return super().write_to_descriptors(w)
|
|
|
|
|
2019-11-30 05:28:31 +00:00
|
|
|
def handle_request(self, request: HttpParser) -> None:
|
2021-11-05 10:25:56 +00:00
|
|
|
url = urlparse.urlsplit(
|
|
|
|
random.choice(ReverseProxyPlugin.REVERSE_PROXY_PASS),
|
|
|
|
)
|
2019-11-30 05:28:31 +00:00
|
|
|
assert url.hostname
|
2021-11-05 10:25:56 +00:00
|
|
|
port = url.port or (
|
|
|
|
DEFAULT_HTTP_PORT if url.scheme ==
|
|
|
|
b'http' else DEFAULT_HTTPS_PORT
|
|
|
|
)
|
|
|
|
self.upstream = TcpServerConnection(text_(url.hostname), port)
|
|
|
|
try:
|
|
|
|
self.upstream.connect()
|
|
|
|
if url.scheme == b'https':
|
|
|
|
self.upstream.wrap(
|
|
|
|
text_(
|
|
|
|
url.hostname,
|
2021-11-06 21:20:11 +00:00
|
|
|
), ca_file=str(self.flags.ca_file),
|
2021-11-05 10:25:56 +00:00
|
|
|
)
|
|
|
|
self.upstream.queue(memoryview(request.build()))
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
logger.info(
|
|
|
|
'Connection refused by upstream server {0}:{1}'.format(
|
|
|
|
text_(url.hostname), port,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
raise HttpProtocolException()
|
2019-11-30 05:28:31 +00:00
|
|
|
|
|
|
|
def on_websocket_open(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def on_websocket_message(self, frame: WebsocketFrame) -> None:
|
|
|
|
pass
|
|
|
|
|
2021-11-05 10:25:56 +00:00
|
|
|
def on_client_connection_close(self) -> None:
|
|
|
|
if self.upstream and not self.upstream.closed:
|
|
|
|
logger.debug('Closing upstream server connection')
|
|
|
|
self.upstream.close()
|
|
|
|
self.upstream = None
|