tls-forge 0.1.0rc5__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.
- tls_forge-0.1.0rc5/LICENSE +202 -0
- tls_forge-0.1.0rc5/NOTICE +48 -0
- tls_forge-0.1.0rc5/PKG-INFO +336 -0
- tls_forge-0.1.0rc5/README.md +311 -0
- tls_forge-0.1.0rc5/THIRD-PARTY-NOTICES.txt +810 -0
- tls_forge-0.1.0rc5/pyproject.toml +80 -0
- tls_forge-0.1.0rc5/src/tlsforge/__init__.py +41 -0
- tls_forge-0.1.0rc5/src/tlsforge/_binary.py +97 -0
- tls_forge-0.1.0rc5/src/tlsforge/_client.py +566 -0
- tls_forge-0.1.0rc5/src/tlsforge/_errors.py +45 -0
- tls_forge-0.1.0rc5/src/tlsforge/py.typed +0 -0
- tls_forge-0.1.0rc5/tests/conftest.py +105 -0
- tls_forge-0.1.0rc5/tests/fake_daemon.py +149 -0
- tls_forge-0.1.0rc5/tests/test_binary.py +130 -0
- tls_forge-0.1.0rc5/tests/test_client.py +661 -0
- tls_forge-0.1.0rc5/tests/test_protocol.py +104 -0
- tls_forge-0.1.0rc5/tests/test_real_daemon.py +42 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Ihar Kazlouski
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
tls-forge
|
|
2
|
+
Copyright 2026 Ihar Kazlouski
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Ihar Kazlouski.
|
|
5
|
+
|
|
6
|
+
Licensed under the Apache License, Version 2.0 (the "License"); see LICENSE.
|
|
7
|
+
Section 4 of that licence obliges anyone who copies, modifies or redistributes
|
|
8
|
+
this work — in source or binary form, and an npm package containing a compiled
|
|
9
|
+
binary is a binary form — to retain the copyright notice above and to carry this
|
|
10
|
+
NOTICE file with it.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
WHAT IS NOT COVERED BY THE GRANT
|
|
14
|
+
--------------------------------
|
|
15
|
+
|
|
16
|
+
Two files in this tree are recordings of another program's output rather than
|
|
17
|
+
the author's work, and the Apache-2.0 grant above is not a claim over them:
|
|
18
|
+
|
|
19
|
+
testdata/chrome151-clienthello.bin
|
|
20
|
+
A TLS ClientHello as Google Chrome 151 put it on the wire, captured on the
|
|
21
|
+
author's own machine and kept as a test fixture. It is a protocol message
|
|
22
|
+
— a measurement of what a program sent — not a copy of that program.
|
|
23
|
+
|
|
24
|
+
profile/data/chrome_151/linux.json
|
|
25
|
+
profile/data/chrome_151/macos.json
|
|
26
|
+
profile/data/chrome_151/windows.json
|
|
27
|
+
The same ClientHello together with the HTTP/2 settings, header order and
|
|
28
|
+
user-agent string observed from the same browser, one file per platform.
|
|
29
|
+
The user-agent strings are Google's; the rest is measurement.
|
|
30
|
+
|
|
31
|
+
None of these files contains any part of Chrome, and nothing here is endorsed by
|
|
32
|
+
or affiliated with Google. Everything else in the tree — the library, the echo
|
|
33
|
+
server, the command line, the Node client, the tests and the prose — is the
|
|
34
|
+
author's work and is covered by LICENSE.
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
THIRD-PARTY SOFTWARE
|
|
38
|
+
--------------------
|
|
39
|
+
|
|
40
|
+
tls-forge compiles to a single static binary, so its dependencies travel inside
|
|
41
|
+
it — including inside the binaries published in the npm packages. Their licences
|
|
42
|
+
are reproduced in full in THIRD-PARTY-NOTICES.txt, which must travel with any
|
|
43
|
+
distributed copy for the same reason this file must.
|
|
44
|
+
|
|
45
|
+
One of them is worth naming here. github.com/bogdanfinn/fhttp ships no licence
|
|
46
|
+
file of its own; its sources carry the Go Authors' copyright header and it is a
|
|
47
|
+
fork of the Go standard library's net/http, so the Go licence is the one that
|
|
48
|
+
applies. It is reproduced in THIRD-PARTY-NOTICES.txt rather than left as a gap.
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: tls-forge
|
|
3
|
+
Version: 0.1.0rc5
|
|
4
|
+
Summary: Scraping HTTP client that gets past TLS fingerprinting — sends a real browser's JA3/JA4, HTTP/2 and header order
|
|
5
|
+
Project-URL: Homepage, https://github.com/Sec-CH-Lemon/tls-forge
|
|
6
|
+
Project-URL: Source, https://github.com/Sec-CH-Lemon/tls-forge
|
|
7
|
+
Project-URL: Changelog, https://github.com/Sec-CH-Lemon/tls-forge/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Ihar Kazlouski
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
License-File: THIRD-PARTY-NOTICES.txt
|
|
13
|
+
Keywords: anti-bot,bot-detection,browser,cloudflare,fingerprint,http2,impersonate,ja3,ja4,scraper,scraping,tls,tls-fingerprint,utls,web-scraping
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Provides-Extra: test
|
|
22
|
+
Requires-Dist: coverage>=7; extra == 'test'
|
|
23
|
+
Requires-Dist: pytest>=8; extra == 'test'
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# TLS Forge for Python
|
|
27
|
+
|
|
28
|
+
`tls-forge` is a typed Python HTTP client whose network fingerprint comes from
|
|
29
|
+
a measured browser profile. It controls the TLS ClientHello, HTTP/2 settings and
|
|
30
|
+
header order that Python's OpenSSL-based clients cannot reproduce by changing a
|
|
31
|
+
`User-Agent`.
|
|
32
|
+
|
|
33
|
+
The Python package handles application orchestration and communicates with one
|
|
34
|
+
long-lived `tls-forge daemon` process. The project overview, CLI and explanation
|
|
35
|
+
of TLS fingerprinting are in the
|
|
36
|
+
[main README](https://github.com/Sec-CH-Lemon/tls-forge#readme).
|
|
37
|
+
|
|
38
|
+
It does not execute page JavaScript and does not solve CAPTCHAs or managed
|
|
39
|
+
challenges. It addresses rejection caused specifically by TLS and HTTP
|
|
40
|
+
fingerprint mismatch.
|
|
41
|
+
|
|
42
|
+
## Requirements and installation
|
|
43
|
+
|
|
44
|
+
- Python 3.9 or newer;
|
|
45
|
+
- a supported platform wheel or Go 1.26.7+ for a custom transport.
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install tls-forge
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Import the package as `tlsforge`, without the hyphen:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
import tlsforge
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Platform wheels include the transport binary. Installation does not run a
|
|
58
|
+
compiler, download an executable through a postinstall hook, or install runtime
|
|
59
|
+
Python dependencies.
|
|
60
|
+
|
|
61
|
+
To use a custom build:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
export TLSFORGE_BIN=/absolute/path/to/tls-forge
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Basic use
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
import tlsforge
|
|
71
|
+
|
|
72
|
+
with tlsforge.Client(profile="chrome") as client:
|
|
73
|
+
response = client.get("https://tls.browserleaks.com/json")
|
|
74
|
+
print(response.status)
|
|
75
|
+
print(response.json())
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The transport is resolved during construction and started lazily on the first
|
|
79
|
+
request. Prefer the context-manager form so the child process is always closed.
|
|
80
|
+
|
|
81
|
+
## Constructor options
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
client = tlsforge.Client(
|
|
85
|
+
profile="chrome_151",
|
|
86
|
+
proxy="http://user:pass@proxy.example:8080",
|
|
87
|
+
timeout=45,
|
|
88
|
+
binary="/absolute/path/to/tls-forge",
|
|
89
|
+
insecure=False,
|
|
90
|
+
cookie_file="cookies.json",
|
|
91
|
+
cookie_set="warm-eu",
|
|
92
|
+
on_stderr=lambda line: print(line),
|
|
93
|
+
)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
| Option | Type and default | Meaning |
|
|
97
|
+
|---|---|---|
|
|
98
|
+
| `profile` | `str | None`, CLI default | Profile name or JSON profile path passed to `tls-forge daemon --profile`. |
|
|
99
|
+
| `proxy` | `str | None`, direct | HTTP, HTTPS or SOCKS proxy URL, optionally with credentials. |
|
|
100
|
+
| `timeout` | `float`, `45.0` | Per-request deadline in seconds. It must be positive and finite. The Go transport receives an additional 15-second grace period so both timeout layers do not race. |
|
|
101
|
+
| `binary` | `str | os.PathLike | None`, auto | Explicit path to the transport executable. A missing explicit path raises `BinaryNotFound` and does not fall through. |
|
|
102
|
+
| `insecure` | `bool`, `False` | Skip upstream certificate verification. Use only for controlled endpoints. |
|
|
103
|
+
| `cookie_file` | path or `None` | Load a warmed session from project JSON, browser-export JSON or Netscape cookies.txt. |
|
|
104
|
+
| `cookie_set` | `str | None` | Select a named set from a multi-session file; when omitted, the daemon chooses one at random. Requires `cookie_file`; without it, `Client(...)` raises `ValueError`. |
|
|
105
|
+
| `on_stderr` | callable or `None` | Receive transport diagnostics one line at a time. Callback exceptions are ignored so diagnostics cannot break the reader thread. |
|
|
106
|
+
|
|
107
|
+
`tlsforge.DEFAULT_TIMEOUT` contains the default Python-side timeout.
|
|
108
|
+
|
|
109
|
+
`Client()` without `profile` uses the daemon's default profile: a locally
|
|
110
|
+
installed `local` profile when available and otherwise the latest bundled
|
|
111
|
+
`chrome` profile.
|
|
112
|
+
|
|
113
|
+
## Request methods
|
|
114
|
+
|
|
115
|
+
### `get(url, *, headers=None, order=None, cookies=None)`
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
response = client.get(
|
|
119
|
+
"https://example.com/page",
|
|
120
|
+
headers={
|
|
121
|
+
"referer": "https://example.com/",
|
|
122
|
+
"accept-language": "en-GB,en;q=0.9",
|
|
123
|
+
},
|
|
124
|
+
order=["referer", "accept-language"],
|
|
125
|
+
cookies=["session=abc"],
|
|
126
|
+
)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### `post(url, body="", *, headers=None, order=None, cookies=None)`
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
response = client.post(
|
|
133
|
+
"https://example.com/api",
|
|
134
|
+
'{"hello":"world"}',
|
|
135
|
+
headers={"content-type": "application/json"},
|
|
136
|
+
order=["content-type"],
|
|
137
|
+
)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### `request(url, *, method="GET", headers=None, order=None, body=None, cookies=None)`
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
response = client.request(
|
|
144
|
+
"https://example.com/resource",
|
|
145
|
+
method="DELETE",
|
|
146
|
+
headers={"authorization": "Bearer token"},
|
|
147
|
+
order=["authorization"],
|
|
148
|
+
cookies=["session=abc"],
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
| Request argument | Type and default | Meaning |
|
|
153
|
+
|---|---|---|
|
|
154
|
+
| `url` | `str`, required | Absolute HTTP or HTTPS URL. |
|
|
155
|
+
| `method` | `str`, `"GET"` | HTTP method. `get()` and `post()` set it automatically. |
|
|
156
|
+
| `headers` | `Mapping[str, str] | None` | Headers layered over the profile. Existing profile names retain their browser position. |
|
|
157
|
+
| `order` | `Sequence[str] | None` | Order for caller-only headers. Profile headers keep their measured browser positions; unnamed caller headers follow in sorted order. |
|
|
158
|
+
| `body` | `str | bytes | None` | Request body. `bytes` is sent as base64 so it arrives intact; `None` omits the field. |
|
|
159
|
+
| `cookies` | `Iterable[str] | None` | `name=value` pairs added to the daemon's jar before the request. |
|
|
160
|
+
|
|
161
|
+
Do not manually set the `cookie` header unless replacing the jar's complete
|
|
162
|
+
output is intentional. `cookies` preserves values received earlier in the
|
|
163
|
+
session.
|
|
164
|
+
|
|
165
|
+
## Response
|
|
166
|
+
|
|
167
|
+
Requests return a frozen `tlsforge.Response` record. Its attributes cannot be
|
|
168
|
+
reassigned; treat the `headers` mapping as response data rather than mutating
|
|
169
|
+
it:
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
Response(
|
|
173
|
+
status=200,
|
|
174
|
+
url="https://example.com/final",
|
|
175
|
+
body="<html>...</html>",
|
|
176
|
+
headers={
|
|
177
|
+
"content-type": ("text/html; charset=utf-8",),
|
|
178
|
+
"set-cookie": ("a=1", "b=2"),
|
|
179
|
+
},
|
|
180
|
+
cookies=("a=1", "b=2"),
|
|
181
|
+
)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
| Field or method | Meaning |
|
|
185
|
+
|---|---|
|
|
186
|
+
| `status` | HTTP status code. Non-2xx responses are still returned normally. |
|
|
187
|
+
| `url` | Final URL after redirects. |
|
|
188
|
+
| `body` | Decompressed response body as `str`. Bytes that are not valid UTF-8 appear as U+FFFD. |
|
|
189
|
+
| `content` | The same body as `bytes`, exactly as it arrived. Use this for images, archives, or anything that is not text. |
|
|
190
|
+
| `headers` | Header names mapped to tuples of field values. Repeated `set-cookie` fields remain separate. |
|
|
191
|
+
| `cookies` | Tuple of `name=value` pairs held for the final URL. |
|
|
192
|
+
| `ok` | `True` for status codes from 200 through 299. |
|
|
193
|
+
| `json(**kwargs)` | Parse `body` with `json.loads` and return its result. |
|
|
194
|
+
|
|
195
|
+
The SDK accepts the old daemon protocol where a header value was a scalar
|
|
196
|
+
string, but always normalises the public result to `tuple[str, ...]`.
|
|
197
|
+
|
|
198
|
+
`Response.json()` raises the standard `json.JSONDecodeError` when the body is
|
|
199
|
+
not JSON.
|
|
200
|
+
|
|
201
|
+
## Exceptions
|
|
202
|
+
|
|
203
|
+
All package-specific errors inherit from `tlsforge.TLSForgeError`.
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
try:
|
|
207
|
+
response = client.get(url)
|
|
208
|
+
except tlsforge.BinaryNotFound:
|
|
209
|
+
# Fix the installation or binary path.
|
|
210
|
+
...
|
|
211
|
+
except tlsforge.Timeout:
|
|
212
|
+
# Retry or increase the deadline.
|
|
213
|
+
...
|
|
214
|
+
except tlsforge.RequestFailed:
|
|
215
|
+
# DNS, connection, proxy or TLS request failure.
|
|
216
|
+
...
|
|
217
|
+
except tlsforge.TransportError:
|
|
218
|
+
# Process startup, exit, pipe or protocol failure.
|
|
219
|
+
...
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
| Exception | Meaning |
|
|
223
|
+
|---|---|
|
|
224
|
+
| `TLSForgeError` | Base class for package errors. |
|
|
225
|
+
| `BinaryNotFound` | No usable transport binary was found, or an explicit path was missing. |
|
|
226
|
+
| `RequestFailed` | The daemon ran, but the HTTP request failed. |
|
|
227
|
+
| `Timeout` | The Python deadline passed. Also inherits from built-in `TimeoutError`. |
|
|
228
|
+
| `TransportError` | The daemon could not start, exited, could not be written to, or returned invalid protocol data. |
|
|
229
|
+
|
|
230
|
+
Invalid caller values such as an empty URL, non-positive timeout or non-callable
|
|
231
|
+
`on_stderr` raise standard `ValueError` or `TypeError`.
|
|
232
|
+
|
|
233
|
+
The client remains usable after a request timeout or transport failure. The next
|
|
234
|
+
request starts a fresh daemon while monotonically increasing request ids prevent
|
|
235
|
+
a late response from being attached to the wrong call.
|
|
236
|
+
|
|
237
|
+
## Identity, threads and close
|
|
238
|
+
|
|
239
|
+
One `Client` is one browser identity:
|
|
240
|
+
|
|
241
|
+
- one transport process;
|
|
242
|
+
- one TLS and HTTP profile;
|
|
243
|
+
- one cookie jar;
|
|
244
|
+
- one proxy and exit IP.
|
|
245
|
+
|
|
246
|
+
The client is thread-safe but serialises requests with a lock. Calls from
|
|
247
|
+
multiple threads queue instead of overlapping. Use a pool of clients for
|
|
248
|
+
parallel scraping, normally one per proxy:
|
|
249
|
+
|
|
250
|
+
```python
|
|
251
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
252
|
+
from contextlib import ExitStack
|
|
253
|
+
from itertools import cycle
|
|
254
|
+
|
|
255
|
+
import tlsforge
|
|
256
|
+
|
|
257
|
+
with ExitStack() as stack:
|
|
258
|
+
clients = [
|
|
259
|
+
stack.enter_context(tlsforge.Client(proxy=proxy))
|
|
260
|
+
for proxy in proxies
|
|
261
|
+
]
|
|
262
|
+
turns = cycle(clients)
|
|
263
|
+
with ThreadPoolExecutor(max_workers=len(clients)) as pool:
|
|
264
|
+
pages = list(pool.map(lambda url: next(turns).get(url), urls))
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
`close()` is idempotent and final. It waits for the current request, stops and
|
|
268
|
+
reaps the daemon, and prevents later requests from silently creating a new
|
|
269
|
+
identity. A finalizer also cleans up a forgotten client, but deterministic
|
|
270
|
+
`with` or `close()` usage is preferred.
|
|
271
|
+
|
|
272
|
+
## Warmed cookie sessions
|
|
273
|
+
|
|
274
|
+
`cookie_file` accepts the same formats as the CLI:
|
|
275
|
+
|
|
276
|
+
- a project JSON file containing multiple named sets;
|
|
277
|
+
- a bare JSON array of sets;
|
|
278
|
+
- a flat browser-extension cookie export;
|
|
279
|
+
- Netscape cookies.txt.
|
|
280
|
+
|
|
281
|
+
```python
|
|
282
|
+
with tlsforge.Client(
|
|
283
|
+
cookie_file="cookies.json",
|
|
284
|
+
cookie_set="warm-eu",
|
|
285
|
+
) as client:
|
|
286
|
+
response = client.get("https://example.com/")
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Expired cookies are removed by the daemon before a set is seeded. Cookie
|
|
290
|
+
domain, path, secure, HttpOnly and expiry attributes are retained by the Go
|
|
291
|
+
cookie jar.
|
|
292
|
+
|
|
293
|
+
The Python SDK currently loads but does not write session files. Use the CLI
|
|
294
|
+
`fetch --save-cookies` or `batch --save-cookies` when the final jar must be
|
|
295
|
+
persisted.
|
|
296
|
+
|
|
297
|
+
## Binary resolution
|
|
298
|
+
|
|
299
|
+
The exported `tlsforge.resolve_binary(explicit=None)` function and `Client` use
|
|
300
|
+
this order:
|
|
301
|
+
|
|
302
|
+
1. constructor `binary` option;
|
|
303
|
+
2. `TLSFORGE_BIN` environment variable;
|
|
304
|
+
3. binary bundled in the platform wheel;
|
|
305
|
+
4. `bin/tls-forge` from a surrounding source checkout;
|
|
306
|
+
5. `tls-forge` on `PATH`.
|
|
307
|
+
|
|
308
|
+
```python
|
|
309
|
+
import tlsforge
|
|
310
|
+
|
|
311
|
+
print(tlsforge.resolve_binary())
|
|
312
|
+
print(tlsforge.bundled_binary())
|
|
313
|
+
print(tlsforge.exe_name())
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
An explicit path that does not exist raises `BinaryNotFound` rather than
|
|
317
|
+
silently selecting a different version.
|
|
318
|
+
|
|
319
|
+
## Development and typing
|
|
320
|
+
|
|
321
|
+
The package includes `py.typed`, so its annotations are available to static
|
|
322
|
+
type checkers.
|
|
323
|
+
|
|
324
|
+
From the repository root:
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
make python-test
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
The suite uses a stand-in daemon and requires 100% statement and branch
|
|
331
|
+
coverage. It does not need a Go build or network access.
|
|
332
|
+
|
|
333
|
+
The project is licensed under Apache-2.0; see
|
|
334
|
+
[`LICENSE`](https://github.com/Sec-CH-Lemon/tls-forge/blob/main/LICENSE),
|
|
335
|
+
[`NOTICE`](https://github.com/Sec-CH-Lemon/tls-forge/blob/main/NOTICE) and
|
|
336
|
+
[`THIRD-PARTY-NOTICES.txt`](https://github.com/Sec-CH-Lemon/tls-forge/blob/main/THIRD-PARTY-NOTICES.txt).
|