Created
April 11, 2013 05:27
-
-
Save darkf/5360967 to your computer and use it in GitHub Desktop.
Twisted HTTP proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Twisted HTTP proxy | |
# You would set REAL_IP to the IP of the server you want to proxy | |
# then override the domain name in your hosts file. Run this, | |
# and then it should proxy through. | |
# Copyright (c) 2013 darkf; licensed under CC-0 (or in the public domain if applicable) | |
from twisted.web import proxy, http | |
from twisted.internet import reactor | |
import urlparse | |
from urllib import quote as urlquote | |
REAL_IP = "actual ip here" | |
class LoggingProxyClient(proxy.ProxyClient): | |
def connectionMade(self): | |
proxy.ProxyClient.connectionMade(self) | |
def connectionLost(self, reason): | |
proxy.ProxyClient.connectionLost(self, reason) | |
def handleHeader(self, key, value): | |
proxy.ProxyClient.handleHeader(self, key, value) | |
def handleResponsePart(self, buffer): | |
proxy.ProxyClient.handleResponsePart(self, buffer) | |
def finish(self): | |
if not self._disconnected: | |
return proxy.ProxyClient.finish(self) | |
class LoggingProxyClientFactory(proxy.ProxyClientFactory): | |
protocol = LoggingProxyClient | |
class LoggingProxyRequest(proxy.ProxyRequest): | |
protocols = {'http': LoggingProxyClientFactory} | |
def process(self): | |
self.uri = "http://%s%s" % (REAL_IP, self.uri) | |
print self.method, self.path | |
proxy.ProxyRequest.process(self) | |
class LoggingProxy(proxy.Proxy): | |
requestFactory = LoggingProxyRequest | |
class ProxyFactory(http.HTTPFactory): | |
protocol = LoggingProxy | |
if __name__ == '__main__': | |
reactor.listenTCP(80, ProxyFactory(), interface="127.0.0.1") | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment