pythonpython-3.xtwisted

Python3 Twisted Reverse Proxy Redirecting Incorrectly


I am trying to make a reverse proxy with Twisted. However, I get some unexpected behaviour when I try adding a child resource.

from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.web import proxy, server

site1 = proxy.ReverseProxyResource('youtube.com', 80, b'')
site2 = proxy.ReverseProxyResource('yahoo.com', 80, b'')

root = Resource()
root.putChild(b"app1", site1)
root.putChild(b"app2", site2)

reactor.listenTCP(8090, Site(root))
reactor.run()

From my understanding, the program should redirect "127.0.0.1/app1:8090" to youtube.com. However, it is simply redirecting to "https://127.0.0.1/app1:8090", which then gives an "Unable to connect" error in my browser.

The problem persists even if I convert site1 to a Site, through:

site1 = Site(proxy.ReverseProxyResource('youtube.com', 80, b''))

Replacing b'' by b'/' also does not solve the issue. I suspect the issue may be something like the browser cannot even find the resource in the first place - which is why it tries to find the https version but still fails. For example, trying to connect to 192.168.1.132 (nothing there) in my browser redirects to https://192... before telling me there's a problem.

Any help or guidance would be much appreciated :)


Solution

  • Not sure if that is a typo or not but in your browser you should try to connect to 127.0.0.1:8090/app1 and not 127.0.0.1/app1:8090 (See where the port is in the url)