Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions packages/ns-flashstart/files/ns-flashstart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand All @@ -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


Expand Down
Loading