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
13 gevent.monkey.patch_all()
22 from socket import error as SocketException
23 from socket import SHUT_RDWR
27 from vpconfig import VPConfig
29 import plugins.modules.ipaddr as ipaddr
30 from clientcounter import ClientCounter
31 from plugins.modules.PluginInterface import VPProxyPlugin
40 class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
44 def handle_one_request(self):
46 Add request to requestlist, handle request and remove from the list
48 HTTPHandler.requestlist.append(self)
49 BaseHTTPServer.BaseHTTPRequestHandler.handle_one_request(self)
50 HTTPHandler.requestlist.remove(self)
52 def closeConnection(self):
56 if self.clientconnected:
57 self.clientconnected = False
61 self.connection.shutdown(SHUT_RDWR)
65 def dieWithError(self, errorcode=500):
67 Close connection with error
69 logging.warning("Dying with error")
70 if self.clientconnected:
71 self.send_error(errorcode)
73 self.closeConnection()
75 def proxyReadWrite(self):
77 Read video stream and send it to client
79 logger = logging.getLogger('http_proxyReadWrite')
80 logger.debug("Started")
83 self.streamstate = True
88 if not self.clientconnected:
89 logger.debug("Client is not connected, terminating")
92 data = self.video.read(4096)
93 if data and self.clientconnected:
94 self.wfile.write(data)
96 logger.warning("Video connection closed")
99 except SocketException:
100 # Video connection dropped
101 logger.warning("Video connection dropped")
104 self.closeConnection()
106 def hangDetector(self):
108 Detect client disconnection while in the middle of something
109 or just normal connection close.
111 logger = logging.getLogger('http_hangDetector')
114 if not self.rfile.read():
119 self.clientconnected = False
120 logger.debug("Client disconnected")
122 self.requestgreenlet.kill()
130 return self.do_GET(headers_only=True)
132 def do_GET(self, headers_only=False):
136 logger = logging.getLogger('http_HTTPHandler')
137 self.clientconnected = True
138 # Don't wait videodestroydelay if error happened
139 self.errorhappened = True
140 # Headers sent flag for fake headers UAs
141 self.headerssent = False
143 self.requestgreenlet = gevent.getcurrent()
144 # Connected client IP address
145 self.clientip = self.request.getpeername()[0]
147 if VPConfig.firewall:
148 # If firewall enabled
149 self.clientinrange = any(map(lambda i: ipaddr.IPAddress(self.clientip) \
150 in ipaddr.IPNetwork(i), VPConfig.firewallnetranges))
152 if (VPConfig.firewallblacklistmode and self.clientinrange) or \
153 (not VPConfig.firewallblacklistmode and not self.clientinrange):
154 logger.info('Dropping connection from ' + self.clientip + ' due to ' + \
156 self.dieWithError(403) # 403 Forbidden
159 logger.info("Accepted connection from " + self.clientip + " path " + self.path)
162 self.splittedpath = self.path.split('/')
163 self.reqtype = self.splittedpath[1].lower()
164 # If first parameter is 'pid' or 'torrent' or it should be handled
166 if not (self.reqtype in ('get','mp4') or self.reqtype in VPStuff.pluginshandlers):
167 self.dieWithError(400) # 400 Bad Request
170 self.dieWithError(400) # 400 Bad Request
173 # Handle request with plugin handler
174 if self.reqtype in VPStuff.pluginshandlers:
176 VPStuff.pluginshandlers.get(self.reqtype).handle(self)
177 except Exception as e:
178 logger.error('Plugin exception: ' + repr(e))
179 logger.error(traceback.format_exc())
182 self.closeConnection()
184 self.handleRequest(headers_only)
186 def handleRequest(self, headers_only):
188 # Limit concurrent connections
189 print VPStuff.clientcounter.total
190 if 0 < VPConfig.maxconns <= VPStuff.clientcounter.total:
191 logger.debug("Maximum connections reached, can't serve this")
192 self.dieWithError(503) # 503 Service Unavailable
195 # Pretend to work fine with Fake UAs or HEAD request.
196 useragent = self.headers.get('User-Agent')
197 logger.debug("HTTP User Agent:"+useragent)
198 fakeua = useragent and useragent in VPConfig.fakeuas
199 if headers_only or fakeua:
201 logger.debug("Got fake UA: " + self.headers.get('User-Agent'))
202 # Return 200 and exit
203 self.send_response(200)
204 self.send_header("Content-Type", "video/mpeg")
206 self.closeConnection()
209 self.path_unquoted = urllib2.unquote('/'.join(self.splittedpath[2:]))
210 # Make list with parameters
212 for i in xrange(3, 8):
214 self.params.append(int(self.splittedpath[i]))
215 except (IndexError, ValueError):
216 self.params.append('0')
218 # Adding client to clientcounter
219 clients = VPStuff.clientcounter.add(self.reqtype+'\\'+self.path_unquoted, self.clientip)
220 # If we are the one client, but sucessfully got vp instance from clientcounter,
221 # then somebody is waiting in the videodestroydelay state
223 # Check if we are first client
224 if VPStuff.clientcounter.get(self.reqtype+'\\'+self.path_unquoted)==1:
225 logger.debug("First client, should create VLC session")
226 shouldcreatevp = True
228 logger.debug("Can reuse existing session")
229 shouldcreatevp = False
231 self.vlcid = hashlib.md5(self.reqtype+'\\'+self.path_unquoted).hexdigest()
233 # Send fake headers if this User-Agent is in fakeheaderuas tuple
236 "Sending fake headers for " + useragent)
237 self.send_response(200)
238 self.send_header("Content-Type", "video/mpeg")
240 # Do not send real headers at all
241 self.headerssent = True
244 self.hanggreenlet = gevent.spawn(self.hangDetector)
245 logger.debug("hangDetector spawned")
249 self.errorhappened = False
252 logger.debug("Got url " + self.path_unquoted)
253 # Force ffmpeg demuxing if set in config
254 if VPConfig.vlcforceffmpeg:
255 self.vlcprefix = 'http/ffmpeg://'
259 logger.debug("Ready to start broadcast....")
260 VPStuff.vlcclient.startBroadcast(
261 self.vlcid, self.vlcprefix + self.path_unquoted, VPConfig.vlcmux, VPConfig.vlcpreaccess, self.reqtype)
262 # Sleep a bit, because sometimes VLC doesn't open port in
266 # Building new VLC url
267 self.url = 'http://' + VPConfig.vlchost + \
268 ':' + str(VPConfig.vlcoutport) + '/' + self.vlcid
269 logger.debug("VLC url " + self.url)
271 # Sending client headers to videostream
272 self.video = urllib2.Request(self.url)
273 for key in self.headers.dict:
274 self.video.add_header(key, self.headers.dict[key])
276 self.video = urllib2.urlopen(self.video)
278 # Sending videostream headers to client
279 if not self.headerssent:
280 self.send_response(self.video.getcode())
281 if self.video.info().dict.has_key('connection'):
282 del self.video.info().dict['connection']
283 if self.video.info().dict.has_key('server'):
284 del self.video.info().dict['server']
285 if self.video.info().dict.has_key('transfer-encoding'):
286 del self.video.info().dict['transfer-encoding']
287 if self.video.info().dict.has_key('keep-alive'):
288 del self.video.info().dict['keep-alive']
290 for key in self.video.info().dict:
291 self.send_header(key, self.video.info().dict[key])
292 # End headers. Next goes video data
294 logger.debug("Headers sent")
297 self.proxyReadWrite()
299 # Waiting until hangDetector is joined
300 self.hanggreenlet.join()
301 logger.debug("Request handler finished")
303 except (vpclient.VPException, vlcclient.VlcException, urllib2.URLError) as e:
304 logger.error("Exception: " + repr(e))
305 self.errorhappened = True
307 except gevent.GreenletExit:
308 # hangDetector told us about client disconnection
312 logger.error(traceback.format_exc())
313 self.errorhappened = True
316 logger.debug("END REQUEST")
317 VPStuff.clientcounter.delete(self.reqtype+'\\'+self.path_unquoted, self.clientip)
318 if not VPStuff.clientcounter.get(self.reqtype+'\\'+self.path_unquoted):
320 logger.debug("That was the last client, destroying VPClient")
321 VPStuff.vlcclient.stopBroadcast(self.vlcid)
327 class HTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
329 def handle_error(self, request, client_address):
330 # Do not print HTTP tracebacks
334 class VPStuff(object):
336 Inter-class interaction class
340 # taken from http://stackoverflow.com/questions/2699907/dropping-root-permissions-in-python
341 def drop_privileges(uid_name, gid_name='nogroup'):
343 # Get the uid/gid from the name
344 running_uid = pwd.getpwnam(uid_name).pw_uid
345 running_uid_home = pwd.getpwnam(uid_name).pw_dir
346 running_gid = grp.getgrnam(gid_name).gr_gid
348 # Remove group privileges
351 # Try setting the new uid/gid
352 os.setgid(running_gid)
353 os.setuid(running_uid)
355 # Ensure a very conservative umask
356 old_umask = os.umask(077)
358 if os.getuid() == running_uid and os.getgid() == running_gid:
360 os.environ['HOME'] = running_uid_home
365 filename=VPConfig.logpath + 'vphttp.log' if VPConfig.loggingtoafile else None,
366 format='%(asctime)s %(levelname)s %(name)s: %(message)s', datefmt='%d.%m.%Y %H:%M:%S', level=VPConfig.debug)
367 logger = logging.getLogger('INIT')
370 # Trying to change dir (would fail in freezed state)
372 os.chdir(os.path.dirname(os.path.realpath(__file__)))
375 # Creating dict of handlers
376 VPStuff.pluginshandlers = dict()
377 # And a list with plugin instances
378 VPStuff.pluginlist = list()
379 pluginsmatch = glob.glob('plugins/*_plugin.py')
380 sys.path.insert(0, 'plugins')
381 pluginslist = [os.path.splitext(os.path.basename(x))[0] for x in pluginsmatch]
382 for i in pluginslist:
383 plugin = __import__(i)
384 plugname = i.split('_')[0].capitalize()
386 plugininstance = getattr(plugin, plugname)(VPConfig, VPStuff)
387 except Exception as e:
388 logger.error("Cannot load plugin " + plugname + ": " + repr(e))
390 logger.debug('Plugin loaded: ' + plugname)
391 for j in plugininstance.handlers:
392 VPStuff.pluginshandlers[j] = plugininstance
393 VPStuff.pluginlist.append(plugininstance)
395 # Check whether we can bind to the defined port safely
396 if os.getuid() != 0 and VPConfig.httpport <= 1024:
397 logger.error("Cannot bind to port " + str(VPConfig.httpport) + " without root privileges")
400 server = HTTPServer((VPConfig.httphost, VPConfig.httpport), HTTPHandler)
401 logger = logging.getLogger('HTTP')
403 # Dropping root privileges if needed
404 if VPConfig.vpproxyuser and os.getuid() == 0:
405 if drop_privileges(VPConfig.vpproxyuser):
406 logger.info("Dropped privileges to user " + VPConfig.vpproxyuser)
408 logger.error("Cannot drop privileges to user " + VPConfig.vpproxyuser)
411 # Creating ClientCounter
412 VPStuff.clientcounter = ClientCounter()
414 DEVNULL = open(os.devnull, 'wb')
416 # Spawning procedures
417 def spawnVLC(cmd, delay = 0):
419 VPStuff.vlc = psutil.Popen(cmd) #, stdout=DEVNULL, stderr=DEVNULL)
427 VPStuff.vlcclient = vlcclient.VlcClient(
428 host=VPConfig.vlchost, port=VPConfig.vlcport, password=VPConfig.vlcpass,
429 out_port=VPConfig.vlcoutport)
431 except vlcclient.VlcException as e:
434 def isRunning(process):
435 if psutil.version_info[0] >= 2:
436 if process.is_running() and process.status() != psutil.STATUS_ZOMBIE:
438 else: # for older versions of psutil
439 if process.is_running() and process.status != psutil.STATUS_ZOMBIE:
443 def findProcess(name):
444 for proc in psutil.process_iter():
446 pinfo = proc.as_dict(attrs=['pid', 'name'])
447 if pinfo['name'] == name:
449 except psutil.AccessDenied:
452 except psutil.NoSuchProcess:
458 # Trying to close all spawned processes gracefully
459 if isRunning(VPStuff.vlc):
460 if VPStuff.vlcclient:
461 VPStuff.vlcclient.destroy()
463 if isRunning(VPStuff.vlc):
467 # This is what we call to stop the server completely
468 def shutdown(signum = 0, frame = 0):
469 logger.info("Stopping server...")
470 # Closing all client connections
471 for connection in server.RequestHandlerClass.requestlist:
473 # Set errorhappened to prevent waiting for videodestroydelay
474 connection.errorhappened = True
475 connection.closeConnection()
477 logger.warning("Cannot kill a connection!")
479 server.server_close()
482 def _reloadconfig(signum=None, frame=None):
484 Reload configuration file.
489 logger = logging.getLogger('reloadconfig')
491 from vpconfig import VPConfig
492 logger.info('Config reloaded')
494 # setting signal handlers
496 gevent.signal(signal.SIGHUP, _reloadconfig)
497 gevent.signal(signal.SIGTERM, shutdown)
498 except AttributeError:
502 VPStuff.vlcProc = VPConfig.vlccmd.split()
503 if spawnVLC(VPStuff.vlcProc, VPConfig.vlcspawntimeout) and connectVLC():
504 logger.info("VLC spawned with pid " + str(VPStuff.vlc.pid))
506 logger.error('Cannot spawn or connect to VLC!')
511 logger.info("Using gevent %s" % gevent.__version__)
512 logger.info("Using psutil %s" % psutil.__version__)
513 logger.info("Using VLC %s" % VPStuff.vlcclient._vlcver)
514 logger.info("Server started.")
516 if not isRunning(VPStuff.vlc):
518 if spawnVLC(VPStuff.vlcProc, VPConfig.vlcspawntimeout) and connectVLC():
519 logger.info("VLC died, respawned it with pid " + str(VPStuff.vlc.pid))
521 logger.error("Cannot spawn VLC!")
524 # Return to our server tasks
525 server.handle_request()
526 except (KeyboardInterrupt, SystemExit):