From d8d54d88d1b62f2f6fad61c2e328b64213b62b02 Mon Sep 17 00:00:00 2001 From: codeking Date: Fri, 20 Sep 2024 02:41:41 +0200 Subject: [PATCH] Update and refactor existing codebase --- core/Constants.py | 5 +++-- core/controllers/ApplicationVersionController.py | 2 +- core/controllers/ClientController.py | 2 +- core/controllers/ConfigurationController.py | 2 +- core/controllers/ConnectionController.py | 10 +++++----- core/controllers/ProfileController.py | 12 ++++++------ core/models/Model.py | 10 +++++----- core/models/Subscription.py | 2 +- core/models/SubscriptionPlan.py | 4 ++-- main.py | 2 +- 10 files changed, 26 insertions(+), 25 deletions(-) diff --git a/core/Constants.py b/core/Constants.py index db34024..ec71cca 100644 --- a/core/Constants.py +++ b/core/Constants.py @@ -6,7 +6,8 @@ import os @dataclass(frozen=True) class Constants: - CLIENT_VERSION: Final[str] = '1.0.0' + SP_CLIENT_VERSION: Final[str] = '1.0.0' + HOME: Final[str] = os.path.expanduser('~') CONFIG_HOME: Final[str] = os.environ.get('XDG_CONFIG_HOME') or os.path.join(HOME, '.config') @@ -24,6 +25,6 @@ class Constants: SP_SESSION_STATE_HOME: Final[str] = SP_STATE_HOME + '/sessions' - SP_STORAGE_DATABASE: Final[str] = SP_DATA_HOME + '/storage.db' + SP_STORAGE_DATABASE_PATH: Final[str] = SP_DATA_HOME + '/storage.db' SP_API_BASE_URL: Final[str] = 'https://api.simplifiedprivacy.is/api/v1' diff --git a/core/controllers/ApplicationVersionController.py b/core/controllers/ApplicationVersionController.py index 9f48d15..4e35d88 100644 --- a/core/controllers/ApplicationVersionController.py +++ b/core/controllers/ApplicationVersionController.py @@ -37,7 +37,7 @@ class ApplicationVersionController: applications = ApplicationController.get_all() application_versions = [] - for application_code in [application.code for application in applications]: + for application_code in (application.code for application in applications): application_version_subset = WebServiceApiService.get_application_versions(application_code, proxies) diff --git a/core/controllers/ClientController.py b/core/controllers/ClientController.py index 22197fa..73cbba9 100644 --- a/core/controllers/ClientController.py +++ b/core/controllers/ClientController.py @@ -21,7 +21,7 @@ class ClientController: @staticmethod def get_version(): - return ClientVersionController.get(Constants.CLIENT_VERSION) + return ClientVersionController.get(Constants.SP_CLIENT_VERSION) @staticmethod def can_be_updated(): diff --git a/core/controllers/ConfigurationController.py b/core/controllers/ConfigurationController.py index 27c7eb1..5dfde4b 100644 --- a/core/controllers/ConfigurationController.py +++ b/core/controllers/ConfigurationController.py @@ -25,7 +25,7 @@ class ConfigurationController: configuration = ConfigurationController.get() - if configuration is None or configuration.connection not in ['system', 'tor']: + if configuration is None or configuration.connection not in ('system', 'tor'): raise UnknownConnectionTypeError('The preferred connection type could not be determined.') return configuration.connection diff --git a/core/controllers/ConnectionController.py b/core/controllers/ConnectionController.py index 1419aee..b617e79 100644 --- a/core/controllers/ConnectionController.py +++ b/core/controllers/ConnectionController.py @@ -89,12 +89,12 @@ class ConnectionController: if profile.connection.is_unprotected(): - if not ConnectionController.system_uses_wireguard_interface(): + if not ConnectionController.system_uses_wireguard_interface(): - if not force: - raise ConnectionUnprotectedError('Connection unprotected while the system is not using a WireGuard interface.') - else: - ProfileController.disable(profile) + if not force: + raise ConnectionUnprotectedError('Connection unprotected while the system is not using a WireGuard interface.') + else: + ProfileController.disable(profile) if profile.connection.code == 'tor': diff --git a/core/controllers/ProfileController.py b/core/controllers/ProfileController.py index 171505f..8c9a3d0 100644 --- a/core/controllers/ProfileController.py +++ b/core/controllers/ProfileController.py @@ -86,13 +86,13 @@ class ProfileController: if profile.is_system_profile(): - profiles = ProfileController.get_all() + subjects = ProfileController.get_all().values() - for key, value in profiles.items(): + for subject in subjects: - if type(value).__name__ == 'SessionProfile': + if subject.is_session_profile(): - if value.connection.is_unprotected() and ProfileController.is_enabled(value) and not force: + if subject.connection.is_unprotected() and ProfileController.is_enabled(subject) and not force: raise ConnectionUnprotectedError('Disabling this system connection would leave one or more sessions exposed.') if SystemStateController.exists(): @@ -174,7 +174,7 @@ class ProfileController: @staticmethod def attach_proxy_configuration(profile: Union[SessionProfile, SystemProfile]): - if type(profile).__name__ != 'SessionProfile': + if not profile.is_session_profile(): return None if profile.subscription is None: @@ -188,7 +188,7 @@ class ProfileController: @staticmethod def get_proxy_configuration(profile: Union[SessionProfile, SystemProfile]): - if type(profile).__name__ != 'SessionProfile': + if not profile.is_session_profile(): return None profile.get_proxy_configuration() diff --git a/core/models/Model.py b/core/models/Model.py index 69f6af5..370d760 100644 --- a/core/models/Model.py +++ b/core/models/Model.py @@ -7,7 +7,7 @@ class Model: @staticmethod def _create_table_if_not_exists(table_name: str, table_definition: str, drop_existing: bool = False): - connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) + connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH) cursor = connection.cursor() if drop_existing: @@ -24,7 +24,7 @@ class Model: if parameters is None: parameters = [] - connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) + connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH) cursor = connection.cursor() cursor.row_factory = row_factory @@ -37,7 +37,7 @@ class Model: @staticmethod def _query_exists(query: str, parameters): - connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) + connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH) cursor = connection.cursor() response = cursor.execute('SELECT EXISTS(' + query + ')', parameters).fetchone() @@ -51,7 +51,7 @@ class Model: if parameters is None: parameters = [] - connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) + connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH) cursor = connection.cursor() cursor.row_factory = row_factory @@ -64,7 +64,7 @@ class Model: @staticmethod def _insert_many(query: str, tuple_factory, items): - connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE) + connection = sqlite3.connect(Constants.SP_STORAGE_DATABASE_PATH) cursor = connection.cursor() cursor.executemany(query, map(tuple_factory, items)) diff --git a/core/models/Subscription.py b/core/models/Subscription.py index a7247f2..801706c 100644 --- a/core/models/Subscription.py +++ b/core/models/Subscription.py @@ -22,7 +22,7 @@ class Subscription: ) def is_active(self): - return self.expires_at is not None and self.expires_at > datetime.now(tz=timezone.utc) + return self.expires_at is not None and self.expires_at > datetime.now(timezone.utc) @staticmethod def from_iso_format(datetime_string: str): diff --git a/core/models/SubscriptionPlan.py b/core/models/SubscriptionPlan.py index 1281770..2b6262b 100644 --- a/core/models/SubscriptionPlan.py +++ b/core/models/SubscriptionPlan.py @@ -38,7 +38,7 @@ class SubscriptionPlan(Model): features_proxy = False features_wireguard = False - if type(connection).__name__ == 'SessionConnection': + if connection.is_session_connection(): if connection.masked: features_proxy = True @@ -61,7 +61,7 @@ class SubscriptionPlan(Model): features_proxy = False features_wireguard = False - if type(connection).__name__ == 'SessionConnection': + if connection.is_session_connection(): if connection.masked: features_proxy = True diff --git a/main.py b/main.py index 0642f85..4b7e800 100644 --- a/main.py +++ b/main.py @@ -172,7 +172,7 @@ if __name__ == '__main__': manage_subscription_input = None - while manage_subscription_input not in ['1', '2', '3', '']: + while manage_subscription_input not in ('1', '2', '3', ''): print('Please select from the following:\n')