summaryrefslogtreecommitdiff
path: root/proxy.py
blob: 585b58b5a52e5476ec9a217506e8f92a1e299019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python -t
import pwd, os, sys, logging, logging.handlers, string
import asynchat, asyncore, socket, httplib, urlparse
try:
    import cStringIO as StringIO
except ImportError:
    import StringIO


endpoints = {
    {'host':'10.1.0.1', 'port':8080, 'speed':220, 'name':'Proxy 10.1'},
    {'host':'10.2.2.11', 'port':8081, 'speed':340, 'name':'Proxy 10.2'},
    {'host':'10.3.0.99', 'port':8080, 'speed':340, 'name':'Proxy 10.3'},
}


class HTTPResponseProducer(object):
    def __init__(self, resp, amt=512):
        self.resp = resp
        self.amt = amt
    def more(self):
        return self.resp.read(self.amt)

class HTTPChannel(asynchat.async_chat):
    def __init__(self, server, sock, addr):
        asynchat.async_chat.__init__(self, sock)
        self.server = server
        self.set_terminator("\r\n\r\n")
        self.request = None
        self.data = StringIO.StringIO()

    def collect_incoming_data(self, data):
        self.data.write(data)
        if self.data.tell() > 16384:
            self.close_when_done()

    def found_terminator(self):
        if not self.request:
            # parse http header
            self.data.seek(0)
            self.request = string.split(self.data.readline(), None, 2)
            if len(self.request) != 3:
                # badly formed request; just shut down
                self.close_when_done()
            else:
                self.server.handle_request(self, self.request[0], self.request[1])
                self.close_when_done()
        else:
            pass # ignore body data, for now

class HTTPProxyServer(asyncore.dispatcher):
    def __init__(self):
        asyncore.dispatcher.__init__(self)
        self.port = 8080
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(("", 8080))
        self.listen(5)

    def handle_accept(self):
        conn, addr = self.accept()
        HTTPChannel(self, conn, addr)

    def handle_request(self, channel, method, path):
        url = urlparse.urlparse(path)
        print method, path
        if method != "GET" or url.query != "":
            #do not handle non-GET or GET with Query (?foo=bla) requests
            return self._bypass_request(channel, method, url)

        #check for content-length header with a HEAD request
        conn = httplib.HTTPConnection(url.hostname, url.port or 80)
        conn.request("HEAD", url.path)
        resp = conn.getresponse()
        content_length = filter(lambda it: it[0] == "content-length", resp.getheaders())
        if len( content_length ) == 0:
            # no content length given, bypass this request
            return self._bypass_request(channel, method, url)
        else:
            content_length = content_length[0][1]

        if content_length < 524288:
            # do not handle requests smaller than 512kb
            return self._bypass_request(channel, method, url)

        print "Content-Length: %s" % (content_length)
        
        # XXX an dieser stelle muss de request aufgeteilt werden
        return self._bypass_request(channel, method, url)
        #print "do some magic for " +str(url)
        #channel.push("HTTP/1.0 200 OK\r\nX-Proxy: Magicproxy (request handled in boost mode)\r\n")
        #channel.close_when_done()
        
    def _bypass_request(self, channel, method, url):
        #XXX hier sollte nicht proxy gespielt werden sondern
        #die daten 1-zu-1 durchgereicht werden.
        #Weiterhin sollte sichergestellt werden, dass die requests
        #zu Host X1 immer über Proxy Y1 geroutet werden
        # etwa proxy=proxies[ stuff(hostname) % len(proxies) ]
        conn = httplib.HTTPConnection(url.hostname, url.port or 80)
        conn.request(method, url.path)
        resp = conn.getresponse()
        channel.push("HTTP/1.0 200 OK\r\nX-Proxy: Magicproxy (request handled in standard mode)\r\n")
        channel.push( "\r\n".join(map(lambda k: "%s: %s" % (k[0],k[1]), resp.getheaders())) )
        channel.push("\r\n\r\n")
        channel.push_with_producer( HTTPResponseProducer(resp) )
        channel.close_when_done()

if __name__ == "__main__":
    proxy = HTTPProxyServer()
    asyncore.loop()