2019-12-03 03:18:43 +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.
|
|
|
|
"""
|
|
|
|
import ssl
|
2020-07-08 07:41:12 +00:00
|
|
|
import socket
|
2021-10-31 19:49:19 +00:00
|
|
|
|
2019-12-03 03:18:43 +00:00
|
|
|
from typing import Optional, Union, Tuple
|
|
|
|
|
|
|
|
from .connection import TcpConnection, tcpConnectionTypes, TcpConnectionUninitializedException
|
2021-10-31 19:49:19 +00:00
|
|
|
|
2019-12-03 03:18:43 +00:00
|
|
|
from ...common.utils import new_socket_connection
|
|
|
|
|
|
|
|
|
|
|
|
class TcpServerConnection(TcpConnection):
|
|
|
|
"""Establishes connection to upstream server."""
|
|
|
|
|
|
|
|
def __init__(self, host: str, port: int):
|
|
|
|
super().__init__(tcpConnectionTypes.SERVER)
|
|
|
|
self._conn: Optional[Union[ssl.SSLSocket, socket.socket]] = None
|
|
|
|
self.addr: Tuple[str, int] = (host, int(port))
|
2021-10-31 19:49:19 +00:00
|
|
|
self.closed = True
|
2019-12-03 03:18:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def connection(self) -> Union[ssl.SSLSocket, socket.socket]:
|
|
|
|
if self._conn is None:
|
|
|
|
raise TcpConnectionUninitializedException()
|
|
|
|
return self._conn
|
|
|
|
|
|
|
|
def connect(self) -> None:
|
2021-10-31 19:49:19 +00:00
|
|
|
if self._conn is None:
|
|
|
|
self._conn = new_socket_connection(self.addr)
|
|
|
|
self.closed = False
|
2020-07-08 07:41:12 +00:00
|
|
|
|
|
|
|
def wrap(self, hostname: str, ca_file: Optional[str]) -> None:
|
|
|
|
ctx = ssl.create_default_context(
|
|
|
|
ssl.Purpose.SERVER_AUTH, cafile=ca_file)
|
|
|
|
ctx.options |= ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_NO_TLSv1
|
|
|
|
ctx.check_hostname = True
|
|
|
|
self.connection.setblocking(True)
|
|
|
|
self._conn = ctx.wrap_socket(
|
|
|
|
self.connection,
|
|
|
|
server_hostname=hostname)
|
|
|
|
self.connection.setblocking(False)
|