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');
240 if self.reqtype in ("ogg","ogv"):
241 self.send_header("Content-Type", "video/ogg")
243 self.send_header("Content-Type", "video/mpeg")
245 # Do not send real headers at all
246 self.headerssent = True
249 self.hanggreenlet = gevent.spawn(self.hangDetector)
250 logger.debug("hangDetector spawned")
254 self.errorhappened = False
257 logger.debug("Got url " + self.path_unquoted)
258 # Force ffmpeg demuxing if set in config
259 if VPConfig.vlcforceffmpeg:
260 self.vlcprefix = 'http/ffmpeg://'
264 logger.info("Starting broadcasting "+self.path)
265 VPStuff.vlcclient.startBroadcast(
266 self.vlcid, self.vlcprefix + self.path_unquoted, VPConfig.vlcmux, VPConfig.vlcpreaccess, self.reqtype)
267 # Sleep a bit, because sometimes VLC doesn't open port in
271 # Building new VLC url
272 self.url = 'http://' + VPConfig.vlchost + \
273 ':' + str(VPConfig.vlcoutport) + '/' + self.vlcid
274 logger.debug("VLC url " + self.url)
276 # Sending client headers to videostream
277 self.video = urllib2.Request(self.url)
278 for key in self.headers.dict:
279 self.video.add_header(key, self.headers.dict[key])
281 self.video = urllib2.urlopen(self.video)
283 # Sending videostream headers to client
284 if not self.headerssent:
285 self.send_response(self.video.getcode())
286 if self.video.info().dict.has_key('connection'):
287 del self.video.info().dict['connection']
288 if self.video.info().dict.has_key('server'):
289 del self.video.info().dict['server']
290 if self.video.info().dict.has_key('transfer-encoding'):
291 del self.video.info().dict['transfer-encoding']
292 if self.video.info().dict.has_key('content-type'):
293 del self.video.info().dict['content-type']
294 if self.video.info().dict.has_key('keep-alive'):
295 del self.video.info().dict['keep-alive']
297 for key in self.video.info().dict:
298 self.send_header(key, self.video.info().dict[key])
300 self.send_header('Cache-Control','no-cache');
302 if self.reqtype=="ogg":
303 self.send_header("Content-Type", "video/ogg")
305 self.send_header("Content-Type", "video/mpeg")
307 # End headers. Next goes video data
309 logger.debug("Headers sent")
312 self.proxyReadWrite()
314 # Waiting until hangDetector is joined
315 self.hanggreenlet.join()
316 logger.debug("Request handler finished")
318 except (vpclient.VPException, vlcclient.VlcException, urllib2.URLError) as e:
319 logger.error("Exception: " + repr(e))
320 self.errorhappened = True
322 except gevent.GreenletExit:
323 # hangDetector told us about client disconnection
327 logger.error(traceback.format_exc())
328 self.errorhappened = True
331 logger.debug("END REQUEST")
332 logger.info("Closed connection from " + self.clientip + " path " + self.path)
333 VPStuff.clientcounter.delete(self.reqtype+'\\'+self.path_unquoted, self.clientip)
334 if not VPStuff.clientcounter.get(self.reqtype+'\\'+self.path_unquoted):
336 logger.debug("That was the last client, destroying VPClient")
337 logger.info("Stopping broadcasting " + self.path)
338 VPStuff.vlcclient.stopBroadcast(self.vlcid)
342 if not self.headersent:
343 logger.error("Problem receiving video stream, no headers!")
344 if VPStuff.clientcounter.total == 0:
345 logger.error("Probably VLC hang")
348 class HTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
350 def handle_error(self, request, client_address):
351 # Do not print HTTP tracebacks
355 class VPStuff(object):
357 Inter-class interaction class
361 # taken from http://stackoverflow.com/questions/2699907/dropping-root-permissions-in-python
362 def drop_privileges(uid_name, gid_name='nogroup'):
364 # Get the uid/gid from the name
365 running_uid = pwd.getpwnam(uid_name).pw_uid
366 running_uid_home = pwd.getpwnam(uid_name).pw_dir
367 running_gid = grp.getgrnam(gid_name).gr_gid
369 # Remove group privileges
372 # Try setting the new uid/gid
373 os.setgid(running_gid)
374 os.setuid(running_uid)
376 # Ensure a very conservative umask
377 old_umask = os.umask(077)
379 if os.getuid() == running_uid and os.getgid() == running_gid:
381 os.environ['HOME'] = running_uid_home
386 filename=VPConfig.logpath + 'vphttp.log' if VPConfig.loggingtoafile else None,
387 format='%(asctime)s %(levelname)s %(name)s: %(message)s', datefmt='%d.%m.%Y %H:%M:%S', level=VPConfig.debug)
388 logger = logging.getLogger('INIT')
391 # Trying to change dir (would fail in freezed state)
393 os.chdir(os.path.dirname(os.path.realpath(__file__)))
396 # Creating dict of handlers
397 VPStuff.pluginshandlers = dict()
398 # And a list with plugin instances
399 VPStuff.pluginlist = list()
400 pluginsmatch = glob.glob('plugins/*_plugin.py')
401 sys.path.insert(0, 'plugins')
402 pluginslist = [os.path.splitext(os.path.basename(x))[0] for x in pluginsmatch]
403 for i in pluginslist:
404 plugin = __import__(i)
405 plugname = i.split('_')[0].capitalize()
407 plugininstance = getattr(plugin, plugname)(VPConfig, VPStuff)
408 except Exception as e:
409 logger.error("Cannot load plugin " + plugname + ": " + repr(e))
411 logger.debug('Plugin loaded: ' + plugname)
412 for j in plugininstance.handlers:
413 logger.info("Registering handler '" + j +"'")
414 VPStuff.pluginshandlers[j] = plugininstance
415 VPStuff.pluginlist.append(plugininstance)
417 # Check whether we can bind to the defined port safely
418 if os.getuid() != 0 and VPConfig.httpport <= 1024:
419 logger.error("Cannot bind to port " + str(VPConfig.httpport) + " without root privileges")
422 server = HTTPServer((VPConfig.httphost, VPConfig.httpport), HTTPHandler)
423 logger = logging.getLogger('HTTP')
425 # Dropping root privileges if needed
426 if VPConfig.vpproxyuser and os.getuid() == 0:
427 if drop_privileges(VPConfig.vpproxyuser):
428 logger.info("Dropped privileges to user " + VPConfig.vpproxyuser)
430 logger.error("Cannot drop privileges to user " + VPConfig.vpproxyuser)
433 # Creating ClientCounter
434 VPStuff.clientcounter = ClientCounter()
436 DEVNULL = open(os.devnull, 'wb')
438 # Spawning procedures
439 def spawnVLC(cmd, delay = 0):
441 VPStuff.vlc = psutil.Popen(cmd) #, stdout=DEVNULL, stderr=DEVNULL)
449 VPStuff.vlcclient = vlcclient.VlcClient(
450 host=VPConfig.vlchost, port=VPConfig.vlcport, password=VPConfig.vlcpass,
451 out_port=VPConfig.vlcoutport)
453 except vlcclient.VlcException as e:
456 def isRunning(process):
457 if psutil.version_info[0] >= 2:
458 if process.is_running() and process.status() != psutil.STATUS_ZOMBIE:
460 else: # for older versions of psutil
461 if process.is_running() and process.status != psutil.STATUS_ZOMBIE:
465 def findProcess(name):
466 for proc in psutil.process_iter():
468 pinfo = proc.as_dict(attrs=['pid', 'name'])
469 if pinfo['name'] == name:
471 except psutil.AccessDenied:
474 except psutil.NoSuchProcess:
480 # Trying to close all spawned processes gracefully
481 if isRunning(VPStuff.vlc):
482 if VPStuff.vlcclient:
483 VPStuff.vlcclient.destroy()
485 if isRunning(VPStuff.vlc):
489 # This is what we call to stop the server completely
490 def shutdown(signum = 0, frame = 0):
491 logger.info("Stopping server...")
492 # Closing all client connections
493 for connection in server.RequestHandlerClass.requestlist:
495 # Set errorhappened to prevent waiting for videodestroydelay
496 connection.errorhappened = True
497 connection.closeConnection()
499 logger.warning("Cannot kill a connection!")
501 server.server_close()
504 def _reloadconfig(signum=None, frame=None):
506 Reload configuration file.
511 logger = logging.getLogger('reloadconfig')
513 from vpconfig import VPConfig
514 logger.info('Config reloaded')
516 # setting signal handlers
518 gevent.signal(signal.SIGHUP, _reloadconfig)
519 gevent.signal(signal.SIGTERM, shutdown)
520 except AttributeError:
524 VPStuff.vlcProc = VPConfig.vlccmd.split()
525 if spawnVLC(VPStuff.vlcProc, VPConfig.vlcspawntimeout) and connectVLC():
526 logger.info("VLC spawned with pid " + str(VPStuff.vlc.pid))
528 logger.error('Cannot spawn or connect to VLC!')
533 logger.info("Using gevent %s" % gevent.__version__)
534 logger.info("Using psutil %s" % psutil.__version__)
535 logger.info("Using VLC %s" % VPStuff.vlcclient._vlcver)
536 logger.info("Server started.")
538 if not isRunning(VPStuff.vlc):
540 if spawnVLC(VPStuff.vlcProc, VPConfig.vlcspawntimeout) and connectVLC():
541 logger.info("VLC died, respawned it with pid " + str(VPStuff.vlc.pid))
543 logger.error("Cannot spawn VLC!")
546 # Return to our server tasks
547 server.handle_request()
548 except (KeyboardInterrupt, SystemExit):