import BaseHTTPServer import ClientConstants as CC import ClientFiles import Cookie import hashlib import httplib import HydrusAudioHandling import HydrusConstants as HC import HydrusDocumentHandling import HydrusExceptions import HydrusFileHandling import HydrusFlashHandling import HydrusNATPunch import HydrusImageHandling import os import random import ServerFiles import SocketServer import threading import time import traceback import urllib import wx import yaml from twisted.internet import reactor, defer from twisted.internet.threads import deferToThread from twisted.web.server import Request, Site, NOT_DONE_YET from twisted.web.resource import Resource from twisted.web.static import File as FileResource, NoRangeStaticProducer from twisted.python import log CLIENT_ROOT_MESSAGE = '''
This hydrus client uses software version ''' + HC.u( HC.SOFTWARE_VERSION ) + ''' and network version ''' + HC.u( HC.NETWORK_VERSION ) + '''.
It only serves requests from 127.0.0.1.
''' ROOT_MESSAGE_BEGIN = '''This hydrus service uses software version ''' + HC.u( HC.SOFTWARE_VERSION ) + ''' and network version ''' + HC.u( HC.NETWORK_VERSION ) + '''.
''' ROOT_MESSAGE_END = '''
''' def ParseFileArguments( path ): HydrusImageHandling.ConvertToPngIfBmp( path ) hash = HydrusFileHandling.GetHashFromPath( path ) try: ( size, mime, width, height, duration, num_frames, num_words ) = HydrusFileHandling.GetFileInfo( path ) except HydrusExceptions.SizeException: raise HydrusExceptions.ForbiddenException( 'File is of zero length!' ) except HydrusExceptions.MimeException: raise HydrusExceptions.ForbiddenException( 'Filetype is not permitted!' ) except Exception as e: raise HydrusExceptions.ForbiddenException( HC.u( e ) ) args = {} args[ 'path' ] = path args[ 'hash' ] = hash args[ 'size' ] = size args[ 'mime' ] = mime if width is not None: args[ 'width' ] = width if height is not None: args[ 'height' ] = height if duration is not None: args[ 'duration' ] = duration if num_frames is not None: args[ 'num_frames' ] = num_frames if num_words is not None: args[ 'num_words' ] = num_words if mime in HC.IMAGES: try: thumbnail = HydrusFileHandling.GenerateThumbnail( path ) except: raise HydrusExceptions.ForbiddenException( 'Could not generate thumbnail from that file.' ) args[ 'thumbnail' ] = thumbnail return args hydrus_favicon = FileResource( HC.STATIC_DIR + os.path.sep + 'hydrus.ico', defaultType = 'image/x-icon' ) local_booru_css = FileResource( HC.STATIC_DIR + os.path.sep + 'local_booru_style.css', defaultType = 'text/css' ) class HydrusDomain( object ): def __init__( self, local_only ): self._local_only = local_only def CheckValid( self, client_ip ): if self._local_only and client_ip != '127.0.0.1': raise HydrusExceptions.ForbiddenException( 'Only local access allowed!' ) class HydrusResourceWelcome( Resource ): def __init__( self, service_key, service_type, message ): Resource.__init__( self ) if service_key == HC.LOCAL_FILE_SERVICE_KEY: body = CLIENT_ROOT_MESSAGE else: body = ROOT_MESSAGE_BEGIN + message + ROOT_MESSAGE_END self._body = body.encode( 'utf-8' ) self._server_version_string = HC.service_string_lookup[ service_type ] + '/' + str( HC.NETWORK_VERSION ) def render_GET( self, request ): request.setHeader( 'Server', self._server_version_string ) return self._body class HydrusResourceCommand( Resource ): def __init__( self, service_key, service_type, domain ): Resource.__init__( self ) self._service_key = service_key self._service_type = service_type self._domain = domain self._server_version_string = HC.service_string_lookup[ service_type ] + '/' + str( HC.NETWORK_VERSION ) def _callbackCheckRestrictions( self, request ): self._checkUserAgent( request ) self._domain.CheckValid( request.getClientIP() ) return request def _callbackParseGETArgs( self, request ): hydrus_args = {} for name in request.args: values = request.args[ name ] value = values[0] if name in ( 'begin', 'expires', 'lifetime', 'num', 'service_type', 'service_port', 'since', 'timespan' ): try: hydrus_args[ name ] = int( value ) except: raise HydrusExceptions.ForbiddenException( 'I was expecting to parse \'' + name + '\' as an integer, but it failed.' ) elif name in ( 'access_key', 'title', 'subject_account_key', 'contact_key', 'hash', 'subject_hash', 'subject_tag', 'message_key', 'share_key' ): try: hydrus_args[ name ] = value.decode( 'hex' ) except: raise HydrusExceptions.ForbiddenException( 'I was expecting to parse \'' + name + '\' as a hex-encoded string, but it failed.' ) if 'subject_account_key' in hydrus_args: hydrus_args[ 'subject_identifier' ] = HC.AccountIdentifier( account_key = hydrus_args[ 'subject_account_key' ] ) elif 'subject_hash' in hydrus_args: if 'subject_tag' in hydrus_args: hydrus_args[ 'subject_identifier' ] = HC.AccountIdentifier( tag = hydrus_args[ 'subject_tag' ], hash = hydrus_args[ 'subject_hash' ] ) else: hydrus_args[ 'subject_identifier' ] = HC.AccountIdentifier( hash = hydrus_args[ 'subject_hash' ] ) request.hydrus_args = hydrus_args return request def _callbackParsePOSTArgs( self, request ): request.content.seek( 0 ) if not request.requestHeaders.hasHeader( 'Content-Type' ): raise HydrusExceptions.ForbiddenException( 'No Content-Type header found!' ) content_types = request.requestHeaders.getRawHeaders( 'Content-Type' ) content_type = content_types[0] try: mime = HC.mime_enum_lookup[ content_type ] except: raise HydrusExceptions.ForbiddenException( 'Did not recognise Content-Type header!' ) if mime == HC.APPLICATION_YAML: yaml_string = request.content.read() request.hydrus_request_data_usage += len( yaml_string ) hydrus_args = yaml.safe_load( yaml_string ) else: temp_path = HC.GetTempPath() with open( temp_path, 'wb' ) as f: for block in HC.ReadFileLikeAsBlocks( request.content, 65536 ): f.write( block ) request.hydrus_request_data_usage += len( block ) hydrus_args = ParseFileArguments( temp_path ) request.hydrus_args = hydrus_args return request def _callbackRenderResponseContext( self, request ): response_context = request.hydrus_response_context status_code = response_context.GetStatusCode() request.setResponseCode( status_code ) for ( k, v, kwargs ) in response_context.GetCookies(): request.addCookie( k, v, **kwargs ) do_finish = True if response_context.HasBody(): ( mime, body ) = response_context.GetMimeBody() content_type = HC.mime_string_lookup[ mime ] content_length = len( body ) request.setHeader( 'Content-Type', content_type ) request.setHeader( 'Content-Length', str( content_length ) ) if type( body ) == unicode: body = body.encode( 'utf-8' ) request.write( body ) elif response_context.HasPath(): path = response_context.GetPath() info = os.lstat( path ) size = info[6] if response_context.IsYAML(): mime = HC.APPLICATION_YAML content_type = HC.mime_string_lookup[ mime ] else: mime = HydrusFileHandling.GetMime( path ) ( base, filename ) = os.path.split( path ) content_type = HC.mime_string_lookup[ mime ] + '; ' + filename content_length = size request.setHeader( 'Content-Type', content_type ) request.setHeader( 'Content-Length', str( content_length ) ) request.setHeader( 'Expires', time.strftime( '%a, %d %b %Y %H:%M:%S GMT', time.gmtime( time.time() + 86400 * 365 ) ) ) request.setHeader( 'Cache-Control', str( 86400 * 365 ) ) fileObject = open( path, 'rb' ) producer = NoRangeStaticProducer( request, fileObject ) producer.start() do_finish = False else: content_length = 0 request.setHeader( 'Content-Length', str( content_length ) ) request.hydrus_request_data_usage += content_length self._recordDataUsage( request ) if do_finish: request.finish() def _callbackDoGETJob( self, request ): def wrap_thread_result( response_context ): request.hydrus_response_context = response_context return request d = deferToThread( self._threadDoGETJob, request ) d.addCallback( wrap_thread_result ) return d def _callbackDoPOSTJob( self, request ): def wrap_thread_result( response_context ): request.hydrus_response_context = response_context return request d = deferToThread( self._threadDoPOSTJob, request ) d.addCallback( wrap_thread_result ) return d def _checkUserAgent( self, request ): request.is_hydrus_user_agent = False if request.requestHeaders.hasHeader( 'User-Agent' ): user_agent_texts = request.requestHeaders.getRawHeaders( 'User-Agent' ) user_agent_text = user_agent_texts[0] try: user_agents = user_agent_text.split( ' ' ) except: return # crazy user agent string, so just assume not a hydrus client for user_agent in user_agents: if '/' in user_agent: ( client, network_version ) = user_agent.split( '/', 1 ) if client == 'hydrus': request.is_hydrus_user_agent = True network_version = int( network_version ) if network_version == HC.NETWORK_VERSION: return else: if network_version < HC.NETWORK_VERSION: message = 'Your client is out of date; please download the latest release.' else: message = 'This server is out of date; please ask its admin to update to the latest release.' raise HydrusExceptions.NetworkVersionException( 'Network version mismatch! This server\'s network version is ' + HC.u( HC.NETWORK_VERSION ) + ', whereas your client\'s is ' + HC.u( network_version ) + '! ' + message ) def _errbackHandleEmergencyError( self, failure, request ): print( failure.getTraceback() ) try: request.write( failure.getTraceback() ) except: pass try: request.finish() except: pass def _errbackHandleProcessingError( self, failure, request ): do_yaml = True try: # the error may have occured before user agent was set up! if not request.is_hydrus_user_agent: do_yaml = False except: pass if do_yaml: default_mime = HC.APPLICATION_YAML default_encoding = lambda x: yaml.safe_dump( HC.u( x ) ) else: default_mime = HC.TEXT_HTML default_encoding = lambda x: HC.u( x ) if failure.type == KeyError: response_context = HC.ResponseContext( 403, mime = default_mime, body = default_encoding( 'It appears one or more parameters required for that request were missing:' + os.linesep + failure.getTraceback() ) ) elif failure.type == HydrusExceptions.PermissionException: response_context = HC.ResponseContext( 401, mime = default_mime, body = default_encoding( failure.value ) ) elif failure.type == HydrusExceptions.ForbiddenException: response_context = HC.ResponseContext( 403, mime = default_mime, body = default_encoding( failure.value ) ) elif failure.type == HydrusExceptions.NotFoundException: response_context = HC.ResponseContext( 404, mime = default_mime, body = default_encoding( failure.value ) ) elif failure.type == HydrusExceptions.NetworkVersionException: response_context = HC.ResponseContext( 426, mime = default_mime, body = default_encoding( failure.value ) ) elif failure.type == HydrusExceptions.SessionException: response_context = HC.ResponseContext( 403, mime = default_mime, body = default_encoding( failure.value ) ) else: print( failure.getTraceback() ) response_context = HC.ResponseContext( 500, mime = default_mime, body = default_encoding( 'The repository encountered an error it could not handle! Here is a dump of what happened, which will also be written to your client.log file. If it persists, please forward it to hydrus.admin@gmail.com:' + os.linesep * 2 + failure.getTraceback() ) ) request.hydrus_response_context = response_context return request def _parseAccessKey( self, request ): if not request.requestHeaders.hasHeader( 'Hydrus-Key' ): raise HydrusExceptions.PermissionException( 'No hydrus key header found!' ) hex_keys = request.requestHeaders.getRawHeaders( 'Hydrus-Key' ) hex_key = hex_keys[0] try: access_key = hex_key.decode( 'hex' ) except: raise HydrusExceptions.ForbiddenException( 'Could not parse the hydrus key!' ) return access_key def _recordDataUsage( self, request ): pass def _threadDoGETJob( self, request ): raise HydrusExceptions.NotFoundException( 'This service does not support that request!' ) def _threadDoPOSTJob( self, request ): raise HydrusExceptions.NotFoundException( 'This service does not support that request!' ) def render_GET( self, request ): request.setHeader( 'Server', self._server_version_string ) d = defer.Deferred() d.addCallback( self._callbackCheckRestrictions ) d.addCallback( self._callbackParseGETArgs ) d.addCallback( self._callbackDoGETJob ) d.addErrback( self._errbackHandleProcessingError, request ) d.addCallback( self._callbackRenderResponseContext ) d.addErrback( self._errbackHandleEmergencyError, request ) reactor.callLater( 0, d.callback, request ) return NOT_DONE_YET def render_POST( self, request ): request.setHeader( 'Server', self._server_version_string ) d = defer.Deferred() d.addCallback( self._callbackCheckRestrictions ) d.addCallback( self._callbackParsePOSTArgs ) d.addCallback( self._callbackDoPOSTJob ) d.addErrback( self._errbackHandleProcessingError, request ) d.addCallback( self._callbackRenderResponseContext ) d.addErrback( self._errbackHandleEmergencyError, request ) reactor.callLater( 0, d.callback, request ) return NOT_DONE_YET class HydrusResourceCommandAccessKey( HydrusResourceCommand ): def _threadDoGETJob( self, request ): registration_key = self._parseAccessKey( request ) access_key = HC.app.Read( 'access_key', registration_key ) body = yaml.safe_dump( { 'access_key' : access_key } ) response_context = HC.ResponseContext( 200, body = body ) return response_context class HydrusResourceCommandAccessKeyVerification( HydrusResourceCommand ): def _threadDoGETJob( self, request ): access_key = self._parseAccessKey( request ) verified = HC.app.Read( 'verify_access_key', self._service_key, access_key ) body = yaml.safe_dump( { 'verified' : verified } ) response_context = HC.ResponseContext( 200, body = body ) return response_context class HydrusResourceCommandBooru( HydrusResourceCommand ): RECORD_GET_DATA_USAGE = False RECORD_POST_DATA_USAGE = False def _recordDataUsage( self, request ): p1 = request.method == 'GET' and self.RECORD_GET_DATA_USAGE p2 = request.method == 'POST' and self.RECORD_POST_DATA_USAGE if p1 or p2: num_bytes = request.hydrus_request_data_usage HC.pubsub.pub( 'service_updates_delayed', { HC.LOCAL_BOORU_SERVICE_KEY : [ HC.ServiceUpdate( HC.SERVICE_UPDATE_REQUEST_MADE, num_bytes ) ] } ) def _callbackCheckRestrictions( self, request ): self._checkUserAgent( request ) self._domain.CheckValid( request.getClientIP() ) return request class HydrusResourceCommandBooruGallery( HydrusResourceCommandBooru ): RECORD_GET_DATA_USAGE = True def _threadDoGETJob( self, request ): # in future, make this a standard frame with a search key that'll load xml or yaml AJAX stuff # with file info included, so the page can sort and whatever share_key = request.hydrus_args[ 'share_key' ] local_booru_manager = HC.app.GetManager( 'local_booru' ) local_booru_manager.CheckShareAuthorised( share_key ) ( name, text, timeout, media_results ) = local_booru_manager.GetGalleryInfo( share_key ) body = ''' ''' if name == '': body += '''''' body += '''
''' + text.replace( os.linesep, newline ).replace( '\n', newline ) + '''
''' body+= '''''' body += '''
''' + text.replace( os.linesep, newline ).replace( '\n', newline ) + '''
''' body+= '''link to ''' + HC.mime_string_lookup[ mime ] + ''' file
''' elif mime == HC.APPLICATION_FLASH: ( width, height ) = media_result.GetResolution() body += ''' ''' else: body += ''' ''' body += '''