-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip cleanly split socket and SSLSocket
- Loading branch information
Showing
6 changed files
with
129 additions
and
87 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
Empty file.
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,93 @@ | ||
from __future__ import annotations | ||
|
||
import ssl | ||
from datetime import datetime, timedelta | ||
from typing import Any | ||
|
||
from devtools import debug | ||
from urllib3.util.ssl_ import ssl_wrap_socket as urllib3_ssl_wrap_socket | ||
|
||
import mocket.state | ||
from mocket.socket import MocketSocket | ||
from mocket.types import _PeerCertRetDictType | ||
|
||
try: | ||
from urllib3.util.ssl_ import wrap_socket as urllib3_wrap_socket | ||
except ImportError: | ||
urllib3_wrap_socket = None | ||
|
||
true_urllib3_wrap_socket = urllib3_wrap_socket | ||
true_urllib3_ssl_wrap_socket = urllib3_ssl_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 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.state.state._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) -> MocketSSLSocket: | ||
kwargs = dict( | ||
family=sock.family, | ||
type=sock.type, | ||
proto=sock.proto, | ||
) | ||
|
||
ssl_socket = MocketSSLSocket(**kwargs) | ||
|
||
ssl_socket._kwargs = sock._kwargs | ||
ssl_socket._true_socket = true_urllib3_ssl_wrap_socket( | ||
sock._true_socket, | ||
**sock._kwargs, | ||
) | ||
|
||
ssl_socket._host = sock._host | ||
ssl_socket._port = sock._port | ||
ssl_socket._address = sock._address | ||
|
||
ssl_socket._timeout = sock._timeout | ||
|
||
# ssl_socket._entry = sock._entry | ||
|
||
return ssl_socket |