diff --git a/core/models/BaseProfile.py b/core/models/BaseProfile.py index 0150e33..b64dc4c 100644 --- a/core/models/BaseProfile.py +++ b/core/models/BaseProfile.py @@ -96,25 +96,28 @@ class BaseProfile: @staticmethod def exists(id: int): - return os.path.isdir(BaseProfile.__get_config_path(id)) and re.match(r'^\d+$', str(id)) + return re.match(r'^\d{1,2}$', str(id)) and os.path.isfile(f'{BaseProfile.__get_config_path(id)}/config.json') @staticmethod def all(): profiles = {} - for id in map(int, sorted(os.listdir(Constants.SP_PROFILE_CONFIG_HOME))): + for directory_entry in os.listdir(Constants.SP_PROFILE_CONFIG_HOME): + + try: + id = int(directory_entry) + except ValueError: + continue if BaseProfile.exists(id): - if os.path.exists(f'{BaseProfile.__get_config_path(id)}/config.json'): + profile = BaseProfile.find_by_id(id) - profile = BaseProfile.find_by_id(id) + if profile is not None: + profiles[id] = profile - if profile is not None: - profiles[id] = profile - - return profiles + return dict(sorted(profiles.items())) @staticmethod def save(profile): diff --git a/core/models/Subscription.py b/core/models/Subscription.py index 1139d07..5211690 100644 --- a/core/models/Subscription.py +++ b/core/models/Subscription.py @@ -21,6 +21,9 @@ class Subscription: ) ) + def get_sanitized_billing_code(self): + return '-'.join(['****'] * 3 + [self.billing_code[-4:]]) + def has_been_activated(self): return self.expires_at is not None diff --git a/core/models/session/SessionProfile.py b/core/models/session/SessionProfile.py index 7a1b665..af60d20 100644 --- a/core/models/session/SessionProfile.py +++ b/core/models/session/SessionProfile.py @@ -60,7 +60,7 @@ class SessionProfile(BaseProfile): return proxy_configuration def has_proxy_configuration(self): - return os.path.exists(f'{self.get_config_path()}/proxy.json') + return os.path.isfile(f'{self.get_config_path()}/proxy.json') def has_wireguard_configuration(self): - return os.path.exists(f'{self.get_config_path()}/wg.conf') + return os.path.isfile(f'{self.get_config_path()}/wg.conf') diff --git a/core/models/system/SystemProfile.py b/core/models/system/SystemProfile.py index 979a5db..105e9fb 100644 --- a/core/models/system/SystemProfile.py +++ b/core/models/system/SystemProfile.py @@ -45,7 +45,7 @@ class SystemProfile(BaseProfile): return f'{self.get_system_config_path()}/wg.conf' def has_wireguard_configuration(self): - return os.path.exists(f'{self.get_system_config_path()}/wg.conf') + return os.path.isfile(f'{self.get_system_config_path()}/wg.conf') def delete(self): diff --git a/main.py b/main.py index 16968ef..0bc9952 100644 --- a/main.py +++ b/main.py @@ -20,7 +20,7 @@ from core.observers.ConnectionObserver import ConnectionObserver from core.observers.InvoiceObserver import InvoiceObserver from core.observers.ProfileObserver import ProfileObserver from pathlib import Path -from typing import Optional +from typing import Optional, Union import argparse import pprint import re @@ -44,10 +44,10 @@ if __name__ == '__main__': invoice_observer.subscribe('processing', lambda event: print('A payment has been detected and is being verified...\n')) invoice_observer.subscribe('settled', lambda event: print('The payment has been successfully verified.\n')) - profile_observer.subscribe('created', lambda event: pprint.pp((event.subject, 'Created'))) - profile_observer.subscribe('destroyed', lambda event: pprint.pp((event.subject, 'Destroyed'))) - profile_observer.subscribe('disabled', lambda event: pprint.pp((event.subject, 'Disabled')) if event.meta.get('explicitly') else None) - profile_observer.subscribe('enabled', lambda event: pprint.pp((event.subject, 'Enabled'))) + profile_observer.subscribe('created', lambda event: pprint.pp((__sanitize_profile(event.subject), 'Created'))) + profile_observer.subscribe('destroyed', lambda event: pprint.pp((__sanitize_profile(event.subject), 'Destroyed'))) + profile_observer.subscribe('disabled', lambda event: pprint.pp((__sanitize_profile(event.subject), 'Disabled')) if event.meta.get('explicitly') else None) + profile_observer.subscribe('enabled', lambda event: pprint.pp((__sanitize_profile(event.subject), 'Enabled'))) def __parse_application_string(application_string: Optional[str] = None): @@ -61,6 +61,15 @@ if __name__ == '__main__': else: return parsed_application_string.groupdict() + def __sanitize_profile(candidate: Optional[Union[SessionProfile, SystemProfile]]): + + if candidate is not None and candidate.has_subscription(): + + sanitized_billing_code = candidate.subscription.get_sanitized_billing_code() + candidate.subscription.billing_code = sanitized_billing_code + + return candidate + pristine_parser = argparse.ArgumentParser(add_help=False) pristine_parser.add_argument('--pristine', '-p', action='store_true') @@ -144,7 +153,13 @@ if __name__ == '__main__': profile_parser.print_help() elif arguments.subcommand == 'list': - pprint.pp(ProfileController.get_all()) + + profiles = ProfileController.get_all() + + for key, value in profiles.items(): + profiles[key] = __sanitize_profile(value) + + pprint.pp(profiles) elif arguments.subcommand == 'show': pprint.pp(ProfileController.get(arguments.id))