Kodi plugin to to play various online streams (mostly Latvian)

playstreamproxy.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #!/usr/bin/python
  2. """
  3. StreamProxy daemon (based on Livestream daemon)
  4. Ensures persistent cookies, User-Agents and others tricks to play protected HLS/DASH streams
  5. """
  6. import os
  7. import sys
  8. import time
  9. import atexit
  10. import re
  11. import binascii
  12. from signal import SIGTERM
  13. from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
  14. from SocketServer import ThreadingMixIn
  15. from urllib import unquote, quote
  16. import urllib,urlparse
  17. #import cookielib,urllib2
  18. import requests
  19. try:
  20. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  21. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  22. except:
  23. pass
  24. HOST_NAME = ""
  25. PORT_NUMBER = 88
  26. DEBUG = False
  27. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  28. sessions = {}
  29. class StreamHandler(BaseHTTPRequestHandler):
  30. def do_HEAD(self):
  31. self.send_response(200)
  32. self.send_header("Server", "StreamProxy")
  33. self.send_header("Content-type", "text/html")
  34. self.end_headers()
  35. def do_GET(self):
  36. """Respond to a GET request."""
  37. SPLIT_CHAR = "~"
  38. SPLIT_CODE = "%7E"
  39. EQ_CODE = "%3D"
  40. COL_CODE = "%3A"
  41. self.log_message("get_url: \n%s", self.path)
  42. p = self.path.split("~")
  43. url = urllib.unquote(p[0][1:])
  44. url = url.replace(COL_CODE, ":")
  45. headers = headers2dict("""
  46. User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A366 Safari/600.1.4
  47. """)
  48. if len(p)>1:
  49. for h in p[1:]:
  50. headers[h.split("=")[0]]=urllib.unquote(h.split("=")[1])
  51. #self.fetch_offline(self.wfile)
  52. try:
  53. self.fetch_url2(self.wfile, url, headers)
  54. except Exception as e:
  55. print "Got Exception: ", str(e)
  56. def fetch_offline(self,wfile):
  57. self.send_response(200)
  58. self.send_header("Server", "StreamProxy")
  59. self.send_header("Content-type", "video/mp4")
  60. self.end_headers()
  61. self.wfile.write(open("offline.mp4", "rb").read())
  62. self.wfile.close()
  63. def fetch_url2(self, wfile, url, headers):
  64. if DEBUG: print "\n***********************************************************"
  65. self.log_message("fetch_url: \n%s", url)
  66. #self.log_message("headers: %s", headers)
  67. base_url = "/".join(url.split("/")[0:-1])
  68. if base_url not in sessions:
  69. sessions[base_url]={}
  70. sessions[base_url]["session"] = requests.Session()
  71. sessions[base_url]["key"] = binascii.a2b_hex(headers["key"]) if "key" in headers and headers["key"] else None
  72. #cj = cookielib.CookieJar()
  73. #sessions[base_url] = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  74. else:
  75. if "key" in headers and headers["key"]: sessions[base_url]["key"] = binascii.a2b_hex(headers["key"])
  76. ses = sessions[base_url]["session"]
  77. key = sessions[base_url]["key"]
  78. if DEBUG: print "**Request headers: "
  79. ses.headers.update(headers)
  80. #ses.addheaders=[]
  81. for h in ses.headers:
  82. #ses.addheaders.append((h,headers[h]))
  83. if DEBUG: print h,"=",ses.headers[h]
  84. r = ses.get(url, stream=True,verify=False)
  85. #r = ses.open(url)
  86. code = r.status_code #r.status_code
  87. if DEBUG: print "**Response:", code #r.status_code
  88. if DEBUG: print "**Response headers: "
  89. for h in r.headers:
  90. if DEBUG: print h,"=",r.headers[h]
  91. self.send_response(code)
  92. print code
  93. if code <> 200:
  94. self.fetch_offline(wfile)
  95. return
  96. if DEBUG: print "**Return headers:"
  97. headers2 = {}
  98. for h in r.headers:
  99. if h.lower() in ("user-agent","server","transfer-encoding","content-encoding","connection"):
  100. if DEBUG: print h," - skipped"
  101. continue
  102. else:
  103. headers2[h] = r.headers[h]
  104. if DEBUG:print h,"=",r.headers[h]
  105. # Content-Type: application/vnd.apple.mpegurl
  106. if r.headers["Content-Type"] == "application/vnd.apple.mpegurl" and key:
  107. content = r.content
  108. content = r.content.replace(base_url+"/","")
  109. content = re.sub("#EXT-X-KEY:METHOD=AES-128.+\n", "", content, 0, re.IGNORECASE | re.MULTILINE)
  110. headers2["Content-Length"] = "%s"%len(content)
  111. self.send_headers(headers2)
  112. wfile.write(content)
  113. # Content-Type: video/MP2T
  114. elif r.headers["Content-Type"] == "video/MP2T" and key:
  115. content = r.content
  116. from Crypto.Cipher import AES
  117. iv = content[:16]
  118. d = AES.new(key, AES.MODE_CBC, iv)
  119. content = d.decrypt(content[16:])
  120. headers2["Content-Length"] = "%s"%len(content)
  121. self.send_headers(headers2)
  122. wfile.write(content)
  123. else:
  124. self.send_headers(headers2)
  125. CHUNK_SIZE = 4 * 1024
  126. if code == 200:
  127. #while True:
  128. #chunk = r.read(CHUNK_SIZE)
  129. #if not chunk:
  130. #break
  131. #wfile.write(chunk)
  132. #pass
  133. #wfile.close()
  134. for chunk in r.iter_content(1024):
  135. try:
  136. #print "#",
  137. wfile.write(chunk)
  138. except Exception as e:
  139. print "Exception: ", str(e)
  140. return
  141. if DEBUG: print " = file downloaded = "
  142. time.sleep(1)
  143. self.wfile.close()
  144. def send_headers(self,headers):
  145. for h in headers:
  146. self.send_header(h, headers[h])
  147. self.end_headers()
  148. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  149. """Handle requests in a separate thread."""
  150. def start(host = HOST_NAME, port = PORT_NUMBER):
  151. httpd = ThreadedHTTPServer((host, port), StreamHandler)
  152. print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  153. try:
  154. httpd.serve_forever()
  155. except KeyboardInterrupt:
  156. pass
  157. httpd.server_close()
  158. print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
  159. class Daemon:
  160. """
  161. A generic daemon class.
  162. Usage: subclass the Daemon class and override the run() method
  163. """
  164. def __init__(self, pidfile, stdin="/dev/null", stdout="/dev/null", stderr="/dev/null"):
  165. self.stdin = stdin
  166. self.stdout = stdout
  167. self.stderr = stderr
  168. self.pidfile = pidfile
  169. def daemonize(self):
  170. """
  171. do the UNIX double-fork magic, see Stevens' "Advanced
  172. Programming in the UNIX Environment" for details (ISBN 0201563177)
  173. http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
  174. """
  175. try:
  176. pid = os.fork()
  177. if pid > 0:
  178. # exit first parent
  179. sys.exit(0)
  180. except OSError, e:
  181. sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
  182. sys.exit(1)
  183. # decouple from parent environment
  184. os.chdir("/")
  185. os.setsid()
  186. os.umask(0)
  187. # do second fork
  188. try:
  189. pid = os.fork()
  190. if pid > 0:
  191. # exit from second parent
  192. sys.exit(0)
  193. except OSError, e:
  194. sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
  195. sys.exit(1)
  196. # redirect standard file descriptors
  197. sys.stdout.flush()
  198. sys.stderr.flush()
  199. si = file(self.stdin, "r")
  200. so = file(self.stdout, "a+")
  201. se = file(self.stderr, "a+", 0)
  202. os.dup2(si.fileno(), sys.stdin.fileno())
  203. os.dup2(so.fileno(), sys.stdout.fileno())
  204. os.dup2(se.fileno(), sys.stderr.fileno())
  205. # write pidfile
  206. atexit.register(self.delpid)
  207. pid = str(os.getpid())
  208. file(self.pidfile,"w+").write("%s\n" % pid)
  209. def delpid(self):
  210. os.remove(self.pidfile)
  211. def start(self):
  212. """
  213. Start the daemon
  214. """
  215. # Check for a pidfile to see if the daemon already runs
  216. try:
  217. pf = file(self.pidfile,"r")
  218. pid = int(pf.read().strip())
  219. pf.close()
  220. except IOError:
  221. pid = None
  222. if pid:
  223. message = "pidfile %s already exist. Daemon already running?\n"
  224. sys.stderr.write(message % self.pidfile)
  225. sys.exit(1)
  226. # Start the daemon
  227. self.daemonize()
  228. self.run()
  229. def stop(self):
  230. """
  231. Stop the daemon
  232. """
  233. # Get the pid from the pidfile
  234. try:
  235. pf = file(self.pidfile,"r")
  236. pid = int(pf.read().strip())
  237. pf.close()
  238. except IOError:
  239. pid = None
  240. if not pid:
  241. message = "pidfile %s does not exist. Daemon not running?\n"
  242. sys.stderr.write(message % self.pidfile)
  243. return # not an error in a restart
  244. # Try killing the daemon process
  245. try:
  246. while 1:
  247. os.kill(pid, SIGTERM)
  248. time.sleep(0.1)
  249. except OSError, err:
  250. err = str(err)
  251. if err.find("No such process") > 0:
  252. if os.path.exists(self.pidfile):
  253. os.remove(self.pidfile)
  254. else:
  255. print str(err)
  256. sys.exit(1)
  257. def restart(self):
  258. """
  259. Restart the daemon
  260. """
  261. self.stop()
  262. self.start()
  263. def run(self):
  264. """
  265. You should override this method when you subclass Daemon. It will be called after the process has been
  266. daemonized by start() or restart().
  267. """
  268. class ProxyDaemon(Daemon):
  269. def run(self):
  270. start()
  271. if __name__ == "__main__":
  272. daemon = ProxyDaemon("/var/run/playstreamproxy.pid")
  273. if len(sys.argv) == 2:
  274. if "start" == sys.argv[1]:
  275. daemon.start()
  276. elif "stop" == sys.argv[1]:
  277. daemon.stop()
  278. elif "restart" == sys.argv[1]:
  279. daemon.restart()
  280. elif "manualstart" == sys.argv[1]:
  281. start()
  282. else:
  283. print "Unknown command"
  284. sys.exit(2)
  285. sys.exit(0)
  286. else:
  287. print "usage: %s start|stop|restart|manualstart" % sys.argv[0]
  288. sys.exit(2)