Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network_utils): Update SSL Adapter #880

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions juriscraper/AbstractSite.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
set_response_encoding,
)
from juriscraper.lib.log_tools import make_default_logger
from juriscraper.lib.network_utils import SSLAdapter
from juriscraper.lib.string_utils import (
CaseNameTweaker,
clean_string,
Expand Down Expand Up @@ -113,6 +114,14 @@ def disable_certificate_verification(self):
"""
self.request["verify"] = False

def set_custom_adapter(self, cipher: str):
"""Set Custom SSL/TLS Adapter for out of date court systems

:param cipher: The court required cipher
:return: None
"""
self.request["session"].mount("https://", SSLAdapter(ciphers=cipher))

def test_mode_enabled(self):
return self.method == "LOCAL"

Expand Down
29 changes: 14 additions & 15 deletions juriscraper/lib/network_utils.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import random
import ssl
import time

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager

from juriscraper.AbstractSite import logger
from urllib3.util import create_urllib3_context


class SSLAdapter(HTTPAdapter):
"""An HTTPS Transport Adapter that uses an arbitrary SSL version."""

def __init__(self, ssl_version=None, **kwargs):
self.ssl_version = ssl_version

def __init__(
self, ssl_version=ssl.PROTOCOL_TLSv1_2, ciphers=None, **kwargs
):
self.ssl_version = ssl_version or ssl.PROTOCOL_TLS
self.ssl_context = create_urllib3_context(
ssl_version=self.ssl_version, ciphers=ciphers
)
super().__init__(**kwargs)

def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(
num_pools=connections,
maxsize=maxsize,
block=block,
ssl_version=self.ssl_version,
)
def init_poolmanager(self, *args, **kwargs):
kwargs["ssl_context"] = self.ssl_context
return super().init_poolmanager(*args, **kwargs)


def add_delay(delay=0, deviation=0):
Expand All @@ -30,6 +27,8 @@ def add_delay(delay=0, deviation=0):
Delay is the number of seconds your program will be stopped for, and
deviation is the number of seconds that the delay can vary.
"""
from juriscraper.AbstractSite import logger

duration = random.randrange(delay - deviation, delay + deviation)
logger.info(f"Adding a delay of {duration} seconds. Please wait.")
time.sleep(duration)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from dateutil.rrule import MONTHLY, rrule

from juriscraper.lib.network_utils import SSLAdapter
from juriscraper.OpinionSite import OpinionSite


Expand All @@ -22,14 +21,6 @@ def __init__(self, *args, **kwargs):
)
]

def _get_adapter_instance(self):
"""Unfortunately this court doesn't support modern crypto, so you have
to manually downgrade the crypto it uses.

See: http://stackoverflow.com/questions/14102416/
"""
return SSLAdapter(ssl_version=ssl.PROTOCOL_TLSv1)

def _get_case_names(self):
return [
e
Expand Down
Loading