tls-client-python 1.15.0.2__tar.gz → 1.15.0.4__tar.gz

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.
Files changed (27) hide show
  1. {tls_client_python-1.15.0.2/tls_client_python.egg-info → tls_client_python-1.15.0.4}/PKG-INFO +4 -4
  2. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/Readme.md +0 -2
  3. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/pyproject.toml +3 -1
  4. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client/__init__.py +1 -1
  5. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client/_core.py +266 -63
  6. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client/bin/tls-client-darwin-amd64.dylib +0 -0
  7. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client/bin/tls-client-darwin-arm64.dylib +0 -0
  8. tls_client_python-1.15.0.2/tls_client/bin/tls-client-linux-amd64.so → tls_client_python-1.15.0.4/tls_client/bin/tls-client-linux-amd64-musl.so +0 -0
  9. tls_client_python-1.15.0.2/tls_client/bin/tls-client-alpine-amd64.so → tls_client_python-1.15.0.4/tls_client/bin/tls-client-linux-amd64.so +0 -0
  10. tls_client_python-1.15.0.4/tls_client/bin/tls-client-windows-386.dll +0 -0
  11. tls_client_python-1.15.0.4/tls_client/bin/tls-client-windows-amd64.dll +0 -0
  12. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4/tls_client_python.egg-info}/PKG-INFO +4 -4
  13. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client_python.egg-info/SOURCES.txt +1 -4
  14. tls_client_python-1.15.0.2/tls_client/bin/tls-client-linux-386.so +0 -0
  15. tls_client_python-1.15.0.2/tls_client/bin/tls-client-linux-arm.so +0 -0
  16. tls_client_python-1.15.0.2/tls_client/bin/tls-client-linux-arm64.so +0 -0
  17. tls_client_python-1.15.0.2/tls_client/bin/tls-client-windows-386.dll +0 -0
  18. tls_client_python-1.15.0.2/tls_client/bin/tls-client-windows-amd64.dll +0 -0
  19. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/LICENSE +0 -0
  20. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/MANIFEST.in +0 -0
  21. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/setup.cfg +0 -0
  22. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/setup.py +0 -0
  23. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client/_default_headers.py +0 -0
  24. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client_python.egg-info/dependency_links.txt +0 -0
  25. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client_python.egg-info/not-zip-safe +0 -0
  26. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client_python.egg-info/requires.txt +0 -0
  27. {tls_client_python-1.15.0.2 → tls_client_python-1.15.0.4}/tls_client_python.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tls-client-python
3
- Version: 1.15.0.2
3
+ Version: 1.15.0.4
4
4
  Summary: High-performance Python binding for bogdanfinn/tls-client via CFFI – zero-copy, panic-proof, full TLS fingerprint control
5
5
  Author: komAAmok
6
6
  License-Expression: MIT
@@ -22,11 +22,13 @@ Classifier: Programming Language :: Python :: 3.9
22
22
  Classifier: Programming Language :: Python :: 3.10
23
23
  Classifier: Programming Language :: Python :: 3.11
24
24
  Classifier: Programming Language :: Python :: 3.12
25
+ Classifier: Programming Language :: Python :: 3.13
26
+ Classifier: Programming Language :: Python :: 3.14
25
27
  Classifier: Programming Language :: Python :: Implementation :: CPython
26
28
  Classifier: Topic :: Internet :: WWW/HTTP
27
29
  Classifier: Topic :: Security
28
30
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
29
- Requires-Python: <3.13,>=3.6
31
+ Requires-Python: <=3.14,>=3.6
30
32
  Description-Content-Type: text/markdown
31
33
  License-File: LICENSE
32
34
  Requires-Dist: cffi>=1.14.0
@@ -40,8 +42,6 @@ Dynamic: license-file
40
42
 
41
43
  **High-performance Python binding for [bogdanfinn/tls-client](https://github.com/bogdanfinn/tls-client) via CFFI.**
42
44
 
43
- Zero-copy FFI boundary, panic-proof, full TLS fingerprint control — with a familiar `requests`-style API.
44
-
45
45
  ---
46
46
 
47
47
  ## What is TLS Fingerprinting?
@@ -6,8 +6,6 @@
6
6
 
7
7
  **High-performance Python binding for [bogdanfinn/tls-client](https://github.com/bogdanfinn/tls-client) via CFFI.**
8
8
 
9
- Zero-copy FFI boundary, panic-proof, full TLS fingerprint control — with a familiar `requests`-style API.
10
-
11
9
  ---
12
10
 
13
11
  ## What is TLS Fingerprinting?
@@ -37,6 +37,8 @@ classifiers = [
37
37
  "Programming Language :: Python :: 3.10",
38
38
  "Programming Language :: Python :: 3.11",
39
39
  "Programming Language :: Python :: 3.12",
40
+ "Programming Language :: Python :: 3.13",
41
+ "Programming Language :: Python :: 3.14",
40
42
  "Programming Language :: Python :: Implementation :: CPython",
41
43
  "Topic :: Internet :: WWW/HTTP",
42
44
  "Topic :: Security",
@@ -44,7 +46,7 @@ classifiers = [
44
46
  ]
45
47
 
46
48
 
47
- requires-python = ">=3.6, <3.13"
49
+ requires-python = ">=3.6, <=3.14"
48
50
 
49
51
  dependencies = [
50
52
  "cffi>=1.14.0",
@@ -25,4 +25,4 @@ __all__ = [
25
25
  "ClientIdentifiers",
26
26
  "clear_client_pool",
27
27
  ]
28
- __version__ = "1.15.0.2"
28
+ __version__ = "1.15.0.4"
@@ -278,6 +278,26 @@ class Request(TypedDict, total=False):
278
278
 
279
279
  Whether to disable IPv6.
280
280
  """
281
+ tcp_ttl: Optional[int]
282
+ """IP TTL 覆盖。0=自动从预设推断。
283
+
284
+ IP TTL override. 0=auto from profile.
285
+ """
286
+ tcp_window_size: Optional[int]
287
+ """TCP 窗口大小覆盖。0=自动从预设推断。
288
+
289
+ TCP window size override. 0=auto from profile.
290
+ """
291
+ tcp_window_scale: Optional[int]
292
+ """TCP 窗口缩放因子覆盖。0=自动从预设推断。
293
+
294
+ TCP window scale override. 0=auto from profile.
295
+ """
296
+ tcp_mss: Optional[int]
297
+ """TCP MSS 覆盖。0=自动从预设推断。
298
+
299
+ TCP MSS override. 0=auto from profile.
300
+ """
281
301
  catch_panics: Optional[bool]
282
302
  """是否捕获 Go panic。
283
303
 
@@ -604,6 +624,10 @@ typedef struct {
604
624
  int disable_http3;
605
625
  int disable_ipv4;
606
626
  int disable_ipv6;
627
+ int tcp_ttl;
628
+ int tcp_window_size;
629
+ int tcp_window_scale;
630
+ int tcp_mss;
607
631
  int without_cookie_jar;
608
632
  int catch_panics;
609
633
  int with_debug;
@@ -651,18 +675,73 @@ int RequestAsync(RequestOptions* opts, uintptr_t request_id, async_ca
651
675
  """
652
676
 
653
677
  # ---------------------------------------------------------------------------
654
- # Platform detection & shared‑library loading
678
+ # Platform-native shared library dynamic discovery (Git-Sync & Wheel Optimized)
655
679
  # ---------------------------------------------------------------------------
656
680
 
681
+ def _detect_libc() -> str:
682
+ """在 Linux 系统上检测 C 运行库类型(glibc 或 musl)。"""
683
+ if sys.platform != "linux":
684
+ return "glibc"
685
+ try:
686
+ libc = ctypes.CDLL(None)
687
+ try:
688
+ libc.__musl__ # pylint: disable=pointless-statement
689
+ return "musl"
690
+ except AttributeError:
691
+ return "glibc"
692
+ except Exception:
693
+ for entry in Path("/lib").glob("ld-musl-*"):
694
+ if entry.is_file():
695
+ return "musl"
696
+ return "unknown"
697
+
698
+
699
+ def _libc_suffix() -> str:
700
+ """如果是 Alpine (musl) 系统,返回 -musl 后缀。"""
701
+ if sys.platform == "linux" and _detect_libc() == "musl":
702
+ return "-musl"
703
+ return ""
704
+
705
+
706
+ def _go_os() -> str:
707
+ return {"darwin": "darwin", "linux": "linux", "win32": "windows"}.get(
708
+ sys.platform, sys.platform
709
+ )
710
+
711
+
712
+ def _go_arch() -> str:
713
+ m = platform.machine().lower()
714
+ return {
715
+ "x86_64": "amd64", "amd64": "amd64",
716
+ "arm64": "arm64", "aarch64": "arm64",
717
+ "armv7l": "arm", "armv6l": "arm",
718
+ "i386": "386", "i686": "386",
719
+ }.get(m, m)
720
+
721
+
722
+ def _shared_lib_ext() -> str:
723
+ if sys.platform == "darwin":
724
+ return ".dylib"
725
+ elif sys.platform == "win32":
726
+ return ".dll"
727
+ return ".so"
728
+
729
+
730
+ def _shared_lib_name() -> str:
731
+ ext = _shared_lib_ext()
732
+ goos = _go_os()
733
+ goarch = _go_arch()
734
+ lc = _libc_suffix()
735
+ return f"tls-client-{goos}-{goarch}{lc}{ext}"
736
+
737
+
657
738
  def _find_library() -> str:
658
739
  """定位平台对应的动态链接库。
659
740
 
660
741
  搜索顺序:
661
742
  1. 读取 ``TLS_CLIENT_LIB`` 环境变量(显式用户覆盖)。
662
- 2. 寻找包目录内 ``tls_client/bin/`` 下的平台专属统一命名文件。
743
+ 2. 寻找包目录内 ``tls_client/bin/`` 下的带架构后缀文件(如 tls-client-windows-amd64.dll)。
663
744
  3. 寻找同级开发目录 ``dist/`` 下的文件。
664
-
665
- 若未找到任何匹配文件,抛出 :exc:`FileNotFoundError`。
666
745
  """
667
746
  # 1. 环境变量显式覆盖
668
747
  env_lib = os.environ.get("TLS_CLIENT_LIB")
@@ -674,36 +753,26 @@ def _find_library() -> str:
674
753
  f"TLS_CLIENT_LIB is set to '{env_lib}' but the file does not exist."
675
754
  )
676
755
 
677
- # 2. 根据系统平台直接映射标准统一文件名
678
- if sys.platform == "win32":
679
- name = "tls-client.dll"
680
- elif sys.platform == "darwin":
681
- name = "tls-client.dylib"
682
- else:
683
- name = "tls-client.so"
684
-
756
+ name = _shared_lib_name()
685
757
  here = Path(__file__).resolve().parent
686
758
 
687
- # 3. 寻找分平台 Wheel 安装后的专属内置路径
759
+ # 2. 寻找包目录 bin/ 下的多系统共享动态库
688
760
  bundled = here / "bin" / name
689
761
  if bundled.exists():
690
762
  return str(bundled)
691
763
 
692
- # 4. 本地开发调试路径
764
+ # 3. 本地开发调试路径
693
765
  dev = here / "dist" / name
694
766
  if dev.exists():
695
767
  return str(dev)
696
768
 
697
- # 详细的异常诊断提示
698
769
  parts = [
699
770
  f"Cannot locate shared library '{name}' inside the package.",
700
771
  "",
701
772
  f" Searched: {bundled}",
702
773
  f" {dev}",
703
774
  "",
704
- " If you are developing locally, ensure the shared library is compiled and placed",
705
- " in either 'tls_client/bin/' or 'tls_client/dist/'.",
706
- " Alternatively, set the 'TLS_CLIENT_LIB' environment variable to point directly to your binary."
775
+ " Set the 'TLS_CLIENT_LIB' environment variable to point directly to your binary."
707
776
  ]
708
777
  raise FileNotFoundError("\n".join(parts))
709
778
 
@@ -1030,7 +1099,7 @@ def _compute_cache_key_hash(r: dict) -> str:
1030
1099
  The format matches Go's buildCacheKey exactly — same field order,
1031
1100
  same separators, same sort order for maps, same hex encoding.
1032
1101
 
1033
- FORMAT VERSION: 1
1102
+ FORMAT VERSION: 2
1034
1103
  If you change the hash format below, increment the version AND update
1035
1104
  Go's buildCacheKey / buildCacheKeyFromConfig in cffi_binding/main.go
1036
1105
  and the corresponding test in test_cache_key_parity.py. Format drift
@@ -1056,7 +1125,8 @@ def _compute_cache_key_hash(r: dict) -> str:
1056
1125
  f"{r['disable_ipv4']}|{r['disable_ipv6']}|{r['follow_redirects']}|"
1057
1126
  f"{r['without_cookie_jar']}|{r['allow_empty_cookies']}|"
1058
1127
  f"{r['with_default_bad_pin_handler']}|"
1059
- f"{r['timeout_seconds']}|{r['timeout_milliseconds']}"
1128
+ f"{r['timeout_seconds']}|{r['timeout_milliseconds']}|"
1129
+ f"{r['tcp_ttl']}|{r['tcp_window_size']}|{r['tcp_window_scale']}|{r['tcp_mss']}"
1060
1130
  )
1061
1131
  h.update(main.encode("utf-8"))
1062
1132
 
@@ -1263,6 +1333,10 @@ def _populate_request_options(
1263
1333
  opts.disable_http3 = _val("disable_http3", True)
1264
1334
  opts.disable_ipv4 = _val("disable_ipv4", True)
1265
1335
  opts.disable_ipv6 = _val("disable_ipv6", True)
1336
+ opts.tcp_ttl = _val("tcp_ttl")
1337
+ opts.tcp_window_size = _val("tcp_window_size")
1338
+ opts.tcp_window_scale = _val("tcp_window_scale")
1339
+ opts.tcp_mss = _val("tcp_mss")
1266
1340
  opts.catch_panics = _val("catch_panics", True)
1267
1341
  opts.with_debug = _val("with_debug", True)
1268
1342
 
@@ -1697,9 +1771,41 @@ class Session:
1697
1771
  """是否禁用 IPv6 / Whether to disable IPv6."""
1698
1772
  return bool(self.defaults["disable_ipv6"])
1699
1773
 
1700
- @disable_ipv6.setter
1701
- def disable_ipv6(self, value: bool) -> None:
1702
- self.defaults["disable_ipv6"] = 1 if value else 0
1774
+ @property
1775
+ def tcp_ttl(self) -> int:
1776
+ """IP TTL / IP TTL value. 0 = auto from profile."""
1777
+ return self.defaults["tcp_ttl"]
1778
+
1779
+ @tcp_ttl.setter
1780
+ def tcp_ttl(self, value: int) -> None:
1781
+ self.defaults["tcp_ttl"] = value
1782
+
1783
+ @property
1784
+ def tcp_window_size(self) -> int:
1785
+ """TCP receive window size. 0 = auto from profile."""
1786
+ return self.defaults["tcp_window_size"]
1787
+
1788
+ @tcp_window_size.setter
1789
+ def tcp_window_size(self, value: int) -> None:
1790
+ self.defaults["tcp_window_size"] = value
1791
+
1792
+ @property
1793
+ def tcp_window_scale(self) -> int:
1794
+ """TCP window scale factor. 0 = auto from profile."""
1795
+ return self.defaults["tcp_window_scale"]
1796
+
1797
+ @tcp_window_scale.setter
1798
+ def tcp_window_scale(self, value: int) -> None:
1799
+ self.defaults["tcp_window_scale"] = value
1800
+
1801
+ @property
1802
+ def tcp_mss(self) -> int:
1803
+ """TCP Maximum Segment Size. 0 = auto from profile."""
1804
+ return self.defaults["tcp_mss"]
1805
+
1806
+ @tcp_mss.setter
1807
+ def tcp_mss(self, value: int) -> None:
1808
+ self.defaults["tcp_mss"] = value
1703
1809
 
1704
1810
  @property
1705
1811
  def allow_empty_cookies(self) -> bool:
@@ -1816,6 +1922,15 @@ class Session:
1816
1922
  disable_ipv4: bool = False,
1817
1923
  # 禁用 IPv6 / Disable IPv6
1818
1924
  disable_ipv6: bool = False,
1925
+ # ── TCP/IP Fingerprint ──
1926
+ # IP TTL (Time To Live). Windows=128, Linux/macOS=64. 0=auto from profile
1927
+ tcp_ttl: int = 0,
1928
+ # TCP receive window size. Windows=64240, Linux/macOS=65535. 0=auto
1929
+ tcp_window_size: int = 0,
1930
+ # TCP window scale factor. Windows=8, Linux=7, macOS=6. 0=auto
1931
+ tcp_window_scale: int = 0,
1932
+ # TCP Maximum Segment Size. Standard Ethernet=1460. 0=auto
1933
+ tcp_mss: int = 0,
1819
1934
  # ── Cookie ──
1820
1935
  # 是否允许空 Cookie / Whether to allow empty-value cookies
1821
1936
  allow_empty_cookies: bool = False,
@@ -1871,6 +1986,10 @@ class Session:
1871
1986
  "disable_http3": 1 if disable_http3 else 0,
1872
1987
  "disable_ipv4": 1 if disable_ipv4 else 0,
1873
1988
  "disable_ipv6": 1 if disable_ipv6 else 0,
1989
+ "tcp_ttl": tcp_ttl,
1990
+ "tcp_window_size": tcp_window_size,
1991
+ "tcp_window_scale": tcp_window_scale,
1992
+ "tcp_mss": tcp_mss,
1874
1993
  "catch_panics": 1 if catch_panics else 0,
1875
1994
  "with_debug": 1 if with_debug else 0,
1876
1995
  }
@@ -2014,6 +2133,14 @@ class Session:
2014
2133
  disable_ipv4: Optional[bool] = None,
2015
2134
  # 覆盖 IPv6 禁用 / Override disable IPv6
2016
2135
  disable_ipv6: Optional[bool] = None,
2136
+ # 覆盖 TCP TTL / Override TCP TTL
2137
+ tcp_ttl: Optional[int] = None,
2138
+ # 覆盖 TCP 窗口大小 / Override TCP window size
2139
+ tcp_window_size: Optional[int] = None,
2140
+ # 覆盖 TCP 窗口缩放 / Override TCP window scale
2141
+ tcp_window_scale: Optional[int] = None,
2142
+ # 覆盖 TCP MSS / Override TCP MSS
2143
+ tcp_mss: Optional[int] = None,
2017
2144
  # 覆盖 panic 捕获 / Override catch-panics
2018
2145
  catch_panics: Optional[bool] = None,
2019
2146
  # 覆盖调试日志 / Override debug logging
@@ -2107,6 +2234,10 @@ class Session:
2107
2234
  "disable_http3": _val("disable_http3", disable_http3, True),
2108
2235
  "disable_ipv4": _val("disable_ipv4", disable_ipv4, True),
2109
2236
  "disable_ipv6": _val("disable_ipv6", disable_ipv6, True),
2237
+ "tcp_ttl": _val("tcp_ttl", tcp_ttl),
2238
+ "tcp_window_size": _val("tcp_window_size", tcp_window_size),
2239
+ "tcp_window_scale": _val("tcp_window_scale", tcp_window_scale),
2240
+ "tcp_mss": _val("tcp_mss", tcp_mss),
2110
2241
  "follow_redirects": _val("follow_redirects", follow_redirects, True),
2111
2242
  "without_cookie_jar": _val("without_cookie_jar", without_cookie_jar, True),
2112
2243
  "allow_empty_cookies": _val("allow_empty_cookies", allow_empty_cookies, True),
@@ -2224,6 +2355,10 @@ class Session:
2224
2355
  opts.disable_http3 = _val("disable_http3", disable_http3, True)
2225
2356
  opts.disable_ipv4 = _val("disable_ipv4", disable_ipv4, True)
2226
2357
  opts.disable_ipv6 = _val("disable_ipv6", disable_ipv6, True)
2358
+ opts.tcp_ttl = _val("tcp_ttl", tcp_ttl)
2359
+ opts.tcp_window_size = _val("tcp_window_size", tcp_window_size)
2360
+ opts.tcp_window_scale = _val("tcp_window_scale", tcp_window_scale)
2361
+ opts.tcp_mss = _val("tcp_mss", tcp_mss)
2227
2362
  opts.catch_panics = _val("catch_panics", catch_panics, True)
2228
2363
  opts.with_debug = _val("with_debug", with_debug, True)
2229
2364
 
@@ -2328,6 +2463,31 @@ def _next_request_id() -> int:
2328
2463
  return _request_counter
2329
2464
 
2330
2465
 
2466
+ def _resolve_async_response(ffi, future, raw_gc):
2467
+ """Unpack C response and resolve future ON THE EVENT LOOP THREAD.
2468
+
2469
+ Called via loop.call_soon_threadsafe from the CFFI callback bridge.
2470
+ By deferring all ffi.string / ffi.buffer / .decode() operations to the
2471
+ event loop thread, the Go OS thread callback releases the GIL in
2472
+ microseconds instead of milliseconds — eliminating GIL contention at
2473
+ high concurrency (>10K req/s).
2474
+
2475
+ C memory is kept alive by ffi.gc(raw_res, lib.FreeResponse) until
2476
+ this function completes and the reference to raw_gc is dropped.
2477
+ """
2478
+ try:
2479
+ if not future.done():
2480
+ if raw_gc.err_msg != ffi.NULL:
2481
+ err = ffi.string(raw_gc.err_msg).decode("utf-8", errors="replace")
2482
+ future.set_exception(RuntimeError(err))
2483
+ else:
2484
+ response = _unpack_c_response(ffi, raw_gc)
2485
+ future.set_result(response)
2486
+ except BaseException as exc:
2487
+ if not future.done():
2488
+ future.set_exception(exc)
2489
+
2490
+
2331
2491
  def _make_async_callback(ffi, lib):
2332
2492
  """Create a CFFI callback that Go goroutines invoke with results.
2333
2493
 
@@ -2347,55 +2507,67 @@ def _make_async_callback(ffi, lib):
2347
2507
 
2348
2508
  @ffi.callback("void(uintptr_t, ResponseResult*)")
2349
2509
  def _on_async_response(request_id: int, raw_res: Any) -> None:
2350
- # The ENTIRE callback body MUST be wrapped in try/except Exception.
2351
2510
  # This callback executes on a Go-managed OS thread. If any Python
2352
2511
  # exception escapes uncaught, the Python interpreter crashes with
2353
- # Fatal Python error: Segmentation fault (Constraint 3).
2354
- try:
2355
- # Step 5-6: Unpack response
2356
- if raw_res == ffi.NULL or raw_res.err_msg != ffi.NULL:
2357
- err = "Go engine returned NULL"
2358
- if raw_res != ffi.NULL and raw_res.err_msg != ffi.NULL:
2359
- err = ffi.string(raw_res.err_msg).decode("utf-8", errors="replace")
2360
- response: Any = RuntimeError(err)
2361
- else:
2362
- response = _unpack_c_response(ffi, raw_res)
2363
- except Exception as exc:
2364
- response = exc
2365
- finally:
2366
- # Step 6: Free C memory in all code paths (success, error, panic)
2367
- if raw_res != ffi.NULL:
2368
- lib.FreeResponse(raw_res)
2369
-
2370
- # Step 9: Schedule Future resolution on the event loop thread.
2371
- # Must use call_soon_threadsafe — direct set_result/set_exception
2372
- # from a non-event-loop thread is NOT thread-safe (Constraint 4).
2512
+ # Fatal Python error: Segmentation fault.
2513
+ #
2514
+ # GIL OPTIMIZATION: Instead of doing expensive ffi.string / ffi.buffer
2515
+ # / .decode() work here (which holds the GIL for milliseconds), we
2516
+ # immediately wrap raw_res with ffi.gc (keeping C memory alive) and
2517
+ # defer ALL unpacking to the event loop thread via call_soon_threadsafe.
2518
+ # The callback releases the GIL in microseconds, eliminating GIL
2519
+ # contention at high concurrency (>10K req/s).
2520
+ gc_bound = False # track whether ffi.gc was called — see except handler
2373
2521
  try:
2522
+ # Phase 1: Pop _pending_requests & cancel defensive timeout.
2374
2523
  with _pending_lock:
2375
2524
  future, _keep_alive, timeout_handle = _pending_requests.pop(
2376
2525
  request_id, (None, None, None)
2377
2526
  )
2378
- # _keep_alive is now released — the goroutine has finished,
2379
- # so the C pointers it held via unsafe.Slice are no longer needed.
2527
+ # _keep_alive is now released — the goroutine has finished.
2380
2528
 
2381
- # Cancel the defensive timeout — the Go goroutine completed.
2382
2529
  if timeout_handle is not None:
2383
2530
  timeout_handle.cancel()
2384
2531
 
2385
- if future is None:
2386
- return # request was cancelled or timed out
2387
-
2388
- if not future.done():
2389
- loop = future.get_loop()
2390
- if isinstance(response, Exception):
2391
- loop.call_soon_threadsafe(future.set_exception, response)
2392
- else:
2393
- loop.call_soon_threadsafe(future.set_result, response)
2394
- except Exception:
2395
- # If even Future resolution fails, there is nothing we can do —
2396
- # the event loop may be shut down. Silently swallow to prevent
2397
- # a process crash.
2398
- pass
2532
+ if future is None or future.done():
2533
+ # Already resolved or timed out. Free C memory and exit.
2534
+ if raw_res != ffi.NULL:
2535
+ lib.FreeResponse(raw_res)
2536
+ return
2537
+
2538
+ # Handle NULL result synchronously (no C memory to manage).
2539
+ if raw_res == ffi.NULL:
2540
+ future.get_loop().call_soon_threadsafe(
2541
+ future.set_exception,
2542
+ RuntimeError("Go engine returned NULL")
2543
+ )
2544
+ return
2545
+
2546
+ # Phase 2: Defer unpacking to event loop thread.
2547
+ # ffi.gc binds FreeResponse to the Python GC — the C memory
2548
+ # stays alive until _resolve_async_response drops raw_gc.
2549
+ raw_gc = ffi.gc(raw_res, lib.FreeResponse)
2550
+ gc_bound = True
2551
+ loop = future.get_loop()
2552
+ loop.call_soon_threadsafe(_resolve_async_response, ffi, future, raw_gc)
2553
+
2554
+ except BaseException:
2555
+ # Log swallowed exception for diagnostics before cleaning up.
2556
+ import traceback as _tb
2557
+ print("[tls-client] async callback suppressed exception:", file=sys.stderr)
2558
+ _tb.print_exc(file=sys.stderr)
2559
+ # Absolute last resort: free C memory, then swallow.
2560
+ # If ffi.gc already bound FreeResponse, release the GC binding
2561
+ # via ffi.release() to prevent double-free when raw_gc is
2562
+ # garbage-collected later (call_soon_threadsafe may raise if
2563
+ # the event loop has already been closed).
2564
+ if raw_res != ffi.NULL:
2565
+ try:
2566
+ if gc_bound:
2567
+ ffi.release(raw_gc)
2568
+ lib.FreeResponse(raw_res)
2569
+ except BaseException:
2570
+ pass
2399
2571
 
2400
2572
  return _on_async_response
2401
2573
 
@@ -2513,6 +2685,15 @@ class AsyncSession:
2513
2685
  disable_ipv4: bool = False,
2514
2686
  # 禁用 IPv6 / Disable IPv6
2515
2687
  disable_ipv6: bool = False,
2688
+ # ── TCP/IP Fingerprint ──
2689
+ # IP TTL (Time To Live). Windows=128, Linux/macOS=64. 0=auto from profile
2690
+ tcp_ttl: int = 0,
2691
+ # TCP receive window size. Windows=64240, Linux/macOS=65535. 0=auto
2692
+ tcp_window_size: int = 0,
2693
+ # TCP window scale factor. Windows=8, Linux=7, macOS=6. 0=auto
2694
+ tcp_window_scale: int = 0,
2695
+ # TCP Maximum Segment Size. Standard Ethernet=1460. 0=auto
2696
+ tcp_mss: int = 0,
2516
2697
  # ── Cookie ──
2517
2698
  # 是否允许空 Cookie / Whether to allow empty-value cookies
2518
2699
  allow_empty_cookies: bool = False,
@@ -2558,6 +2739,10 @@ class AsyncSession:
2558
2739
  read_buffer_size=read_buffer_size,
2559
2740
  disable_ipv4=disable_ipv4,
2560
2741
  disable_ipv6=disable_ipv6,
2742
+ tcp_ttl=tcp_ttl,
2743
+ tcp_window_size=tcp_window_size,
2744
+ tcp_window_scale=tcp_window_scale,
2745
+ tcp_mss=tcp_mss,
2561
2746
  allow_empty_cookies=allow_empty_cookies,
2562
2747
  without_cookie_jar=without_cookie_jar,
2563
2748
  catch_panics=catch_panics,
@@ -2654,8 +2839,13 @@ class AsyncSession:
2654
2839
  opts.disable_http3 = _val("disable_http3", True)
2655
2840
  opts.disable_ipv4 = _val("disable_ipv4", True)
2656
2841
  opts.disable_ipv6 = _val("disable_ipv6", True)
2842
+ opts.tcp_ttl = _val("tcp_ttl")
2843
+ opts.tcp_window_size = _val("tcp_window_size")
2844
+ opts.tcp_window_scale = _val("tcp_window_scale")
2845
+ opts.tcp_mss = _val("tcp_mss")
2657
2846
  opts.catch_panics = _val("catch_panics", True)
2658
2847
  opts.with_debug = _val("with_debug", True)
2848
+
2659
2849
 
2660
2850
  # String fields
2661
2851
  for name, field in [
@@ -2741,6 +2931,10 @@ class AsyncSession:
2741
2931
  "disable_http3": _ckb("disable_http3"),
2742
2932
  "disable_ipv4": _ckb("disable_ipv4"),
2743
2933
  "disable_ipv6": _ckb("disable_ipv6"),
2934
+ "tcp_ttl": _ck("tcp_ttl"),
2935
+ "tcp_window_size": _ck("tcp_window_size"),
2936
+ "tcp_window_scale": _ck("tcp_window_scale"),
2937
+ "tcp_mss": _ck("tcp_mss"),
2744
2938
  "follow_redirects": _ckb("follow_redirects"),
2745
2939
  "without_cookie_jar": _ckb("without_cookie_jar"),
2746
2940
  "allow_empty_cookies": _ckb("allow_empty_cookies"),
@@ -2781,7 +2975,7 @@ class AsyncSession:
2781
2975
  go_timeout = float(opts.timeout_seconds) if opts.timeout_seconds > 0 else 30.0
2782
2976
  if opts.timeout_milliseconds > 0:
2783
2977
  go_timeout = float(opts.timeout_milliseconds) / 1000.0
2784
- safe_timeout = max(60.0, min(go_timeout * 2.0 + 10.0, 600.0))
2978
+ safe_timeout = max(60.0, min(go_timeout * 2.0 + 2.0, 600.0))
2785
2979
 
2786
2980
  def _on_zombie_timeout(rid: int) -> None:
2787
2981
  # Safe: by this point the Go goroutine has either completed
@@ -2972,6 +3166,10 @@ class AsyncSession:
2972
3166
  catch_panics: Optional[bool] = None,
2973
3167
  # 覆盖调试日志 / Override debug logging
2974
3168
  with_debug: Optional[bool] = None,
3169
+ tcp_ttl: Optional[int] = None,
3170
+ tcp_window_size: Optional[int] = None,
3171
+ tcp_window_scale: Optional[int] = None,
3172
+ tcp_mss: Optional[int] = None,
2975
3173
  **kwargs: Any,
2976
3174
  ) -> Response:
2977
3175
  """通过 Go 引擎执行单次 HTTP 请求(异步)。
@@ -3012,7 +3210,12 @@ class AsyncSession:
3012
3210
  allow_empty_cookies=allow_empty_cookies,
3013
3211
  without_cookie_jar=without_cookie_jar,
3014
3212
  disable_http3=disable_http3, disable_ipv4=disable_ipv4,
3015
- disable_ipv6=disable_ipv6, catch_panics=catch_panics,
3213
+ disable_ipv6=disable_ipv6,
3214
+ tcp_ttl=tcp_ttl,
3215
+ tcp_window_size=tcp_window_size,
3216
+ tcp_window_scale=tcp_window_scale,
3217
+ tcp_mss=tcp_mss,
3218
+ catch_panics=catch_panics,
3016
3219
  with_debug=with_debug,
3017
3220
  **kwargs,
3018
3221
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tls-client-python
3
- Version: 1.15.0.2
3
+ Version: 1.15.0.4
4
4
  Summary: High-performance Python binding for bogdanfinn/tls-client via CFFI – zero-copy, panic-proof, full TLS fingerprint control
5
5
  Author: komAAmok
6
6
  License-Expression: MIT
@@ -22,11 +22,13 @@ Classifier: Programming Language :: Python :: 3.9
22
22
  Classifier: Programming Language :: Python :: 3.10
23
23
  Classifier: Programming Language :: Python :: 3.11
24
24
  Classifier: Programming Language :: Python :: 3.12
25
+ Classifier: Programming Language :: Python :: 3.13
26
+ Classifier: Programming Language :: Python :: 3.14
25
27
  Classifier: Programming Language :: Python :: Implementation :: CPython
26
28
  Classifier: Topic :: Internet :: WWW/HTTP
27
29
  Classifier: Topic :: Security
28
30
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
29
- Requires-Python: <3.13,>=3.6
31
+ Requires-Python: <=3.14,>=3.6
30
32
  Description-Content-Type: text/markdown
31
33
  License-File: LICENSE
32
34
  Requires-Dist: cffi>=1.14.0
@@ -40,8 +42,6 @@ Dynamic: license-file
40
42
 
41
43
  **High-performance Python binding for [bogdanfinn/tls-client](https://github.com/bogdanfinn/tls-client) via CFFI.**
42
44
 
43
- Zero-copy FFI boundary, panic-proof, full TLS fingerprint control — with a familiar `requests`-style API.
44
-
45
45
  ---
46
46
 
47
47
  ## What is TLS Fingerprinting?
@@ -6,13 +6,10 @@ setup.py
6
6
  tls_client/__init__.py
7
7
  tls_client/_core.py
8
8
  tls_client/_default_headers.py
9
- tls_client/bin/tls-client-alpine-amd64.so
10
9
  tls_client/bin/tls-client-darwin-amd64.dylib
11
10
  tls_client/bin/tls-client-darwin-arm64.dylib
12
- tls_client/bin/tls-client-linux-386.so
11
+ tls_client/bin/tls-client-linux-amd64-musl.so
13
12
  tls_client/bin/tls-client-linux-amd64.so
14
- tls_client/bin/tls-client-linux-arm.so
15
- tls_client/bin/tls-client-linux-arm64.so
16
13
  tls_client/bin/tls-client-windows-386.dll
17
14
  tls_client/bin/tls-client-windows-amd64.dll
18
15
  tls_client_python.egg-info/PKG-INFO