2 # -*- coding: utf-8 -*-
4 VPProxy: HTTP/HLS Stream to HTTP Multiplexing Proxy
6 Based on AceProxy (https://github.com/ValdikSS/AceProxy) design
11 # Monkeypatching and all the stuff
12 gevent.monkey.patch_all()
13 # Startup delay for daemon restart
23 from socket import error as SocketException
24 from socket import SHUT_RDWR
28 from vpconfig import VPConfig
31 import plugins.modules.ipaddr as ipaddr
32 from clientcounter import ClientCounter
33 from plugins.modules.PluginInterface import VPProxyPlugin
42 class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
46 def handle_one_request(self):
48 Add request to requestlist, handle request and remove from the list
50 HTTPHandler.requestlist.append(self)
51 BaseHTTPServer.BaseHTTPRequestHandler.handle_one_request(self)
52 HTTPHandler.requestlist.remove(self)
54 def closeConnection(self):
58 if self.clientconnected:
59 self.clientconnected = False
63 self.connection.shutdown(SHUT_RDWR)
67 def dieWithError(self, errorcode=500):
69 Close connection with error
71 logging.warning("Dying with error")
72 if self.clientconnected:
73 self.send_error(errorcode)
75 self.closeConnection()
77 def proxyReadWrite(self):
79 Read video stream and send it to client
81 logger = logging.getLogger('http_proxyReadWrite')
82 logger.debug("Started")
85 self.streamstate = True
90 if not self.clientconnected:
91 logger.debug("Client is not connected, terminating")
94 data = self.video.read(4096)
95 if data and self.clientconnected:
96 self.wfile.write(data)
98 logger.warning("Video connection closed")
101 except SocketException:
102 # Video connection dropped
103 logger.warning("Video connection dropped")
106 self.closeConnection()
108 def hangDetector(self):
110 Detect client disconnection while in the middle of something
111 or just normal connection close.
113 logger = logging.getLogger('http_hangDetector')
116 if not self.rfile.read():
121 self.clientconnected = False
122 logger.debug("Client disconnected")
124 self.requestgreenlet.kill()
132 return self.do_GET(headers_only=True)
134 def do_GET(self, headers_only=False):
138 logger = logging.getLogger('http_HTTPHandler')
139 self.clientconnected = True
140 # Don't wait videodestroydelay if error happened
141 self.errorhappened = True
142 # Headers sent flag for fake headers UAs
143 self.headerssent = False
145 self.requestgreenlet = gevent.getcurrent()
146 # Connected client IP address
147 self.clientip = self.request.getpeername()[0]
149 if VPConfig.firewall:
150 # If firewall enabled
151 self.clientinrange = any(map(lambda i: ipaddr.IPAddress(self.clientip) \
152 in ipaddr.IPNetwork(i), VPConfig.firewallnetranges))
154 if (VPConfig.firewallblacklistmode and self.clientinrange) or \
155 (not VPConfig.firewallblacklistmode and not self.clientinrange):
156 logger.info('Dropping connection from ' + self.clientip + ' due to ' + \
158 self.dieWithError(403) # 403 Forbidden
161 logger.info("Accepted connection from " + self.clientip + " path " + self.path)
164 self.splittedpath = self.path.split('/')
165 self.reqtype = self.splittedpath[1].lower()
166 # If first parameter is 'pid' or 'torrent' or it should be handled
168 if not (self.reqtype in ('get','mp4','ogg','ogv') or self.reqtype in VPStuff.pluginshandlers):
169 self.dieWithError(400) # 400 Bad Request
172 self.dieWithError(400) # 400 Bad Request
175 # Handle request with plugin handler
176 if self.reqtype in VPStuff.pluginshandlers:
178 VPStuff.pluginshandlers.get(self.reqtype).handle(self)
179 except Exception as e:
180 logger.error('Plugin exception: ' + repr(e))
181 logger.error(traceback.format_exc())
184 self.closeConnection()
186 self.handleRequest(headers_only)
188 def handleRequest(self, headers_only):
190 # Limit concurrent connections
191 if 0 < VPConfig.maxconns <= VPStuff.clientcounter.total:
192 logger.debug("Maximum connections reached, can't serve this")
193 self.dieWithError(503) # 503 Service Unavailable
196 # Pretend to work fine with Fake UAs or HEAD request.
197 useragent = self.headers.get('User-Agent')
198 logger.debug("HTTP User Agent:"+useragent)
199 fakeua = useragent and useragent in VPConfig.fakeuas
200 if headers_only or fakeua:
202 logger.debug("Got fake UA: " + self.headers.get('User-Agent'))
203 # Return 200 and exit
204 self.send_response(200)
205 self.send_header("Content-Type", "video/mpeg")
207 self.closeConnection()
210 self.path_unquoted = urllib2.unquote('/'.join(self.splittedpath[2:]))
211 # Make list with parameters
213 for i in xrange(3, 8):
215 self.params.append(int(self.splittedpath[i]))
216 except (IndexError, ValueError):
217 self.params.append('0')
219 # Adding client to clientcounter
220 clients = VPStuff.clientcounter.add(self.reqtype+'\\'+self.path_unquoted, self.clientip)
221 # If we are the one client, but sucessfully got vp instance from clientcounter,
222 # then somebody is waiting in the videodestroydelay state
224 # Check if we are first client
225 if VPStuff.clientcounter.get(self.reqtype+'\\'+self.path_unquoted)==1:
226 logger.debug("First client, should create VLC session")
227 shouldcreatevp = True
229 logger.debug("Can reuse existing session")
230 shouldcreatevp = False
232 self.vlcid = hashlib.md5(self.reqtype+'\\'+self.path_unquoted).hexdigest()
234 # Send fake headers if this User-Agent is in fakeheaderuas tuple
237 "Sending fake headers for " + useragent)
238 self.send_response(200)
239 self.send_header('Cache-Control','no-cache, no-store, must-revalidate');
240 self.send_header('Pragma','no-cache');
241 if self.reqtype in ("ogg","ogv"):
242 self.send_header("Content-Type", "video/ogg")
244 self.send_header("Content-Type", "video/mpeg")
246 # Do not send real headers at all
247 self.headerssent = True
250 self.hanggreenlet = gevent.spawn(self.hangDetector)
251 logger.debug("hangDetector spawned")
255 self.errorhappened = False
258 logger.debug("Got url " + self.path_unquoted)
259 # Force ffmpeg demuxing if set in config
260 if VPConfig.vlcforceffmpeg:
261 self.vlcprefix = 'http/ffmpeg://'
265 logger.info("Starting broadcasting "+self.path)
266 VPStuff.vlcclient.startBroadcast(
267 self.vlcid, self.vlcprefix + self.path_unquoted, VPConfig.vlcmux, VPConfig.vlcpreaccess, self.reqtype)
268 # Sleep a bit, because sometimes VLC doesn't open port in
272 # Building new VLC url
273 self.url = 'http://' + VPConfig.vlchost + \
274 ':' + str(VPConfig.vlcoutport) + '/' + self.vlcid
275 logger.debug("VLC url " + self.url)
277 # Sending client headers to videostream
278 self.video = urllib2.Request(self.url)
279 for key in self.headers.dict:
280 self.video.add_header(key, self.headers.dict[key])
282 self.video = urllib2.urlopen(self.video)
284 # Sending videostream headers to client
285 if not self.headerssent:
286 self.send_response(self.video.getcode())
287 if self.video.info().dict.has_key('connection'):
288 del self.video.info().dict['connection']
289 if self.video.info().dict.has_key('server'):
290 del self.video.info().dict['server']
291 if self.video.info().dict.has_key('transfer-encoding'):
292 del self.video.info().dict['transfer-encoding']
293 if self.video.info().dict.has_key('content-type'):
294 del self.video.info().dict['content-type']
295 if self.video.info().dict.has_key('keep-alive'):
296 del self.video.info().dict['keep-alive']
298 for key in self.video.info().dict:
299 self.send_header(key, self.video.info().dict[key])
301 self.send_header('Cache-Control','no-cache, no-store, must-revalidate');
302 self.send_header('Pragma','no-cache');
304 if self.reqtype=="ogg":
305 self.send_header("Content-Type", "video/ogg")
307 self.send_header("Content-Type", "video/mpeg")
309 # End headers. Next goes video data
311 logger.debug("Headers sent")
314 self.proxyReadWrite()
316 # Waiting until hangDetector is joined
317 self.hanggreenlet.join()
318 logger.debug("Request handler finished")
320 except (vpclient.VPException, vlcclient.VlcException, urllib2.URLError) as e:
321 logger.error("Exception: " + repr(e))
322 self.errorhappened = True
324 except gevent.GreenletExit:
325 # hangDetector told us about client disconnection
329 logger.error(traceback.format_exc())
330 self.errorhappened = True
333 logger.debug("END REQUEST")
334 logger.info("Closed connection from " + self.clientip + " path " + self.path)
335 VPStuff.clientcounter.delete(self.reqtype+'\\'+self.path_unquoted, self.clientip)
336 if not VPStuff.clientcounter.get(self.reqtype+'\\'+self.path_unquoted):
338 logger.debug("That was the last client, destroying VPClient")
339 logger.info("Stopping broadcasting " + self.path)
340 VPStuff.vlcclient.stopBroadcast(self.vlcid)
344 if not self.headerssent:
345 logger.error("Problem receiving video stream, no headers!")
346 if VPStuff.clientcounter.total == 0:
347 logger.error("Probably VLC hang")
350 class HTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
352 def handle_error(self, request, client_address):
353 # Do not print HTTP tracebacks
357 class VPStuff(object):
359 Inter-class interaction class
363 # taken from http://stackoverflow.com/questions/2699907/dropping-root-permissions-in-python
364 def drop_privileges(uid_name, gid_name='nogroup'):
366 # Get the uid/gid from the name
367 running_uid = pwd.getpwnam(uid_name).pw_uid
368 running_uid_home = pwd.getpwnam(uid_name).pw_dir
369 running_gid = grp.getgrnam(gid_name).gr_gid
371 # Remove group privileges
374 # Try setting the new uid/gid
375 os.setgid(running_gid)
376 os.setuid(running_uid)
378 # Ensure a very conservative umask
379 old_umask = os.umask(077)
381 if os.getuid() == running_uid and os.getgid() == running_gid:
383 os.environ['HOME'] = running_uid_home
388 filename=VPConfig.logpath + 'vphttp.log' if VPConfig.loggingtoafile else None,
389 format='%(asctime)s %(levelname)s %(name)s: %(message)s', datefmt='%d.%m.%Y %H:%M:%S', level=VPConfig.debug)
390 logger = logging.getLogger('INIT')
393 # Trying to change dir (would fail in freezed state)
395 os.chdir(os.path.dirname(os.path.realpath(__file__)))
398 # Creating dict of handlers
399 VPStuff.pluginshandlers = dict()
400 # And a list with plugin instances
401 VPStuff.pluginlist = list()
402 pluginsmatch = glob.glob('plugins/*_plugin.py')
403 sys.path.insert(0, 'plugins')
404 pluginslist = [os.path.splitext(os.path.basename(x))[0] for x in pluginsmatch]
405 for i in pluginslist:
406 plugin = __import__(i)
407 plugname = i.split('_')[0].capitalize()
409 plugininstance = getattr(plugin, plugname)(VPConfig, VPStuff)
410 except Exception as e:
411 logger.error("Cannot load plugin " + plugname + ": " + repr(e))
413 logger.debug('Plugin loaded: ' + plugname)
414 for j in plugininstance.handlers:
415 logger.info("Registering handler '" + j +"'")
416 VPStuff.pluginshandlers[j] = plugininstance
417 VPStuff.pluginlist.append(plugininstance)
419 # Check whether we can bind to the defined port safely
420 if os.getuid() != 0 and VPConfig.httpport <= 1024:
421 logger.error("Cannot bind to port " + str(VPConfig.httpport) + " without root privileges")
424 server = HTTPServer((VPConfig.httphost, VPConfig.httpport), HTTPHandler)
425 logger = logging.getLogger('HTTP')
427 # Dropping root privileges if needed
428 if VPConfig.vpproxyuser and os.getuid() == 0:
429 if drop_privileges(VPConfig.vpproxyuser):
430 logger.info("Dropped privileges to user " + VPConfig.vpproxyuser)
432 logger.error("Cannot drop privileges to user " + VPConfig.vpproxyuser)
435 # Creating ClientCounter
436 VPStuff.clientcounter = ClientCounter()
438 DEVNULL = open(os.devnull, 'wb')
440 # Spawning procedures
441 def spawnVLC(cmd, delay = 0):
443 VPStuff.vlc = psutil.Popen(cmd) #, stdout=DEVNULL, stderr=DEVNULL)
451 VPStuff.vlcclient = vlcclient.VlcClient(
452 host=VPConfig.vlchost, port=VPConfig.vlcport, password=VPConfig.vlcpass,
453 out_port=VPConfig.vlcoutport)
455 except vlcclient.VlcException as e:
458 def isRunning(process):
459 if psutil.version_info[0] >= 2:
460 if process.is_running() and process.status() != psutil.STATUS_ZOMBIE:
462 else: # for older versions of psutil
463 if process.is_running() and process.status != psutil.STATUS_ZOMBIE:
467 def findProcess(name):
468 for proc in psutil.process_iter():
470 pinfo = proc.as_dict(attrs=['pid', 'name'])
471 if pinfo['name'] == name:
473 except psutil.AccessDenied:
476 except psutil.NoSuchProcess:
482 # Trying to close all spawned processes gracefully
483 if isRunning(VPStuff.vlc):
484 if VPStuff.vlcclient:
485 VPStuff.vlcclient.destroy()
487 if isRunning(VPStuff.vlc):
491 # This is what we call to stop the server completely
492 def shutdown(signum = 0, frame = 0):
493 logger.info("Stopping server...")
494 # Closing all client connections
495 for connection in server.RequestHandlerClass.requestlist:
497 # Set errorhappened to prevent waiting for videodestroydelay
498 connection.errorhappened = True
499 connection.closeConnection()
501 logger.warning("Cannot kill a connection!")
503 server.server_close()
506 def _reloadconfig(signum=None, frame=None):
508 Reload configuration file.
513 logger = logging.getLogger('reloadconfig')
515 from vpconfig import VPConfig
516 logger.info('Config reloaded')
518 # setting signal handlers
520 gevent.signal(signal.SIGHUP, _reloadconfig)
521 gevent.signal(signal.SIGTERM, shutdown)
522 except AttributeError:
526 VPStuff.vlcProc = VPConfig.vlccmd.split()
527 if spawnVLC(VPStuff.vlcProc, VPConfig.vlcspawntimeout) and connectVLC():
528 logger.info("VLC spawned with pid " + str(VPStuff.vlc.pid))
530 logger.error('Cannot spawn or connect to VLC!')
535 logger.info("Using gevent %s" % gevent.__version__)
536 logger.info("Using psutil %s" % psutil.__version__)
537 logger.info("Using VLC %s" % VPStuff.vlcclient._vlcver)
538 logger.info("Server started.")
540 if not isRunning(VPStuff.vlc):
542 if spawnVLC(VPStuff.vlcProc, VPConfig.vlcspawntimeout) and connectVLC():
543 logger.info("VLC died, respawned it with pid " + str(VPStuff.vlc.pid))
545 logger.error("Cannot spawn VLC!")
548 # Return to our server tasks
549 server.handle_request()
550 except (KeyboardInterrupt, SystemExit):