Skip to content
Closed
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
31 changes: 28 additions & 3 deletions qsticky.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import aiohttp
import asyncio
import ipaddress
import signal
import ssl
from typing import Optional, Dict, Any
Expand Down Expand Up @@ -98,6 +99,9 @@ def __init__(self):
self.last_login_failed = False
self.first_run = True
self.last_known_port = None
self.use_unsafe_qbit_cookie_jar = self._is_ip_address(
self.settings.qbittorrent_host
)

def _setup_logger(self) -> logging.Logger:
logger = logging.getLogger("qsticky")
Expand All @@ -110,6 +114,19 @@ def _setup_logger(self) -> logging.Logger:
logger.addHandler(handler)
return logger

def _is_ip_address(self, host: str) -> bool:
try:
ipaddress.ip_address(host)
return True
except ValueError:
return False

def _get_qbit_cookie_jar(self) -> Optional[aiohttp.CookieJar]:
if not self.use_unsafe_qbit_cookie_jar:
return None

return aiohttp.CookieJar(unsafe=True)

async def get_current_qbit_port(self) -> Optional[int]:
self.logger.debug("Retrieving current qBittorrent port")
try:
Expand Down Expand Up @@ -151,7 +168,11 @@ async def _init_session(self) -> None:
self.logger.debug("SSL verification enabled")

connector = aiohttp.TCPConnector(ssl=ssl_context)
self.session = aiohttp.ClientSession(timeout=timeout, connector=connector)
self.session = aiohttp.ClientSession(
timeout=timeout,
connector=connector,
cookie_jar=self._get_qbit_cookie_jar()
)
self.logger.debug("Session initialized with timeouts")

async def _login(self) -> bool:
Expand Down Expand Up @@ -293,7 +314,11 @@ async def handle_port_change(self) -> None:
ssl_context.verify_mode = ssl.CERT_NONE

connector = aiohttp.TCPConnector(ssl=ssl_context)
async with aiohttp.ClientSession(timeout=ClientTimeout(total=30), connector=connector) as session:
async with aiohttp.ClientSession(
timeout=ClientTimeout(total=30),
connector=connector,
cookie_jar=self._get_qbit_cookie_jar()
) as session:
self.session = session
try:
new_port = await self._get_forwarded_port()
Expand Down Expand Up @@ -473,4 +498,4 @@ async def main() -> None:
await manager.cleanup()

if __name__ == "__main__":
asyncio.run(main())
asyncio.run(main())
Loading