Skip to content

Commit c2d02c5

Browse files
authored
Fix 83 - IPv6 and Timeouts (#86)
* Refactor `AuthorizationManager` to use `net.Session` and handle connection errors during authentication. * Add `net.Session` with default timeout and improve error logging in `auth.py` * Clarify comment on exception logging to highlight sensitive data exposure. * Add request timeouts to handle stalled IPv6 connections.
1 parent f0509cf commit c2d02c5

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

gogdl/auth.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os.path
66
import requests
77
import time
8+
from gogdl import net
89
from gogdl import version
910

1011
CODE_URL = "https://auth.gog.com/token?client_id=46899977096215655&client_secret=9d85c43b1482497dbbce61f6e4aa173a433796eeae2ca8c5f6129f2dc4de46d9&grant_type=authorization_code&redirect_uri=https%3A%2F%2Fembed.gog.com%2Fon_login_success%3Forigin%3Dclient&code="
@@ -14,7 +15,7 @@
1415

1516
class AuthorizationManager:
1617
def __init__(self, config_path):
17-
self.session = requests.session()
18+
self.session = net.Session()
1819
self.logger = logging.getLogger("AUTH")
1920

2021
self.config_path = config_path
@@ -120,7 +121,13 @@ def handle_cli(self, arguments, unknown_arguments):
120121
self.logger.debug("Handling cli")
121122

122123
if arguments.authorization_code:
123-
response = self.session.get(CODE_URL + arguments.authorization_code)
124+
try:
125+
response = self.session.get(CODE_URL + arguments.authorization_code)
126+
except (requests.ConnectionError, requests.Timeout) as e:
127+
# Don't log the exception itself because it contains client id and client secret
128+
self.logger.error(f"Failed to reach GOG ({type(e).__name__})")
129+
print(json.dumps({"error": True}))
130+
return
124131

125132
if not response.ok:
126133
print(json.dumps({"error": True}))

gogdl/net.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Add request timeouts. Matters when ipv6 is given by ISP but not routed
2+
# python will automatically fallback to ipv4 when the ipv6 request timeouts
3+
import requests
4+
5+
# connect, read
6+
TIMEOUT = (10, 30)
7+
8+
9+
class Session(requests.Session):
10+
def request(self, method, url, **kwargs):
11+
if kwargs.get("timeout") is None:
12+
kwargs["timeout"] = TIMEOUT
13+
return super().request(method, url, **kwargs)

0 commit comments

Comments
 (0)