Esempio n. 1
0
 def InitModel( self ):
     
     HydrusController.HydrusController.InitModel( self )
     
     self._services = self.Read( 'services' )
     
     [ self._admin_service ] = [ service for service in self._services if service.GetServiceType() == HC.SERVER_ADMIN ]
     
     self.server_session_manager = HydrusSessions.HydrusSessionManagerServer()
     
     self._service_keys_to_connected_ports = {}
Esempio n. 2
0
    def __init__(self, win, only_run):

        self.app = win
        self.win = win
        self.only_run = only_run

        self.db_dir = tempfile.mkdtemp()

        global DB_DIR

        DB_DIR = self.db_dir

        self._server_files_dir = os.path.join(self.db_dir, 'server_files')
        self._updates_dir = os.path.join(self.db_dir, 'test_updates')

        client_files_default = os.path.join(self.db_dir, 'client_files')

        HydrusPaths.MakeSureDirectoryExists(self._server_files_dir)
        HydrusPaths.MakeSureDirectoryExists(self._updates_dir)
        HydrusPaths.MakeSureDirectoryExists(client_files_default)

        HG.controller = self
        HG.client_controller = self
        HG.server_controller = self
        HG.test_controller = self

        self.db = self
        self.gui = self

        self.frame_splash_status = ClientGUISplash.FrameSplashStatus()

        self._call_to_threads = []

        self._pubsub = HydrusPubSub.HydrusPubSub(self, lambda o: True)

        self.new_options = ClientOptions.ClientOptions()

        HC.options = ClientDefaults.GetClientDefaultOptions()

        self.options = HC.options

        def show_text(text):
            pass

        HydrusData.ShowText = show_text

        self._reads = {}

        self._reads['local_booru_share_keys'] = []
        self._reads['messaging_sessions'] = []
        self._reads['options'] = ClientDefaults.GetClientDefaultOptions()
        self._reads['file_system_predicates'] = []
        self._reads['media_results'] = []

        self._param_reads = {}

        self.example_tag_repo_service_key = HydrusData.GenerateKey()

        services = []

        services.append(
            ClientServices.GenerateService(CC.LOCAL_BOORU_SERVICE_KEY,
                                           HC.LOCAL_BOORU, 'local booru'))
        services.append(
            ClientServices.GenerateService(CC.CLIENT_API_SERVICE_KEY,
                                           HC.CLIENT_API_SERVICE,
                                           'client api'))
        services.append(
            ClientServices.GenerateService(CC.COMBINED_LOCAL_FILE_SERVICE_KEY,
                                           HC.COMBINED_LOCAL_FILE,
                                           'all local files'))
        services.append(
            ClientServices.GenerateService(CC.LOCAL_FILE_SERVICE_KEY,
                                           HC.LOCAL_FILE_DOMAIN, 'my files'))
        services.append(
            ClientServices.GenerateService(CC.TRASH_SERVICE_KEY,
                                           HC.LOCAL_FILE_TRASH_DOMAIN,
                                           'trash'))
        services.append(
            ClientServices.GenerateService(CC.DEFAULT_LOCAL_TAG_SERVICE_KEY,
                                           HC.LOCAL_TAG, 'my tags'))
        services.append(
            ClientServices.GenerateService(self.example_tag_repo_service_key,
                                           HC.TAG_REPOSITORY,
                                           'example tag repo'))
        services.append(
            ClientServices.GenerateService(CC.COMBINED_TAG_SERVICE_KEY,
                                           HC.COMBINED_TAG, 'all known tags'))
        services.append(
            ClientServices.GenerateService(
                LOCAL_RATING_LIKE_SERVICE_KEY, HC.LOCAL_RATING_LIKE,
                'example local rating like service'))
        services.append(
            ClientServices.GenerateService(
                LOCAL_RATING_NUMERICAL_SERVICE_KEY, HC.LOCAL_RATING_NUMERICAL,
                'example local rating numerical service'))

        self._reads['services'] = services

        client_files_locations = {}

        for prefix in HydrusData.IterateHexPrefixes():

            for c in ('f', 't'):

                client_files_locations[c + prefix] = client_files_default

        self._reads['client_files_locations'] = client_files_locations

        self._reads['sessions'] = []
        self._reads['tag_parents'] = {}
        self._reads['tag_siblings_all_ideals'] = {}
        self._reads['in_inbox'] = False

        self._writes = collections.defaultdict(list)

        self._managers = {}

        self.column_list_manager = ClientGUIListManager.ColumnListManager()

        self.services_manager = ClientServices.ServicesManager(self)
        self.client_files_manager = ClientFiles.ClientFilesManager(self)

        self.parsing_cache = ClientCaches.ParsingCache()

        bandwidth_manager = ClientNetworkingBandwidth.NetworkBandwidthManager()
        session_manager = ClientNetworkingSessions.NetworkSessionManager()
        domain_manager = ClientNetworkingDomain.NetworkDomainManager()

        ClientDefaults.SetDefaultDomainManagerData(domain_manager)

        login_manager = ClientNetworkingLogin.NetworkLoginManager()

        self.network_engine = ClientNetworking.NetworkEngine(
            self, bandwidth_manager, session_manager, domain_manager,
            login_manager)

        self.CallToThreadLongRunning(self.network_engine.MainLoop)

        self.tag_display_manager = ClientTagsHandling.TagDisplayManager()

        self._managers['undo'] = ClientManagers.UndoManager(self)
        self.server_session_manager = HydrusSessions.HydrusSessionManagerServer(
        )

        self.bitmap_manager = ClientManagers.BitmapManager(self)

        self.local_booru_manager = ClientCaches.LocalBooruCache(self)
        self.client_api_manager = ClientAPI.APIManager()

        self._cookies = {}

        self._job_scheduler = HydrusThreading.JobScheduler(self)

        self._job_scheduler.start()
    def test_server(self):

        discard = HG.test_controller.GetWrite(
            'session')  # just to discard gumph from testserver

        session_key_1 = HydrusData.GenerateKey()
        service_key = HydrusData.GenerateKey()

        permissions = [
            HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS,
            HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES
        ]

        account_type = HydrusNetwork.AccountType.GenerateAdminAccountType(
            HC.SERVER_ADMIN)
        created = HydrusData.GetNow() - 100000
        expires = HydrusData.GetNow() + 300

        account_key_1 = HydrusData.GenerateKey()
        account_key_2 = HydrusData.GenerateKey()

        access_key_1 = HydrusData.GenerateKey()
        hashed_access_key_1 = hashlib.sha256(access_key_1).digest()

        access_key_2 = HydrusData.GenerateKey()
        hashed_access_key_2 = hashlib.sha256(access_key_2).digest()

        account = HydrusNetwork.Account(account_key_1, account_type, created,
                                        expires)
        account_2 = HydrusNetwork.Account(account_key_2, account_type, created,
                                          expires)

        # test timeout

        expires = HydrusData.GetNow() - 10

        HG.test_controller.SetRead('sessions', [
            (session_key_1, service_key, account, hashed_access_key_1, expires)
        ])

        session_manager = HydrusSessions.HydrusSessionManagerServer()

        with self.assertRaises(HydrusExceptions.SessionException):

            session_manager.GetAccount(service_key, session_key_1)

        # test missing

        with self.assertRaises(HydrusExceptions.SessionException):

            session_manager.GetAccount(service_key, HydrusData.GenerateKey())

        # test fetching a session already in db, after bootup

        expires = HydrusData.GetNow() + 300

        HG.test_controller.SetRead('sessions', [
            (session_key_1, service_key, account, hashed_access_key_1, expires)
        ])

        session_manager = HydrusSessions.HydrusSessionManagerServer()

        read_account = session_manager.GetAccount(service_key, session_key_1)

        self.assertIs(read_account, account)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_1)

        self.assertIs(read_account, account)

        # test too busy to add a new session for a new account it doesn't know about

        HG.server_busy.acquire()

        with self.assertRaises(HydrusExceptions.ServerBusyException):

            session_manager.AddSession(service_key, HydrusData.GenerateKey())

            session_manager.GetAccountFromAccessKey(service_key,
                                                    HydrusData.GenerateKey())

        # but ok to get for a session that already exists while busy

        session_manager.GetAccount(service_key, session_key_1)
        session_manager.GetAccountFromAccessKey(service_key, access_key_1)

        HG.server_busy.release()

        # test adding a session

        HG.test_controller.ClearWrites('session')

        expires = HydrusData.GetNow() + 300

        HG.test_controller.SetRead('account_key_from_access_key',
                                   account_key_2)
        HG.test_controller.SetRead('account', account_2)

        (session_key_2,
         expires_2) = session_manager.AddSession(service_key, access_key_2)

        [(args, kwargs)] = HG.test_controller.GetWrite('session')

        (written_session_key, written_service_key, written_account_key,
         written_expires) = args

        self.assertEqual(
            (session_key_2, service_key, account_key_2, expires_2),
            (written_session_key, written_service_key, written_account_key,
             written_expires))

        read_account = session_manager.GetAccount(service_key, session_key_2)

        self.assertIs(read_account, account_2)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_2)

        self.assertIs(read_account, account_2)

        # test adding a new session for an account already in the manager

        HG.test_controller.SetRead('account_key_from_access_key',
                                   account_key_1)
        HG.test_controller.SetRead('account', account)

        (session_key_3,
         expires_3) = session_manager.AddSession(service_key, access_key_1)

        [(args, kwargs)] = HG.test_controller.GetWrite('session')

        (written_session_key, written_service_key, written_account_key,
         written_expires) = args

        self.assertEqual(
            (session_key_3, service_key, account_key_1, expires_3),
            (written_session_key, written_service_key, written_account_key,
             written_expires))

        read_account = session_manager.GetAccount(service_key, session_key_1)

        self.assertIs(read_account, account)

        read_account = session_manager.GetAccount(service_key, session_key_3)

        self.assertIs(read_account, account)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_1)

        self.assertIs(read_account, account)

        # test individual account refresh

        expires = HydrusData.GetNow() + 300

        new_obj_account_1 = HydrusNetwork.Account(account_key_1, account_type,
                                                  created, expires)

        HG.test_controller.SetRead('account', new_obj_account_1)

        session_manager.RefreshAccounts(service_key, [account_key_1])

        read_account = session_manager.GetAccount(service_key, session_key_1)

        self.assertIs(read_account, new_obj_account_1)

        read_account = session_manager.GetAccount(service_key, session_key_3)

        self.assertIs(read_account, new_obj_account_1)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_1)

        self.assertIs(read_account, new_obj_account_1)

        # test all account refresh

        expires = HydrusData.GetNow() + 300

        new_obj_account_2 = HydrusNetwork.Account(account_key_2, account_type,
                                                  created, expires)

        HG.test_controller.SetRead(
            'sessions', [(session_key_1, service_key, new_obj_account_2,
                          hashed_access_key_2, expires),
                         (session_key_2, service_key, new_obj_account_1,
                          hashed_access_key_1, expires),
                         (session_key_3, service_key, new_obj_account_2,
                          hashed_access_key_2, expires)])

        session_manager.RefreshAllAccounts()

        read_account = session_manager.GetAccount(service_key, session_key_1)

        self.assertIs(read_account, new_obj_account_2)

        read_account = session_manager.GetAccount(service_key, session_key_2)

        self.assertIs(read_account, new_obj_account_1)

        read_account = session_manager.GetAccount(service_key, session_key_3)

        self.assertIs(read_account, new_obj_account_2)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_1)

        self.assertIs(read_account, new_obj_account_1)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_2)

        self.assertIs(read_account, new_obj_account_2)