Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: handle new encrypted URLs (again) (xHamster) (#1325)

NTFSvolume6e7bdf85fa5e
diff --git a/cyberdrop_dl/crawlers/xhamster.py b/cyberdrop_dl/crawlers/xhamster.py
index 29bcf2c08..47696757d 100644
--- a/cyberdrop_dl/crawlers/xhamster.py
+++ b/cyberdrop_dl/crawlers/xhamster.py
@@ -14,7 +14,7 @@ from cyberdrop_dl.exceptions import ScrapeError
from cyberdrop_dl.utils.utilities import error_handling_wrapper, get_text_between, parse_url, xor_decrypt
if TYPE_CHECKING:
- from collections.abc import Iterable
+ from collections.abc import Callable, Iterable
from cyberdrop_dl.data_structures.url_objects import ScrapeItem
@@ -30,17 +30,23 @@ class Selector:
NEXT_PAGE = "a[data-page='next']"
-def _decrypt_url(b64_url: str) -> str | None:
- if b64_url.startswith("http") or b64_url.startswith("/"):
- return b64_url
+def _decrypt_url(raw_url: str) -> str | None:
+ if raw_url.startswith("http") or raw_url.startswith("/"):
+ return raw_url
try:
- decoded_url = base64.b64decode(b64_url)
+ if _is_hex(raw_url):
+ return _decode_hex_url(raw_url)
+ except ValueError:
+ pass
+
+ try:
+ decoded_url = base64.b64decode(raw_url)
if decoded_url.startswith(b"xor_"):
return xor_decrypt(decoded_url[4:], _DECRYPTION_KEY)
if decoded_url.startswith(b"rot13_"):
return codecs.decode(decoded_url[6:].decode(), "rot_13")
except ValueError:
- return
+ pass
def _parse_url(b64_url: str) -> AbsoluteHttpURL:
@@ -326,3 +332,71 @@ def _parse_xplayer_sources(initials: dict[str, Any]) -> Iterable[Format]:
for codec, formats_list in standard_sources.items():
for format_dict in formats_list:
yield from parse_format(format_dict, codec)
+
+
+def _ensure_signed_32int(int32: int) -> int:
+ unsigned_32_bit = int32 & 0xFFFFFFFF
+ if unsigned_32_bit >= 0x80000000:
+ return unsigned_32_bit - 0x100000000
+ return unsigned_32_bit
+
+
+def _make_decoder(algo: int, seed: int) -> Callable[[], int]:
+ current_step = seed
+ if algo == 1:
+
+ def decode_next() -> int:
+ nonlocal current_step
+ current_step = _ensure_signed_32int(current_step * 1664525) + 1013904223
+ return current_step & 255
+
+ return decode_next
+
+ if algo == 2:
+
+ def decode_next() -> int:
+ nonlocal current_step
+
+ current_step = current_step & 0xFFFFFFFF
+ current_step ^= (current_step << 13) & 0xFFFFFFFF
+ current_step ^= (current_step >> 17) & 0xFFFFFFFF
+ current_step ^= (current_step << 5) & 0xFFFFFFFF
+ current_step = _ensure_signed_32int(current_step)
+
+ return current_step & 255
+
+ return decode_next
+
+ if algo == 3:
+
+ def decode_next() -> int:
+ nonlocal current_step
+ val = current_step = (current_step + 2654435769) & 0xFFFFFFFF
+ val ^= val >> 16
+ val = (val * 2246822519) & 0xFFFFFFFF
+ val ^= val >> 13
+ val = (val * 3266489917) & 0xFFFFFFFF
+ val ^= val >> 16
+
+ return val & 255
+
+ return decode_next
+
+ raise ValueError(f"Unknown crypto algo: {algo}")
+
+
+def _is_hex(hex_string: str) -> bool:
+ try:
+ int(hex_string, 16)
+ return True
+ except ValueError:
+ return False
+
+
+def _decode_hex_url(encrypted_url: str) -> str:
+ array = bytearray.fromhex(encrypted_url)
+ algo = array[0]
+ seed = _ensure_signed_32int(array[1] | (array[2] << 8) | (array[3] << 16) | (array[4] << 24))
+ decode_next = _make_decoder(algo, seed)
+ decoded_array = bytearray([(array[idx + 5] ^ decode_next()) & 255 for idx in range(len(array) - 5)])
+ return decoded_array.decode("utf-8")
diff --git a/tests/crawlers/test_cases/xhamster.py b/tests/crawlers/test_cases/xhamster.py
index 0024da545..1c2fc92b1 100644
--- a/tests/crawlers/test_cases/xhamster.py
+++ b/tests/crawlers/test_cases/xhamster.py
@@ -71,12 +71,12 @@ TEST_CASES = [
"datetime": 1738917721,
},
],
- 34,
+ 35,
),
(
"https://xhamster.com/creators/danielagomezz/exclusive",
[],
- 32,
+ 33,
),
(
"https://xhamster.com/creators/danielagomezz/photos",
diff --git a/tests/crawlers/test_xhamster.py b/tests/crawlers/test_xhamster.py
new file mode 100644
index 000000000..f88868a29
--- /dev/null
+++ b/tests/crawlers/test_xhamster.py
@@ -0,0 +1,21 @@
+import pytest
+
+from cyberdrop_dl.crawlers.xhamster import _decode_hex_url
+
+
+ "raw_url, expected_output",
+ [
+ (
+ "01fe0bef492d94cb623a2e4c49fbe1239f7e1105bda61daecca10c105273f634a5cca31dc107eaa9d731f0ec43f44d54f4439ee517bc9cacc3c439bfe70a4d4f5ff915a90eb50f9237ed3dc29ba5a4f40e7e11de62611932102c92cb47692cad3bd5f8cbe489f42dc4d1c6f776d75a93ea0126e796002457f60174e9224588c7902d5de946f51808a0f9aed4d43d78fd56a085d97e2d817f5a69dec286a568a7700d3fc32ad5786d03df0439b41c9ed76a8b733bdc427f5b374aab206289d7a8be11e0449064d64efc64a41708207b7282",
+ "https://video-nss-a.xhcdn.com/f_bjveXDoEYe3nrBnyILCA==,1758589200/media=hls4/multi=256x144:144p:,426x240:240p:,854x480:480p:,1280x720:720p:,1920x1080:1080p:,3840x2160:2160p:/027/453/344/_TPL_.av1.mp4.m3u8",
+ ),
+ (
+ "030251bf078b3c34108b5ec467b21781c6e865fcbe2f0fec4fa03c7bedf6634783bb816af8803b19971f0360b2d33dfa77e7e170544e6f3a69d77e93526ba71710858822c5b0c7371fe8af1aa21f7db5e46a1b329eceeceeadbb845e6398c431a107cdee5c478335d3e0b46e9be59e4b492a702f0c257339238c63607de262e7c8d30bba40d8febad01b1c33cf2b177b1a3e04d826286b9aaf45bf6c4229b2ea31de015312067881b8e97e25df7fdc",
+ "https://video-nss-a.xhcdn.com/if6CPa9MjylU2jNYhxtjWg==,1758596400/media=hls4/multi=256x144:144p:,426x240:240p:,854x480:480p:,1280x720:720p:/017/537/816/_TPL_.av1.mp4.m3u8",
+ ),
+ ],
+)
+def test_decode_hex_url(raw_url: str, expected_output: str) -> None:
+ result = _decode_hex_url(raw_url)
+ assert result == expected_output