From f4648b08e6805afa6c464fb4de649ec50b520b44 Mon Sep 17 00:00:00 2001 From: Alessandro Pignotti Date: Sun, 13 Oct 2024 11:22:53 +0200 Subject: [PATCH] Svelte: Restore support for Tailscale connection --- src/lib/NetworkingTab.svelte | 103 +++++++++++++++++++++++++++++++++-- src/lib/SideBar.svelte | 2 +- src/lib/network.js | 46 ++++++++++++++++ vite.config.js | 2 +- 4 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 src/lib/network.js diff --git a/src/lib/NetworkingTab.svelte b/src/lib/NetworkingTab.svelte index 9900d37..e431695 100644 --- a/src/lib/NetworkingTab.svelte +++ b/src/lib/NetworkingTab.svelte @@ -1,11 +1,102 @@

Networking

-{#if currentIp === null} -

Connect to Tailscale

-{:else} -

Current IP: {currentIp}

-{/if} +

{getButtonText($connectionState)}

WebVM can connect to the Internet via Tailscale

Using Tailscale is required since browser do not support TCP/UDP sockets (yet!)

diff --git a/src/lib/SideBar.svelte b/src/lib/SideBar.svelte index dc1d780..7704974 100644 --- a/src/lib/SideBar.svelte +++ b/src/lib/SideBar.svelte @@ -40,7 +40,7 @@
{#if activeInfo === 'Networking'} - + {:else}

{activeInfo}

{/if} diff --git a/src/lib/network.js b/src/lib/network.js new file mode 100644 index 0000000..f78de86 --- /dev/null +++ b/src/lib/network.js @@ -0,0 +1,46 @@ +import { writable } from 'svelte/store'; + +let params = new URLSearchParams("?"+window.location.hash.substr(1)); +let authKey = params.get("authKey") || undefined; +let controlUrl = params.get("controlUrl") || undefined; +let dashboardUrl = controlUrl ? null : "https://login.tailscale.com/admin/machines"; +let resolveLogin = null; +let loginPromise = new Promise((f,r) => { + resolveLogin = f; +}); +let connectionState = writable("DISCONNECTED"); + +function loginUrlCb(url) +{ + resolveLogin(url); +} + +function stateUpdateCb(state) +{ + switch(state) + { + case 6 /*Running*/: + { + connectionState.set("CONNECTED"); + break; + } + } +} + +function netmapUpdateCb(map) +{ + networkData.currentIp = map.self.addresses[0]; +} + +export async function startLogin() +{ + connectionState.set("LOGINSTARTING"); + const url = await loginPromise; + networkData.loginUrl = url; + connectionState.set("LOGINREADY"); + return url; +} + +export const networkInterface = { authKey: authKey, controlUrl: controlUrl, loginUrlCb: loginUrlCb, stateUpdateCb: stateUpdateCb, netmapUpdateCb: netmapUpdateCb }; + +export const networkData = { currentIp: null, connectionState: connectionState, loginUrl: null, dashboardUrl: dashboardUrl } diff --git a/vite.config.js b/vite.config.js index 3c4a9be..a0f0141 100644 --- a/vite.config.js +++ b/vite.config.js @@ -17,8 +17,8 @@ export default defineConfig({ targets: [ { src: 'tower.ico', dest: '' }, { src: 'scrollbar.css', dest: '' }, - { src: 'network.js', dest: '' }, { src: 'serviceWorker.js', dest: '' }, + { src: 'login.html', dest: '' }, { src: 'assets/', dest: '' } ] })