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

Add the hacky solution for SSL: DH_KEY_TOO_SMALL error #114

Merged
merged 2 commits into from
Apr 5, 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
15 changes: 15 additions & 0 deletions HinetPy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from multiprocessing.pool import ThreadPool

import requests
from requests.adapters import HTTPAdapter
from urllib3 import PoolManager
from urllib3.util import create_urllib3_context

from .header import NETWORK
from .utils import (
Expand All @@ -33,6 +36,16 @@
logger = logging.getLogger(__name__)


# Hacking solution for "ssl.SSLError: [SSL: DH_KEY_TOO_SMALL] dh key too small" error.
# Reference: https://stackoverflow.com/a/76217135
class AddedCipherAdapter(HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=False):
ctx = create_urllib3_context(ciphers=":HIGH:!DH:!aNULL")
self.poolmanager = PoolManager(
num_pools=connections, maxsize=maxsize, block=block, ssl_context=ctx
)


class BaseClient:
"""
Base client to login in the Hi-net website.
Expand Down Expand Up @@ -168,6 +181,8 @@ def login(self, user, password):
if len(password) > 12:
logger.warning("Password longer than 12 characters may be truncated.")
self.session = requests.Session()
self.session.mount(self._HINET, AddedCipherAdapter())
self.session.mount(self._AUTH, AddedCipherAdapter())
self.session.get(self._AUTH, timeout=self.timeout) # get cookie
resp = self.session.post(
self._AUTH,
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

0.8.2 (2024-04-05)
------------------

- Add the updated solution for the "ssl.SSLError: [SSL: DH_KEY_TOO_SMALL] dh key too small" error

0.8.1 (2024-03-31)
------------------

Expand Down