From c65fdd8ebbd745ba03e5477eb317343aace3ebba Mon Sep 17 00:00:00 2001 From: Roman Bazalevsky Date: Tue, 3 Nov 2015 11:55:26 +0300 Subject: [PATCH 1/1] =?utf8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?utf8?q?=D0=BD=D0=B0=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA?= =?utf8?q?=D0=B0=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BA=D0=BE?= =?utf8?q?=D0=B2=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=B0=20X-Forwarde?= =?utf8?q?d...=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=20?= =?utf8?q?=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=20=D1=81=D1=82=D0=B0=D1=82=D0=B8?= =?utf8?q?=D1=81=D1=82=D0=B8=D0=BA=D0=B8=20=D1=81=D0=BE=D0=B5=D0=B4=D0=B8?= =?utf8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D0=B9.=20=D0=A3=D0=B1=D1=80=D0=B0=D0=BD?= =?utf8?q?=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B8=D0=B9=20=D0=BE=D1=82=D0=BB?= =?utf8?q?=D0=B0=D0=B4=D0=BE=D1=87=D0=BD=D1=8B=D0=B9=20=D0=B2=D1=8B=D0=B2?= =?utf8?q?=D0=BE=D0=B4=20=D0=B2=20stdout.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- plugins/stat_plugin.py | 9 +++++++-- vlcclient/vlcclient.py | 2 -- vpconfig.py | 8 ++++---- vphttp.py | 34 +++++++++++++++------------------- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/plugins/stat_plugin.py b/plugins/stat_plugin.py index 1cf9b5e..99e6e0a 100644 --- a/plugins/stat_plugin.py +++ b/plugins/stat_plugin.py @@ -21,7 +21,12 @@ class Stat(VPProxyPlugin): '

Connected clients: ' + str(self.stuff.clientcounter.total) + '

') connection.wfile.write( '
Concurrent connections limit: ' + str(self.config.maxconns) + '
') + connection.wfile.write('') for i in self.stuff.clientcounter.clients: - connection.wfile.write(str(i) + ' : ' + str(self.stuff.clientcounter.clients[i][0]) + ' ' + - str(self.stuff.clientcounter.clients[i][1]) + '
') + connection.wfile.write('') + connection.wfile.write('') + connection.wfile.write('
urlcountclients
' + str(i) + ' ' + str(self.stuff.clientcounter.clients[i][0]) + '') + for client in self.stuff.clientcounter.clients[i][1]: + connection.wfile.write(str(client) + '
') + connection.wfile.write('
') connection.wfile.write('') diff --git a/vlcclient/vlcclient.py b/vlcclient/vlcclient.py index 80fc0f7..1fa5e43 100644 --- a/vlcclient/vlcclient.py +++ b/vlcclient/vlcclient.py @@ -9,7 +9,6 @@ import telnetlib import logging from vlcmessages import * import time -from pprint import pprint class VlcException(Exception): @@ -181,7 +180,6 @@ class VlcClient(object): self._resultlock.acquire() to_stop=set() for stream,lasttime in self.streams.iteritems(): - print stream,lasttime if time.time()-lasttime>timeout: to_stop.add(stream) for stream in to_stop: diff --git a/vpconfig.py b/vpconfig.py index 97d24fb..f26c8ee 100644 --- a/vpconfig.py +++ b/vpconfig.py @@ -8,7 +8,7 @@ import logging class VPConfig(): # Message level (DEBUG, INFO, WARNING, ERROR, CRITICAL) - debug = logging.INFO + debug = logging.WARNING # HTTP Server host httphost = '0.0.0.0' # HTTP Server port @@ -39,7 +39,7 @@ class VPConfig(): # ---------------------------------------------------- # # VLC cmd line (use `--file-logging --logfile=filepath` to write log) - vlccmd = "vlc -I telnet --clock-jitter 0 --network-caching 500 --sout-mux-caching 2000 --telnet-password admin --telnet-port 4212" + vlccmd = "cvlc -I telnet --clock-jitter 0 --network-caching 500 --sout-mux-caching 2000 --telnet-password admin --telnet-port 4212 --http-user-agent=\"SmartLabs/1.5.9\"" # VLC spawn timeout # Adjust this if you get error 'Cannot spawn VLC!' vlcspawntimeout = 15 @@ -66,9 +66,9 @@ class VPConfig(): vlcforceffmpeg = False # Delay before closing connection when client disconnects # In seconds. - videodestroydelay = 5 + videodestroydelay = 30 # Pre-buffering timeout. In seconds. - videotimeout = 30 + videotimeout = 45 # # Some video players (mostly STBs and Smart TVs) can generate dummy requests # to detect MIME-type or something before playing. diff --git a/vphttp.py b/vphttp.py index 7ed52f7..040e94c 100644 --- a/vphttp.py +++ b/vphttp.py @@ -138,6 +138,7 @@ class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): ''' GET request handler ''' + logger = logging.getLogger('http_HTTPHandler') self.clientconnected = True # Don't wait videodestroydelay if error happened @@ -149,6 +150,14 @@ class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): # Connected client IP address self.clientip = self.request.getpeername()[0] + req_headers = self.headers + self.client_data = { + 'ip': self.clientip, + 'forwarded-for': req_headers.get('X-Forwarded-For'), + 'client-agent': req_headers.get('User-Agent'), + 'uuid': uuid.uuid4() + } + if VPConfig.firewall: # If firewall enabled self.clientinrange = any(map(lambda i: ipaddr.IPAddress(self.clientip) \ @@ -190,8 +199,6 @@ class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): def handleRequest(self, headers_only): - self.unic = uuid.uuid4() - # Limit concurrent connections if 0 < VPConfig.maxconns <= VPStuff.clientcounter.total: logger.debug("Maximum connections reached, can't serve this") @@ -222,19 +229,19 @@ class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.params.append('0') # Adding client to clientcounter - clients = VPStuff.clientcounter.add(self.reqtype+'\\'+self.path_unquoted, self.clientip, self.unic) + clients = VPStuff.clientcounter.add(self.reqtype+'/'+self.path_unquoted, self.client_data) # If we are the one client, but sucessfully got vp instance from clientcounter, # then somebody is waiting in the videodestroydelay state # Check if we are first client - if VPStuff.clientcounter.get(self.reqtype+'\\'+self.path_unquoted)==1: + if VPStuff.clientcounter.get(self.reqtype+'/'+self.path_unquoted)==1: logger.debug("First client, should create VLC session") shouldcreatevp = True else: logger.debug("Can reuse existing session") shouldcreatevp = False - self.vlcid = hashlib.md5(self.reqtype+'\\'+self.path_unquoted).hexdigest() + self.vlcid = hashlib.md5(self.reqtype+'/'+self.path_unquoted).hexdigest() # Send fake headers if this User-Agent is in fakeheaderuas tuple if fakeua: @@ -338,19 +345,7 @@ class HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): finally: logger.debug("END REQUEST") logger.info("Closed connection from " + self.clientip + " path " + self.path) - VPStuff.clientcounter.delete(self.reqtype+'\\'+self.path_unquoted, self.clientip, self.unic) -# if not VPStuff.clientcounter.get(self.reqtype+'\\'+self.path_unquoted): -# try: -# logger.debug("That was the last client, destroying VPClient") -# logger.info("Stopping broadcasting " + self.path) -# VPStuff.vlcclient.stopBroadcast(self.vlcid) -# except vlcclient.VlcException: -# logger.error("VLC connection problem, %s client(s)!" % VPStuff.clientcounter.total) -# if VPStuff.clientcounter.total == 0: -# logger.error("Probably VLC hang") -# VPStuff.vlc.kill() -# except: -# pass + VPStuff.clientcounter.delete(self.reqtype+'/'+self.path_unquoted, self.client_data) self.vp.destroy() class HTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): @@ -525,7 +520,7 @@ sched = BackgroundScheduler() sched.start() def clean_streams(): - VPStuff.vlcclient.clean_streams(15) + VPStuff.vlcclient.clean_streams(VPConfig.videodestroydelay) job = sched.add_job(clean_streams, 'interval', seconds=15) @@ -562,4 +557,5 @@ try: # Return to our server tasks server.handle_request() except (KeyboardInterrupt, SystemExit): + sched.shutdown() shutdown() -- 2.34.1