Skip to content

Commit

Permalink
Merge pull request #880 from freelawproject/819-improve-custom-adapte…
Browse files Browse the repository at this point in the history
…r-support

feat(network_utils): Update SSL Adapter
  • Loading branch information
flooie authored Jan 24, 2024
2 parents 71eabf3 + 7a74bbc commit ca93ed6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
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

0 comments on commit ca93ed6

Please sign in to comment.