Skip to content

Add ModifyRequestHeaderPlugin #1420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
- [Proxy Pool Plugin](#proxypoolplugin)
- [Filter By Client IP Plugin](#filterbyclientipplugin)
- [Modify Chunk Response Plugin](#modifychunkresponseplugin)
- [Modify Request Header Plugin](#modifyrequestheaderplugin)
- [Cloudflare DNS Resolver Plugin](#cloudflarednsresolverplugin)
- [Custom DNS Resolver Plugin](#customdnsresolverplugin)
- [Custom Network Interface](#customnetworkinterface)
Expand Down Expand Up @@ -932,6 +933,31 @@ plugin

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

### ModifyRequestHeaderPlugin

This plugin demonstrate how to modify outgoing HTTPS request headers under TLS interception mode.

Start `proxy.py` as:

```console
❯ proxy \
--plugins proxy.plugin.ModifyRequestHeaderPlugin \
... [TLS interception flags] ...
```

Verify using `curl -x localhost:8899 --cacert ca-cert.pem https://httpbin.org/get`:

```console
{
"args": {},
"headers": {
... [redacted] ...,
"X-Proxy-Py-Version": "2.4.4rc6.dev15+gf533c711"
},
... [redacted] ...
}
```

### CloudflareDnsResolverPlugin

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).
Expand Down
2 changes: 1 addition & 1 deletion proxy/http/server/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def on_client_data(self, raw: memoryview) -> None:
self.pipeline_request = None

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

def on_client_connection_close(self) -> None:
Expand Down
2 changes: 2 additions & 0 deletions proxy/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from .filter_by_client_ip import FilterByClientIpPlugin
from .filter_by_url_regex import FilterByURLRegexPlugin
from .modify_chunk_response import ModifyChunkResponsePlugin
from .modify_request_header import ModifyRequestHeaderPlugin
from .redirect_to_custom_server import RedirectToCustomServerPlugin


Expand All @@ -53,4 +54,5 @@
'CustomDnsResolverPlugin',
'CloudflareDnsResolverPlugin',
'ProgramNamePlugin',
'ModifyRequestHeaderPlugin',
]
40 changes: 40 additions & 0 deletions proxy/plugin/modify_request_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
"""
proxy.py
~~~~~~~~
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
Network monitoring, controls & Application development, testing, debugging.

:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.
"""
from typing import Optional

from ..http.proxy import HttpProxyBasePlugin
from ..http.parser import HttpParser
from ..common.utils import bytes_
from ..common.version import __version__


class ModifyRequestHeaderPlugin(HttpProxyBasePlugin):
"""Modify request header before sending to upstream server."""

# def before_upstream_connection(self, request: HttpParser) -> Optional[HttpParser]:
# """NOTE: Use this for HTTP only request headers modification."""
# request.add_header(
# b"x-proxy-py-version",
# bytes_(__version__),
# )
# return request

def handle_client_request(self, request: HttpParser) -> Optional[HttpParser]:
"""NOTE: This is for HTTPS request headers modification when under TLS interception.

For HTTPS requests, modification of request under TLS interception WILL NOT WORK
through before_upstream_connection.
"""
request.add_header(
b'x-proxy-py-version',
bytes_(__version__),
)
return request
Loading