Skip to content

Commit a7077cf

Browse files
Add ModifyRequestHeaderPlugin (#1420)
* Add `ModifyRequestHeaderPlugin` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add to README * Fix lint issues shown by `Python3.11.8` --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e34da54 commit a7077cf

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- [Proxy Pool Plugin](#proxypoolplugin)
6060
- [Filter By Client IP Plugin](#filterbyclientipplugin)
6161
- [Modify Chunk Response Plugin](#modifychunkresponseplugin)
62+
- [Modify Request Header Plugin](#modifyrequestheaderplugin)
6263
- [Cloudflare DNS Resolver Plugin](#cloudflarednsresolverplugin)
6364
- [Custom DNS Resolver Plugin](#customdnsresolverplugin)
6465
- [Custom Network Interface](#customnetworkinterface)
@@ -932,6 +933,31 @@ plugin
932933

933934
Modify `ModifyChunkResponsePlugin` to your taste. Example, instead of sending hard-coded chunks, parse and modify the original `JSON` chunks received from the upstream server.
934935

936+
### ModifyRequestHeaderPlugin
937+
938+
This plugin demonstrate how to modify outgoing HTTPS request headers under TLS interception mode.
939+
940+
Start `proxy.py` as:
941+
942+
```console
943+
proxy \
944+
--plugins proxy.plugin.ModifyRequestHeaderPlugin \
945+
... [TLS interception flags] ...
946+
```
947+
948+
Verify using `curl -x localhost:8899 --cacert ca-cert.pem https://httpbin.org/get`:
949+
950+
```console
951+
{
952+
"args": {},
953+
"headers": {
954+
... [redacted] ...,
955+
"X-Proxy-Py-Version": "2.4.4rc6.dev15+gf533c711"
956+
},
957+
... [redacted] ...
958+
}
959+
```
960+
935961
### CloudflareDnsResolverPlugin
936962

937963
This plugin uses `Cloudflare` hosted `DNS-over-HTTPS` [API](https://developers.cloudflare.com/1.1.1.1/encrypted-dns/dns-over-https/make-api-requests/dns-json) (json).

proxy/http/server/web.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def on_client_data(self, raw: memoryview) -> None:
217217
self.pipeline_request = None
218218

219219
def on_response_chunk(self, chunk: List[memoryview]) -> List[memoryview]:
220-
self._response_size += sum([len(c) for c in chunk])
220+
self._response_size += sum(len(c) for c in chunk)
221221
return chunk
222222

223223
def on_client_connection_close(self) -> None:

proxy/plugin/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from .filter_by_client_ip import FilterByClientIpPlugin
3333
from .filter_by_url_regex import FilterByURLRegexPlugin
3434
from .modify_chunk_response import ModifyChunkResponsePlugin
35+
from .modify_request_header import ModifyRequestHeaderPlugin
3536
from .redirect_to_custom_server import RedirectToCustomServerPlugin
3637

3738

@@ -53,4 +54,5 @@
5354
'CustomDnsResolverPlugin',
5455
'CloudflareDnsResolverPlugin',
5556
'ProgramNamePlugin',
57+
'ModifyRequestHeaderPlugin',
5658
]

proxy/plugin/modify_request_header.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
proxy.py
4+
~~~~~~~~
5+
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
6+
Network monitoring, controls & Application development, testing, debugging.
7+
8+
:copyright: (c) 2013-present by Abhinav Singh and contributors.
9+
:license: BSD, see LICENSE for more details.
10+
"""
11+
from typing import Optional
12+
13+
from ..http.proxy import HttpProxyBasePlugin
14+
from ..http.parser import HttpParser
15+
from ..common.utils import bytes_
16+
from ..common.version import __version__
17+
18+
19+
class ModifyRequestHeaderPlugin(HttpProxyBasePlugin):
20+
"""Modify request header before sending to upstream server."""
21+
22+
# def before_upstream_connection(self, request: HttpParser) -> Optional[HttpParser]:
23+
# """NOTE: Use this for HTTP only request headers modification."""
24+
# request.add_header(
25+
# b"x-proxy-py-version",
26+
# bytes_(__version__),
27+
# )
28+
# return request
29+
30+
def handle_client_request(self, request: HttpParser) -> Optional[HttpParser]:
31+
"""NOTE: This is for HTTPS request headers modification when under TLS interception.
32+
33+
For HTTPS requests, modification of request under TLS interception WILL NOT WORK
34+
through before_upstream_connection.
35+
"""
36+
request.add_header(
37+
b'x-proxy-py-version',
38+
bytes_(__version__),
39+
)
40+
return request

0 commit comments

Comments
 (0)