ja3-tls-client 2.1.0__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,95 @@
1
+ Metadata-Version: 2.1
2
+ Name: ja3-tls-client
3
+ Version: 2.1.0
4
+ Summary: HTTP client with JA3 / HTTP2 fingerprint spoofing (bundled Go tls-client backend)
5
+ Requires-Python: >=3.9
6
+ Description-Content-Type: text/markdown
7
+
8
+ # ja3client
9
+
10
+ HTTP-клиент с подменой TLS-отпечатка (JA3) и HTTP/2-отпечатка. Go-бэкенд
11
+ (`ja3proxy.exe`, на базе bogdanfinn/tls-client) лежит внутри пакета и
12
+ **запускается сам** при первом запросе: на свободном порту, без окна консоли.
13
+ Когда скрипт завершается (даже если он упал или его убили), бэкенд тоже закрывается.
14
+ Запускать `main.go` вручную больше не нужно.
15
+
16
+ ## Установка (один раз)
17
+
18
+ ```
19
+ cd C:\Users\admin\Desktop\ja33
20
+ pip install -e .
21
+ ```
22
+
23
+ После этого `import ja3client` работает из любого скрипта и любой папки.
24
+ Флаг `-e` означает, что правки в этой папке подхватываются сразу, без переустановки.
25
+
26
+ ## Использование
27
+
28
+ ```python
29
+ from ja3client import Ja3Session
30
+
31
+ s = Ja3Session("chrome_150", proxy="http://user:pass@host:port")
32
+ r = s.get("https://example.com", params={"q": 1})
33
+ r = s.post("https://example.com/api", json={"a": 1}) # или data={...} / data=b"..."
34
+ print(r.status_code, r.headers["content-type"], r.cookies, r.json())
35
+ ```
36
+
37
+ ### Встроенные профили
38
+
39
+ Полный ClientHello + HTTP/2 SETTINGS/priority от tls-client, плюс заголовки
40
+ по умолчанию (User-Agent, sec-ch-ua, порядок заголовков), чтобы они не спорили с TLS.
41
+
42
+ | Имя | Что это |
43
+ |---|---|
44
+ | `chrome_120` … `chrome_152`, `chrome_150_psk` | Chrome desktop (`chrome` = 150) |
45
+ | `firefox_120` … `firefox_148` | Firefox (`firefox` = 148) |
46
+ | `safari_ios_17_0` … `safari_ios_26_0` | Safari iOS (`iphone` = 26.0) |
47
+ | `okhttp4_android_7` … `okhttp4_android_13` | Android okhttp (`okhttp` / `android` = 13) |
48
+ | `opera_91`, `brave_146`, `zalando_android_mobile`, `nike_ios_mobile`, … | остальные из tls-client (без заголовков по умолчанию) |
49
+
50
+ Полный список: `ja3client.list_profiles()`.
51
+ Без заголовков по умолчанию: `Ja3Session("chrome_150", default_headers=False)`.
52
+
53
+ ### Свой отпечаток
54
+
55
+ ```python
56
+ Ja3Session(ja3="771,4865-...,0-23-65281-...,29-23-24,0", insert_grease=True)
57
+
58
+ # с HTTP/2, алгоритмами подписи и ALPN
59
+ Ja3Session(
60
+ ja3=JA3,
61
+ h2_settings={"HEADER_TABLE_SIZE": 65536, "ENABLE_PUSH": 0, "INITIAL_WINDOW_SIZE": 6291456},
62
+ pseudo_header_order=[":method", ":authority", ":scheme", ":path"],
63
+ connection_flow=15663105,
64
+ signature_algorithms=[0x0403, 0x0804, 0x0401],
65
+ alpn=["h2", "http/1.1"],
66
+ )
67
+
68
+ # сохранить под именем и использовать как встроенный
69
+ import ja3client
70
+ ja3client.register_profile("my_app", ja3=JA3, headers={"user-agent": "MyApp/1.0"})
71
+ Ja3Session("my_app")
72
+ ```
73
+
74
+ Если у своего профиля не заданы HTTP/2-настройки, берутся как у Chrome.
75
+
76
+ ### Прочее
77
+
78
+ - `ja3client.get(url, profile="okhttp", proxy=...)` — одноразовый запрос без сессии.
79
+ - `verify=True` — включить проверку сертификатов (по умолчанию выключена, как было в старой версии).
80
+ - `force_http1=True`, `allow_redirects=False`, `timeout=...`, `header_order=[...]`.
81
+ - Сессия потокобезопасна; можно параллелить через ThreadPoolExecutor.
82
+ - Старый код `from ja3_client import Ja3Session` продолжает работать, если запускать его из этой папки.
83
+
84
+ ### Настройка бэкенда
85
+
86
+ ```python
87
+ ja3client.configure(binary=r"D:\path\ja3proxy.exe") # exe лежит в другом месте
88
+ ja3client.configure(api="127.0.0.1:8080") # подключиться к уже запущенному серверу
89
+ ja3client.configure(debug=True) # показывать логи Go
90
+ ```
91
+ То же самое через переменные окружения: `JA3PROXY_BIN`, `JA3PROXY_API`, `JA3PROXY_DEBUG=1`.
92
+
93
+ ### Пересборка exe
94
+
95
+ Исходники лежат в `go_src/`. Нужен Go 1.24+, затем запусти `go_src\build.bat`.
@@ -0,0 +1,11 @@
1
+ ja3client/__init__.py,sha256=8Mv3f_vjarj2BpB0zY3fd1XhmUKbZgix8os3XN7wsvc,3512
2
+ ja3client/_names.py,sha256=9PneyR1rK9X3VTVTR4YWN3pz-b945hzNecVzJHllqNM,2140
3
+ ja3client/_server.py,sha256=gjYh0gWV_bwTcunMAQ5GjbSvLtxAPW3hXVAl3vxleQY,4891
4
+ ja3client/my_profiles.py,sha256=ibpKy0jP2-ldYr5Wk5pYdSnVYxDdXvNV7paZ8fZghg0,1319
5
+ ja3client/profiles.py,sha256=ePuHvhaF2vPgIa4XCatnU30PBfyyK1v23VDiKlKtr5I,9824
6
+ ja3client/session.py,sha256=8xcqptaFMVtOG1HU5O2QDSlaPu11zEv_X96W3ueP1Qw,11539
7
+ ja3client/bin/ja3proxy-windows-amd64.exe,sha256=lGE6uqoopQZERD12_wO5_mG1qsGQh-x6iXP8oceC7B4,12337152
8
+ ja3_tls_client-2.1.0.dist-info/METADATA,sha256=WLG6NJYr4vyuB3jfVfqlCFFSSOzsVdqPXY1koDK2cKc,4597
9
+ ja3_tls_client-2.1.0.dist-info/WHEEL,sha256=Ica8fhmbe52XbO1cyRYPNSx0KW5onuxFfHy_a18hkCU,93
10
+ ja3_tls_client-2.1.0.dist-info/top_level.txt,sha256=rTwRpTlww0aaIbtZuw3BoabMaaVsZAUv5CylI5wKA_g,10
11
+ ja3_tls_client-2.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: build_wheels.py
3
+ Root-Is-Purelib: false
4
+ Tag: py3-none-win_amd64
@@ -0,0 +1 @@
1
+ ja3client
ja3client/__init__.py ADDED
@@ -0,0 +1,110 @@
1
+ """
2
+ ja3client — HTTP-клиент с подменой TLS (JA3) и HTTP/2 отпечатка.
3
+
4
+ import ja3client
5
+
6
+ s = ja3client.Session("chrome_150", proxy="http://user:pass@host:port")
7
+ r = s.get("https://tls.peet.ws/api/all")
8
+ print(r.status_code, r.json())
9
+
10
+ Сервер ja3proxy.exe запускается автоматически в фоне при первом запросе
11
+ и сам завершается вместе с Python. Запускать main.go вручную не нужно.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ from pathlib import Path
17
+
18
+ from ._server import server as _server
19
+ from .profiles import ALIASES, PRESETS, Profile, get_profile, register_profile
20
+ from typing import TYPE_CHECKING, Union
21
+
22
+ from .session import CaseInsensitiveDict, HTTPError, Ja3Error, Ja3Response, Ja3Session
23
+
24
+ if TYPE_CHECKING:
25
+ from ._names import BuiltinProfileName
26
+ from .my_profiles import MyProfileName
27
+ ProfileName = Union[BuiltinProfileName, MyProfileName]
28
+
29
+ __version__ = "2.1.0"
30
+
31
+ Session = Ja3Session
32
+
33
+ __all__ = [
34
+ "Session", "Ja3Session", "Ja3Response", "Ja3Error", "HTTPError", "Profile",
35
+ "PRESETS", "ALIASES", "register_profile", "get_profile", "list_profiles",
36
+ "configure", "shutdown", "request", "get", "post", "put", "patch", "delete", "head",
37
+ ]
38
+
39
+
40
+ def configure(binary: str | Path | None = None, api: str | None = None, debug: bool | None = None):
41
+ """
42
+ binary — путь к ja3proxy(.exe), если он лежит не в ja3client/bin/
43
+ api — адрес уже запущенного сервера ("127.0.0.1:8080"); тогда ничего не запускается
44
+ debug — показывать логи Go-сервера в консоли
45
+ """
46
+ if binary is not None:
47
+ _server.binary = Path(binary)
48
+ if api is not None:
49
+ _server.external = api or None
50
+ if debug is not None:
51
+ _server.debug = debug
52
+ _server.stop()
53
+
54
+
55
+ def shutdown():
56
+ """Остановить фоновый сервер (обычно не нужно — он закрывается сам)."""
57
+ _server.stop()
58
+
59
+
60
+ def list_profiles(builtin: bool = True) -> list[str]:
61
+ """Все доступные имена профилей: встроенные в exe + пресеты/алиасы Python."""
62
+ names = set(PRESETS) | set(ALIASES)
63
+ if builtin:
64
+ names |= set(_builtin())
65
+ return sorted(names, key=str.lower)
66
+
67
+
68
+ def _builtin() -> list[str]:
69
+ import http.client
70
+ import json
71
+ host, port = _server.address().rsplit(":", 1)
72
+ c = http.client.HTTPConnection(host, int(port), timeout=10)
73
+ try:
74
+ c.request("GET", "/profiles")
75
+ return json.loads(c.getresponse().read())["profiles"]
76
+ finally:
77
+ c.close()
78
+
79
+
80
+ # --- одноразовые запросы без явной сессии (как requests.get) -----------------
81
+
82
+ def request(method: str, url: str, profile: ProfileName | Profile | str = "chrome_150", proxy: str | None = None,
83
+ **kw) -> Ja3Response:
84
+ with Ja3Session(profile, proxy=proxy) as s:
85
+ return s.request(method, url, **kw)
86
+
87
+
88
+ def get(url, **kw):
89
+ return request("GET", url, **kw)
90
+
91
+
92
+ def post(url, **kw):
93
+ return request("POST", url, **kw)
94
+
95
+
96
+ def put(url, **kw):
97
+ return request("PUT", url, **kw)
98
+
99
+
100
+ def patch(url, **kw):
101
+ return request("PATCH", url, **kw)
102
+
103
+
104
+ def delete(url, **kw):
105
+ return request("DELETE", url, **kw)
106
+
107
+
108
+ def head(url, **kw):
109
+ kw.setdefault("allow_redirects", False)
110
+ return request("HEAD", url, **kw)
ja3client/_names.py ADDED
@@ -0,0 +1,101 @@
1
+ """
2
+ Список встроенных имён профилей — для автодополнения в IDE.
3
+ Свои профили добавляются в my_profiles.py, не здесь.
4
+ """
5
+ from typing import Literal
6
+
7
+ BuiltinProfileName = Literal[
8
+ "default",
9
+ "chrome",
10
+ "chrome_latest",
11
+ "firefox",
12
+ "safari_ios",
13
+ "iphone",
14
+ "okhttp",
15
+ "android",
16
+ "brave_146",
17
+ "brave_146_psk",
18
+ "chrome_103",
19
+ "chrome_104",
20
+ "chrome_105",
21
+ "chrome_106",
22
+ "chrome_107",
23
+ "chrome_108",
24
+ "chrome_109",
25
+ "chrome_110",
26
+ "chrome_111",
27
+ "chrome_112",
28
+ "chrome_116_psk",
29
+ "chrome_116_psk_pq",
30
+ "chrome_117",
31
+ "chrome_120",
32
+ "chrome_124",
33
+ "chrome_130_psk",
34
+ "chrome_131",
35
+ "chrome_131_psk",
36
+ "chrome_133",
37
+ "chrome_133_psk",
38
+ "chrome_144",
39
+ "chrome_144_psk",
40
+ "chrome_146",
41
+ "chrome_146_psk",
42
+ "chrome_150",
43
+ "chrome_150_psk",
44
+ "chrome_152",
45
+ "chrome_152_psk",
46
+ "cloudscraper",
47
+ "confirmed_android",
48
+ "confirmed_ios",
49
+ "firefox_102",
50
+ "firefox_104",
51
+ "firefox_105",
52
+ "firefox_106",
53
+ "firefox_108",
54
+ "firefox_110",
55
+ "firefox_117",
56
+ "firefox_120",
57
+ "firefox_123",
58
+ "firefox_132",
59
+ "firefox_133",
60
+ "firefox_135",
61
+ "firefox_146_psk",
62
+ "firefox_147",
63
+ "firefox_147_psk",
64
+ "firefox_148",
65
+ "mesh_android",
66
+ "mesh_android_1",
67
+ "mesh_android_2",
68
+ "mesh_ios",
69
+ "mesh_ios_1",
70
+ "mesh_ios_2",
71
+ "mms_ios",
72
+ "mms_ios_1",
73
+ "mms_ios_2",
74
+ "mms_ios_3",
75
+ "nike_android_mobile",
76
+ "nike_ios_mobile",
77
+ "okhttp4_android_7",
78
+ "okhttp4_android_8",
79
+ "okhttp4_android_9",
80
+ "okhttp4_android_10",
81
+ "okhttp4_android_11",
82
+ "okhttp4_android_12",
83
+ "okhttp4_android_13",
84
+ "opera_89",
85
+ "opera_90",
86
+ "opera_91",
87
+ "safari_15_6_1",
88
+ "safari_16_0",
89
+ "safari_ios_15_5",
90
+ "safari_ios_15_6",
91
+ "safari_ios_16_0",
92
+ "safari_ios_17_0",
93
+ "safari_ios_18_0",
94
+ "safari_ios_18_5",
95
+ "safari_ios_26_0",
96
+ "safari_ipad_15_6",
97
+ "zalando_android_mobile",
98
+ "zalando_ios_mobile",
99
+ ]
100
+
101
+ BUILTIN_NAMES = frozenset(BuiltinProfileName.__args__)
ja3client/_server.py ADDED
@@ -0,0 +1,135 @@
1
+ """
2
+ Автоматический запуск ja3proxy.exe в фоне.
3
+
4
+ Бинарник лежит рядом с пакетом (ja3client/bin/). При первом запросе он
5
+ запускается на свободном порту, а при выходе из Python — завершается сам
6
+ (через закрытие stdin, поэтому даже при падении/kill скрипта процесс не повиснет).
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import atexit
12
+ import os
13
+ import subprocess
14
+ import sys
15
+ import threading
16
+ from pathlib import Path
17
+
18
+ _BIN_DIR = Path(__file__).resolve().parent / "bin"
19
+
20
+
21
+ def _default_binary() -> Path:
22
+ env = os.environ.get("JA3PROXY_BIN")
23
+ if env:
24
+ return Path(env)
25
+ import platform
26
+ m = platform.machine().lower()
27
+ arch = "arm64" if m in ("arm64", "aarch64", "armv8", "armv8l") else "amd64"
28
+ if sys.platform.startswith("win"):
29
+ names = [f"ja3proxy-windows-{arch}.exe", "ja3proxy-windows-amd64.exe", "ja3proxy.exe"]
30
+ elif sys.platform == "darwin":
31
+ names = [f"ja3proxy-darwin-{arch}", "ja3proxy"]
32
+ else:
33
+ names = [f"ja3proxy-linux-{arch}", "ja3proxy-linux", "ja3proxy"]
34
+ for n in names:
35
+ p = _BIN_DIR / n
36
+ if p.exists():
37
+ return p
38
+ return _BIN_DIR / names[0]
39
+
40
+
41
+ class _Server:
42
+ def __init__(self):
43
+ self._lock = threading.Lock()
44
+ self._proc: subprocess.Popen | None = None
45
+ self._addr: str | None = None
46
+ self.binary: Path | None = None # можно переопределить через configure()
47
+ self.external: str | None = os.environ.get("JA3PROXY_API") # уже запущенный сервер
48
+ self.debug = bool(os.environ.get("JA3PROXY_DEBUG"))
49
+ self.start_timeout = 15.0
50
+
51
+ # -- public ---------------------------------------------------------
52
+ def address(self) -> str:
53
+ """host:port работающего сервера (запускает его при необходимости)."""
54
+ if self.external:
55
+ return self.external.replace("http://", "").rstrip("/")
56
+ with self._lock:
57
+ if self._proc is not None and self._proc.poll() is None and self._addr:
58
+ return self._addr
59
+ self._start()
60
+ return self._addr # type: ignore[return-value]
61
+
62
+ def restart(self) -> str:
63
+ with self._lock:
64
+ self._stop()
65
+ return self.address()
66
+
67
+ def stop(self):
68
+ with self._lock:
69
+ self._stop()
70
+
71
+ # -- internal -------------------------------------------------------
72
+ def _start(self):
73
+ binary = Path(self.binary) if self.binary else _default_binary()
74
+ if not binary.exists():
75
+ raise FileNotFoundError(
76
+ f"Не найден бинарник ja3proxy: {binary}\n"
77
+ "Положи ja3proxy.exe в папку ja3client/bin/ или укажи путь "
78
+ "через ja3client.configure(binary=...) / переменную JA3PROXY_BIN."
79
+ )
80
+ if not sys.platform.startswith("win"):
81
+ try:
82
+ os.chmod(binary, os.stat(binary).st_mode | 0o111)
83
+ except OSError:
84
+ pass
85
+
86
+ kwargs: dict = {}
87
+ if sys.platform.startswith("win"):
88
+ kwargs["creationflags"] = 0x08000000 # CREATE_NO_WINDOW — без чёрного окна консоли
89
+
90
+ self._proc = subprocess.Popen(
91
+ [str(binary), "-addr", "127.0.0.1:0", "-watch-stdin"],
92
+ stdin=subprocess.PIPE,
93
+ stdout=subprocess.PIPE,
94
+ stderr=None if self.debug else subprocess.DEVNULL,
95
+ **kwargs,
96
+ )
97
+
98
+ result: dict = {}
99
+
100
+ def _read():
101
+ line = self._proc.stdout.readline().decode("utf-8", "replace").strip() # type: ignore[union-attr]
102
+ result["line"] = line
103
+
104
+ t = threading.Thread(target=_read, daemon=True)
105
+ t.start()
106
+ t.join(self.start_timeout)
107
+ line = result.get("line", "")
108
+ if not line.startswith("JA3PROXY_LISTENING "):
109
+ code = self._proc.poll()
110
+ self._stop()
111
+ raise RuntimeError(
112
+ f"ja3proxy не запустился (exit={code}, вывод={line!r}). "
113
+ "Запусти с JA3PROXY_DEBUG=1, чтобы увидеть логи."
114
+ )
115
+ self._addr = line.split(" ", 1)[1].strip()
116
+
117
+ def _stop(self):
118
+ p = self._proc
119
+ self._proc = None
120
+ self._addr = None
121
+ if p is None:
122
+ return
123
+ try:
124
+ if p.stdin:
125
+ p.stdin.close() # сервер сам выйдет по EOF
126
+ p.wait(timeout=3)
127
+ except Exception:
128
+ try:
129
+ p.kill()
130
+ except Exception:
131
+ pass
132
+
133
+
134
+ server = _Server()
135
+ atexit.register(server.stop)
Binary file
@@ -0,0 +1,36 @@
1
+ """
2
+ ТВОИ ПРОФИЛИ — добавляй сюда.
3
+
4
+ 1. Добавь профиль в словарь MY_PROFILES.
5
+ 2. Добавь это же имя в MyProfileName (тогда оно будет выпадать в подсказках VS Code).
6
+
7
+ После этого в любом скрипте: Ja3Session("my_app")
8
+ """
9
+ from typing import Literal
10
+
11
+ from .profiles import Profile
12
+
13
+ MY_PROFILES: dict[str, Profile] = {
14
+ "my_app": Profile(
15
+ ja3=(
16
+ "771,"
17
+ "4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,"
18
+ "0-51764-18-10-23-17613-45-11-27-16-13-35-43-65037-51,"
19
+ "4588-29-23-24,"
20
+ "0"
21
+ ),
22
+ grease=False,
23
+ # h2_settings={"HEADER_TABLE_SIZE": 65536, "ENABLE_PUSH": 0,
24
+ # "INITIAL_WINDOW_SIZE": 6291456, "MAX_HEADER_LIST_SIZE": 262144},
25
+ # pseudo_header_order=[":method", ":authority", ":scheme", ":path"],
26
+ # connection_flow=15663105,
27
+ # headers={"user-agent": "MyApp/1.0"},
28
+ # header_order=["user-agent", "accept-encoding"],
29
+ ),
30
+ # "второй_профиль": Profile(ja3="...", headers={...}),
31
+ }
32
+
33
+ # имена для автодополнения — держи в синхроне с MY_PROFILES
34
+ MyProfileName = Literal[
35
+ "my_app",
36
+ ]
ja3client/profiles.py ADDED
@@ -0,0 +1,212 @@
1
+ """
2
+ Профили отпечатков.
3
+
4
+ Есть два вида:
5
+
6
+ 1. Встроенные (из tls-client, вшиты в ja3proxy.exe): полный TLS ClientHello +
7
+ HTTP/2 SETTINGS/priority как у настоящего браузера/приложения.
8
+ Список: ja3client.list_profiles() (chrome_150, chrome_146, firefox_148,
9
+ safari_ios_26_0, okhttp4_android_13, ... — около 90 штук).
10
+
11
+ 2. Кастомные: любая JA3-строка + (опционально) свои HTTP/2 настройки,
12
+ алгоритмы подписи, ALPN.
13
+
14
+ Для части профилей ниже заданы ещё и заголовки по умолчанию (User-Agent,
15
+ sec-ch-ua, порядок заголовков), чтобы TLS-отпечаток не противоречил
16
+ User-Agent. Это разумные значения «как у браузера», а не байт-в-байт копия —
17
+ при необходимости переопредели headers / header_order.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ from dataclasses import dataclass, field, replace
23
+
24
+
25
+ @dataclass
26
+ class Profile:
27
+ # либо имя встроенного профиля tls-client ...
28
+ tls_profile: str | None = None
29
+ # ... либо своя JA3-строка
30
+ ja3: str | None = None
31
+ grease: bool = False
32
+ signature_algorithms: list[int] | None = None # для своего JA3 (ext 13)
33
+ alpn: list[str] | None = None # для своего JA3 (ext 16)
34
+ # HTTP/2 (только для своего JA3): порядок важен
35
+ h2_settings: dict[str, int] | None = None # {"HEADER_TABLE_SIZE": 65536, ...}
36
+ pseudo_header_order: list[str] | None = None # [":method", ":authority", ":scheme", ":path"]
37
+ connection_flow: int | None = None # WINDOW_UPDATE, у Chrome 15663105
38
+ # заголовки по умолчанию
39
+ headers: dict[str, str] = field(default_factory=dict)
40
+ header_order: list[str] | None = None
41
+
42
+ def to_payload(self) -> dict:
43
+ if self.ja3:
44
+ custom: dict = {"ja3": self.ja3, "grease": self.grease}
45
+ if self.signature_algorithms:
46
+ custom["signatureAlgorithms"] = list(self.signature_algorithms)
47
+ if self.alpn:
48
+ custom["alpn"] = list(self.alpn)
49
+ if self.h2_settings:
50
+ custom["h2Settings"] = [{"name": k, "value": int(v)} for k, v in self.h2_settings.items()]
51
+ if self.pseudo_header_order:
52
+ custom["pseudoHeaderOrder"] = list(self.pseudo_header_order)
53
+ if self.connection_flow:
54
+ custom["connectionFlow"] = int(self.connection_flow)
55
+ return {"custom": custom}
56
+ return {"profile": self.tls_profile or "chrome_150"}
57
+
58
+ def copy(self, **changes) -> "Profile":
59
+ return replace(self, **changes)
60
+
61
+
62
+ # ---------------------------------------------------------------- заголовки
63
+
64
+ def _chrome_headers(ver: int, platform: str = "Windows") -> tuple[dict, list]:
65
+ if platform == "Android":
66
+ ua = (f"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 "
67
+ f"(KHTML, like Gecko) Chrome/{ver}.0.0.0 Mobile Safari/537.36")
68
+ mobile = "?1"
69
+ else:
70
+ ua = (f"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
71
+ f"(KHTML, like Gecko) Chrome/{ver}.0.0.0 Safari/537.36")
72
+ mobile = "?0"
73
+ h = {
74
+ "sec-ch-ua": f'"Chromium";v="{ver}", "Google Chrome";v="{ver}", "Not_A Brand";v="99"',
75
+ "sec-ch-ua-mobile": mobile,
76
+ "sec-ch-ua-platform": f'"{platform}"',
77
+ "upgrade-insecure-requests": "1",
78
+ "user-agent": ua,
79
+ "accept": ("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,"
80
+ "image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"),
81
+ "sec-fetch-site": "none",
82
+ "sec-fetch-mode": "navigate",
83
+ "sec-fetch-user": "?1",
84
+ "sec-fetch-dest": "document",
85
+ "accept-encoding": "gzip, deflate, br, zstd",
86
+ "accept-language": "en-US,en;q=0.9",
87
+ "priority": "u=0, i",
88
+ }
89
+ order = ["host", "content-length", "sec-ch-ua", "sec-ch-ua-mobile", "sec-ch-ua-platform",
90
+ "upgrade-insecure-requests", "user-agent", "content-type", "accept", "origin",
91
+ "sec-fetch-site", "sec-fetch-mode", "sec-fetch-user", "sec-fetch-dest", "referer",
92
+ "accept-encoding", "accept-language", "cookie", "priority"]
93
+ return h, order
94
+
95
+
96
+ def _firefox_headers(ver: int) -> tuple[dict, list]:
97
+ h = {
98
+ "user-agent": f"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:{ver}.0) Gecko/20100101 Firefox/{ver}.0",
99
+ "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
100
+ "accept-language": "en-US,en;q=0.5",
101
+ "accept-encoding": "gzip, deflate, br, zstd",
102
+ "upgrade-insecure-requests": "1",
103
+ "sec-fetch-dest": "document",
104
+ "sec-fetch-mode": "navigate",
105
+ "sec-fetch-site": "none",
106
+ "sec-fetch-user": "?1",
107
+ "priority": "u=0, i",
108
+ "te": "trailers",
109
+ }
110
+ order = ["host", "user-agent", "accept", "accept-language", "accept-encoding", "content-type",
111
+ "content-length", "origin", "referer", "cookie", "upgrade-insecure-requests",
112
+ "sec-fetch-dest", "sec-fetch-mode", "sec-fetch-site", "sec-fetch-user", "priority", "te"]
113
+ return h, order
114
+
115
+
116
+ def _safari_ios_headers(safari_ver: str, ios: str) -> tuple[dict, list]:
117
+ h = {
118
+ "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
119
+ "sec-fetch-site": "none",
120
+ "sec-fetch-mode": "navigate",
121
+ "user-agent": (f"Mozilla/5.0 (iPhone; CPU iPhone OS {ios} like Mac OS X) AppleWebKit/605.1.15 "
122
+ f"(KHTML, like Gecko) Version/{safari_ver} Mobile/15E148 Safari/604.1"),
123
+ "accept-language": "en-US,en;q=0.9",
124
+ "sec-fetch-dest": "document",
125
+ "accept-encoding": "gzip, deflate, br",
126
+ "priority": "u=0, i",
127
+ }
128
+ order = ["host", "content-type", "accept", "sec-fetch-site", "origin", "sec-fetch-mode",
129
+ "user-agent", "referer", "sec-fetch-dest", "content-length", "accept-language",
130
+ "priority", "accept-encoding", "cookie"]
131
+ return h, order
132
+
133
+
134
+ def _okhttp_headers(ver: str = "4.12.0") -> tuple[dict, list]:
135
+ h = {"user-agent": f"okhttp/{ver}", "accept-encoding": "gzip"}
136
+ order = ["content-type", "content-length", "host", "connection", "accept-encoding",
137
+ "cookie", "user-agent"]
138
+ return h, order
139
+
140
+
141
+ def _p(tls_profile: str, hdr: tuple[dict, list] | None = None) -> Profile:
142
+ if hdr is None:
143
+ return Profile(tls_profile=tls_profile)
144
+ return Profile(tls_profile=tls_profile, headers=dict(hdr[0]), header_order=list(hdr[1]))
145
+
146
+
147
+ # ---------------------------------------------------------------- пресеты
148
+
149
+ PRESETS: dict[str, Profile] = {}
150
+
151
+ for _v in (120, 124, 131, 133, 144, 146, 150, 152):
152
+ PRESETS[f"chrome_{_v}"] = _p(f"chrome_{_v}", _chrome_headers(_v))
153
+ for _v in (131, 133, 144, 146, 150, 152):
154
+ PRESETS[f"chrome_{_v}_psk"] = _p(f"chrome_{_v}_PSK", _chrome_headers(_v))
155
+ for _v in (120, 123, 132, 133, 135, 147, 148):
156
+ PRESETS[f"firefox_{_v}"] = _p(f"firefox_{_v}", _firefox_headers(_v))
157
+ PRESETS["safari_ios_17_0"] = _p("safari_ios_17_0", _safari_ios_headers("17.0", "17_0"))
158
+ PRESETS["safari_ios_18_0"] = _p("safari_ios_18_0", _safari_ios_headers("18.0", "18_0"))
159
+ PRESETS["safari_ios_18_5"] = _p("safari_ios_18_5", _safari_ios_headers("18.5", "18_5"))
160
+ PRESETS["safari_ios_26_0"] = _p("safari_ios_26_0", _safari_ios_headers("26.0", "18_6"))
161
+ for _a in range(7, 14):
162
+ PRESETS[f"okhttp4_android_{_a}"] = _p(f"okhttp4_android_{_a}", _okhttp_headers())
163
+
164
+ # короткие алиасы
165
+ ALIASES = {
166
+ "default": "chrome_150",
167
+ "chrome": "chrome_150",
168
+ "chrome_latest": "chrome_152",
169
+ "firefox": "firefox_148",
170
+ "safari_ios": "safari_ios_26_0",
171
+ "iphone": "safari_ios_26_0",
172
+ "okhttp": "okhttp4_android_13",
173
+ "android": "okhttp4_android_13",
174
+ }
175
+
176
+
177
+ def register_profile(name: str, profile: Profile | None = None, **kwargs) -> Profile:
178
+ """Добавить свой именованный профиль, чтобы потом писать Ja3Session("my_app")."""
179
+ prof = profile or Profile(**kwargs)
180
+ PRESETS[name.lower()] = prof
181
+ return prof
182
+
183
+
184
+ def get_profile(name_or_ja3: str | Profile, default_headers: bool = True) -> Profile:
185
+ """Имя пресета / встроенного профиля / JA3-строка / Profile -> Profile."""
186
+ if isinstance(name_or_ja3, Profile):
187
+ return name_or_ja3.copy()
188
+ s = name_or_ja3.strip()
189
+ if s.count(",") == 4 and s.split(",")[0].isdigit(): # похоже на JA3
190
+ return Profile(ja3=s)
191
+ key = ALIASES.get(s.lower(), s.lower())
192
+ if key in PRESETS:
193
+ prof = PRESETS[key].copy()
194
+ if not default_headers:
195
+ prof = prof.copy(headers={}, header_order=None)
196
+ return prof
197
+ if key in BUILTIN_NAMES:
198
+ # любое другое имя из tls-client (zalando_ios_mobile, opera_91, ...) — без заголовков
199
+ return Profile(tls_profile=key)
200
+ import difflib
201
+ known = sorted(set(PRESETS) | set(ALIASES) | BUILTIN_NAMES)
202
+ close = difflib.get_close_matches(key, known, n=5, cutoff=0.5)
203
+ hint = f" Может, имелось в виду: {', '.join(close)}?" if close else ""
204
+ raise ValueError(f"Неизвестный профиль {s!r}.{hint} Все имена: ja3client.list_profiles()")
205
+
206
+
207
+ # свои профили из my_profiles.py
208
+ from ._names import BUILTIN_NAMES # noqa: E402
209
+ from .my_profiles import MY_PROFILES # noqa: E402
210
+
211
+ for _name, _prof in MY_PROFILES.items():
212
+ PRESETS[_name.lower()] = _prof
ja3client/session.py ADDED
@@ -0,0 +1,338 @@
1
+ from __future__ import annotations
2
+
3
+ import base64
4
+ import http.client
5
+ import json as _json
6
+ import threading
7
+ import uuid
8
+ from collections.abc import Mapping
9
+ from urllib.parse import urlencode, urlsplit, urlunsplit
10
+
11
+ from ._server import server
12
+ from typing import TYPE_CHECKING, Union
13
+
14
+ from .profiles import Profile, get_profile
15
+
16
+ if TYPE_CHECKING:
17
+ from ._names import BuiltinProfileName
18
+ from .my_profiles import MyProfileName
19
+ ProfileName = Union[BuiltinProfileName, MyProfileName]
20
+
21
+
22
+ class Ja3Error(RuntimeError):
23
+ """Ошибка на стороне ja3proxy (сеть, TLS, прокси, невалидный JA3 ...)."""
24
+
25
+
26
+ class HTTPError(Ja3Error):
27
+ def __init__(self, msg, response):
28
+ super().__init__(msg)
29
+ self.response = response
30
+
31
+
32
+ # ------------------------------------------------------------------ utils
33
+
34
+ class CaseInsensitiveDict(dict):
35
+ """dict заголовков с доступом без учёта регистра (как в requests)."""
36
+
37
+ def __init__(self, data=None):
38
+ super().__init__()
39
+ self._keys: dict[str, str] = {}
40
+ if data:
41
+ for k, v in dict(data).items():
42
+ self[k] = v
43
+
44
+ def __setitem__(self, k, v):
45
+ lk = k.lower()
46
+ if lk in self._keys and self._keys[lk] != k:
47
+ super().__delitem__(self._keys[lk])
48
+ self._keys[lk] = k
49
+ super().__setitem__(k, v)
50
+
51
+ def __getitem__(self, k):
52
+ return super().__getitem__(self._keys[k.lower()])
53
+
54
+ def __delitem__(self, k):
55
+ super().__delitem__(self._keys.pop(k.lower()))
56
+
57
+ def __contains__(self, k):
58
+ return isinstance(k, str) and k.lower() in self._keys
59
+
60
+ def get(self, k, default=None):
61
+ return self[k] if k in self else default
62
+
63
+ def pop(self, k, *default):
64
+ if k in self:
65
+ v = self[k]
66
+ del self[k]
67
+ return v
68
+ if default:
69
+ return default[0]
70
+ raise KeyError(k)
71
+
72
+ def setdefault(self, k, v=None):
73
+ if k not in self:
74
+ self[k] = v
75
+ return self[k]
76
+
77
+ def update(self, other=(), **kw):
78
+ for k, v in dict(other, **kw).items():
79
+ self[k] = v
80
+
81
+ def copy(self):
82
+ return CaseInsensitiveDict(self)
83
+
84
+
85
+ _local = threading.local()
86
+
87
+
88
+ def _api_call(path: str, payload: dict, timeout: float) -> dict:
89
+ """POST на локальный ja3proxy через keep-alive соединение (своё на каждый поток)."""
90
+ body = _json.dumps(payload, ensure_ascii=False).encode("utf-8")
91
+ for attempt in range(2):
92
+ addr = server.address()
93
+ conn = getattr(_local, "conn", None)
94
+ if conn is None or getattr(_local, "addr", None) != addr:
95
+ host, port = addr.rsplit(":", 1)
96
+ conn = http.client.HTTPConnection(host, int(port), timeout=timeout)
97
+ _local.conn, _local.addr = conn, addr
98
+ conn.timeout = timeout
99
+ if conn.sock is not None:
100
+ conn.sock.settimeout(timeout)
101
+ try:
102
+ conn.request("POST", path, body=body, headers={"Content-Type": "application/json"})
103
+ r = conn.getresponse()
104
+ data = r.read()
105
+ if r.status != 200:
106
+ raise Ja3Error(f"ja3proxy HTTP {r.status}: {data[:300]!r}")
107
+ return _json.loads(data)
108
+ except (ConnectionError, http.client.HTTPException, OSError) as e:
109
+ conn.close()
110
+ _local.conn = None
111
+ if attempt == 0 and not isinstance(e, TimeoutError):
112
+ continue # соединение протухло или сервер перезапустился — пробуем ещё раз
113
+ raise Ja3Error(f"нет связи с ja3proxy: {e}") from e
114
+ raise Ja3Error("unreachable")
115
+
116
+
117
+ # ------------------------------------------------------------------ response
118
+
119
+ class Ja3Response:
120
+ def __init__(self, data: dict, request_info: dict | None = None):
121
+ self._data = data
122
+ self.status_code: int = data.get("status", 0)
123
+ self.url: str = data.get("url", "")
124
+ self.http_version: str = data.get("proto", "")
125
+ self.headers_list: dict[str, list[str]] = data.get("headers") or {}
126
+ self.headers = CaseInsensitiveDict({k: ", ".join(v) for k, v in self.headers_list.items()})
127
+ self.cookies: dict[str, str] = data.get("cookies") or {}
128
+ self.request = request_info or {}
129
+ b64 = data.get("bodyBase64")
130
+ if b64:
131
+ self.content: bytes = base64.b64decode(b64)
132
+ else:
133
+ self.content = (data.get("body") or "").encode("utf-8")
134
+ self.encoding = "utf-8"
135
+
136
+ @property
137
+ def text(self) -> str:
138
+ return self.content.decode(self.encoding or "utf-8", "replace")
139
+
140
+ @property
141
+ def ok(self) -> bool:
142
+ return 200 <= self.status_code < 400
143
+
144
+ def json(self, **kw):
145
+ return _json.loads(self.content, **kw)
146
+
147
+ def raise_for_status(self):
148
+ if self.status_code >= 400:
149
+ raise HTTPError(f"{self.status_code} for url: {self.url}", self)
150
+ return self
151
+
152
+ def __bool__(self):
153
+ return self.ok
154
+
155
+ def __repr__(self):
156
+ return f"<Ja3Response [{self.status_code}] {self.url}>"
157
+
158
+
159
+ # ------------------------------------------------------------------ session
160
+
161
+ class Ja3Session:
162
+ """
163
+ Сессия с заданным TLS/HTTP2 отпечатком. Куки живут между запросами.
164
+
165
+ Ja3Session("chrome_150") # встроенный профиль + заголовки Chrome 150
166
+ Ja3Session("okhttp") # okhttp4 android 13
167
+ Ja3Session("771,4865-4866-...,0-23-...,29-23-24,0") # свой JA3
168
+ Ja3Session(ja3="771,...", h2_settings={...}) # свой JA3 + свой HTTP/2
169
+ Ja3Session(Profile(...)) # полностью свой профиль
170
+ """
171
+
172
+ def __init__(
173
+ self,
174
+ profile: ProfileName | Profile | str | None = None,
175
+ *,
176
+ ja3: str | None = None,
177
+ proxy: str | None = None,
178
+ timeout: int = 30,
179
+ insert_grease: bool | None = None,
180
+ force_http1: bool = False,
181
+ verify: bool = False,
182
+ headers: dict | None = None,
183
+ header_order: list[str] | None = None,
184
+ default_headers: bool = True,
185
+ h2_settings: dict[str, int] | None = None,
186
+ pseudo_header_order: list[str] | None = None,
187
+ connection_flow: int | None = None,
188
+ signature_algorithms: list[int] | None = None,
189
+ alpn: list[str] | None = None,
190
+ ):
191
+ if ja3:
192
+ prof = Profile(ja3=ja3)
193
+ elif profile is None:
194
+ prof = get_profile("chrome_150", default_headers)
195
+ else:
196
+ prof = get_profile(profile, default_headers)
197
+
198
+ # переопределения для своего JA3
199
+ if insert_grease is not None:
200
+ prof.grease = insert_grease
201
+ if h2_settings is not None:
202
+ prof.h2_settings = h2_settings
203
+ if pseudo_header_order is not None:
204
+ prof.pseudo_header_order = pseudo_header_order
205
+ if connection_flow is not None:
206
+ prof.connection_flow = connection_flow
207
+ if signature_algorithms is not None:
208
+ prof.signature_algorithms = signature_algorithms
209
+ if alpn is not None:
210
+ prof.alpn = alpn
211
+
212
+ self.profile = prof
213
+ self.proxy = proxy
214
+ self.timeout = timeout
215
+ self.force_http1 = force_http1
216
+ self.verify = verify
217
+ self.headers = CaseInsensitiveDict(prof.headers if default_headers else {})
218
+ if headers:
219
+ self.headers.update(headers)
220
+ self.header_order = header_order or prof.header_order
221
+ self.cookies: dict[str, str] = {}
222
+ self.session_id = uuid.uuid4().hex
223
+
224
+ # совместимость со старым API
225
+ @property
226
+ def ja3(self):
227
+ return self.profile.ja3
228
+
229
+ def set_cookie(self, name: str, value: str):
230
+ self.cookies[name] = value
231
+
232
+ def request(
233
+ self,
234
+ method: str,
235
+ url: str,
236
+ *,
237
+ params: Mapping | list | None = None,
238
+ headers: dict | None = None,
239
+ cookies: dict | None = None,
240
+ data=None,
241
+ json=None,
242
+ allow_redirects: bool = True,
243
+ timeout: int | None = None,
244
+ header_order: list[str] | None = None,
245
+ ) -> Ja3Response:
246
+ if params:
247
+ parts = urlsplit(url)
248
+ q = urlencode(params, doseq=True)
249
+ url = urlunsplit(parts._replace(query=f"{parts.query}&{q}" if parts.query else q))
250
+
251
+ merged = self.headers.copy()
252
+ if headers:
253
+ for k, v in headers.items():
254
+ if v is None:
255
+ merged.pop(k, None)
256
+ else:
257
+ merged[k] = v
258
+
259
+ payload_body: dict = {}
260
+ if json is not None:
261
+ payload_body["body"] = _json.dumps(json, separators=(",", ":"), ensure_ascii=False)
262
+ merged.setdefault("content-type", "application/json")
263
+ elif data is not None:
264
+ if isinstance(data, (bytes, bytearray, memoryview)):
265
+ payload_body["bodyBase64"] = base64.b64encode(bytes(data)).decode()
266
+ elif isinstance(data, str):
267
+ payload_body["body"] = data
268
+ else: # dict / list of tuples -> form
269
+ payload_body["body"] = urlencode(data, doseq=True)
270
+ merged.setdefault("content-type", "application/x-www-form-urlencoded")
271
+
272
+ jar = dict(self.cookies)
273
+ if cookies:
274
+ jar.update(cookies)
275
+
276
+ t = timeout or self.timeout
277
+ payload = {
278
+ "sessionId": self.session_id,
279
+ "method": method.upper(),
280
+ "url": url,
281
+ "proxy": self.proxy or "",
282
+ "headers": dict(merged),
283
+ "cookies": jar,
284
+ "followRedirects": allow_redirects,
285
+ "timeoutSeconds": t,
286
+ "forceHttp1": self.force_http1,
287
+ "insecureSkipVerify": not self.verify,
288
+ **self.profile.to_payload(),
289
+ **payload_body,
290
+ }
291
+ order = header_order or self.header_order
292
+ if order:
293
+ payload["headerOrder"] = list(order)
294
+
295
+ data_out = _api_call("/request", payload, timeout=t + 15)
296
+ if data_out.get("error"):
297
+ raise Ja3Error(data_out["error"])
298
+ resp = Ja3Response(data_out, {"method": method.upper(), "url": url, "headers": dict(merged)})
299
+ self.cookies.update(resp.cookies)
300
+ return resp
301
+
302
+ def get(self, url, **kw):
303
+ return self.request("GET", url, **kw)
304
+
305
+ def post(self, url, **kw):
306
+ return self.request("POST", url, **kw)
307
+
308
+ def put(self, url, **kw):
309
+ return self.request("PUT", url, **kw)
310
+
311
+ def patch(self, url, **kw):
312
+ return self.request("PATCH", url, **kw)
313
+
314
+ def delete(self, url, **kw):
315
+ return self.request("DELETE", url, **kw)
316
+
317
+ def head(self, url, **kw):
318
+ kw.setdefault("allow_redirects", False)
319
+ return self.request("HEAD", url, **kw)
320
+
321
+ def options(self, url, **kw):
322
+ return self.request("OPTIONS", url, **kw)
323
+
324
+ def close(self):
325
+ try:
326
+ _api_call("/session/delete", {"sessionId": self.session_id}, timeout=10)
327
+ except Exception:
328
+ pass
329
+
330
+ def __enter__(self):
331
+ return self
332
+
333
+ def __exit__(self, *exc):
334
+ self.close()
335
+
336
+ def __repr__(self):
337
+ p = self.profile
338
+ return f"<Ja3Session {p.tls_profile or 'custom-ja3'} proxy={self.proxy!r}>"