<s> """<STR_LIT>""" <EOL> import BaseHTTPServer <EOL> import cStringIO <EOL> import datetime <EOL> import httplib <EOL> import glob <EOL> import gzip <EOL> import hashlib <EOL> import io <EOL> import json <EOL> import mimetypes <EOL> import os <EOL> import re <EOL> import socket <EOL> import SocketServer <EOL> import subprocess <EOL> import threading <EOL> import time <EOL> import traceback <EOL> import urllib <EOL> import urlparse <EOL> from core . addr import addr_to_int <EOL> from core . addr import int_to_addr <EOL> from core . addr import make_mask <EOL> from core . attribdict import AttribDict <EOL> from core . common import get_regex <EOL> from core . common import ipcat_lookup <EOL> from core . common import worst_asns <EOL> from core . enums import HTTP_HEADER <EOL> from core . settings import config <EOL> from core . settings import CONTENT_EXTENSIONS_EXCLUSIONS <EOL> from core . settings import DATE_FORMAT <EOL> from core . settings import DISABLED_CONTENT_EXTENSIONS <EOL> from core . settings import DISPOSED_NONCES <EOL> from core . settings import HTML_DIR <EOL> from core . settings import HTTP_TIME_FORMAT <EOL> from core . settings import MAX_NOFILE <EOL> from core . settings import NAME <EOL> from core . settings import PING_RESPONSE <EOL> from core . settings import SERVER_HEADER <EOL> from core . settings import SESSION_COOKIE_NAME <EOL> from core . settings import SESSION_EXPIRATION_HOURS <EOL> from core . settings import SESSION_ID_LENGTH <EOL> from core . settings import SESSIONS <EOL> from core . settings import TRAILS_FILE <EOL> from core . settings import UNAUTHORIZED_SLEEP_TIME <EOL> from core . settings import VERSION <EOL> try : <EOL> import _strptime <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> import resource <EOL> resource . setrlimit ( resource . RLIMIT_NOFILE , ( MAX_NOFILE , MAX_NOFILE ) ) <EOL> except : <EOL> pass <EOL> def start_httpd ( address = None , port = None , join = False , pem = None ) : <EOL> """<STR_LIT>""" <EOL> class ThreadingServer ( SocketServer . ThreadingMixIn , BaseHTTPServer . HTTPServer ) : <EOL> def server_bind ( self ) : <EOL> self . socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> BaseHTTPServer . HTTPServer . server_bind ( self ) <EOL> def finish_request ( self , * args , ** kwargs ) : <EOL> try : <EOL> BaseHTTPServer . HTTPServer . finish_request ( self , * args , ** kwargs ) <EOL> except : <EOL> if config . SHOW_DEBUG : <EOL> traceback . print_exc ( ) <EOL> class SSLThreadingServer ( ThreadingServer ) : <EOL> def __init__ ( self , server_address , pem , HandlerClass ) : <EOL> import OpenSSL <EOL> ThreadingServer . __init__ ( self , server_address , HandlerClass ) <EOL> ctx = OpenSSL . SSL . Context ( OpenSSL . SSL . TLSv1_METHOD ) <EOL> ctx . use_privatekey_file ( pem ) <EOL> ctx . use_certificate_file ( pem ) <EOL> self . socket = OpenSSL . SSL . Connection ( ctx , socket . socket ( self . address_family , self . socket_type ) ) <EOL> self . server_bind ( ) <EOL> self . server_activate ( ) <EOL> def shutdown_request ( self , request ) : <EOL> try : <EOL> request . shutdown ( ) <EOL> except : <EOL> if config . SHOW_DEBUG : <EOL> traceback . print_exc ( ) <EOL> class ReqHandler ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> def do_GET ( self ) : <EOL> path , query = self . path . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) if '<STR_LIT:?>' in self . path else ( self . path , "<STR_LIT>" ) <EOL> params = { } <EOL> content = None <EOL> skip = False <EOL> if hasattr ( self , "<STR_LIT:data>" ) : <EOL> params . update ( urlparse . parse_qs ( self . data ) ) <EOL> if query : <EOL> params . update ( urlparse . parse_qs ( query ) ) <EOL> for key in params : <EOL> if params [ key ] : <EOL> params [ key ] = params [ key ] [ - <NUM_LIT:1> ] <EOL> if path == '<STR_LIT:/>' : <EOL> path = "<STR_LIT>" <EOL> path = path . strip ( '<STR_LIT:/>' ) <EOL> extension = os . path . splitext ( path ) [ - <NUM_LIT:1> ] . lower ( ) <EOL> if hasattr ( self , "<STR_LIT>" % path ) : <EOL> content = getattr ( self , "<STR_LIT>" % path ) ( params ) <EOL> else : <EOL> path = path . replace ( '<STR_LIT:/>' , os . path . sep ) <EOL> path = os . path . abspath ( os . path . join ( HTML_DIR , path ) ) . strip ( ) <EOL> if not os . path . isfile ( path ) and os . path . isfile ( "<STR_LIT>" % path ) : <EOL> path = "<STR_LIT>" % path <EOL> if "<STR_LIT:..>" not in os . path . relpath ( path , HTML_DIR ) and os . path . isfile ( path ) and ( extension not in DISABLED_CONTENT_EXTENSIONS or os . path . split ( path ) [ - <NUM_LIT:1> ] in CONTENT_EXTENSIONS_EXCLUSIONS ) : <EOL> mtime = time . gmtime ( os . path . getmtime ( path ) ) <EOL> if_modified_since = self . headers . get ( HTTP_HEADER . IF_MODIFIED_SINCE ) <EOL> if if_modified_since and extension not in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if_modified_since = [ _ for _ in if_modified_since . split ( '<STR_LIT:;>' ) if _ . upper ( ) . endswith ( "<STR_LIT>" ) ] [ <NUM_LIT:0> ] <EOL> if time . mktime ( mtime ) <= time . mktime ( time . strptime ( if_modified_since , HTTP_TIME_FORMAT ) ) : <EOL> self . send_response ( httplib . NOT_MODIFIED ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> skip = True <EOL> if not skip : <EOL> content = open ( path , "<STR_LIT:rb>" ) . read ( ) <EOL> last_modified = time . strftime ( HTTP_TIME_FORMAT , mtime ) <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , mimetypes . guess_type ( path ) [ <NUM_LIT:0> ] or "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . LAST_MODIFIED , last_modified ) <EOL> if extension not in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> self . send_header ( HTTP_HEADER . EXPIRES , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CACHE_CONTROL , "<STR_LIT>" ) <EOL> else : <EOL> self . send_header ( HTTP_HEADER . CACHE_CONTROL , "<STR_LIT>" ) <EOL> else : <EOL> self . send_response ( httplib . NOT_FOUND ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> content = '<STR_LIT>' % self . path . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] <EOL> if content is not None : <EOL> for match in re . finditer ( r"<STR_LIT>" , content ) : <EOL> name = match . group ( <NUM_LIT:1> ) <EOL> _ = getattr ( self , "<STR_LIT>" % name . lower ( ) , None ) <EOL> if _ : <EOL> content = self . _format ( content , ** { name : _ ( ) } ) <EOL> if "<STR_LIT>" in self . headers . getheader ( HTTP_HEADER . ACCEPT_ENCODING , "<STR_LIT>" ) : <EOL> self . send_header ( HTTP_HEADER . CONTENT_ENCODING , "<STR_LIT>" ) <EOL> _ = cStringIO . StringIO ( ) <EOL> compress = gzip . GzipFile ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:9> , _ ) <EOL> compress . _stream = _ <EOL> compress . write ( content ) <EOL> compress . flush ( ) <EOL> compress . close ( ) <EOL> content = compress . _stream . getvalue ( ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_LENGTH , str ( len ( content ) ) ) <EOL> self . end_headers ( ) <EOL> if content : <EOL> self . wfile . write ( content ) <EOL> self . wfile . flush ( ) <EOL> self . wfile . close ( ) <EOL> def do_POST ( self ) : <EOL> length = self . headers . getheader ( HTTP_HEADER . CONTENT_LENGTH ) <EOL> data = self . rfile . read ( int ( length ) ) <EOL> data = urllib . unquote_plus ( data ) <EOL> self . data = data <EOL> self . do_GET ( ) <EOL> def get_session ( self ) : <EOL> retval = None <EOL> cookie = self . headers . get ( HTTP_HEADER . COOKIE ) <EOL> if cookie : <EOL> match = re . search ( r"<STR_LIT>" % SESSION_COOKIE_NAME , cookie ) <EOL> if match : <EOL> session = match . group ( <NUM_LIT:1> ) <EOL> if session in SESSIONS : <EOL> if SESSIONS [ session ] . client_ip != self . client_address [ <NUM_LIT:0> ] : <EOL> pass <EOL> elif SESSIONS [ session ] . expiration > time . time ( ) : <EOL> retval = SESSIONS [ session ] <EOL> else : <EOL> del SESSIONS [ session ] <EOL> return retval <EOL> def delete_session ( self ) : <EOL> cookie = self . headers . get ( HTTP_HEADER . COOKIE ) <EOL> if cookie : <EOL> match = re . search ( r"<STR_LIT>" % SESSION_COOKIE_NAME , cookie ) <EOL> if match : <EOL> session = match . group ( <NUM_LIT:1> ) <EOL> if session in SESSIONS : <EOL> del SESSIONS [ session ] <EOL> def version_string ( self ) : <EOL> return SERVER_HEADER <EOL> def end_headers ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> BaseHTTPServer . BaseHTTPRequestHandler . end_headers ( self ) <EOL> self . _headers_ended = True <EOL> def log_message ( self , format , * args ) : <EOL> return <EOL> def finish ( self ) : <EOL> try : <EOL> BaseHTTPServer . BaseHTTPRequestHandler . finish ( self ) <EOL> except : <EOL> if config . SHOW_DEBUG : <EOL> traceback . print_exc ( ) <EOL> def _version ( self ) : <EOL> return VERSION <EOL> def _format ( self , content , ** params ) : <EOL> if content : <EOL> for key , value in params . items ( ) : <EOL> content = content . replace ( "<STR_LIT>" % key , value ) <EOL> return content <EOL> def _login ( self , params ) : <EOL> valid = False <EOL> if params . get ( "<STR_LIT:username>" ) and params . get ( "<STR_LIT>" ) and params . get ( "<STR_LIT>" ) : <EOL> if params . get ( "<STR_LIT>" ) not in DISPOSED_NONCES : <EOL> DISPOSED_NONCES . add ( params . get ( "<STR_LIT>" ) ) <EOL> for entry in ( config . USERS or [ ] ) : <EOL> entry = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , entry ) <EOL> username , stored_hash , uid , netfilter = entry . split ( '<STR_LIT::>' ) <EOL> if username == params . get ( "<STR_LIT:username>" ) : <EOL> try : <EOL> if params . get ( "<STR_LIT>" ) == hashlib . sha256 ( stored_hash . strip ( ) + params . get ( "<STR_LIT>" ) ) . hexdigest ( ) : <EOL> valid = True <EOL> break <EOL> except : <EOL> if config . SHOW_DEBUG : <EOL> traceback . print_exc ( ) <EOL> if valid : <EOL> session_id = os . urandom ( SESSION_ID_LENGTH ) . encode ( "<STR_LIT>" ) <EOL> expiration = time . time ( ) + <NUM_LIT> * SESSION_EXPIRATION_HOURS <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . SET_COOKIE , "<STR_LIT>" % ( SESSION_COOKIE_NAME , session_id , time . strftime ( HTTP_TIME_FORMAT , time . gmtime ( expiration ) ) ) ) <EOL> if netfilter in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> netfilters = None <EOL> else : <EOL> addresses = set ( ) <EOL> netmasks = set ( ) <EOL> for item in set ( re . split ( r"<STR_LIT>" , netfilter ) ) : <EOL> item = item . strip ( ) <EOL> if '<STR_LIT:/>' in item : <EOL> _ = item . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> if _ . isdigit ( ) and int ( _ ) >= <NUM_LIT:16> : <EOL> lower = addr_to_int ( item . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] ) <EOL> mask = make_mask ( int ( _ ) ) <EOL> upper = lower | ( <NUM_LIT> ^ mask ) <EOL> while lower <= upper : <EOL> addresses . add ( int_to_addr ( lower ) ) <EOL> lower += <NUM_LIT:1> <EOL> else : <EOL> netmasks . add ( item ) <EOL> elif '<STR_LIT:->' in item : <EOL> _ = item . split ( '<STR_LIT:->' ) <EOL> lower , upper = addr_to_int ( _ [ <NUM_LIT:0> ] ) , addr_to_int ( _ [ <NUM_LIT:1> ] ) <EOL> while lower <= upper : <EOL> addresses . add ( int_to_addr ( lower ) ) <EOL> lower += <NUM_LIT:1> <EOL> elif re . search ( r"<STR_LIT>" , item ) : <EOL> addresses . add ( item ) <EOL> netfilters = netmasks <EOL> if addresses : <EOL> netfilters . add ( get_regex ( addresses ) ) <EOL> SESSIONS [ session_id ] = AttribDict ( { "<STR_LIT:username>" : username , "<STR_LIT>" : uid , "<STR_LIT>" : netfilters , "<STR_LIT>" : expiration , "<STR_LIT>" : self . client_address [ <NUM_LIT:0> ] } ) <EOL> else : <EOL> time . sleep ( UNAUTHORIZED_SLEEP_TIME ) <EOL> self . send_response ( httplib . UNAUTHORIZED ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , "<STR_LIT>" ) <EOL> content = "<STR_LIT>" % ( "<STR_LIT:success>" if valid else "<STR_LIT>" ) <EOL> if not subprocess . mswindows : <EOL> try : <EOL> subprocess . check_output ( "<STR_LIT>" % ( NAME . lower ( ) , os . getpid ( ) , "<STR_LIT>" if valid else "<STR_LIT>" , params . get ( "<STR_LIT:username>" ) , self . client_address [ <NUM_LIT:0> ] , self . client_address [ <NUM_LIT:1> ] ) , stderr = subprocess . STDOUT , shell = True ) <EOL> except Exception : <EOL> if config . SHOW_DEBUG : <EOL> traceback . print_exc ( ) <EOL> return content <EOL> def _logout ( self , params ) : <EOL> self . delete_session ( ) <EOL> self . send_response ( httplib . FOUND ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . LOCATION , "<STR_LIT:/>" ) <EOL> def _whoami ( self , params ) : <EOL> session = self . get_session ( ) <EOL> username = session . username if session else "<STR_LIT>" <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , "<STR_LIT>" ) <EOL> return username <EOL> def _check_ip ( self , params ) : <EOL> session = self . get_session ( ) <EOL> if session is None : <EOL> self . send_response ( httplib . UNAUTHORIZED ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> return None <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , "<STR_LIT>" ) <EOL> try : <EOL> result_worst = worst_asns ( params . get ( "<STR_LIT:address>" ) ) <EOL> if result_worst : <EOL> result_ipcat = result_worst <EOL> else : <EOL> _ = ( ipcat_lookup ( params . get ( "<STR_LIT:address>" ) ) or "<STR_LIT>" ) . lower ( ) . split ( '<STR_LIT:U+0020>' ) <EOL> result_ipcat = _ [ <NUM_LIT:1> ] if _ [ <NUM_LIT:0> ] == '<STR_LIT>' else _ [ <NUM_LIT:0> ] <EOL> return ( "<STR_LIT:%s>" if not params . get ( "<STR_LIT>" ) else "<STR_LIT>" % params . get ( "<STR_LIT>" ) ) % json . dumps ( { "<STR_LIT>" : result_ipcat , "<STR_LIT>" : str ( result_worst is not None ) . lower ( ) } ) <EOL> except : <EOL> if config . SHOW_DEBUG : <EOL> traceback . print_exc ( ) <EOL> def _trails ( self , params ) : <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , "<STR_LIT>" ) <EOL> return open ( TRAILS_FILE , "<STR_LIT:rb>" ) . read ( ) <EOL> def _ping ( self , params ) : <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , "<STR_LIT>" ) <EOL> return PING_RESPONSE <EOL> def _events ( self , params ) : <EOL> session = self . get_session ( ) <EOL> if session is None : <EOL> self . send_response ( httplib . UNAUTHORIZED ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> return None <EOL> start , end , size , total = None , None , - <NUM_LIT:1> , None <EOL> content = None <EOL> event_log_path = os . path . join ( config . LOG_DIR , "<STR_LIT>" % params . get ( "<STR_LIT:date>" , "<STR_LIT>" ) ) <EOL> if os . path . exists ( event_log_path ) : <EOL> total = os . stat ( event_log_path ) . st_size <EOL> if self . headers . get ( HTTP_HEADER . RANGE ) : <EOL> match = re . search ( r"<STR_LIT>" , self . headers [ HTTP_HEADER . RANGE ] ) <EOL> if match : <EOL> start , end = int ( match . group ( <NUM_LIT:1> ) ) , int ( match . group ( <NUM_LIT:2> ) ) <EOL> max_size = end - start + <NUM_LIT:1> <EOL> end = min ( total - <NUM_LIT:1> , end ) <EOL> size = end - start + <NUM_LIT:1> <EOL> if start == <NUM_LIT:0> or not session . range_handle : <EOL> session . range_handle = open ( event_log_path , "<STR_LIT:rb>" ) <EOL> if session . netfilters is None : <EOL> session . range_handle . seek ( start ) <EOL> self . send_response ( httplib . PARTIAL_CONTENT ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_RANGE , "<STR_LIT>" % ( start , end , total ) ) <EOL> content = session . range_handle . read ( size ) <EOL> else : <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , "<STR_LIT>" ) <EOL> buffer , addresses , netmasks , regex = cStringIO . StringIO ( ) , set ( ) , [ ] , "<STR_LIT>" <EOL> for netfilter in session . netfilters : <EOL> if not netfilter : <EOL> continue <EOL> if '<STR_LIT:/>' in netfilter : <EOL> netmasks . append ( netfilter ) <EOL> elif re . search ( r"<STR_LIT>" , netfilter ) : <EOL> addresses . add ( netfilter ) <EOL> elif '<STR_LIT>' in netfilter : <EOL> regex = r"<STR_LIT>" % netfilter <EOL> else : <EOL> print "<STR_LIT>" % netfilter <EOL> return <EOL> for line in session . range_handle : <EOL> display = False <EOL> ip = None <EOL> if regex : <EOL> match = re . search ( regex , line ) <EOL> if match : <EOL> ip = match . group ( <NUM_LIT:1> ) <EOL> display = True <EOL> if not display and ( addresses or netmasks ) : <EOL> for match in re . finditer ( r"<STR_LIT>" , line ) : <EOL> if not display : <EOL> ip = match . group ( <NUM_LIT:1> ) <EOL> else : <EOL> break <EOL> if ip in addresses : <EOL> display = True <EOL> break <EOL> elif netmasks : <EOL> for _ in netmasks : <EOL> prefix , mask = _ . split ( '<STR_LIT:/>' ) <EOL> if addr_to_int ( ip ) & make_mask ( int ( mask ) ) == addr_to_int ( prefix ) : <EOL> addresses . add ( ip ) <EOL> display = True <EOL> break <EOL> if display : <EOL> if "<STR_LIT>" % ip in line or "<STR_LIT>" % ip in line : <EOL> line = re . sub ( r"<STR_LIT>" % re . escape ( ip ) , "<STR_LIT>" % ip , line ) <EOL> buffer . write ( line ) <EOL> if buffer . tell ( ) >= max_size : <EOL> break <EOL> content = buffer . getvalue ( ) <EOL> end = start + len ( content ) - <NUM_LIT:1> <EOL> self . send_header ( HTTP_HEADER . CONTENT_RANGE , "<STR_LIT>" % ( start , end , end + <NUM_LIT:1> + max_size * ( len ( content ) >= max_size ) ) ) <EOL> if len ( content ) < max_size : <EOL> session . range_handle . close ( ) <EOL> session . range_handle = None <EOL> if size == - <NUM_LIT:1> : <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , "<STR_LIT>" ) <EOL> self . end_headers ( ) <EOL> with open ( event_log_path , "<STR_LIT:rb>" ) as f : <EOL> while True : <EOL> data = f . read ( io . DEFAULT_BUFFER_SIZE ) <EOL> if not data : <EOL> break <EOL> else : <EOL> self . wfile . write ( data ) <EOL> else : <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> if self . headers . get ( HTTP_HEADER . RANGE ) : <EOL> self . send_header ( HTTP_HEADER . CONTENT_RANGE , "<STR_LIT>" ) <EOL> return content <EOL> def _counts ( self , params ) : <EOL> counts = { } <EOL> session = self . get_session ( ) <EOL> if session is None : <EOL> self . send_response ( httplib . UNAUTHORIZED ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> return None <EOL> self . send_response ( httplib . OK ) <EOL> self . send_header ( HTTP_HEADER . CONNECTION , "<STR_LIT>" ) <EOL> self . send_header ( HTTP_HEADER . CONTENT_TYPE , "<STR_LIT:application/json>" ) <EOL> match = re . search ( r"<STR_LIT>" , params . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> if match : <EOL> min_ = datetime . datetime . strptime ( match . group ( <NUM_LIT:0> ) , DATE_FORMAT ) <EOL> else : <EOL> min_ = datetime . datetime . fromtimestamp ( <NUM_LIT:0> ) <EOL> match = re . search ( r"<STR_LIT>" , params . get ( "<STR_LIT:to>" , "<STR_LIT>" ) ) <EOL> if match : <EOL> max_ = datetime . datetime . strptime ( match . group ( <NUM_LIT:0> ) , DATE_FORMAT ) <EOL> else : <EOL> max_ = datetime . datetime . now ( ) <EOL> min_ = min_ . replace ( hour = <NUM_LIT:0> , minute = <NUM_LIT:0> , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> max_ = max_ . replace ( hour = <NUM_LIT> , minute = <NUM_LIT> , second = <NUM_LIT> , microsecond = <NUM_LIT> ) <EOL> for filepath in sorted ( glob . glob ( os . path . join ( config . LOG_DIR , "<STR_LIT>" ) ) ) : <EOL> filename = os . path . basename ( filepath ) <EOL> if not re . search ( r"<STR_LIT>" , filename ) : <EOL> continue <EOL> try : <EOL> current = datetime . datetime . strptime ( os . path . splitext ( filename ) [ <NUM_LIT:0> ] , DATE_FORMAT ) <EOL> except : <EOL> if config . SHOW_DEBUG : <EOL> traceback . print_exc ( ) <EOL> else : <EOL> if min_ <= current <= max_ : <EOL> timestamp = int ( time . mktime ( current . timetuple ( ) ) ) <EOL> size = os . path . getsize ( filepath ) <EOL> with open ( filepath , "<STR_LIT:rb>" ) as f : <EOL> content = f . read ( io . DEFAULT_BUFFER_SIZE ) <EOL> if size >= io . DEFAULT_BUFFER_SIZE : <EOL> total = <NUM_LIT:1.0> * content . count ( '<STR_LIT:\n>' ) * size / io . DEFAULT_BUFFER_SIZE <EOL> counts [ timestamp ] = int ( round ( total / <NUM_LIT:100> ) * <NUM_LIT:100> ) <EOL> else : <EOL> counts [ timestamp ] = content . count ( '<STR_LIT:\n>' ) <EOL> return json . dumps ( counts ) <EOL> class SSLReqHandler ( ReqHandler ) : <EOL> def setup ( self ) : <EOL> self . connection = self . request <EOL> self . rfile = socket . _fileobject ( self . request , "<STR_LIT:rb>" , self . rbufsize ) <EOL> self . wfile = socket . _fileobject ( self . request , "<STR_LIT:wb>" , self . wbufsize ) <EOL> try : <EOL> if pem : <EOL> server = SSLThreadingServer ( ( address or '<STR_LIT>' , int ( port ) if str ( port or "<STR_LIT>" ) . isdigit ( ) else <NUM_LIT:0> ) , pem , SSLReqHandler ) <EOL> else : <EOL> server = ThreadingServer ( ( address or '<STR_LIT>' , int ( port ) if str ( port or "<STR_LIT>" ) . isdigit ( ) else <NUM_LIT:0> ) , ReqHandler ) <EOL> except Exception as ex : <EOL> if "<STR_LIT>" in str ( ex ) : <EOL> exit ( "<STR_LIT>" ) <EOL> elif "<STR_LIT>" in str ( ex ) : <EOL> exit ( "<STR_LIT>" % config . HTTP_ADDRESS ) <EOL> elif "<STR_LIT>" in str ( ex ) : <EOL> exit ( "<STR_LIT>" % config . HTTP_ADDRESS ) <EOL> else : <EOL> raise <EOL> print "<STR_LIT>" % ( '<STR_LIT:S>' if pem else "<STR_LIT>" , '<STR_LIT:s>' if pem else "<STR_LIT>" , server . server_address [ <NUM_LIT:0> ] , server . server_address [ <NUM_LIT:1> ] ) <EOL> print "<STR_LIT>" <EOL> if join : <EOL> server . serve_forever ( ) <EOL> else : <EOL> thread = threading . Thread ( target = server . serve_forever ) <EOL> thread . daemon = True <EOL> thread . start ( ) </s>
