[Plugin] How to parameterize? #519
-
I'm looking for ways to parameterize custom plugins. In my case I want to write one that acts like a "man in the middle" who some value to the HTTP header before passing on the request. Is there some recommended pattern? So far I have something like this, which is not ideal: from typing import Optional
from proxy.http.parser import HttpParser
from proxy.http.proxy import HttpProxyBasePlugin
class ModifyHeaderPlugin(HttpProxyBasePlugin):
"""Modify request header before sending to upstream server."""
# This seems not to be a working pattern:
# def __init__(self, headers: Optional[dict] = None, **kwargs):
# self.added_headers = headers or {}
# # HttpProxyBasePlugin.__init__(self, **kwargs)
def before_upstream_connection(self, request: HttpParser) -> Optional[HttpParser]:
return request
def handle_client_request(self, request: HttpParser) -> Optional[HttpParser]:
# I'd prefer to pass this token to the constructor:
request.add_header(b'Authorization', bytes(f"Bearer {token}", "utf-8"))
return request
def handle_upstream_chunk(self, chunk: memoryview) -> memoryview:
return chunk
def on_upstream_connection_close(self) -> None:
pass |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
@deeplook Apologies first of all for missing all your discussions. I'll get to your other threads shortly.
|
Beta Was this translation helpful? Give feedback.
-
If the token here is static i.e. same for all outgoing requests, then
Then you can use this flag within the plugin as:
If the token itself is dynamic, then it will be best to dynamically fetch the token from within the plugin when necessary. I hope this helps. |
Beta Was this translation helpful? Give feedback.
If the token here is static i.e. same for all outgoing requests, then
flags
will simply do the trick. You can define a flag like:Then you can use this flag within the plugin as:
self.flags.bearer_token
If the token itself is dynamic, then it will be best to dynamically fetch the token from within the plugin when necessary.
I hope…