-
-
Notifications
You must be signed in to change notification settings - Fork 589
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
proxy.http.client
utility and base SSH classes (#1395)
* Add `proxy.http.client` utility and base SSH classes * py_class_role
- Loading branch information
1 parent
c24862b
commit 7824847
Showing
7 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[Unit] | ||
Description=ProxyPy Server | ||
After=network.target | ||
|
||
[Service] | ||
Type=simple | ||
User=proxypy | ||
Group=proxypy | ||
ExecStart=proxy --hostname 0.0.0.0 | ||
Restart=always | ||
SyslogIdentifier=proxypy | ||
LimitNOFILE=65536 | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# -*- 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 logging | ||
import argparse | ||
from abc import abstractmethod | ||
from typing import TYPE_CHECKING, Any | ||
|
||
|
||
try: | ||
if TYPE_CHECKING: # pragma: no cover | ||
from paramiko.channel import Channel | ||
|
||
from ...common.types import HostPort | ||
except ImportError: # pragma: no cover | ||
pass | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class BaseSshTunnelHandler: | ||
|
||
def __init__(self, flags: argparse.Namespace) -> None: | ||
self.flags = flags | ||
|
||
@abstractmethod | ||
def on_connection( | ||
self, | ||
chan: 'Channel', | ||
origin: 'HostPort', | ||
server: 'HostPort', | ||
) -> None: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def shutdown(self) -> None: | ||
raise NotImplementedError() | ||
|
||
|
||
class BaseSshTunnelListener: | ||
|
||
def __init__( | ||
self, | ||
flags: argparse.Namespace, | ||
handler: BaseSshTunnelHandler, | ||
*args: Any, | ||
**kwargs: Any, | ||
) -> None: | ||
self.flags = flags | ||
self.handler = handler | ||
|
||
def __enter__(self) -> 'BaseSshTunnelListener': | ||
self.setup() | ||
return self | ||
|
||
def __exit__(self, *args: Any) -> None: | ||
self.shutdown() | ||
|
||
@abstractmethod | ||
def is_alive(self) -> bool: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def is_active(self) -> bool: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def setup(self) -> None: | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def shutdown(self) -> None: | ||
raise NotImplementedError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- 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 | ||
from typing import Optional | ||
|
||
from .parser import HttpParser, httpParserTypes | ||
from ..common.utils import build_http_request, new_socket_connection | ||
from ..common.constants import HTTPS_PROTO, DEFAULT_TIMEOUT | ||
|
||
|
||
def client( | ||
host: bytes, | ||
port: int, | ||
path: bytes, | ||
method: bytes, | ||
body: Optional[bytes] = None, | ||
conn_close: bool = True, | ||
scheme: bytes = HTTPS_PROTO, | ||
timeout: float = DEFAULT_TIMEOUT, | ||
) -> Optional[HttpParser]: | ||
"""Makes a request to remote registry endpoint""" | ||
request = build_http_request( | ||
method=method, | ||
url=path, | ||
headers={ | ||
b'Host': host, | ||
b'Content-Type': b'application/x-www-form-urlencoded', | ||
}, | ||
body=body, | ||
conn_close=conn_close, | ||
) | ||
try: | ||
conn = new_socket_connection((host.decode(), port)) | ||
except ConnectionRefusedError: | ||
return None | ||
try: | ||
sock = ( | ||
ssl.wrap_socket(sock=conn, ssl_version=ssl.PROTOCOL_TLSv1_2) | ||
if scheme == HTTPS_PROTO | ||
else conn | ||
) | ||
except Exception: | ||
conn.close() | ||
return None | ||
parser = HttpParser( | ||
httpParserTypes.RESPONSE_PARSER, | ||
) | ||
sock.settimeout(timeout) | ||
try: | ||
sock.sendall(request) | ||
while True: | ||
chunk = sock.recv(1024) | ||
if not chunk: | ||
break | ||
parser.parse(memoryview(chunk)) | ||
if parser.is_complete: | ||
break | ||
finally: | ||
sock.close() | ||
return parser |