Skip to content

Commit f2257a7

Browse files
committed
feat(proxy): move to proxy module and support unix sockets
1 parent ea1d187 commit f2257a7

File tree

12 files changed

+286
-283
lines changed

12 files changed

+286
-283
lines changed

python/examples/proxy.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
import asyncio
22
import rnet
33
from rnet import Client, Proxy
4-
from rnet.emulation import Emulation
54

65

76
async def main():
87
# Create a client with multiple proxies
98
client = Client(
10-
proxies=[
11-
Proxy.http("socks5h://abc:[email protected]:6152"),
12-
Proxy.https(url="socks5h://127.0.0.1:6153", username="abc", password="def"),
13-
Proxy.http(
14-
url="http://abc:[email protected]:6152",
15-
custom_http_auth="abcedf",
16-
custom_http_headers={"User-Agent": "rnet", "x-custom-header": "value"},
17-
),
18-
Proxy.all(
19-
url="socks5h://abc:[email protected]:6153",
20-
exclusion="google.com, facebook.com, twitter.com",
21-
),
22-
],
9+
proxies=[Proxy.http("socks5h://abc:[email protected]:6152")],
2310
)
2411

2512
resp = await client.get("https://httpbin.io/anything")

python/rnet/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .tls import *
1313
from .dns import *
1414
from .redirect import *
15+
from .proxy import *
1516

1617
__all__ = (
1718
header.__all__
@@ -23,4 +24,5 @@
2324
+ tls.__all__
2425
+ dns.__all__
2526
+ redirect.__all__
27+
+ proxy.__all__
2628
) # type: ignore

python/rnet/__init__.pyi

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ from .dns import ResolverOptions
2121
from .http1 import Http1Options
2222
from .http2 import Http2Options
2323
from .redirect import History
24+
from .proxy import *
2425
from .cookie import *
2526
from .header import *
2627
from .emulation import *
@@ -159,77 +160,6 @@ class Part:
159160
"""
160161
...
161162

162-
class ProxyParams(TypedDict):
163-
username: NotRequired[str]
164-
r"""Username for proxy authentication."""
165-
166-
password: NotRequired[str]
167-
r"""Password for proxy authentication."""
168-
169-
custom_http_auth: NotRequired[str]
170-
r"""Custom HTTP proxy authentication header value."""
171-
172-
custom_http_headers: NotRequired[Dict[str, str] | HeaderMap]
173-
r"""Custom HTTP proxy headers."""
174-
175-
exclusion: NotRequired[str]
176-
r"""List of domains to exclude from proxying."""
177-
178-
@final
179-
class Proxy:
180-
r"""
181-
A proxy server for a request.
182-
Supports HTTP, HTTPS, SOCKS4, SOCKS4a, SOCKS5, and SOCKS5h protocols.
183-
"""
184-
185-
@staticmethod
186-
def http(url: str, **kwargs: Unpack[ProxyParams]) -> "Proxy":
187-
r"""
188-
Creates a new HTTP proxy.
189-
190-
This method sets up a proxy server for HTTP requests.
191-
192-
# Examples
193-
194-
```python
195-
import rnet
196-
197-
proxy = rnet.Proxy.http("http://proxy.example.com")
198-
```
199-
"""
200-
201-
@staticmethod
202-
def https(url: str, **kwargs: Unpack[ProxyParams]) -> "Proxy":
203-
r"""
204-
Creates a new HTTPS proxy.
205-
206-
This method sets up a proxy server for HTTPS requests.
207-
208-
# Examples
209-
210-
```python
211-
import rnet
212-
213-
proxy = rnet.Proxy.https("https://proxy.example.com")
214-
```
215-
"""
216-
217-
@staticmethod
218-
def all(url: str, **kwargs: Unpack[ProxyParams]) -> "Proxy":
219-
r"""
220-
Creates a new proxy for all protocols.
221-
222-
This method sets up a proxy server for all types of requests (HTTP, HTTPS, etc.).
223-
224-
# Examples
225-
226-
```python
227-
import rnet
228-
229-
proxy = rnet.Proxy.all("https://proxy.example.com")
230-
```
231-
"""
232-
233163
class Message:
234164
r"""
235165
A WebSocket message.
@@ -564,7 +494,7 @@ class WebSocket:
564494
def __aexit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> Any: ...
565495
def __str__(self) -> str: ...
566496

567-
class ClientParams(TypedDict):
497+
class ClientConfig(TypedDict):
568498
emulation: NotRequired[Emulation | EmulationOption]
569499
"""Emulation config."""
570500

@@ -1078,7 +1008,7 @@ class Client:
10781008

10791009
def __init__(
10801010
self,
1081-
**kwargs: Unpack[ClientParams],
1011+
**kwargs: Unpack[ClientConfig],
10821012
) -> None:
10831013
r"""
10841014
Creates a new Client instance.

python/rnet/blocking.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
from rnet import (
3-
ClientParams,
3+
ClientConfig,
44
History,
55
Message,
66
Request,
@@ -205,7 +205,7 @@ class Client:
205205

206206
def __init__(
207207
self,
208-
**kwargs: Unpack[ClientParams],
208+
**kwargs: Unpack[ClientConfig],
209209
) -> None:
210210
r"""
211211
Creates a new blocking Client instance.

python/rnet/proxy.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from typing import Dict, NotRequired, TypedDict, Unpack, final
2+
3+
from rnet.header import HeaderMap
4+
5+
__all__ = ["Proxy"]
6+
7+
8+
class ProxyConfig(TypedDict):
9+
username: NotRequired[str]
10+
r"""Username for proxy authentication."""
11+
12+
password: NotRequired[str]
13+
r"""Password for proxy authentication."""
14+
15+
custom_http_auth: NotRequired[str]
16+
r"""Custom HTTP proxy authentication header value."""
17+
18+
custom_http_headers: NotRequired[Dict[str, str] | HeaderMap]
19+
r"""Custom HTTP proxy headers."""
20+
21+
exclusion: NotRequired[str]
22+
r"""List of domains to exclude from proxying."""
23+
24+
25+
@final
26+
class Proxy:
27+
r"""
28+
A proxy server for a request.
29+
Supports HTTP, HTTPS, SOCKS4, SOCKS4a, SOCKS5, and SOCKS5h protocols.
30+
"""
31+
32+
@staticmethod
33+
def http(url: str, **kwargs: Unpack[ProxyConfig]) -> "Proxy":
34+
r"""
35+
Creates a new HTTP proxy.
36+
37+
This method sets up a proxy server for HTTP requests.
38+
39+
# Examples
40+
41+
```python
42+
import rnet
43+
44+
proxy = rnet.Proxy.http("http://proxy.example.com")
45+
```
46+
"""
47+
...
48+
49+
@staticmethod
50+
def https(url: str, **kwargs: Unpack[ProxyConfig]) -> "Proxy":
51+
r"""
52+
Creates a new HTTPS proxy.
53+
54+
This method sets up a proxy server for HTTPS requests.
55+
56+
# Examples
57+
58+
```python
59+
import rnet
60+
61+
proxy = rnet.Proxy.https("https://proxy.example.com")
62+
```
63+
"""
64+
...
65+
66+
@staticmethod
67+
def all(url: str, **kwargs: Unpack[ProxyConfig]) -> "Proxy":
68+
r"""
69+
Creates a new proxy for all protocols.
70+
71+
This method sets up a proxy server for all types of requests (HTTP, HTTPS, etc.).
72+
73+
# Examples
74+
75+
```python
76+
import rnet
77+
78+
proxy = rnet.Proxy.all("https://proxy.example.com")
79+
```
80+
"""
81+
...
82+
83+
@staticmethod
84+
def unix(path: str, **kwargs: Unpack[ProxyConfig]) -> "Proxy":
85+
r"""
86+
Creates a new UNIX socket proxy.
87+
88+
This method sets up a proxy server using a UNIX domain socket.
89+
90+
# Examples
91+
92+
```python
93+
import rnet
94+
95+
proxy = rnet.Proxy.unix("/var/run/docker.sock")
96+
```
97+
"""
98+
...

python/rnet/redirect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Callable, Optional, Sequence, final
1+
from typing import Callable, Sequence, final
22

33
from .header import HeaderMap
44
from . import StatusCode

0 commit comments

Comments
 (0)