Created
March 8, 2011 09:37
-
-
Save mopemope/860091 to your computer and use it in GitHub Desktop.
logging proxy server
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
from twisted.web import proxy, http | |
from twisted.internet import reactor | |
import re | |
import psyco | |
psyco.full() | |
store_type = ['text/html', 'json'] | |
ignore_host = [] | |
tube8_re = re.compile("^http://www.tube8.com/[a-z]+/[\w\d.:_-]+/\d{3,6}/", re.M) | |
def _parse_type(val): | |
res = val.split(';') | |
if len(res) == 2: | |
type, char = res | |
char = char.split('=')[1] | |
else: | |
res.append('utf8') | |
type, char = res | |
return (type.strip(), char.strip()) | |
class LogProxyClient(proxy.ProxyClient): | |
def handleHeader(self, key, value): | |
proxy.ProxyClient.handleHeader(self, key, value) | |
if key == 'Content-Type': | |
type, char = _parse_type(value) | |
if type in store_type: | |
self.content_type = type | |
self.charset = char | |
self._save = "" | |
def handleResponsePart(self, data): | |
proxy.ProxyClient.handleResponsePart(self, data) | |
#if hasattr(self, "_save"): | |
# self._save += data | |
def handleResponseEnd(self): | |
proxy.ProxyClient.handleResponseEnd(self) | |
uri = self.father.uri | |
if uri.startswith("http://video.xnxx.com/video"): | |
print(uri) | |
f = open("xnxx.txt", "a+") | |
f.write(uri+"\n") | |
f.close() | |
elif tube8_re.search(uri): | |
print(uri) | |
f = open("tube8.txt", "a+") | |
f.write(uri+"\n") | |
f.close() | |
elif uri.startswith("http://www.jizzhut.com/videos/"): | |
print(uri) | |
f = open("jizzhut.txt", "a+") | |
f.write(uri+"\n") | |
f.close() | |
elif uri.startswith("http://xhamster.com/movies/"): | |
print(uri) | |
f = open("xhamster.txt", "a+") | |
f.write(uri+"\n") | |
f.close() | |
elif uri.startswith("http://www.empflix.com/view.php"): | |
print(uri) | |
f = open("empflix.txt", "a+") | |
f.write(uri+"\n") | |
f.close() | |
#if hasattr(self, "_save"): | |
# req_host = self.father.getRequestHostname() | |
# if not req_host in ignore_host: | |
#self.father.proxylog.write(self.father.uri, self.charset, self.content_type, self._save) | |
#print "write %s %s" % (self.father.uri, self.content_type) | |
# del(self._save) | |
class LogProxyClientFactory(proxy.ProxyClientFactory): | |
def buildProtocol(self, addr): | |
client = proxy.ProxyClientFactory.buildProtocol(self, addr) | |
client.__class__ = LogProxyClient | |
return client | |
class LogProxyRequest(proxy.ProxyRequest): | |
protocols = {'http': LogProxyClientFactory} | |
def __init__(self, *args): | |
proxy.ProxyRequest.__init__(self, *args) | |
class LogProxy(proxy.Proxy): | |
def __init__(self): | |
proxy.Proxy.__init__(self) | |
def requestFactory(self, *args): | |
return LogProxyRequest(*args) | |
class ProxyFactory(http.HTTPFactory): | |
def __init__(self): | |
http.HTTPFactory.__init__(self) | |
def buildProtocol(self, addr): | |
protocol = LogProxy() | |
return protocol | |
def start_server(port=8000): | |
reactor.listenTCP(port, ProxyFactory()) | |
reactor.run() | |
if __name__ == '__main__': | |
start_server() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment