egress-proxy 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.
- egress_proxy-0.1.0/LICENSE +202 -0
- egress_proxy-0.1.0/NOTICE +5 -0
- egress_proxy-0.1.0/PKG-INFO +312 -0
- egress_proxy-0.1.0/README.md +265 -0
- egress_proxy-0.1.0/pyproject.toml +147 -0
- egress_proxy-0.1.0/pyproject.toml.orig +128 -0
- egress_proxy-0.1.0/src/egress_proxy/__init__.py +10 -0
- egress_proxy-0.1.0/src/egress_proxy/client/__init__.py +0 -0
- egress_proxy-0.1.0/src/egress_proxy/client/aiohttp/__init__.py +8 -0
- egress_proxy-0.1.0/src/egress_proxy/client/aiohttp/factory.py +17 -0
- egress_proxy-0.1.0/src/egress_proxy/client/aiohttp/middleware.py +47 -0
- egress_proxy-0.1.0/src/egress_proxy/client/httpx/__init__.py +9 -0
- egress_proxy-0.1.0/src/egress_proxy/client/httpx/client.py +31 -0
- egress_proxy-0.1.0/src/egress_proxy/client/httpx/route.py +46 -0
- egress_proxy-0.1.0/src/egress_proxy/client/httpx/transport.py +47 -0
- egress_proxy-0.1.0/src/egress_proxy/extras.py +13 -0
- egress_proxy-0.1.0/src/egress_proxy/protocol.py +14 -0
- egress_proxy-0.1.0/src/egress_proxy/py.typed +0 -0
- egress_proxy-0.1.0/src/egress_proxy/server/__init__.py +3 -0
- egress_proxy-0.1.0/src/egress_proxy/server/addresses.py +55 -0
- egress_proxy-0.1.0/src/egress_proxy/server/application.py +53 -0
- egress_proxy-0.1.0/src/egress_proxy/server/asgi.py +7 -0
- egress_proxy-0.1.0/src/egress_proxy/server/forward.py +159 -0
- egress_proxy-0.1.0/src/egress_proxy/server/headers.py +133 -0
- egress_proxy-0.1.0/src/egress_proxy/server/main.py +86 -0
- egress_proxy-0.1.0/src/egress_proxy/server/rejection.py +28 -0
- egress_proxy-0.1.0/src/egress_proxy/server/routes.py +114 -0
- egress_proxy-0.1.0/src/egress_proxy/server/settings.py +33 -0
- egress_proxy-0.1.0/src/egress_proxy/server/upstreams.py +71 -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 [yyyy] [name of copyright owner]
|
|
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,312 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: egress-proxy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Self-hosted egress proxy with drop-in integrations for Python HTTP clients and upstream HTTP/SOCKS rotation
|
|
5
|
+
Keywords: httpx,aiohttp,proxy,egress,socks5,http-proxy,proxy-rotation
|
|
6
|
+
Author: botifyex
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
License-File: NOTICE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Framework :: AsyncIO
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Topic :: Internet :: Proxy Servers
|
|
20
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Dist: egress-proxy[client,server] ; extra == 'all'
|
|
24
|
+
Requires-Dist: egress-proxy[client-httpx,client-aiohttp] ; extra == 'client'
|
|
25
|
+
Requires-Dist: aiohttp>=3.13.0 ; extra == 'client-aiohttp'
|
|
26
|
+
Requires-Dist: yarl>=1.21.0 ; extra == 'client-aiohttp'
|
|
27
|
+
Requires-Dist: httpx>=0.25.1,<1 ; extra == 'client-httpx'
|
|
28
|
+
Requires-Dist: sniffio>=1.3.0 ; extra == 'client-httpx'
|
|
29
|
+
Requires-Dist: aiohttp>=3.13.0 ; extra == 'server'
|
|
30
|
+
Requires-Dist: aiohttp-socks>=0.11.0 ; extra == 'server'
|
|
31
|
+
Requires-Dist: certifi>=2024.7.4 ; extra == 'server'
|
|
32
|
+
Requires-Dist: httptools>=0.7.1 ; extra == 'server'
|
|
33
|
+
Requires-Dist: pydantic-settings>=2.0.0 ; extra == 'server'
|
|
34
|
+
Requires-Dist: uvicorn>=0.24.0 ; extra == 'server'
|
|
35
|
+
Requires-Dist: yarl>=1.21.0 ; extra == 'server'
|
|
36
|
+
Requires-Python: >=3.11
|
|
37
|
+
Project-URL: Homepage, https://github.com/botifyex/egress-proxy
|
|
38
|
+
Project-URL: Repository, https://github.com/botifyex/egress-proxy
|
|
39
|
+
Project-URL: Issues, https://github.com/botifyex/egress-proxy/issues
|
|
40
|
+
Project-URL: Changelog, https://github.com/botifyex/egress-proxy/blob/master/CHANGELOG.md
|
|
41
|
+
Provides-Extra: all
|
|
42
|
+
Provides-Extra: client
|
|
43
|
+
Provides-Extra: client-aiohttp
|
|
44
|
+
Provides-Extra: client-httpx
|
|
45
|
+
Provides-Extra: server
|
|
46
|
+
Description-Content-Type: text/markdown
|
|
47
|
+
|
|
48
|
+
# egress-proxy
|
|
49
|
+
|
|
50
|
+
[](https://pypi.org/project/egress-proxy/)
|
|
51
|
+
[](https://pypi.org/project/egress-proxy/)
|
|
52
|
+
[](https://github.com/botifyex/egress-proxy/actions/workflows/ci.yml)
|
|
53
|
+
[](https://github.com/botifyex/egress-proxy/blob/master/LICENSE)
|
|
54
|
+
|
|
55
|
+
Send the HTTP requests of your Python project through your own server with a drop-in integration
|
|
56
|
+
for its HTTP client: they leave from the server's IP, or from upstream HTTP and SOCKS proxies it
|
|
57
|
+
rotates through, with identifying headers removed.
|
|
58
|
+
|
|
59
|
+
## Quick start
|
|
60
|
+
|
|
61
|
+
**1. Start the server** on the machine whose IP the requests should come from:
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
pip install "egress-proxy[server]"
|
|
65
|
+
python -c "import secrets; print('PROXY__KEY=' + secrets.token_urlsafe(32))" > .env
|
|
66
|
+
egress-proxy --host 0.0.0.0 --port 8000
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The generated key is in `.env`; the client needs it.
|
|
70
|
+
|
|
71
|
+
**2. Use it in your project** with the HTTP client you already have:
|
|
72
|
+
|
|
73
|
+
```sh
|
|
74
|
+
pip install "egress-proxy[client-httpx]" # httpx
|
|
75
|
+
pip install "egress-proxy[client-aiohttp]" # aiohttp
|
|
76
|
+
pip install "egress-proxy[client]" # both
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
import asyncio
|
|
81
|
+
|
|
82
|
+
from egress_proxy.client.httpx import AsyncClient
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
async def main() -> None:
|
|
86
|
+
async with AsyncClient(egress_url="http://SERVER_IP:8000", egress_key="PROXY_KEY") as client:
|
|
87
|
+
response = await client.get("https://api.ipify.org?format=json")
|
|
88
|
+
print(response.json())
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
asyncio.run(main())
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
It prints the server's IP. The `egress_*` parameters configure the proxy; everything else goes to
|
|
95
|
+
the HTTP client unchanged.
|
|
96
|
+
|
|
97
|
+
## httpx
|
|
98
|
+
|
|
99
|
+
**Replace the client class.** `Client` and `AsyncClient` are `httpx.Client` / `httpx.AsyncClient`
|
|
100
|
+
subclasses and accept all of their options:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from egress_proxy.client.httpx import AsyncClient, Client
|
|
104
|
+
|
|
105
|
+
client = AsyncClient(
|
|
106
|
+
egress_url="http://SERVER_IP:8000",
|
|
107
|
+
egress_key="PROXY_KEY",
|
|
108
|
+
base_url="https://api.example.com",
|
|
109
|
+
timeout=10,
|
|
110
|
+
)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**Keep your own client** and only swap its transport (`Transport` for `httpx.Client`):
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
import httpx
|
|
117
|
+
from egress_proxy.client.httpx import AsyncTransport
|
|
118
|
+
|
|
119
|
+
client = httpx.AsyncClient(
|
|
120
|
+
transport=AsyncTransport(egress_url="http://SERVER_IP:8000", egress_key="PROXY_KEY"),
|
|
121
|
+
)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**Subclass it** like any httpx client:
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
class Api(AsyncClient):
|
|
128
|
+
def __init__(self) -> None:
|
|
129
|
+
super().__init__(
|
|
130
|
+
egress_url="http://SERVER_IP:8000",
|
|
131
|
+
egress_key="PROXY_KEY",
|
|
132
|
+
base_url="https://api.example.com",
|
|
133
|
+
)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
To tune the connection to the proxy server, pass `egress_transport`, for example
|
|
137
|
+
`egress_transport=httpx.AsyncHTTPTransport(http2=True, retries=2)`.
|
|
138
|
+
|
|
139
|
+
| Situation | Raised |
|
|
140
|
+
|---|---|
|
|
141
|
+
| Wrong key or invalid target URL | `egress_proxy.client.httpx.Refused` (a `httpx.TransportError`) |
|
|
142
|
+
| Target did not answer in time | `httpx.TimeoutException` |
|
|
143
|
+
| Target or proxy server unreachable | `httpx.ConnectError` |
|
|
144
|
+
|
|
145
|
+
## aiohttp
|
|
146
|
+
|
|
147
|
+
**Create a session.** `session` returns a regular `aiohttp.ClientSession` and accepts all of its
|
|
148
|
+
options:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from egress_proxy.client.aiohttp import session
|
|
152
|
+
|
|
153
|
+
client = session(
|
|
154
|
+
egress_url="http://SERVER_IP:8000",
|
|
155
|
+
egress_key="PROXY_KEY",
|
|
156
|
+
base_url="https://api.example.com/",
|
|
157
|
+
)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
**Keep your own session** and add the middleware; put it last so your own middlewares see the
|
|
161
|
+
target URL:
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
import aiohttp
|
|
165
|
+
from egress_proxy.client.aiohttp import Middleware
|
|
166
|
+
|
|
167
|
+
client = aiohttp.ClientSession(
|
|
168
|
+
middlewares=[Middleware(egress_url="http://SERVER_IP:8000", egress_key="PROXY_KEY")],
|
|
169
|
+
)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`response.url`, redirects and cookies refer to the target, exactly as without the proxy.
|
|
173
|
+
|
|
174
|
+
| Situation | Raised |
|
|
175
|
+
|---|---|
|
|
176
|
+
| Wrong key or invalid target URL | `egress_proxy.client.aiohttp.Refused` (an `aiohttp.ClientError`) |
|
|
177
|
+
| Target did not answer in time | `aiohttp.ServerTimeoutError` (also a `TimeoutError`) |
|
|
178
|
+
| Target unreachable | `aiohttp.ClientConnectionError` |
|
|
179
|
+
| Proxy server unreachable | `aiohttp.ClientConnectorError` |
|
|
180
|
+
|
|
181
|
+
With either client, errors are the library's own exceptions, so existing `except` blocks keep
|
|
182
|
+
working, and responses with error statuses (404, 500, …) come back as normal responses.
|
|
183
|
+
|
|
184
|
+
## Server configuration
|
|
185
|
+
|
|
186
|
+
Settings are read from `.env` in the working directory or from environment variables, which take
|
|
187
|
+
precedence. Template: [`.env.example`](https://github.com/botifyex/egress-proxy/blob/master/.env.example).
|
|
188
|
+
|
|
189
|
+
| Variable | Default | Meaning |
|
|
190
|
+
|---|---|---|
|
|
191
|
+
| `PROXY__KEY` | required | Key every client must send |
|
|
192
|
+
| `PROXY__TIMEOUT` | `30` | Seconds to wait for the target |
|
|
193
|
+
| `PROXY__CONNECT` | `10` | Seconds to connect to the target |
|
|
194
|
+
| `PROXY__UPSTREAMS_FILE` | none | File with upstream proxies, see below; `--upstreams-file` overrides it |
|
|
195
|
+
| `PROXY__UPSTREAMS` | `[]` | Upstream proxies inline, as a JSON list |
|
|
196
|
+
| `PROXY__COOLDOWN` | `30` | Seconds an unreachable upstream proxy is left out |
|
|
197
|
+
| `PROXY__ATTEMPTS` | `3` | Upstream proxies tried per request before giving up |
|
|
198
|
+
| `PROXY__ALLOW_LOCAL` | `false` | Allow targets on local and private networks, see below |
|
|
199
|
+
| `SERVER__HOST` | `127.0.0.1` | Address to listen on |
|
|
200
|
+
| `SERVER__PORT` | `8000` | Port to listen on |
|
|
201
|
+
|
|
202
|
+
The client's own timeout still applies (httpx defaults to 5 seconds), so raise it as well for slow
|
|
203
|
+
targets: `AsyncClient(..., timeout=60)`.
|
|
204
|
+
|
|
205
|
+
`egress-proxy` accepts every [uvicorn option](https://uvicorn.dev/settings/), and command-line
|
|
206
|
+
values override `.env`:
|
|
207
|
+
|
|
208
|
+
```sh
|
|
209
|
+
egress-proxy --host 0.0.0.0 --port 8000 --workers 4 --log-level warning
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
The access log is off by default, since every line would read `GET / 200`; enable it with
|
|
213
|
+
`--access-log`.
|
|
214
|
+
|
|
215
|
+
**Use HTTPS over the internet.** The key travels in a request header, so encrypt the connection,
|
|
216
|
+
either directly or behind a TLS-terminating reverse proxy, and use `https://` in the client:
|
|
217
|
+
|
|
218
|
+
```sh
|
|
219
|
+
egress-proxy --host 0.0.0.0 --port 443 --ssl-keyfile key.pem --ssl-certfile cert.pem
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Local networks
|
|
223
|
+
|
|
224
|
+
By default the server refuses targets that are not publicly routable, so a client cannot use it to
|
|
225
|
+
reach services next to it: loopback, private networks (`10.0.0.0/8`, `172.16.0.0/12`,
|
|
226
|
+
`192.168.0.0/16`, `fc00::/7`), link-local addresses including cloud metadata at
|
|
227
|
+
`169.254.169.254`, shared and reserved ranges, multicast, and IPv6 forms that embed such an IPv4
|
|
228
|
+
address. Host names are checked after DNS resolution and the connection uses only the checked
|
|
229
|
+
addresses, so a name that points to a local address, or later starts to, is refused as well.
|
|
230
|
+
Refused requests get a 400 with `x-egress-failure: invalid`.
|
|
231
|
+
|
|
232
|
+
To proxy into local networks on purpose, set `PROXY__ALLOW_LOCAL=true`.
|
|
233
|
+
|
|
234
|
+
Through upstream proxies, local IP addresses in the target are still refused, but host names are
|
|
235
|
+
resolved by the proxy and reach whatever its network serves under that name.
|
|
236
|
+
|
|
237
|
+
## Upstream proxies
|
|
238
|
+
|
|
239
|
+
Without upstream proxies the server sends requests itself. List HTTP or SOCKS proxies and it spreads
|
|
240
|
+
requests across them in turn instead, so they leave from the proxies' IPs:
|
|
241
|
+
|
|
242
|
+
```sh
|
|
243
|
+
cat > proxies.txt <<'EOF'
|
|
244
|
+
socks5h://user:password@proxy-1.example.net:1080
|
|
245
|
+
http://user:password@proxy-2.example.net:3128
|
|
246
|
+
EOF
|
|
247
|
+
echo "PROXY__UPSTREAMS_FILE=proxies.txt" >> .env
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
One proxy URL per line; blank lines and `#` comments are ignored. Template:
|
|
251
|
+
[`proxies.example.txt`](https://github.com/botifyex/egress-proxy/blob/master/proxies.example.txt).
|
|
252
|
+
|
|
253
|
+
| Scheme | Proxy |
|
|
254
|
+
|---|---|
|
|
255
|
+
| `http://`, `https://` | HTTP proxy (CONNECT), `https` reaches the proxy over TLS |
|
|
256
|
+
| `socks5h://` | SOCKS5, host names resolved by the proxy |
|
|
257
|
+
| `socks5://` | SOCKS5, host names resolved by the egress server |
|
|
258
|
+
| `socks4://` | SOCKS4 |
|
|
259
|
+
|
|
260
|
+
The file can also be given on the command line, which overrides `.env`:
|
|
261
|
+
|
|
262
|
+
```sh
|
|
263
|
+
egress-proxy --upstreams-file proxies.txt --host 0.0.0.0
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
A few proxies can go straight into `.env` instead: `PROXY__UPSTREAMS=["socks5h://host:1080"]`.
|
|
267
|
+
Both sources can be combined; the file comes first.
|
|
268
|
+
|
|
269
|
+
If a proxy cannot be reached, the request moves on to the next one and the unreachable proxy is
|
|
270
|
+
left out for `PROXY__COOLDOWN` seconds. A proxy that refuses the tunnel, for example over a wrong
|
|
271
|
+
password or a target it cannot reach, is skipped for that request only. The client gets a 502 once
|
|
272
|
+
`PROXY__ATTEMPTS` proxies have failed. An invalid proxy URL stops the server at startup with its
|
|
273
|
+
file and line.
|
|
274
|
+
|
|
275
|
+
## How it works
|
|
276
|
+
|
|
277
|
+
The client sends each request to the server with the target URL and the key in headers. The
|
|
278
|
+
server checks the key, forwards the request over a pooled keep-alive connection and streams the
|
|
279
|
+
response back unchanged. Redirects and cookies are handled by your client, never stored on the
|
|
280
|
+
server.
|
|
281
|
+
|
|
282
|
+
Before forwarding, the server removes every header that could reveal the client or the proxy,
|
|
283
|
+
including those a load balancer or CDN in front of it adds:
|
|
284
|
+
|
|
285
|
+
- client addresses: `X-Forwarded-For`, `Forwarded`, `X-Real-IP`, `X-Client-IP`, `Client-IP`,
|
|
286
|
+
`True-Client-IP`, `X-Originating-IP`, `X-Remote-Addr`, `CF-Connecting-IP`, `Fastly-Client-IP`,
|
|
287
|
+
`CloudFront-Viewer-Address`, `X-Azure-ClientIP` and any other name containing `forwarded`,
|
|
288
|
+
`client-ip`, `real-ip`, `remote-addr` or a similar fragment;
|
|
289
|
+
- proxy and CDN markers: `Via`, `CF-Ray`, `CDN-Loop`, `X-Amzn-Trace-Id`, `X-Envoy-*`,
|
|
290
|
+
`CloudFront-Viewer-*` and similar;
|
|
291
|
+
- hop-by-hop headers, including every header named in `Connection` (RFC 9110), and `Proxy`
|
|
292
|
+
(httpoxy).
|
|
293
|
+
|
|
294
|
+
Names are compared case-insensitively with `_` treated as `-`, since many servers map both to the
|
|
295
|
+
same variable. Credentials such as `Authorization`, `Cookie`, `CF-Access-Client-Id` or
|
|
296
|
+
`Fastly-Key` pass through untouched.
|
|
297
|
+
|
|
298
|
+
## Requirements
|
|
299
|
+
|
|
300
|
+
Python 3.11+. The clients need httpx 0.25.1+ or aiohttp 3.13+; the server runs on uvicorn and
|
|
301
|
+
aiohttp and handles about 13,000 requests per second on a single core.
|
|
302
|
+
|
|
303
|
+
## License
|
|
304
|
+
|
|
305
|
+
[Apache-2.0](https://github.com/botifyex/egress-proxy/blob/master/LICENSE): use, modify and
|
|
306
|
+
distribute it freely, including commercially. Keep the license and the
|
|
307
|
+
[NOTICE](https://github.com/botifyex/egress-proxy/blob/master/NOTICE) with every copy and mark the
|
|
308
|
+
files you change.
|
|
309
|
+
|
|
310
|
+
Contributing and releases:
|
|
311
|
+
[CONTRIBUTING.md](https://github.com/botifyex/egress-proxy/blob/master/CONTRIBUTING.md).
|
|
312
|
+
Security issues: [SECURITY.md](https://github.com/botifyex/egress-proxy/blob/master/SECURITY.md).
|