<s> import Cookie <EOL> import HydrusConstants as HC <EOL> import HydrusData <EOL> import HydrusExceptions <EOL> import HydrusGlobals <EOL> import HydrusServerResources <EOL> import ServerFiles <EOL> import yaml <EOL> class HydrusResourceBusyCheck ( HydrusServerResources . Resource ) : <EOL> def __init__ ( self ) : <EOL> HydrusServerResources . Resource . __init__ ( self ) <EOL> self . _server_version_string = HC . service_string_lookup [ HC . SERVER_ADMIN ] + '<STR_LIT:/>' + str ( HC . NETWORK_VERSION ) <EOL> def render_GET ( self , request ) : <EOL> request . setResponseCode ( <NUM_LIT:200> ) <EOL> request . setHeader ( '<STR_LIT>' , self . _server_version_string ) <EOL> if HydrusGlobals . server_busy : return '<STR_LIT:1>' <EOL> else : return '<STR_LIT:0>' <EOL> class HydrusResourceCommandAccessKey ( HydrusServerResources . HydrusResourceCommand ) : <EOL> def _threadDoGETJob ( self , request ) : <EOL> registration_key = self . _parseAccessKey ( request ) <EOL> access_key = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , registration_key ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : access_key } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandShutdown ( HydrusServerResources . HydrusResourceCommand ) : <EOL> def _threadDoPOSTJob ( self , request ) : <EOL> HydrusGlobals . server_controller . ShutdownFromServer ( ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> ) <EOL> return response_context <EOL> class HydrusResourceCommandAccessKeyVerification ( HydrusServerResources . HydrusResourceCommand ) : <EOL> def _threadDoGETJob ( self , request ) : <EOL> access_key = self . _parseAccessKey ( request ) <EOL> verified = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key , access_key ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : verified } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandInit ( HydrusServerResources . HydrusResourceCommand ) : <EOL> def _threadDoGETJob ( self , request ) : <EOL> access_key = HydrusGlobals . server_controller . Read ( '<STR_LIT>' ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : access_key } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandSessionKey ( HydrusServerResources . HydrusResourceCommand ) : <EOL> def _threadDoGETJob ( self , request ) : <EOL> access_key = self . _parseAccessKey ( request ) <EOL> session_manager = HydrusGlobals . server_controller . GetServerSessionManager ( ) <EOL> ( session_key , expires ) = session_manager . AddSession ( self . _service_key , access_key ) <EOL> now = HydrusData . GetNow ( ) <EOL> max_age = now - expires <EOL> cookies = [ ( '<STR_LIT>' , session_key . encode ( '<STR_LIT>' ) , { '<STR_LIT>' : max_age , '<STR_LIT:path>' : '<STR_LIT:/>' } ) ] <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , cookies = cookies ) <EOL> return response_context <EOL> class HydrusResourceCommandRestricted ( HydrusServerResources . HydrusResourceCommand ) : <EOL> GET_PERMISSION = HC . GENERAL_ADMIN <EOL> POST_PERMISSION = HC . GENERAL_ADMIN <EOL> def _callbackCheckRestrictions ( self , request ) : <EOL> self . _checkServerBusy ( ) <EOL> self . _checkUserAgent ( request ) <EOL> self . _domain . CheckValid ( request . getClientIP ( ) ) <EOL> self . _checkSession ( request ) <EOL> self . _checkPermission ( request ) <EOL> return request <EOL> def _checkPermission ( self , request ) : <EOL> account = request . hydrus_account <EOL> method = request . method <EOL> permission = None <EOL> if method == '<STR_LIT:GET>' : permission = self . GET_PERMISSION <EOL> elif method == '<STR_LIT:POST>' : permission = self . POST_PERMISSION <EOL> if permission is not None : account . CheckPermission ( permission ) <EOL> return request <EOL> def _checkSession ( self , request ) : <EOL> if not request . requestHeaders . hasHeader ( '<STR_LIT>' ) : raise HydrusExceptions . PermissionException ( '<STR_LIT>' ) <EOL> cookie_texts = request . requestHeaders . getRawHeaders ( '<STR_LIT>' ) <EOL> cookie_text = cookie_texts [ <NUM_LIT:0> ] <EOL> try : <EOL> cookies = Cookie . SimpleCookie ( cookie_text ) <EOL> if '<STR_LIT>' not in cookies : session_key = None <EOL> else : session_key = cookies [ '<STR_LIT>' ] . value . decode ( '<STR_LIT>' ) <EOL> except : raise Exception ( '<STR_LIT>' ) <EOL> session_manager = HydrusGlobals . server_controller . GetServerSessionManager ( ) <EOL> account = session_manager . GetAccount ( self . _service_key , session_key ) <EOL> request . hydrus_account = account <EOL> return request <EOL> def _recordDataUsage ( self , request ) : <EOL> path = request . path [ <NUM_LIT:1> : ] <EOL> if request . method == '<STR_LIT:GET>' : method = HC . GET <EOL> else : method = HC . POST <EOL> if ( self . _service_type , method , path ) in HC . BANDWIDTH_CONSUMING_REQUESTS : <EOL> account = request . hydrus_account <EOL> if account is not None : <EOL> num_bytes = request . hydrus_request_data_usage <EOL> account . RequestMade ( num_bytes ) <EOL> HydrusGlobals . server_controller . pub ( '<STR_LIT>' , ( account . GetAccountKey ( ) , num_bytes ) ) <EOL> class HydrusResourceCommandRestrictedAccount ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = None <EOL> POST_PERMISSION = HC . MANAGE_USERS <EOL> def _threadDoGETJob ( self , request ) : <EOL> account = request . hydrus_account <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : account } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> def _threadDoPOSTJob ( self , request ) : <EOL> admin_account = request . hydrus_account <EOL> admin_account_key = admin_account . GetAccountKey ( ) <EOL> action = request . hydrus_args [ '<STR_LIT:action>' ] <EOL> subject_identifiers = request . hydrus_args [ '<STR_LIT>' ] <EOL> subject_account_keys = { HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key , subject_identifier ) for subject_identifier in subject_identifiers } <EOL> kwargs = request . hydrus_args <EOL> HydrusGlobals . server_controller . WriteSynchronous ( '<STR_LIT>' , self . _service_key , admin_account_key , action , subject_account_keys , kwargs ) <EOL> session_manager = HydrusGlobals . server_controller . GetServerSessionManager ( ) <EOL> session_manager . RefreshAccounts ( self . _service_key , subject_account_keys ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedAccountInfo ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GENERAL_ADMIN <EOL> def _threadDoGETJob ( self , request ) : <EOL> subject_identifier = request . hydrus_args [ '<STR_LIT>' ] <EOL> subject_account_key = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key , subject_identifier ) <EOL> account_info = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key , subject_account_key ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : account_info } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedAccountTypes ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GENERAL_ADMIN <EOL> POST_PERMISSION = HC . GENERAL_ADMIN <EOL> def _threadDoGETJob ( self , request ) : <EOL> account_types = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : account_types } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> def _threadDoPOSTJob ( self , request ) : <EOL> edit_log = request . hydrus_args [ '<STR_LIT>' ] <EOL> HydrusGlobals . server_controller . WriteSynchronous ( '<STR_LIT>' , self . _service_key , edit_log ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedBackup ( HydrusResourceCommandRestricted ) : <EOL> POST_PERMISSION = HC . GENERAL_ADMIN <EOL> def _threadDoPOSTJob ( self , request ) : <EOL> def do_it ( ) : <EOL> HydrusGlobals . server_busy = True <EOL> HydrusGlobals . server_controller . WriteSynchronous ( '<STR_LIT>' ) <EOL> HydrusGlobals . server_busy = False <EOL> HydrusGlobals . server_controller . CallToThread ( do_it ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedIP ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GENERAL_ADMIN <EOL> def _threadDoGETJob ( self , request ) : <EOL> hash = request . hydrus_args [ '<STR_LIT>' ] <EOL> ( ip , timestamp ) = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key , hash ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : ip , '<STR_LIT>' : timestamp } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedNews ( HydrusResourceCommandRestricted ) : <EOL> POST_PERMISSION = HC . GENERAL_ADMIN <EOL> def _threadDoPOSTJob ( self , request ) : <EOL> news = request . hydrus_args [ '<STR_LIT>' ] <EOL> HydrusGlobals . server_controller . WriteSynchronous ( '<STR_LIT>' , self . _service_key , news ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedNumPetitions ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . RESOLVE_PETITIONS <EOL> def _threadDoGETJob ( self , request ) : <EOL> num_petitions = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : num_petitions } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedPetition ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . RESOLVE_PETITIONS <EOL> def _threadDoGETJob ( self , request ) : <EOL> petition = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key ) <EOL> body = petition . DumpToNetworkString ( ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , mime = HC . APPLICATION_JSON , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedRegistrationKeys ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GENERAL_ADMIN <EOL> def _threadDoGETJob ( self , request ) : <EOL> num = request . hydrus_args [ '<STR_LIT>' ] <EOL> title = request . hydrus_args [ '<STR_LIT:title>' ] <EOL> if '<STR_LIT>' in request . hydrus_args : lifetime = request . hydrus_args [ '<STR_LIT>' ] <EOL> else : lifetime = None <EOL> registration_keys = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key , num , title , lifetime ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : registration_keys } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedRepositoryFile ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GET_DATA <EOL> POST_PERMISSION = HC . POST_DATA <EOL> def _threadDoGETJob ( self , request ) : <EOL> hash = request . hydrus_args [ '<STR_LIT>' ] <EOL> path = ServerFiles . GetPath ( '<STR_LIT:file>' , hash ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , path = path ) <EOL> return response_context <EOL> def _threadDoPOSTJob ( self , request ) : <EOL> account = request . hydrus_account <EOL> account_key = account . GetAccountKey ( ) <EOL> file_dict = request . hydrus_args <EOL> file_dict [ '<STR_LIT>' ] = request . getClientIP ( ) <EOL> HydrusGlobals . server_controller . WriteSynchronous ( '<STR_LIT:file>' , self . _service_key , account_key , file_dict ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedRepositoryThumbnail ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GET_DATA <EOL> def _threadDoGETJob ( self , request ) : <EOL> hash = request . hydrus_args [ '<STR_LIT>' ] <EOL> path = ServerFiles . GetPath ( '<STR_LIT>' , hash ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , path = path ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedServices ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GENERAL_ADMIN <EOL> POST_PERMISSION = HC . GENERAL_ADMIN <EOL> def _threadDoPOSTJob ( self , request ) : <EOL> account = request . hydrus_account <EOL> account_key = account . GetAccountKey ( ) <EOL> edit_log = request . hydrus_args [ '<STR_LIT>' ] <EOL> service_keys_to_access_keys = HydrusGlobals . server_controller . WriteSynchronous ( '<STR_LIT>' , account_key , edit_log ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : service_keys_to_access_keys } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedServicesInfo ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GENERAL_ADMIN <EOL> POST_PERMISSION = HC . GENERAL_ADMIN <EOL> def _threadDoGETJob ( self , request ) : <EOL> services_info = HydrusGlobals . server_controller . Read ( '<STR_LIT>' ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : services_info } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedStats ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GENERAL_ADMIN <EOL> def _threadDoGETJob ( self , request ) : <EOL> stats = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key ) <EOL> body = yaml . safe_dump ( { '<STR_LIT>' : stats } ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , body = body ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedContentUpdate ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GET_DATA <EOL> POST_PERMISSION = HC . POST_DATA <EOL> def _threadDoGETJob ( self , request ) : <EOL> begin = request . hydrus_args [ '<STR_LIT>' ] <EOL> subindex = request . hydrus_args [ '<STR_LIT>' ] <EOL> path = ServerFiles . GetContentUpdatePackagePath ( self . _service_key , begin , subindex ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , path = path , is_json = True ) <EOL> return response_context <EOL> def _threadDoPOSTJob ( self , request ) : <EOL> account = request . hydrus_account <EOL> account_key = account . GetAccountKey ( ) <EOL> update = request . hydrus_args [ '<STR_LIT>' ] <EOL> HydrusGlobals . server_controller . WriteSynchronous ( '<STR_LIT>' , self . _service_key , account_key , update ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedImmediateContentUpdate ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . RESOLVE_PETITIONS <EOL> def _threadDoGETJob ( self , request ) : <EOL> content_update = HydrusGlobals . server_controller . Read ( '<STR_LIT>' , self . _service_key ) <EOL> network_string = content_update . DumpToNetworkString ( ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , mime = HC . APPLICATION_JSON , body = network_string ) <EOL> return response_context <EOL> class HydrusResourceCommandRestrictedServiceUpdate ( HydrusResourceCommandRestricted ) : <EOL> GET_PERMISSION = HC . GET_DATA <EOL> def _threadDoGETJob ( self , request ) : <EOL> begin = request . hydrus_args [ '<STR_LIT>' ] <EOL> path = ServerFiles . GetServiceUpdatePackagePath ( self . _service_key , begin ) <EOL> response_context = HydrusServerResources . ResponseContext ( <NUM_LIT:200> , path = path , is_json = True ) <EOL> return response_context </s>
