Updated set_endpoint logic, and comments explaining the new logic

This commit is contained in:
king
2026-02-01 23:33:46 +01:00
parent 6d84925e9c
commit 4da15a2275

View File

@@ -68,13 +68,15 @@ function nm_unified_format(provider, data) {
}; };
} }
function wireguard_connect(ctx,args) { 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() { function mullvad_parse() {
const conf = json(readfile("/etc/providers/config_"+provider_name+".json"));
let raw_apiresp = readfile('/tmp/mullvad_endpoints'); let raw_apiresp = readfile('/tmp/mullvad_endpoints');
let wg_part = {}; let wg_part = {};
@@ -85,63 +87,64 @@ function mullvad_parse() {
log("uclient-fetch error",2); log("uclient-fetch error",2);
raw_apiresp = readfile('/tmp/mullvad_endpoints'); raw_apiresp = readfile('/tmp/mullvad_endpoints');
wg_part = parsed_resp['wireguard'];
const parsed_resp = json(raw_apiresp);
wg_part = parsed_resp['wireguard'];
} }
const parsed_resp = json(raw_apiresp);
log("Loading... mullvad"); log("Loading... mullvad");
let parsed_resp = json(raw_apiresp);
log(parsed_resp); log(parsed_resp);
wg_part = parsed_resp['wireguard']; wg_part = parsed_resp['wireguard'];
const port = 51820; const port = 51820;
let data = []; let data = [];
let i = 0;
MAX_ENDPOINT_COUNT = 5;
OFFSET = 0;
for (relay in wg_part['relays']) { for (relay in wg_part['relays']) {
if(i > MAX_ENDPOINT_COUNT || i < OFFSET) {
break;
}
log("Creating endpoint obj for " + relay); log("Creating endpoint obj for " + relay);
e_v4 = endpoint_format(relay['ipv4_addr_in'],port); e_v4 = endpoint_format(relay['ipv4_addr_in'],port);
e_v6 = endpoint_format(relay['ipv6_addr_in'],port); e_v6 = endpoint_format(relay['ipv6_addr_in'],port);
tunnel = tunnel_ip_format(config.ipv4,config.ipv6,e_v4,e_v6);
log("Created tunnel->"+ e_v4 + e_v6 + "\n"+ tunnel); // 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 Endpoint -> "+ e_v4 + "\n" + e_v6 + "\n"+ tunnel);
print(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); return nm_unified_format('mullvad',content);
} }
// An array first containing the mullvad_endpoints and then a timestamp of the current time
mullvad_endpoints = { // Used to calculate if the endpoints are old
nm_unified: mullvad_parse(), mullvad_endpoints = [mullvad_parse(),time()]
date: time()
};
const mainPipeHandle = pipe(); const mainPipeHandle = pipe();
let success = uloop.init(); let uloop = uloop.init();
if(!success) { if(!uloop) {
log("Error initiating uloop, exiting ...", 2)
exit(1); exit(1);
} }
let p = pipe(); let p = pipe();
let uloop_success = uloop.init(); // 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
if(!uloop_success) { // 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
exit(1)
}
let i = 0; let i = 0;
log("+++ Creating uloop handle using fd: "); log("+++ Creating uloop handle using fd: ");
const handle = uloop.handle(p[0],(e) => { const handle = uloop.handle(p[1],(e) => {
if(e & uloop.ULOOP_READ) if(e & uloop.ULOOP_WRITE)
log("Read event" + i); log("Write event " + p[0].read(128));
i++;
},uloop.ULOOP_READ); },uloop.ULOOP_READ);
log("Created Handle for Event Messages - Event Handle:\n" + handle.fileno()); log("Created Handle for Event Messages - Event Handle:\n" + handle.fileno());