pymasque 0.1.0__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.
- pymasque-0.1.0/LICENSE +165 -0
- pymasque-0.1.0/PKG-INFO +123 -0
- pymasque-0.1.0/README.md +102 -0
- pymasque-0.1.0/pyproject.toml +40 -0
- pymasque-0.1.0/setup.cfg +4 -0
- pymasque-0.1.0/src/pymasque/__init__.py +342 -0
- pymasque-0.1.0/src/pymasque/socks.py +68 -0
- pymasque-0.1.0/src/pymasque.egg-info/PKG-INFO +123 -0
- pymasque-0.1.0/src/pymasque.egg-info/SOURCES.txt +10 -0
- pymasque-0.1.0/src/pymasque.egg-info/dependency_links.txt +1 -0
- pymasque-0.1.0/src/pymasque.egg-info/requires.txt +7 -0
- pymasque-0.1.0/src/pymasque.egg-info/top_level.txt +1 -0
pymasque-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
pymasque-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pymasque
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A minimal implementation of a Cloudflare Warp MASQUE client in Python.
|
|
5
|
+
Author: ading2210
|
|
6
|
+
License-Expression: LGPL-3.0-or-later
|
|
7
|
+
Project-URL: Homepage, https://github.com/MercuryWorkshop/pymasque
|
|
8
|
+
Project-URL: Issues, https://github.com/MercuryWorkshop/pymasque/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: niquests
|
|
15
|
+
Requires-Dist: qh3
|
|
16
|
+
Requires-Dist: cryptography
|
|
17
|
+
Requires-Dist: platformdirs
|
|
18
|
+
Provides-Extra: socks
|
|
19
|
+
Requires-Dist: asyncio-socks-server; extra == "socks"
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# pymasque
|
|
23
|
+
|
|
24
|
+
pymasque is a minimal implementation of a Cloudflare Warp MASQUE client in Python, powered by [qh3](https://github.com/jawah/qh3).
|
|
25
|
+
|
|
26
|
+
It only implements the [L4 mode of the MASQUE proxy](https://gist.github.com/ading2210/d146549760cac2e1e1b70db3b5805584). This is faster and does not require a userspace IP stack, but only supports TCP.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Install it as a library:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
pip3 install pymasque
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Install it with a built-in SOCKS5 proxy:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
pip3 install pymasque[socks]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### SOCKS5 Proxy Mode
|
|
45
|
+
|
|
46
|
+
Use the SOCKS5 proxy mode to launch a SOCKS5 server that proxies all connections over Warp:
|
|
47
|
+
```
|
|
48
|
+
$ python3 -m pymasque.socks -h
|
|
49
|
+
usage: pymasque.socks [-h] [--host HOST] [--port PORT] [--auth AUTH] [--log-level {DEBUG,INFO,WARNING,ERROR}]
|
|
50
|
+
[--log-format {text,json}]
|
|
51
|
+
|
|
52
|
+
SOCKS5 proxy routing through Cloudflare WARP/MASQUE
|
|
53
|
+
|
|
54
|
+
options:
|
|
55
|
+
-h, --help show this help message and exit
|
|
56
|
+
--host HOST bind address
|
|
57
|
+
--port PORT bind port
|
|
58
|
+
--auth AUTH username:password
|
|
59
|
+
--log-level {DEBUG,INFO,WARNING,ERROR}
|
|
60
|
+
--log-format {text,json}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
On first run, a new Cloudflare Warp account will be created, the details of which will be cached to the disk.
|
|
64
|
+
|
|
65
|
+
### As a Library
|
|
66
|
+
|
|
67
|
+
pymasque is easy to integrate into your asyncio applications.
|
|
68
|
+
|
|
69
|
+
### Basic Example:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
import asyncio
|
|
73
|
+
import pymasque
|
|
74
|
+
|
|
75
|
+
async def main():
|
|
76
|
+
warp = pymasque.WarpConnection()
|
|
77
|
+
stream = await warp.open_stream("cloudflare.com", 80)
|
|
78
|
+
|
|
79
|
+
#send an http/1.1 payload
|
|
80
|
+
payload = "GET /cdn-cgi/trace HTTP/1.1\r\nHost: cloudflare.com\r\nConnection: keepalive\r\n\r\n"
|
|
81
|
+
await stream.send(payload.encode())
|
|
82
|
+
response = await stream.recv()
|
|
83
|
+
print(response.decode())
|
|
84
|
+
|
|
85
|
+
asyncio.run(main())
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`pymasque.WarpConnection` and `pymasque.WarpStream` contain all that you need for sending TCP traffic over Cloudflare workers.
|
|
89
|
+
|
|
90
|
+
#### Creating New MASQUE Connections:
|
|
91
|
+
|
|
92
|
+
Creating a new `pymasque.WarpConnection` object and calling `.connect()` on it will create a new MASQUE connection to Cloudflare Warp.
|
|
93
|
+
|
|
94
|
+
You don't need to explicitly call `.connect()`, it will automatically run when you create the first stream.
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
warp = pymasque.WarpConnection()
|
|
98
|
+
await warp.connect()
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Creating New Streams:
|
|
102
|
+
|
|
103
|
+
Streams can be created by calling `WarpConnection.open_stream(hostname, port)`. The hostname can be any domain name, IPv4 address, or IPv6 address. If a domain name is provided, it will be resolved locally using the system DNS resolver. This will return a `pymasque.WarpStream` object.
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
warp = pymasque.WarpConnection()
|
|
107
|
+
stream = await warp.open_stream("example.com", 1234)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Once you have your stream object, you may call `stream.recv()` or `stream.send(data)` to recieve or send data. The data sent must be a bytes object.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
await stream.send(b"hello world")
|
|
114
|
+
response = await stream.recv()
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
This project is licensed under the [GNU LGPL v3](https://www.gnu.org/licenses/lgpl-3.0.html).
|
|
120
|
+
|
|
121
|
+
> This license is mainly applied to libraries. You may copy, distribute and modify the software provided that modifications are described and licensed for free under LGPL. Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under LGPL, but applications that use the library don't have to be.
|
|
122
|
+
>
|
|
123
|
+
> \- From [tldrlegal.com](https://www.tldrlegal.com/license/gnu-lesser-general-public-license-v3-lgpl-3)
|
pymasque-0.1.0/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# pymasque
|
|
2
|
+
|
|
3
|
+
pymasque is a minimal implementation of a Cloudflare Warp MASQUE client in Python, powered by [qh3](https://github.com/jawah/qh3).
|
|
4
|
+
|
|
5
|
+
It only implements the [L4 mode of the MASQUE proxy](https://gist.github.com/ading2210/d146549760cac2e1e1b70db3b5805584). This is faster and does not require a userspace IP stack, but only supports TCP.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Install it as a library:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
pip3 install pymasque
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Install it with a built-in SOCKS5 proxy:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
pip3 install pymasque[socks]
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### SOCKS5 Proxy Mode
|
|
24
|
+
|
|
25
|
+
Use the SOCKS5 proxy mode to launch a SOCKS5 server that proxies all connections over Warp:
|
|
26
|
+
```
|
|
27
|
+
$ python3 -m pymasque.socks -h
|
|
28
|
+
usage: pymasque.socks [-h] [--host HOST] [--port PORT] [--auth AUTH] [--log-level {DEBUG,INFO,WARNING,ERROR}]
|
|
29
|
+
[--log-format {text,json}]
|
|
30
|
+
|
|
31
|
+
SOCKS5 proxy routing through Cloudflare WARP/MASQUE
|
|
32
|
+
|
|
33
|
+
options:
|
|
34
|
+
-h, --help show this help message and exit
|
|
35
|
+
--host HOST bind address
|
|
36
|
+
--port PORT bind port
|
|
37
|
+
--auth AUTH username:password
|
|
38
|
+
--log-level {DEBUG,INFO,WARNING,ERROR}
|
|
39
|
+
--log-format {text,json}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
On first run, a new Cloudflare Warp account will be created, the details of which will be cached to the disk.
|
|
43
|
+
|
|
44
|
+
### As a Library
|
|
45
|
+
|
|
46
|
+
pymasque is easy to integrate into your asyncio applications.
|
|
47
|
+
|
|
48
|
+
### Basic Example:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import asyncio
|
|
52
|
+
import pymasque
|
|
53
|
+
|
|
54
|
+
async def main():
|
|
55
|
+
warp = pymasque.WarpConnection()
|
|
56
|
+
stream = await warp.open_stream("cloudflare.com", 80)
|
|
57
|
+
|
|
58
|
+
#send an http/1.1 payload
|
|
59
|
+
payload = "GET /cdn-cgi/trace HTTP/1.1\r\nHost: cloudflare.com\r\nConnection: keepalive\r\n\r\n"
|
|
60
|
+
await stream.send(payload.encode())
|
|
61
|
+
response = await stream.recv()
|
|
62
|
+
print(response.decode())
|
|
63
|
+
|
|
64
|
+
asyncio.run(main())
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`pymasque.WarpConnection` and `pymasque.WarpStream` contain all that you need for sending TCP traffic over Cloudflare workers.
|
|
68
|
+
|
|
69
|
+
#### Creating New MASQUE Connections:
|
|
70
|
+
|
|
71
|
+
Creating a new `pymasque.WarpConnection` object and calling `.connect()` on it will create a new MASQUE connection to Cloudflare Warp.
|
|
72
|
+
|
|
73
|
+
You don't need to explicitly call `.connect()`, it will automatically run when you create the first stream.
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
warp = pymasque.WarpConnection()
|
|
77
|
+
await warp.connect()
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### Creating New Streams:
|
|
81
|
+
|
|
82
|
+
Streams can be created by calling `WarpConnection.open_stream(hostname, port)`. The hostname can be any domain name, IPv4 address, or IPv6 address. If a domain name is provided, it will be resolved locally using the system DNS resolver. This will return a `pymasque.WarpStream` object.
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
warp = pymasque.WarpConnection()
|
|
86
|
+
stream = await warp.open_stream("example.com", 1234)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Once you have your stream object, you may call `stream.recv()` or `stream.send(data)` to recieve or send data. The data sent must be a bytes object.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
await stream.send(b"hello world")
|
|
93
|
+
response = await stream.recv()
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
This project is licensed under the [GNU LGPL v3](https://www.gnu.org/licenses/lgpl-3.0.html).
|
|
99
|
+
|
|
100
|
+
> This license is mainly applied to libraries. You may copy, distribute and modify the software provided that modifications are described and licensed for free under LGPL. Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under LGPL, but applications that use the library don't have to be.
|
|
101
|
+
>
|
|
102
|
+
> \- From [tldrlegal.com](https://www.tldrlegal.com/license/gnu-lesser-general-public-license-v3-lgpl-3)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#run 'python3 -m build' to build the project
|
|
2
|
+
#then run 'twine upload dist/*' to publish
|
|
3
|
+
|
|
4
|
+
[build-system]
|
|
5
|
+
requires = ["setuptools>=84.0", "toml"]
|
|
6
|
+
build-backend = "setuptools.build_meta"
|
|
7
|
+
|
|
8
|
+
[tool.setuptools]
|
|
9
|
+
packages = ["pymasque"]
|
|
10
|
+
package-dir = {"" = "src"}
|
|
11
|
+
|
|
12
|
+
[project]
|
|
13
|
+
name = "pymasque"
|
|
14
|
+
version = "0.1.0"
|
|
15
|
+
license = "LGPL-3.0-or-later"
|
|
16
|
+
authors = [
|
|
17
|
+
{name="ading2210"},
|
|
18
|
+
]
|
|
19
|
+
description = "A minimal implementation of a Cloudflare Warp MASQUE client in Python."
|
|
20
|
+
readme = "README.md"
|
|
21
|
+
requires-python = ">=3.9"
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Operating System :: OS Independent",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"niquests",
|
|
28
|
+
"qh3",
|
|
29
|
+
"cryptography",
|
|
30
|
+
"platformdirs"
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
socks = [
|
|
35
|
+
"asyncio-socks-server"
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Homepage = "https://github.com/MercuryWorkshop/pymasque"
|
|
40
|
+
Issues = "https://github.com/MercuryWorkshop/pymasque/issues"
|
pymasque-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import base64
|
|
3
|
+
import logging
|
|
4
|
+
import re
|
|
5
|
+
import json
|
|
6
|
+
import ssl
|
|
7
|
+
import socket
|
|
8
|
+
import asyncio
|
|
9
|
+
import ipaddress
|
|
10
|
+
from datetime import datetime, timezone, timedelta
|
|
11
|
+
|
|
12
|
+
import platformdirs
|
|
13
|
+
import niquests
|
|
14
|
+
import qh3
|
|
15
|
+
from cryptography import x509
|
|
16
|
+
from cryptography.hazmat.primitives import hashes, serialization
|
|
17
|
+
from cryptography.hazmat.primitives.asymmetric import ec
|
|
18
|
+
from cryptography.hazmat.primitives.serialization import load_der_private_key, load_pem_public_key
|
|
19
|
+
|
|
20
|
+
#a client for the cloudflare warp l4 proxy service
|
|
21
|
+
#based on https://github.com/Diniboy1123/usque
|
|
22
|
+
|
|
23
|
+
cf_api = "https://api.cloudflareclient.com/v0a4471"
|
|
24
|
+
cf_headers = {
|
|
25
|
+
"User-Agent": "WARP for Android",
|
|
26
|
+
"CF-Client-Version": "a-6.35-4471"
|
|
27
|
+
}
|
|
28
|
+
cf_sni = "consumer-masque-proxy.cloudflareclient.com"
|
|
29
|
+
|
|
30
|
+
dirs = platformdirs.PlatformDirs("pymasque")
|
|
31
|
+
data_dir = dirs.user_data_path
|
|
32
|
+
registration_file = data_dir / "warp_config.json"
|
|
33
|
+
|
|
34
|
+
async def register():
|
|
35
|
+
reg_key = base64.b64encode(os.urandom(32)).decode()
|
|
36
|
+
tos_date = datetime.now().astimezone().isoformat(timespec="milliseconds")
|
|
37
|
+
|
|
38
|
+
reg_response = await niquests.apost(cf_api + "/reg", json={
|
|
39
|
+
"key": reg_key,
|
|
40
|
+
"install_id": "",
|
|
41
|
+
"fcm_token": "",
|
|
42
|
+
"tos": tos_date,
|
|
43
|
+
"model": "PC",
|
|
44
|
+
"serial_number": os.urandom(4).hex(),
|
|
45
|
+
"os_version": "",
|
|
46
|
+
"key_type": "curve25519",
|
|
47
|
+
"tunnel_type": "wireguard",
|
|
48
|
+
"locale": "en_US"
|
|
49
|
+
}, headers=cf_headers)
|
|
50
|
+
reg_response.raise_for_status()
|
|
51
|
+
registration = reg_response.json()
|
|
52
|
+
|
|
53
|
+
device_id = registration["id"]
|
|
54
|
+
token = registration["token"]
|
|
55
|
+
private_key = ec.generate_private_key(ec.SECP256R1())
|
|
56
|
+
private_key_der = private_key.private_bytes(serialization.Encoding.DER, serialization.PrivateFormat.TraditionalOpenSSL, serialization.NoEncryption())
|
|
57
|
+
private_key_str = base64.b64encode(private_key_der).decode()
|
|
58
|
+
public_key_der = private_key.public_key().public_bytes(serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo)
|
|
59
|
+
public_key_str = base64.b64encode(public_key_der).decode()
|
|
60
|
+
|
|
61
|
+
enroll_response = await niquests.apatch(cf_api + f"/reg/{device_id}", json={
|
|
62
|
+
"key": public_key_str,
|
|
63
|
+
"key_type": "secp256r1",
|
|
64
|
+
"tunnel_type": "masque",
|
|
65
|
+
"name": ""
|
|
66
|
+
}, headers={**cf_headers, "Authorization": f"Bearer {token}"})
|
|
67
|
+
enroll_response.raise_for_status()
|
|
68
|
+
enrollment = enroll_response.json()
|
|
69
|
+
|
|
70
|
+
peer = enrollment["config"]["peers"][0]
|
|
71
|
+
endpoint_ipv4 = peer["endpoint"]["v4"].split(":")[0]
|
|
72
|
+
endpoint_ipv6 = re.findall(r"\[(.+)\]:0", peer["endpoint"]["v6"])[0]
|
|
73
|
+
|
|
74
|
+
warp_config = {
|
|
75
|
+
"id": device_id,
|
|
76
|
+
"token": token,
|
|
77
|
+
"private_key": private_key_str,
|
|
78
|
+
"endpoint_ipv4": endpoint_ipv4,
|
|
79
|
+
"endpoint_ipv6": endpoint_ipv6,
|
|
80
|
+
"endpoint_public_key": peer["public_key"]
|
|
81
|
+
}
|
|
82
|
+
return warp_config
|
|
83
|
+
|
|
84
|
+
async def read_registration():
|
|
85
|
+
if registration_file.exists():
|
|
86
|
+
return json.loads(registration_file.read_text())
|
|
87
|
+
else:
|
|
88
|
+
logging.info("creating new cf warp registration")
|
|
89
|
+
registration = await register()
|
|
90
|
+
data_dir.mkdir(exist_ok=True, parents=True)
|
|
91
|
+
registration_file.write_text(json.dumps(registration, indent=2))
|
|
92
|
+
logging.info(f"saved cf warp registration data to {registration_file}")
|
|
93
|
+
return registration
|
|
94
|
+
|
|
95
|
+
async def resolve_ip(hostname):
|
|
96
|
+
try:
|
|
97
|
+
infos = await asyncio.get_running_loop().getaddrinfo(hostname, None, type=socket.SOCK_STREAM)
|
|
98
|
+
except socket.gaierror as e:
|
|
99
|
+
raise ConnectionError(f"DNS resolution failed for {hostname}: {e}") from e
|
|
100
|
+
return str(ipaddress.ip_address(infos[0][4][0]))
|
|
101
|
+
|
|
102
|
+
class WarpQuicProtocol(qh3.asyncio.QuicConnectionProtocol):
|
|
103
|
+
def __init__(self, *args, **kwargs):
|
|
104
|
+
super().__init__(*args, **kwargs)
|
|
105
|
+
self.h3 = qh3.H3Connection(self._quic)
|
|
106
|
+
self.streams = {}
|
|
107
|
+
self.send_waiters = set()
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def closed(self):
|
|
111
|
+
return self._closed.is_set()
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def peercert(self):
|
|
115
|
+
return self._quic.get_peercert()
|
|
116
|
+
|
|
117
|
+
def open_h3_stream_id(self):
|
|
118
|
+
return self._quic.get_next_available_stream_id(is_unidirectional=False)
|
|
119
|
+
|
|
120
|
+
def reset_h3_stream(self, stream_id):
|
|
121
|
+
try:
|
|
122
|
+
self._quic.reset_stream(stream_id, 0)
|
|
123
|
+
self.transmit()
|
|
124
|
+
except Exception:
|
|
125
|
+
pass
|
|
126
|
+
|
|
127
|
+
def notify_send_waiters(self):
|
|
128
|
+
for fut in list(self.send_waiters):
|
|
129
|
+
if not fut.done():
|
|
130
|
+
fut.set_result(None)
|
|
131
|
+
|
|
132
|
+
def transmit(self):
|
|
133
|
+
try:
|
|
134
|
+
super().transmit()
|
|
135
|
+
finally:
|
|
136
|
+
self.notify_send_waiters()
|
|
137
|
+
|
|
138
|
+
def quic_event_received(self, event):
|
|
139
|
+
if getattr(event, "stream_id", None) is None:
|
|
140
|
+
super().quic_event_received(event)
|
|
141
|
+
for h3_event in self.h3.handle_event(event):
|
|
142
|
+
if stream := self.streams.get(h3_event.stream_id):
|
|
143
|
+
stream.on_h3_event(h3_event)
|
|
144
|
+
|
|
145
|
+
class WarpStream:
|
|
146
|
+
CONNECTING, OPEN, CLOSED = range(3)
|
|
147
|
+
|
|
148
|
+
def __init__(self, proto, stream_id, timeout=15, max_buffered=128*1024):
|
|
149
|
+
self.proto = proto
|
|
150
|
+
self.id = stream_id
|
|
151
|
+
self.timeout = timeout
|
|
152
|
+
self.max_buffered = max_buffered
|
|
153
|
+
self.headers = asyncio.get_running_loop().create_future()
|
|
154
|
+
self.incoming = asyncio.Queue()
|
|
155
|
+
self.state = WarpStream.CONNECTING
|
|
156
|
+
self.proto.streams[stream_id] = self
|
|
157
|
+
|
|
158
|
+
def on_h3_event(self, hev):
|
|
159
|
+
if isinstance(hev, qh3.h3_events.HeadersReceived):
|
|
160
|
+
if not self.headers.done():
|
|
161
|
+
self.headers.set_result((hev.headers, hev.stream_ended))
|
|
162
|
+
if hev.stream_ended:
|
|
163
|
+
self.incoming.put_nowait((b"", True))
|
|
164
|
+
elif isinstance(hev, qh3.h3_events.DataReceived):
|
|
165
|
+
self.incoming.put_nowait((hev.data, hev.stream_ended))
|
|
166
|
+
elif isinstance(hev, (qh3.h3_events.StreamReset, qh3.h3_events.StopSending)):
|
|
167
|
+
self.incoming.put_nowait((None, True))
|
|
168
|
+
if not self.headers.done():
|
|
169
|
+
self.headers.set_exception(ConnectionResetError(f"stream {self.id} reset"))
|
|
170
|
+
self.proto.notify_send_waiters()
|
|
171
|
+
|
|
172
|
+
async def ensure_response(self):
|
|
173
|
+
if self.state is WarpStream.OPEN:
|
|
174
|
+
return
|
|
175
|
+
try:
|
|
176
|
+
async with asyncio.timeout(self.timeout):
|
|
177
|
+
headers = (await self.headers)[0]
|
|
178
|
+
except TimeoutError:
|
|
179
|
+
raise ConnectionError("CONNECT timed out")
|
|
180
|
+
status = dict(headers).get(b":status", b"").decode()
|
|
181
|
+
if not status.startswith("2"):
|
|
182
|
+
raise ConnectionError(f"CONNECT rejected with status {status}")
|
|
183
|
+
self.state = WarpStream.OPEN
|
|
184
|
+
|
|
185
|
+
async def recv(self):
|
|
186
|
+
if self.state is WarpStream.CLOSED:
|
|
187
|
+
return None
|
|
188
|
+
try:
|
|
189
|
+
await self.ensure_response()
|
|
190
|
+
except ConnectionError:
|
|
191
|
+
self.state = WarpStream.CLOSED
|
|
192
|
+
return None
|
|
193
|
+
data, ended = await self.incoming.get()
|
|
194
|
+
if data is None or ended:
|
|
195
|
+
self.state = WarpStream.CLOSED
|
|
196
|
+
return None
|
|
197
|
+
return data
|
|
198
|
+
|
|
199
|
+
async def send(self, data, max_buffered=None):
|
|
200
|
+
if not data:
|
|
201
|
+
return
|
|
202
|
+
if self.state is WarpStream.CLOSED:
|
|
203
|
+
raise ConnectionResetError(f"stream {self.id} closed")
|
|
204
|
+
self.write(data)
|
|
205
|
+
await self.drain(max_buffered=max_buffered)
|
|
206
|
+
|
|
207
|
+
def write(self, data):
|
|
208
|
+
if not data or self.state is WarpStream.CLOSED:
|
|
209
|
+
return
|
|
210
|
+
try:
|
|
211
|
+
self.proto.h3.send_data(self.id, data, False)
|
|
212
|
+
self.proto.transmit()
|
|
213
|
+
except (ValueError, RuntimeError) as e:
|
|
214
|
+
raise ConnectionResetError(f"stream {self.id} closed") from e
|
|
215
|
+
|
|
216
|
+
async def drain(self, max_buffered=None):
|
|
217
|
+
if self.state is WarpStream.CLOSED:
|
|
218
|
+
return
|
|
219
|
+
limit = self.max_buffered if max_buffered is None else max_buffered
|
|
220
|
+
while limit is not None and self.proto._quic.get_send_buffer_size(self.id) > limit:
|
|
221
|
+
if self.state is WarpStream.CLOSED:
|
|
222
|
+
return
|
|
223
|
+
if self.proto.closed:
|
|
224
|
+
raise ConnectionError(f"stream {self.id} connection closed")
|
|
225
|
+
fut = asyncio.get_running_loop().create_future()
|
|
226
|
+
self.proto.send_waiters.add(fut)
|
|
227
|
+
try:
|
|
228
|
+
await fut
|
|
229
|
+
finally:
|
|
230
|
+
self.proto.send_waiters.discard(fut)
|
|
231
|
+
|
|
232
|
+
def close(self):
|
|
233
|
+
self.state = WarpStream.CLOSED
|
|
234
|
+
try:
|
|
235
|
+
self.incoming.put_nowait((None, True))
|
|
236
|
+
except Exception:
|
|
237
|
+
pass
|
|
238
|
+
self.proto.reset_h3_stream(self.id)
|
|
239
|
+
self.proto.streams.pop(self.id, None)
|
|
240
|
+
self.proto.notify_send_waiters()
|
|
241
|
+
if not self.headers.done():
|
|
242
|
+
self.headers.cancel()
|
|
243
|
+
|
|
244
|
+
async def read(self, n=-1):
|
|
245
|
+
return await self.recv() or b""
|
|
246
|
+
|
|
247
|
+
async def wait_closed(self):
|
|
248
|
+
return None
|
|
249
|
+
|
|
250
|
+
class WarpConnection:
|
|
251
|
+
OPEN, CLOSING = range(2)
|
|
252
|
+
|
|
253
|
+
def __init__(self):
|
|
254
|
+
self.quic_context = None
|
|
255
|
+
self.quic_protocol = None
|
|
256
|
+
self.state = WarpConnection.OPEN
|
|
257
|
+
|
|
258
|
+
async def connect(self):
|
|
259
|
+
if self.quic_protocol is not None and not self.quic_protocol.closed:
|
|
260
|
+
return self.quic_protocol
|
|
261
|
+
registration = await read_registration()
|
|
262
|
+
|
|
263
|
+
private_key_der = base64.b64decode(registration["private_key"])
|
|
264
|
+
private_key = load_der_private_key(private_key_der, password=None)
|
|
265
|
+
pkcs8_der = private_key.private_bytes(serialization.Encoding.DER, serialization.PrivateFormat.PKCS8, serialization.NoEncryption())
|
|
266
|
+
quic_private_key = qh3._hazmat.EcPrivateKey(pkcs8_der, 256, True)
|
|
267
|
+
|
|
268
|
+
now = datetime.now(timezone.utc)
|
|
269
|
+
cert_der = (
|
|
270
|
+
x509.CertificateBuilder()
|
|
271
|
+
.serial_number(1)
|
|
272
|
+
.issuer_name(x509.Name([]))
|
|
273
|
+
.subject_name(x509.Name([]))
|
|
274
|
+
.public_key(private_key.public_key())
|
|
275
|
+
.not_valid_before(now)
|
|
276
|
+
.not_valid_after(now + timedelta(hours=24))
|
|
277
|
+
.sign(private_key, hashes.SHA256())
|
|
278
|
+
.public_bytes(serialization.Encoding.DER)
|
|
279
|
+
)
|
|
280
|
+
quic_cert = qh3._hazmat.Certificate(cert_der)
|
|
281
|
+
|
|
282
|
+
quic_config = qh3.QuicConfiguration(
|
|
283
|
+
is_client=True, alpn_protocols=["h3"], server_name=cf_sni,
|
|
284
|
+
certificate=quic_cert, private_key=quic_private_key,
|
|
285
|
+
verify_mode=ssl.CERT_NONE, verify_hostname=False,
|
|
286
|
+
offer_ec_key_shares=True,
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
logging.info("connecting to cloudflare warp server")
|
|
290
|
+
self.quic_context = qh3.asyncio.connect(registration["endpoint_ipv4"], 443, configuration=quic_config, create_protocol=WarpQuicProtocol)
|
|
291
|
+
self.quic_protocol = await self.quic_context.__aenter__()
|
|
292
|
+
|
|
293
|
+
peer_public_key = self.quic_protocol.peercert.public_key()
|
|
294
|
+
edge_key = load_pem_public_key(registration["endpoint_public_key"].encode())
|
|
295
|
+
edge_public_key = edge_key.public_bytes(serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint)
|
|
296
|
+
if peer_public_key != edge_public_key:
|
|
297
|
+
await self.close()
|
|
298
|
+
raise ssl.SSLCertVerificationError("edge public key does not match")
|
|
299
|
+
asyncio.create_task(self.keepalive())
|
|
300
|
+
return self.quic_protocol
|
|
301
|
+
|
|
302
|
+
async def close(self):
|
|
303
|
+
if self.quic_context is not None:
|
|
304
|
+
await self.quic_context.__aexit__(None, None, None)
|
|
305
|
+
self.quic_context = None
|
|
306
|
+
self.quic_protocol = None
|
|
307
|
+
|
|
308
|
+
def close_all(self):
|
|
309
|
+
self.state = WarpConnection.CLOSING
|
|
310
|
+
proto = self.quic_protocol
|
|
311
|
+
if proto is None:
|
|
312
|
+
return
|
|
313
|
+
for stream in list(proto.streams.values()):
|
|
314
|
+
try:
|
|
315
|
+
stream.close()
|
|
316
|
+
except Exception:
|
|
317
|
+
pass
|
|
318
|
+
|
|
319
|
+
async def open_stream(self, hostname, port, timeout=15, max_buffered=128*1024):
|
|
320
|
+
if self.state is WarpConnection.CLOSING:
|
|
321
|
+
raise ConnectionError("server shutting down")
|
|
322
|
+
proto = await self.connect()
|
|
323
|
+
try:
|
|
324
|
+
ip = str(ipaddress.ip_address(hostname))
|
|
325
|
+
except ValueError:
|
|
326
|
+
ip = await resolve_ip(hostname)
|
|
327
|
+
authority = f"[{ip}]:{port}" if ":" in ip else f"{ip}:{port}"
|
|
328
|
+
stream = WarpStream(proto, proto.open_h3_stream_id(), timeout, max_buffered)
|
|
329
|
+
proto.h3.send_headers(stream.id, [
|
|
330
|
+
(b":method", b"CONNECT"),
|
|
331
|
+
(b":authority", authority.encode()),
|
|
332
|
+
], end_stream=False)
|
|
333
|
+
proto.transmit()
|
|
334
|
+
return stream
|
|
335
|
+
|
|
336
|
+
async def keepalive(self):
|
|
337
|
+
while self.quic_protocol:
|
|
338
|
+
await asyncio.sleep(15)
|
|
339
|
+
try:
|
|
340
|
+
await self.quic_protocol.ping()
|
|
341
|
+
except Exception:
|
|
342
|
+
continue
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
from pymasque import WarpConnection
|
|
2
|
+
|
|
3
|
+
try:
|
|
4
|
+
from asyncio_socks_server import Addon, Address, Connection, Server
|
|
5
|
+
except ImportError as e:
|
|
6
|
+
raise ImportError("asyncio-socks-server is required for pymasque.socks. Run \"pip install 'pymasque[socks]'\" to install it.")
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
import asyncio, uvloop
|
|
10
|
+
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
|
11
|
+
except ImportError as e:
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
class WarpAddon(Addon):
|
|
15
|
+
def __init__(self):
|
|
16
|
+
self.warp = WarpConnection()
|
|
17
|
+
|
|
18
|
+
async def on_connect(self, flow):
|
|
19
|
+
stream = await self.warp.open_stream(flow.dst.host, flow.dst.port)
|
|
20
|
+
await stream.ensure_response()
|
|
21
|
+
return Connection(
|
|
22
|
+
reader=stream,
|
|
23
|
+
writer=stream,
|
|
24
|
+
address=Address(flow.dst.host, flow.dst.port),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
async def on_stop(self):
|
|
28
|
+
await self.warp.close()
|
|
29
|
+
|
|
30
|
+
class WarpServer(Server):
|
|
31
|
+
def __init__(self, **kwargs):
|
|
32
|
+
addon = WarpAddon()
|
|
33
|
+
super().__init__(addons=[addon], **kwargs)
|
|
34
|
+
self.addon = addon
|
|
35
|
+
|
|
36
|
+
def request_shutdown(self):
|
|
37
|
+
try:
|
|
38
|
+
self.addon.warp.close_all()
|
|
39
|
+
except Exception:
|
|
40
|
+
pass
|
|
41
|
+
super().request_shutdown()
|
|
42
|
+
|
|
43
|
+
def serve(**kwargs):
|
|
44
|
+
WarpServer(**kwargs).run()
|
|
45
|
+
|
|
46
|
+
def main(argv=None):
|
|
47
|
+
import argparse
|
|
48
|
+
|
|
49
|
+
parser = argparse.ArgumentParser(
|
|
50
|
+
prog="pymasque.socks",
|
|
51
|
+
description="SOCKS5 proxy routing through Cloudflare WARP/MASQUE",
|
|
52
|
+
)
|
|
53
|
+
parser.add_argument("--host", default="127.0.0.1", help="bind address")
|
|
54
|
+
parser.add_argument("--port", type=int, default=1080, help="bind port")
|
|
55
|
+
parser.add_argument("--auth", default=None, help="username:password")
|
|
56
|
+
parser.add_argument("--log-level", default="INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR"])
|
|
57
|
+
parser.add_argument("--log-format", default="text", choices=["text", "json"])
|
|
58
|
+
args = parser.parse_args(argv)
|
|
59
|
+
auth = None
|
|
60
|
+
if args.auth:
|
|
61
|
+
user, _, passwd = args.auth.partition(":")
|
|
62
|
+
auth = (user, passwd)
|
|
63
|
+
serve(host=args.host, port=args.port, auth=auth,
|
|
64
|
+
log_level=args.log_level, log_format=args.log_format)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
main()
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pymasque
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A minimal implementation of a Cloudflare Warp MASQUE client in Python.
|
|
5
|
+
Author: ading2210
|
|
6
|
+
License-Expression: LGPL-3.0-or-later
|
|
7
|
+
Project-URL: Homepage, https://github.com/MercuryWorkshop/pymasque
|
|
8
|
+
Project-URL: Issues, https://github.com/MercuryWorkshop/pymasque/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: niquests
|
|
15
|
+
Requires-Dist: qh3
|
|
16
|
+
Requires-Dist: cryptography
|
|
17
|
+
Requires-Dist: platformdirs
|
|
18
|
+
Provides-Extra: socks
|
|
19
|
+
Requires-Dist: asyncio-socks-server; extra == "socks"
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# pymasque
|
|
23
|
+
|
|
24
|
+
pymasque is a minimal implementation of a Cloudflare Warp MASQUE client in Python, powered by [qh3](https://github.com/jawah/qh3).
|
|
25
|
+
|
|
26
|
+
It only implements the [L4 mode of the MASQUE proxy](https://gist.github.com/ading2210/d146549760cac2e1e1b70db3b5805584). This is faster and does not require a userspace IP stack, but only supports TCP.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Install it as a library:
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
pip3 install pymasque
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Install it with a built-in SOCKS5 proxy:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
pip3 install pymasque[socks]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### SOCKS5 Proxy Mode
|
|
45
|
+
|
|
46
|
+
Use the SOCKS5 proxy mode to launch a SOCKS5 server that proxies all connections over Warp:
|
|
47
|
+
```
|
|
48
|
+
$ python3 -m pymasque.socks -h
|
|
49
|
+
usage: pymasque.socks [-h] [--host HOST] [--port PORT] [--auth AUTH] [--log-level {DEBUG,INFO,WARNING,ERROR}]
|
|
50
|
+
[--log-format {text,json}]
|
|
51
|
+
|
|
52
|
+
SOCKS5 proxy routing through Cloudflare WARP/MASQUE
|
|
53
|
+
|
|
54
|
+
options:
|
|
55
|
+
-h, --help show this help message and exit
|
|
56
|
+
--host HOST bind address
|
|
57
|
+
--port PORT bind port
|
|
58
|
+
--auth AUTH username:password
|
|
59
|
+
--log-level {DEBUG,INFO,WARNING,ERROR}
|
|
60
|
+
--log-format {text,json}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
On first run, a new Cloudflare Warp account will be created, the details of which will be cached to the disk.
|
|
64
|
+
|
|
65
|
+
### As a Library
|
|
66
|
+
|
|
67
|
+
pymasque is easy to integrate into your asyncio applications.
|
|
68
|
+
|
|
69
|
+
### Basic Example:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
import asyncio
|
|
73
|
+
import pymasque
|
|
74
|
+
|
|
75
|
+
async def main():
|
|
76
|
+
warp = pymasque.WarpConnection()
|
|
77
|
+
stream = await warp.open_stream("cloudflare.com", 80)
|
|
78
|
+
|
|
79
|
+
#send an http/1.1 payload
|
|
80
|
+
payload = "GET /cdn-cgi/trace HTTP/1.1\r\nHost: cloudflare.com\r\nConnection: keepalive\r\n\r\n"
|
|
81
|
+
await stream.send(payload.encode())
|
|
82
|
+
response = await stream.recv()
|
|
83
|
+
print(response.decode())
|
|
84
|
+
|
|
85
|
+
asyncio.run(main())
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`pymasque.WarpConnection` and `pymasque.WarpStream` contain all that you need for sending TCP traffic over Cloudflare workers.
|
|
89
|
+
|
|
90
|
+
#### Creating New MASQUE Connections:
|
|
91
|
+
|
|
92
|
+
Creating a new `pymasque.WarpConnection` object and calling `.connect()` on it will create a new MASQUE connection to Cloudflare Warp.
|
|
93
|
+
|
|
94
|
+
You don't need to explicitly call `.connect()`, it will automatically run when you create the first stream.
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
warp = pymasque.WarpConnection()
|
|
98
|
+
await warp.connect()
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Creating New Streams:
|
|
102
|
+
|
|
103
|
+
Streams can be created by calling `WarpConnection.open_stream(hostname, port)`. The hostname can be any domain name, IPv4 address, or IPv6 address. If a domain name is provided, it will be resolved locally using the system DNS resolver. This will return a `pymasque.WarpStream` object.
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
warp = pymasque.WarpConnection()
|
|
107
|
+
stream = await warp.open_stream("example.com", 1234)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Once you have your stream object, you may call `stream.recv()` or `stream.send(data)` to recieve or send data. The data sent must be a bytes object.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
await stream.send(b"hello world")
|
|
114
|
+
response = await stream.recv()
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
This project is licensed under the [GNU LGPL v3](https://www.gnu.org/licenses/lgpl-3.0.html).
|
|
120
|
+
|
|
121
|
+
> This license is mainly applied to libraries. You may copy, distribute and modify the software provided that modifications are described and licensed for free under LGPL. Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under LGPL, but applications that use the library don't have to be.
|
|
122
|
+
>
|
|
123
|
+
> \- From [tldrlegal.com](https://www.tldrlegal.com/license/gnu-lesser-general-public-license-v3-lgpl-3)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/pymasque/__init__.py
|
|
5
|
+
src/pymasque/socks.py
|
|
6
|
+
src/pymasque.egg-info/PKG-INFO
|
|
7
|
+
src/pymasque.egg-info/SOURCES.txt
|
|
8
|
+
src/pymasque.egg-info/dependency_links.txt
|
|
9
|
+
src/pymasque.egg-info/requires.txt
|
|
10
|
+
src/pymasque.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pymasque
|