-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Split ssl-specific parts of MocketSocket into MocketSSLSocket
- Loading branch information
Showing
5 changed files
with
121 additions
and
90 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
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,98 @@ | ||
from __future__ import annotations | ||
|
||
import contextlib | ||
import ssl | ||
from datetime import datetime, timedelta | ||
from typing import Any | ||
|
||
import urllib3.util.ssl_ | ||
|
||
from mocket.mocket import Mocket | ||
from mocket.socket import MocketSocket | ||
from mocket.types import _PeerCertRetDictType | ||
|
||
true_ssl_wrap_socket = None | ||
true_urllib3_ssl_wrap_socket = urllib3.util.ssl_.ssl_wrap_socket | ||
true_urllib3_wrap_socket = None | ||
|
||
with contextlib.suppress(ImportError): | ||
# from Py3.12 it's only under SSLContext | ||
from ssl import wrap_socket as ssl_wrap_socket | ||
|
||
true_ssl_wrap_socket = ssl_wrap_socket | ||
|
||
with contextlib.suppress(ImportError): | ||
from urllib3.util.ssl_ import wrap_socket as urllib3_wrap_socket | ||
|
||
true_urllib3_wrap_socket = urllib3_wrap_socket | ||
|
||
|
||
class MocketSSLSocket(MocketSocket): | ||
def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
super().__init__(*args, **kwargs) | ||
|
||
self._did_handshake = False | ||
|
||
def read(self, buffersize: int | None = None) -> bytes: | ||
rv = self.io.read(buffersize) | ||
if rv: | ||
self._sent_non_empty_bytes = True | ||
if self._did_handshake and not self._sent_non_empty_bytes: | ||
raise ssl.SSLWantReadError("The operation did not complete (read)") | ||
return rv | ||
|
||
def write(self, data: bytes) -> int | None: | ||
return self.send(data) | ||
|
||
def do_handshake(self) -> None: | ||
self._did_handshake = True | ||
|
||
def getpeercert(self, binary_form: bool = False) -> _PeerCertRetDictType: | ||
if not (self._host and self._port): | ||
self._address = self._host, self._port = Mocket._address | ||
|
||
now = datetime.now() | ||
shift = now + timedelta(days=30 * 12) | ||
return { | ||
"notAfter": shift.strftime("%b %d %H:%M:%S GMT"), | ||
"subjectAltName": ( | ||
("DNS", f"*.{self._host}"), | ||
("DNS", self._host), | ||
("DNS", "*"), | ||
), | ||
"subject": ( | ||
(("organizationName", f"*.{self._host}"),), | ||
(("organizationalUnitName", "Domain Control Validated"),), | ||
(("commonName", f"*.{self._host}"),), | ||
), | ||
} | ||
|
||
def ciper(self) -> tuple[str, str, str]: | ||
return ("ADH", "AES256", "SHA") | ||
|
||
def compression(self) -> str | None: | ||
return ssl.OP_NO_COMPRESSION | ||
|
||
def unwrap(self) -> MocketSocket: | ||
return self | ||
|
||
@classmethod | ||
def _create(cls, sock: MocketSocket, *args, **kwargs) -> MocketSSLSocket: | ||
ssl_socket = MocketSSLSocket() | ||
|
||
ssl_socket._kwargs = kwargs | ||
ssl_socket._true_socket = true_urllib3_ssl_wrap_socket( | ||
sock._true_socket, | ||
**kwargs, | ||
) | ||
|
||
ssl_socket._timeout = sock._timeout | ||
|
||
ssl_socket._host = sock._host | ||
ssl_socket._port = sock._port | ||
ssl_socket._address = sock._address | ||
|
||
ssl_socket._io = sock._io | ||
ssl_socket._entry = sock._entry | ||
|
||
return ssl_socket |
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