Enigma2 plugin to to play various online streams (mostly Latvian).

playstreamproxy.py 10KB

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