From a29cf6a03f837615c39118b605ebd4295d23bfe9 Mon Sep 17 00:00:00 2001 From: king Date: Sun, 1 Feb 2026 21:38:54 +0100 Subject: [PATCH] Initial Commit, updated function names and created some comments to better explain the code --- set_endpoint.uc | 171 +++++++++++++++++++++++++++++++++++++++++ set_endpoint_events.uc | 10 +++ 2 files changed, 181 insertions(+) create mode 100644 set_endpoint.uc create mode 100644 set_endpoint_events.uc diff --git a/set_endpoint.uc b/set_endpoint.uc new file mode 100644 index 0000000..d8b9244 --- /dev/null +++ b/set_endpoint.uc @@ -0,0 +1,171 @@ +#!/bin/ucode +import * as uloop from 'uloop'; +import { readfile, popen, pipe } from 'fs'; +import { event_fns } from './set_endpoint_events.uc' +// 1. Aggregate all Endpoints +// 2. Start pipe and loop and listen for set_endpoint commands +// 3. set_endpoint command structure: tunnel_ip,endpoint_type,endpoint_selector +// 4. Wenn der befehl failed dann werden alle endpoints zurückgegeben #} + +function log(msg,code) { + let prefix = "[+] "; + if(code == 1) { + prefix = "[!W] "; + } + + if(code == 2) { + prefix = "[!!!Err] "; + } + + print(prefix + msg + "\n"); +} + +function endpoint_format(ip,port) { + return { + endpoint: { + ip: ip, + port: port + } + }; +} + +function tunnel_ip_format(v4_addr, v6_addr, v4_endpoint, v6_endpoint) { + return { + tunnel: { + ipv4: { + if_addr: v4_addr, + endpoint: v4_endpoint + }, + ipv6: { + if_addr: v6_addr, + endpoint: v6_endpoint + } + } + }; +} + +function wireguard_format(name, pubkey, private_key_path, tunnel_props) { + return { + wireguard: { + name: name, + tunnel: tunnel_props, + public_key: pubkey, + private_key: private_key_path + } + }; +} + +function socks5_format() {} + +function nm_unified_format(provider, data) { + return { + nm_unified: { + provider: provider, + data: data // Should be array of wireguard or openvpn or socks format + } + }; +} + +const provider_parser = { + config: function(provider_name) { + const conf = json(readfile("/etc/nm_providers/config_"+provider_name+".json")); + return conf; + } +}; + +function wireguard_connect(ctx,args) { + +} + +let wireguard = { + connect: wireguard_connect, +}; + +// Converts mullvad config to wireguard endpoint obj +function mullvad_fn(config) { + let raw_apiresp = readfile('/tmp/mullvad_endpoints'); + let wg_part = {}; + + log(raw_apiresp); + + if(raw_apiresp == null) { + 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']; + } + + log("Loading... mullvad"); + let parsed_resp = json(raw_apiresp); + log(parsed_resp); + wg_part = parsed_resp['wireguard']; + + const port = 51820; + let content = []; + + for (relay in wg_part['relays']) { + 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); + + log("Created tunnel->"+ e_v4 + e_v6 + "\n"+ tunnel); + + print(tunnel); + push(content,wireguard_format(relay['hostname'], relay['public_key'], config.private_key_path, tunnel)); + } + + return nm_unified_format('mullvad',content); +} + + +const endpoint_providers = { + 'mullvad': { ...wireguard, ...mullvad } +}; + + +let endpoints = []; + +for (provider_name in keys(endpoint_providers)) { + let provider = endpoint_providers[provider_name]; + let curr_ep = provider.get(provider.config); + + curr_ep['provider'] = provider; + endpoints[provider_name] = curr_ep; + + log(curr_ep); + + print("[+] Endpoint for " + provider_name + " created\n"); +} + +const mainPipeHandle = pipe(); + +let success = uloop.init(); +if(!success) { + exit(1); +} + + +let p = pipe(); + +let uloop_success = uloop.init(); + +if(!uloop_success) { + exit(1) +} + +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++; +},uloop.ULOOP_READ); + +log("Created Handle for Event Messages - Event Handle:\n" + handle.fileno()); + +uloop.run(); diff --git a/set_endpoint_events.uc b/set_endpoint_events.uc new file mode 100644 index 0000000..d61f88d --- /dev/null +++ b/set_endpoint_events.uc @@ -0,0 +1,10 @@ +const event_fns = {} + +# Set country midendpoint with a specific provider +event_fns["set_midpoint_country"] = {} +# +event_fns["set_proxy_props"] = {} + +return { + event_fns: event_fns +}