abhinavsingh / proxy.py

💫 Ngrok FRP Alternative • ⚡ Fast • 🪶 Lightweight • 0️⃣ Dependency • 🔌 Pluggable • 😈 TLS interception • 🔒 DNS-over-HTTPS • 🔥 Poor Man's VPN • ⏪ Reverse & ⏩ Forward • 👮🏿 "Proxy Server" framework • 🌐 "Web Server" framework • ➵ ➶ ➷ ➠ "PubSub" framework • 👷 "Work" acceptor & executor framework
https://abhinavsingh.com/proxy-py-a-lightweight-single-file-http-proxy-server-in-python/
BSD 3-Clause "New" or "Revised" License
2.91k stars 568 forks source link

httpProxyBasePlugin methods not being called #1412

Closed sandrich closed 1 month ago

sandrich commented 1 month ago

I try to implement a plugin for proxy.py but for HttpProxyBasePlugin I cannot get any function to be executed (before_upstream_connection as an example). I'd expect a print statement to be shown in the output.

import proxy

from typing import Optional, List, Union, Tuple

from proxy.http.proxy import HttpProxyBasePlugin
from proxy.http.parser import HttpParser
from proxy.http.server import ReverseProxyBasePlugin

# class FsProxyPlugin(ReverseProxyBasePlugin):
#     def routes(self):
#         print("test")
#         return []

class FsProxyPlugin(HttpProxyBasePlugin):
    def before_upstream_connection(
            self, request: HttpParser,
    ) -> Optional[HttpParser]:
        print(request.headers)
        return request

if __name__ == "__main__":
    with proxy.Proxy(
        input_args=[
            "--threaded",
            "--enable-reverse-proxy"
        ],
        enable_web_server=True,
        port=8080,
        plugins=[FsProxyPlugin]
    ) as _:
        proxy.sleep_loop()

If I use ReverseProxyBasePlugin instead, then I get the print statement. What am I missing?

abhinavsingh commented 1 month ago

Likely an issue with your local environment. I took your code, pasted it in a file, started the server, tried curl -x to use proxy server and all seems to be working as expected.

$ python issue1412.py
                                                                                                                                 ─╯
2024-05-21 14:22:38,578 - pid:49817 [I] plugins.load:89 - Loaded plugin proxy.http.proxy.HttpProxyPlugin
2024-05-21 14:22:38,578 - pid:49817 [I] plugins.load:89 - Loaded plugin proxy.http.server.HttpWebServerPlugin
2024-05-21 14:22:38,578 - pid:49817 [I] plugins.load:89 - Loaded plugin proxy.http.server.reverse.ReverseProxy
2024-05-21 14:22:38,578 - pid:49817 [I] plugins.load:89 - Loaded plugin __main__.FsProxyPlugin
{b'host': (b'Host', b'google.com:443'), b'user-agent': (b'User-Agent', b'curl/8.4.0'), b'proxy-connection': (b'Proxy-Connection', b'Keep-Alive')}
2024-05-21 14:22:42,686 - pid:49827 [I] server.access_log:388 - 127.0.0.1:54018 - CONNECT google.com:443 - 7671 bytes - 498.72ms
$ cat issue1412.py

from typing import Optional

import proxy
from proxy.http.proxy import HttpProxyBasePlugin
from proxy.http.parser import HttpParser

class FsProxyPlugin(HttpProxyBasePlugin):
    def before_upstream_connection(
        self,
        request: HttpParser,
    ) -> Optional[HttpParser]:
        print(request.headers)
        return request

if __name__ == "__main__":
    with proxy.Proxy(
        input_args=["--threaded", "--enable-reverse-proxy"],
        enable_web_server=True,
        port=8080,
        plugins=[FsProxyPlugin],
    ) as _:
        proxy.sleep_loop()
sandrich commented 1 month ago

thanks @abhinavsingh . Indeed it seems to be an env issue. Worked on a different system.