From 4da15a22759f76365adac50926e2ace1a389b6b8 Mon Sep 17 00:00:00 2001 From: king Date: Sun, 1 Feb 2026 23:33:46 +0100 Subject: [PATCH] Updated set_endpoint logic, and comments explaining the new logic --- set_endpoint.uc | 57 ++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/set_endpoint.uc b/set_endpoint.uc index 6ca3a5a..4037c4f 100644 --- a/set_endpoint.uc +++ b/set_endpoint.uc @@ -68,13 +68,15 @@ function nm_unified_format(provider, data) { }; } + function wireguard_connect(ctx,args) { } -// Converts mullvad config to wireguard endpoint obj +// Converts mullvad endpoints respecting its config to nm wireguard endpoint obj function mullvad_parse() { + const conf = json(readfile("/etc/providers/config_"+provider_name+".json")); let raw_apiresp = readfile('/tmp/mullvad_endpoints'); let wg_part = {}; @@ -84,64 +86,65 @@ function mullvad_parse() { status = system('uclient-fetch -q -O /tmp/mullvad_endpoints https://api.mullvad.net/app/v1/relays'); log("uclient-fetch error",2); - raw_apiresp = readfile('/tmp/mullvad_endpoints'); - wg_part = parsed_resp['wireguard']; - - const parsed_resp = json(raw_apiresp); - wg_part = parsed_resp['wireguard']; + raw_apiresp = readfile('/tmp/mullvad_endpoints'); } + + const parsed_resp = json(raw_apiresp); log("Loading... mullvad"); - let parsed_resp = json(raw_apiresp); log(parsed_resp); wg_part = parsed_resp['wireguard']; const port = 51820; let data = []; + let i = 0; + MAX_ENDPOINT_COUNT = 5; + OFFSET = 0; for (relay in wg_part['relays']) { + if(i > MAX_ENDPOINT_COUNT || i < OFFSET) { + break; + } log("Creating endpoint obj for " + relay); e_v4 = endpoint_format(relay['ipv4_addr_in'],port); e_v6 = endpoint_format(relay['ipv6_addr_in'],port); - tunnel = tunnel_ip_format(config.ipv4,config.ipv6,e_v4,e_v6); + + // Create Mullvad tunnel, getting its internal ip address from the config files (should be set to a database later) + tunnel = tunnel_ip_format(conf.ipv4, conf.ipv6, e_v4, e_v6); - log("Created tunnel->"+ e_v4 + e_v6 + "\n"+ tunnel); + log("Created Endpoint -> "+ e_v4 + "\n" + e_v6 + "\n"+ tunnel); print(tunnel); - push(data,wireguard_format(relay['hostname'], relay['public_key'], config.private_key_path, tunnel)); + push(data, wireguard_format(relay['hostname'], relay['public_key'], config.private_key_path, tunnel)); + i += 1; } return nm_unified_format('mullvad',content); } - -mullvad_endpoints = { - nm_unified: mullvad_parse(), - date: time() -}; +// An array first containing the mullvad_endpoints and then a timestamp of the current time +// Used to calculate if the endpoints are old +mullvad_endpoints = [mullvad_parse(),time()] const mainPipeHandle = pipe(); -let success = uloop.init(); -if(!success) { +let uloop = uloop.init(); +if(!uloop) { + log("Error initiating uloop, exiting ...", 2) exit(1); } - let p = pipe(); -let uloop_success = uloop.init(); - -if(!uloop_success) { - exit(1) -} +// 1. Read sessionID,CommandID. After reading the initation state is started. CommandID can be resolved to its CommandObject and the pre function can be executed +// 2. Read sessionID,commaseperatedvalues after the initation state and the pre function got executed the objects parameters can be populated for the after function +// 3. Read sessionID,OpendedCommandID. If the Command initation state was opened for that sessionID execute the apply function using the parameters from step 2 as parameters and then execute the post function let i = 0; log("+++ Creating uloop handle using fd: "); -const handle = uloop.handle(p[0],(e) => { - if(e & uloop.ULOOP_READ) - log("Read event" + i); - i++; +const handle = uloop.handle(p[1],(e) => { + if(e & uloop.ULOOP_WRITE) + log("Write event " + p[0].read(128)); },uloop.ULOOP_READ); log("Created Handle for Event Messages - Event Handle:\n" + handle.fileno());