From 3d36ee0e5346418ad3a0b47e024a12d11e7b5ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 4 Feb 2026 20:58:59 +0100 Subject: [PATCH] [common] support multiple codes/blocks for '--xff' for example '--xff JP,CN,105.48.0.0/12' --- docs/configuration.rst | 13 ++++++++----- docs/options.md | 6 +++--- gallery_dl/extractor/utils/geo.py | 19 ++++++++++++++----- gallery_dl/option.py | 4 ++-- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 3ef0ab0e..26891d49 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -973,21 +973,24 @@ Description extractor.*.geo-bypass ---------------------- Type - ``string`` + * ``string`` + * ``list`` of ``string`` Default ``"auto"`` Example * ``"JP"`` * ``"105.48.0.0/12"`` + * ``"JP,CN,105.48.0.0/12"`` + * ``["JP", "CN", "105.48.0.0/12"]`` Description - Use fake IPs as + Use a random IP as fake `X-Forwarded-For `__ header to try bypassing geographic restrictions. - | Can be either an + | Can be either `ISO 3166-2 `__ - country code - | or an IP block in CIDR notation. + country codes + | or IP blocks in CIDR notation. extractor.*.headers diff --git a/docs/options.md b/docs/options.md index 861563f4..67bf3b1c 100644 --- a/docs/options.md +++ b/docs/options.md @@ -93,9 +93,9 @@ --http-timeout SECONDS Timeout for HTTP connections (default: 30.0) --proxy URL Use the specified proxy --xff VALUE Use a fake 'X-Forwarded-For' HTTP header to try - bypassing geographic restrictions. Can be an IP - block in CIDR notation or a two-letter ISO - 3166-2 country code + bypassing geographic restrictions. Can be IP + blocks in CIDR notation or two-letter ISO + 3166-2 country codes (12.0.0.0/8,FR,CN) --source-address IP Client-side IP address to bind to -4, --force-ipv4 Make all connections via IPv4 -6, --force-ipv6 Make all connections via IPv6 diff --git a/gallery_dl/extractor/utils/geo.py b/gallery_dl/extractor/utils/geo.py index be966fde..bde524ef 100644 --- a/gallery_dl/extractor/utils/geo.py +++ b/gallery_dl/extractor/utils/geo.py @@ -258,12 +258,21 @@ COUNTRY_IP_MAP = { } -def random_ipv4(block): - if len(block) == 2: - block = COUNTRY_IP_MAP.get(block.upper()) - if not block: - return None +def random_ipv4(blocks): + if isinstance(blocks, str): + blocks = blocks.split(",") + cidr = [] + for block in blocks: + if len(block) == 2: + block = COUNTRY_IP_MAP.get(block.upper()) + if not block: + continue + cidr.append(block) + if not cidr: + return None + + block = random.choice(cidr) addr, _, preflen = block.partition("/") if not preflen: return addr diff --git a/gallery_dl/option.py b/gallery_dl/option.py index ff3a49ee..7adca7eb 100644 --- a/gallery_dl/option.py +++ b/gallery_dl/option.py @@ -494,8 +494,8 @@ def build_parser(): "--xff", dest="geo-bypass", metavar="VALUE", action=ConfigAction, help=("Use a fake 'X-Forwarded-For' HTTP header to try bypassing " - "geographic restrictions. Can be an IP block in CIDR notation " - "or a two-letter ISO 3166-2 country code") + "geographic restrictions. Can be IP blocks in CIDR notation " + "or two-letter ISO 3166-2 country codes (12.0.0.0/8,FR,CN)") ) networking.add_argument( "--source-address",