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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
'''
A akamaihd.net m3u playlist interceptor, very largely borrowed from
Tiny HTTP Proxy 0.2.1, by SUSUKI Hisao.

Requires cPython 2.7.x.

Sylvain <fourmanoit@gmail.com>, 2012 - 2013.

History
-------
2013-01-23: add support for quick consecutive playlist dump, 
            as sometimes a good list get overridden by a bad one
            in the dump process. Thanks to Kristoffer Laurin-Racicot
            for the improvement suggestion and code.
2012-12-15: initial release.
'''
import BaseHTTPServer, select, socket, SocketServer, urlparse, \
       cStringIO, datetime, os

class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler):
    __base = BaseHTTPServer.BaseHTTPRequestHandler
    __base_handle = __base.handle

    server_version = 'TinyHTTPProxy/0.2.1'
    rbufsize = 0                        # self.rfile be unbuffered

    def handle(self):
        (ip, port) =  self.client_address
        if hasattr(self, 'allowed_clients') and ip not in self.allowed_clients:
            self.raw_requestline = self.rfile.readline()
            if self.parse_request(): self.send_error(403)
        else:
            self.__base_handle()

    def _connect_to(self, netloc, soc):
        i = netloc.find(':')
        if i >= 0:
            host_port = netloc[:i], int(netloc[i+1:])
        else:
            host_port = netloc, 80
        print "\t" "connect to %s:%d" % host_port
        try: soc.connect(host_port)
        except socket.error, arg:
            try: msg = arg[1]
            except: msg = arg
            self.send_error(404, msg)
            return 0
        return 1

    def do_CONNECT(self):
        soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            if self._connect_to(self.path, soc):
                self.log_request(200)
                self.wfile.write(self.protocol_version +
                                 " 200 Connection established\r\n")
                self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
                self.wfile.write("\r\n")
                self._read_write(soc, 300)
        finally:
            print "\t" "bye"
            soc.close()
            self.connection.close()

    def do_GET(self):
        (scm, netloc, path, params, query, fragment) = urlparse.urlparse(
            self.path, 'http')
        if scm != 'http' or fragment or not netloc:
            self.send_error(400, "bad url %s" % self.path)
            return
        if 'akamaihd.net' in netloc:
            payload = cStringIO.StringIO()
        else:
            payload = None
        soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            if self._connect_to(netloc, soc):
                self.log_request()
                cmd = '%s %s %s\r\n' % (
                    self.command,
                    urlparse.urlunparse(('', '', path, params, query, '')),
                    self.request_version)
                if payload is not None:
                    payload.write(cmd)
                soc.send(cmd)
                self.headers['Connection'] = 'close'
                del self.headers['Proxy-Connection']
                self.headers['Accept-Encoding'] = ''
                for key_val in self.headers.items():
                    kv = '%s: %s\r\n' % key_val
                    soc.send(kv)
                    if payload is not None:
                        payload.write(kv)
                soc.send('\r\n')
                if payload is not None:
                    payload.write('\r\n')
                self._read_write(soc, payload = payload)
        finally:
            print "\t" "bye"
            soc.close()
            self.connection.close()

    def _read_write(self, soc, max_idling = 20, payload = None):
        iw = [self.connection, soc]
        ow = []
        count = 0

        while True:
            count += 1
            (ins, _, exs) = select.select(iw, ow, iw, 3)
            if exs: break
            if ins:
                for i in ins:
                    if i is soc:
                        out = self.connection
                    else:
                        out = soc
                    data = i.recv(8192)
                    if data:
                        if payload is not None:
                            payload.write(data)
                        out.send(data)
                        count = 0
            else:
                print "\t" "idle", count
            if count == max_idling: break

        if payload is not None:
            content = payload.getvalue()
            if len(content) > 0:
                max_tries = 3
                basepath = datetime.datetime.now().strftime(
                    '/tmp/playlist_%Y_%m_%d_%Hh%Mm%Ss')
                for idx in xrange(max_tries):
                    path = '%s%s.txt' % (
                        basepath, '_%d' % (idx + 1) if idx > 0 else '')
                    if not os.path.exists(path):
                        print '\tdumping playlist to "%s"' % path
                        with file(path, 'w') as f:
                            f.write(content)
                        break
                else:
                    print ('\tWARNING too many playlist '
                           'dumps this second (%d)') % max_tries
                    

    do_HEAD  = do_GET
    do_POST  = do_GET
    do_PUT   = do_GET
    do_DELETE= do_GET

class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
                           BaseHTTPServer.HTTPServer):
    def server_bind(self):
        BaseHTTPServer.HTTPServer.server_bind(self)
        # This is to avoid "address already in use" OSError
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

if __name__ == '__main__':
    BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)