Windows: Content server: Fix regression in previous release that broke testing for local connections. Fixes #2046673 [v7.2 -> Adding book via calibredb and content server on the same machine fails](https://bugs.launchpad.net/calibre/+bug/2046673)

This commit is contained in:
Kovid Goyal 2023-12-18 11:57:32 +05:30
parent 0cb1210d02
commit c5dec9241f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -13,6 +13,7 @@ import traceback
from contextlib import suppress from contextlib import suppress
from functools import lru_cache, partial from functools import lru_cache, partial
from io import BytesIO from io import BytesIO
from typing import Union
from calibre import as_unicode from calibre import as_unicode
from calibre.constants import iswindows from calibre.constants import iswindows
@ -154,6 +155,15 @@ def is_ip_trusted(remote_addr, trusted_ips):
return False return False
def is_local_address(addr: Union[ipaddress.IPv4Address, ipaddress.IPv6Address, None]):
if addr is None:
return False
if addr.is_loopback:
return True
ipv4_mapped = getattr(addr, 'ipv4_mapped', None)
return getattr(ipv4_mapped, 'is_loopback', False)
class Connection: # {{{ class Connection: # {{{
def __init__(self, socket, opts, ssl_context, tdir, addr, pool, log, access_log, wakeup): def __init__(self, socket, opts, ssl_context, tdir, addr, pool, log, access_log, wakeup):
@ -165,7 +175,7 @@ class Connection: # {{{
except Exception: except Exception:
# In case addr is None, which can occasionally happen # In case addr is None, which can occasionally happen
self.remote_addr = self.remote_port = self.parsed_remote_addr = None self.remote_addr = self.remote_port = self.parsed_remote_addr = None
self.is_trusted_ip = bool(self.opts.local_write and getattr(self.parsed_remote_addr, 'is_loopback', False)) self.is_trusted_ip = bool(self.opts.local_write and is_local_address(self.parsed_remote_addr))
if not self.is_trusted_ip and self.opts.trusted_ips and self.parsed_remote_addr is not None: if not self.is_trusted_ip and self.opts.trusted_ips and self.parsed_remote_addr is not None:
self.is_trusted_ip = is_ip_trusted(self.parsed_remote_addr, parsed_trusted_ips(self.opts.trusted_ips)) self.is_trusted_ip = is_ip_trusted(self.parsed_remote_addr, parsed_trusted_ips(self.opts.trusted_ips))
self.orig_send_bufsize = self.send_bufsize = 4096 self.orig_send_bufsize = self.send_bufsize = 4096