<s> from kitt . interfaces import moduleProvides , IDroneDService <EOL> moduleProvides ( IDroneDService ) <EOL> from kitt . util import dictwrapper <EOL> import os <EOL> SERVICENAME = '<STR_LIT>' <EOL> SERVICECONFIG = dictwrapper ( { <EOL> '<STR_LIT>' : int ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT:12> ) , <EOL> '<STR_LIT>' : os . path . join ( os . path . sep , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } ) <EOL> from twisted . python . failure import Failure <EOL> from twisted . application import internet <EOL> from twisted . web import server , static , resource <EOL> from twisted . internet import defer , task <EOL> import config <EOL> from kitt . util import unpackify <EOL> from kitt . decorators import deferredAsThread <EOL> from kitt import blaster <EOL> from droned . logging import logWithContext <EOL> from droned . entity import Entity <EOL> from droned . clients import cancelTask <EOL> from droned . clients . blaster import blast <EOL> import time <EOL> import gc <EOL> http_log = logWithContext ( type = '<STR_LIT:http>' , route = SERVICENAME ) <EOL> server_log = logWithContext ( type = SERVICENAME ) <EOL> gremlin_log = logWithContext ( type = '<STR_LIT>' , route = SERVICENAME ) <EOL> DIGEST_INIT = None <EOL> try : <EOL> import hashlib <EOL> DIGEST_INIT = hashlib . sha1 <EOL> except ImportError : <EOL> import sha <EOL> DIGEST_INIT = sha . new <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> import urllib <EOL> parentService = None <EOL> service = None <EOL> server . version = '<STR_LIT>' <EOL> from droned . models . server import drone <EOL> class Gremlin ( resource . Resource ) : <EOL> """<STR_LIT>""" <EOL> @ deferredAsThread <EOL> def _serialize_objects ( self , request ) : <EOL> buf = StringIO ( ) <EOL> for obj in gc . get_objects ( ) : <EOL> if isinstance ( obj , Entity ) and obj . serializable and obj . __class__ . isValid ( obj ) : <EOL> try : <EOL> buf . write ( obj . serialize ( ) ) <EOL> except : <EOL> gremlin_log ( Failure ( ) . getTraceback ( ) ) <EOL> result = buf . getvalue ( ) <EOL> buf . close ( ) <EOL> return result <EOL> def render_GET ( self , request ) : <EOL> Type = "<STR_LIT>" <EOL> def _render ( result , r ) : <EOL> if r . finished : return <EOL> r . setHeader ( "<STR_LIT:Content-Type>" , Type ) <EOL> r . setHeader ( "<STR_LIT>" , str ( len ( result ) ) ) <EOL> r . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> r . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> r . write ( result ) <EOL> r . finish ( ) <EOL> d = self . _serialize_objects ( request ) <EOL> d . addCallback ( _render , request ) <EOL> d . addErrback ( lambda x : gremlin_log ( x . getTraceback ( ) ) and x or x ) <EOL> return server . NOT_DONE_YET <EOL> class Prime ( resource . Resource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> resource . Resource . __init__ ( self ) <EOL> def render_GET ( self , request ) : <EOL> def _render ( result , r ) : <EOL> if not r . finished : <EOL> r . setHeader ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> r . setHeader ( "<STR_LIT>" , str ( len ( result ) ) ) <EOL> r . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> r . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> r . write ( result ) <EOL> r . finish ( ) <EOL> try : <EOL> config . reactor . callLater ( <EOL> config . DRONED_PRIME_TTL , <EOL> drone . releasePrime , <EOL> int ( result ) <EOL> ) <EOL> except : pass <EOL> d = drone . getprime ( ) <EOL> d . addCallback ( str ) <EOL> d . addCallback ( _render , request ) <EOL> return server . NOT_DONE_YET <EOL> class Control ( resource . Resource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> resource . Resource . __init__ ( self ) <EOL> def msgBufResponse ( self , result ) : <EOL> """<STR_LIT>""" <EOL> response , contentType = result <EOL> try : <EOL> return { <EOL> '<STR_LIT>' : urllib . quote ( blaster . Serialize ( ) . execute ( <EOL> contentType , drone . formatResults ( response ) ) ) , <EOL> '<STR_LIT:type>' : contentType , <EOL> '<STR_LIT:code>' : <NUM_LIT:200> <EOL> } <EOL> except : <EOL> failure = Failure ( ) <EOL> Msg = { <EOL> '<STR_LIT:code>' : <NUM_LIT:1> , <EOL> '<STR_LIT:description>' : failure . getErrorMessage ( ) , <EOL> '<STR_LIT>' : failure . getTraceback ( ) , <EOL> '<STR_LIT:error>' : True <EOL> } <EOL> return { <EOL> '<STR_LIT>' : urllib . quote ( blaster . Serialize ( ) . execute ( <EOL> contentType , Msg ) ) , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : <NUM_LIT> <EOL> } <EOL> @ defer . deferredGenerator <EOL> def execute ( self , request ) : <EOL> """<STR_LIT>""" <EOL> result = None <EOL> contentType = '<STR_LIT>' <EOL> try : <EOL> request . content . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> BufferedMessage = request . content . read ( ) <EOL> contentType = request . getHeader ( '<STR_LIT>' ) <EOL> host = request . getClientIP ( ) <EOL> if not bool ( contentType in blaster . MIMESerialize ) : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> _dict = blaster . Deserialize ( ) . execute ( contentType , <EOL> urllib . unquote ( BufferedMessage ) ) <EOL> digest = DIGEST_INIT ( ) <EOL> keyID = _dict [ "<STR_LIT:key>" ] <EOL> action = _dict [ "<STR_LIT:action>" ] <EOL> argstr = _dict [ "<STR_LIT>" ] <EOL> magicStr = _dict [ "<STR_LIT>" ] <EOL> magicNumber = abs ( unpackify ( magicStr ) ) <EOL> timestamp = _dict [ "<STR_LIT:time>" ] <EOL> signature = _dict [ "<STR_LIT>" ] <EOL> if argstr != "<STR_LIT>" : <EOL> payload = str ( magicStr ) + str ( timestamp ) + "<STR_LIT>" % ( action , argstr ) <EOL> else : <EOL> payload = str ( magicStr ) + str ( timestamp ) + "<STR_LIT:%s>" % ( action , ) <EOL> digest . update ( payload ) <EOL> assumed = digest . hexdigest ( ) <EOL> trusted = drone . keyRing . publicDecrypt ( keyID , signature ) <EOL> d = drone . validateMessage ( magicNumber ) <EOL> wfd = defer . waitForDeferred ( d ) <EOL> yield wfd <EOL> if not wfd . getResult ( ) : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> if magicNumber == <NUM_LIT:0> : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> if trusted != assumed : <EOL> raise AssertionError ( "<STR_LIT>" % ( assumed , trusted ) ) <EOL> func = drone . get_action ( action ) <EOL> if not func : <EOL> raise AssertionError ( "<STR_LIT>" % ( action , ) ) <EOL> if argstr != "<STR_LIT>" : <EOL> server_log ( '<STR_LIT>' % ( action , argstr , keyID , host ) ) <EOL> else : <EOL> server_log ( '<STR_LIT>' % ( action , keyID , host ) ) <EOL> d = defer . maybeDeferred ( func , argstr ) <EOL> wfd = defer . waitForDeferred ( d ) <EOL> yield wfd <EOL> result = wfd . getResult ( ) <EOL> except : <EOL> result = Failure ( ) <EOL> yield ( result , contentType ) <EOL> def render_POST ( self , request ) : <EOL> def _render ( result , r ) : <EOL> if r . finished : <EOL> server_log ( "<STR_LIT>" ) <EOL> r . channel . connectionLost ( None ) <EOL> return <EOL> data = result . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> r . setHeader ( "<STR_LIT>" , result . get ( '<STR_LIT:type>' , '<STR_LIT>' ) ) <EOL> r . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> r . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> r . setHeader ( "<STR_LIT>" , str ( len ( data ) ) ) <EOL> r . setResponseCode ( result . get ( '<STR_LIT:code>' , <NUM_LIT:200> ) ) <EOL> r . write ( str ( data ) ) <EOL> r . finish ( ) <EOL> d = self . execute ( request ) <EOL> d . addBoth ( self . msgBufResponse ) <EOL> d . addCallback ( _render , request ) <EOL> return server . NOT_DONE_YET <EOL> class DroneSite ( server . Site ) : <EOL> """<STR_LIT>""" <EOL> def log ( self , request ) : <EOL> """<STR_LIT>""" <EOL> line = '<STR_LIT>' % ( <EOL> request . getClientIP ( ) , <EOL> '<STR_LIT>' % ( self . _escape ( request . method ) , <EOL> self . _escape ( request . uri ) , <EOL> self . _escape ( request . clientproto ) ) , <EOL> request . code , <EOL> request . sentLength or "<STR_LIT:->" , <EOL> self . _escape ( request . getHeader ( "<STR_LIT>" ) or "<STR_LIT:->" ) , <EOL> self . _escape ( request . getHeader ( "<STR_LIT>" ) or "<STR_LIT:->" ) ) <EOL> if ( '<STR_LIT>' in line ) or ( '<STR_LIT>' in line ) or ( '<STR_LIT>' in line ) or ( '<STR_LIT>' in line ) : <EOL> return <EOL> http_log ( line ) <EOL> def install ( _parentService ) : <EOL> global parentService <EOL> global SERVICECONFIG <EOL> for var , val in SERVICECONFIG . wrapped . items ( ) : <EOL> setattr ( config , var , val ) <EOL> parentService = _parentService <EOL> def start ( ) : <EOL> global service <EOL> if not running ( ) : <EOL> kwargs = { '<STR_LIT>' : <NUM_LIT> * <NUM_LIT> * <NUM_LIT:12> , '<STR_LIT>' : None } <EOL> try : kwargs . update ( { '<STR_LIT>' : config . DRONED_SERVER_TIMEOUT } ) <EOL> except : pass <EOL> dr = static . File ( config . DRONED_WEBROOT ) <EOL> dr . putChild ( '<STR_LIT>' , Prime ( ) ) <EOL> dr . putChild ( '<STR_LIT>' , Control ( ) ) <EOL> dr . putChild ( '<STR_LIT>' , Gremlin ( ) ) <EOL> site = DroneSite ( dr , ** kwargs ) <EOL> service = internet . TCPServer ( config . DRONED_PORT , site ) <EOL> service . setName ( SERVICENAME ) <EOL> service . setServiceParent ( parentService ) <EOL> def stop ( ) : <EOL> global service <EOL> if running ( ) : <EOL> service . disownServiceParent ( ) <EOL> service . stopService ( ) <EOL> service = None <EOL> def running ( ) : <EOL> global service <EOL> return bool ( service ) and service . running <EOL> __all__ = [ '<STR_LIT:start>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] </s>
