diff --git a/packages/ns-flashstart/files/ns-flashstart b/packages/ns-flashstart/files/ns-flashstart index d55fe6992..2a1eaec31 100644 --- a/packages/ns-flashstart/files/ns-flashstart +++ b/packages/ns-flashstart/files/ns-flashstart @@ -231,11 +231,11 @@ def __sync_pro_profiles(): # get already existing dnsmasq instances managed by flashstart local_instances = __fetch_local_dhcp_instances() - # fetch config - config = __fetch_config() + # fetch profiles + dhcp = __fetch_profiles() # use just the first, since we don't support multiple profiles in pro setup - first_server = config['dhcp'][0] + first_server = dhcp[0] __add_profile(CONST_PORT_START, first_server) # remove every other profile other than the first for local_instance in local_instances: @@ -306,17 +306,17 @@ def __sync_pro_plus_profiles(): redirect_instances = __fetch_instanced_services('firewall', 'redirect') added_redirects = [] - # fetch config - config = __fetch_config() - remote_ids = [profile['id'] for profile in config['dhcp']] + # fetch profiles + dhcp = __fetch_profiles() + remote_ids = [profile['id'] for profile in dhcp] starting_port = CONST_PORT_START # generate bypass firewall set __add_bypass_ipset() added_ip_sets = ['ns_flashstart_bypass'] - + # sort the profiles by catch-all, so that the catch-all profile is always the last one - dhcp_entries = sorted(config['dhcp'], key=lambda entry: entry['catch-all']) + dhcp_entries = sorted(dhcp, key=lambda entry: entry['catch-all']) for profile in dhcp_entries: __add_profile(starting_port, profile) # generate IPSet for each profile @@ -364,8 +364,10 @@ def __sync_pro_plus_profiles(): subprocess.run(["uci", "reorder", f"firewall.{redirect}={index}"], capture_output=True) index += 1 + sessions = __fetch_sessions() + active_sessions = dict() - for session in config['sessions']: + for session in sessions: entries = [] if session['ip'] is None: # syncing an object @@ -433,27 +435,46 @@ def sync(): __sync_pro_profiles() -def __fetch_config(): +def __fetch_profiles() -> list: client = __get_client() username = e_uci.get('flashstart', 'global', 'username', default='') - profiles = client.get(f'{FLASHSTART_API_ENDPOINT}/network/profiles/{username}') - profiles.raise_for_status() - data = {'dhcp': [], 'sessions': []} - for profile in profiles.json(): - data['dhcp'].append({ - 'id': nethsec.utils.get_id(hashlib.sha256(profile['name'].encode()).hexdigest()[:10]), - 'name': profile['name'], - 'catch-all': profile['is_catch-all'], - 'dns_code': profile['dns_code'], + dhcp = [] + try: + profiles = client.get(f'{FLASHSTART_API_ENDPOINT}/network/profiles/{username}', timeout=10) + profiles.raise_for_status() + for profile in profiles.json(): + dhcp.append({ + 'id': nethsec.utils.get_id(hashlib.sha256(profile['name'].encode()).hexdigest()[:10]), + 'name': profile['name'], + 'catch-all': profile['is_catch-all'], + 'dns_code': profile['dns_code'], + 'servers': [ + profile['dns']['ipv4']['primary_dns'], + profile['dns']['ipv4']['secondary_dns'] + ] + }) + except requests.RequestException: + dhcp.append({ + 'id': nethsec.utils.get_id(hashlib.sha256('Fallback'.encode()).hexdigest()[:10]), + 'name': 'Fallback', + 'catch-all': True, + 'dns_code': 1, 'servers': [ - profile['dns']['ipv4']['primary_dns'], - profile['dns']['ipv4']['secondary_dns'] + '185.236.104.104', + '185.236.105.105' ] }) + return dhcp + + +def __fetch_sessions() -> list: + client = __get_client() + username = e_uci.get('flashstart', 'global', 'username', default='') sessions = client.get(f'{FLASHSTART_API_ENDPOINT}/sessions/list/{username}') sessions.raise_for_status() + data = [] for session in sessions.json(): - data['sessions'].append({ + data.append({ 'username': session['username'], 'ip': session['ip'], 'dns_code': session['dns_code'], @@ -462,7 +483,6 @@ def __fetch_config(): # jobs will be appended into a list, since it has a different format we won't use it for now # however still needs to be fetched to reset the pending job counter client.post(f'{FLASHSTART_API_ENDPOINT}/jobs/{username}') - return data