Skip to content

Commit 02c065b

Browse files
committed
fix(site): improve wasm init speed, various copy changes
1 parent fd75e24 commit 02c065b

File tree

11 files changed

+659
-387
lines changed

11 files changed

+659
-387
lines changed

cmd/wasm/main_js.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,32 @@ func newWush(cfg js.Value) map[string]any {
7272
cfg.Get("onWebrtcCandidate"),
7373
)
7474

75-
err = ov.PickDERPHome(ctx)
76-
if err != nil {
77-
panic(err)
75+
// Try to get stored DERP home from localStorage
76+
localStorage := js.Global().Get("localStorage")
77+
storedDerpHome := localStorage.Call("getItem", "derpHome")
78+
if !storedDerpHome.IsNull() {
79+
// Parse stored DERP home and use it
80+
derpID := uint16(js.Global().Get("parseInt").Invoke(storedDerpHome).Int())
81+
if region := dm.Regions[int(derpID)]; region != nil {
82+
ov.DerpRegionID = derpID
83+
hlog("Using stored DERP home: %s", region.RegionName)
84+
} else {
85+
// If stored DERP home is invalid, pick a new one
86+
err = ov.PickDERPHome(ctx)
87+
if err != nil {
88+
panic(err)
89+
}
90+
// Store the newly picked DERP home
91+
localStorage.Call("setItem", "derpHome", fmt.Sprint(ov.DerpRegionID))
92+
}
93+
} else {
94+
// No stored DERP home, pick a new one
95+
err = ov.PickDERPHome(ctx)
96+
if err != nil {
97+
panic(err)
98+
}
99+
// Store the picked DERP home
100+
localStorage.Call("setItem", "derpHome", fmt.Sprint(ov.DerpRegionID))
78101
}
79102

80103
s, err := tsserver.NewServer(ctx, logger, ov, dm)
@@ -91,11 +114,13 @@ func newWush(cfg js.Value) map[string]any {
91114
panic(err)
92115
}
93116

94-
_, err = ts.Up(ctx)
95-
if err != nil {
96-
panic(err)
97-
}
98-
hlog("WireGuard is ready")
117+
go func() {
118+
_, err = ts.Up(ctx)
119+
if err != nil {
120+
panic(err)
121+
}
122+
hlog("WireGuard is ready")
123+
}()
99124

100125
cpListener, err := ts.Listen("tcp", ":4444")
101126
if err != nil {

cmd/wush/cp.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"net/http/httputil"
1111
"net/netip"
12+
"net/url"
1213
"os"
1314
"path/filepath"
1415
"strings"
@@ -59,7 +60,13 @@ func initAuth(authFlag *string, ca *overlay.ClientAuth) serpent.MiddlewareFunc {
5960
}
6061
}
6162

62-
err := ca.Parse(strings.TrimSpace(*authFlag))
63+
// If the user provided a URL, extract the auth key from the fragment.
64+
authKey := *authFlag
65+
if u, err := url.Parse(*authFlag); err == nil && u.Fragment != "" {
66+
authKey = u.Fragment
67+
}
68+
69+
err := ca.Parse(strings.TrimSpace(authKey))
6370
if err != nil {
6471
return fmt.Errorf("parse auth key: %w", err)
6572
}

cmd/wush/serve.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func serveCmd() *serpent.Command {
8686
hlog("Your auth key is:")
8787
fmt.Println("\t>", cliui.Code(r.ClientAuth().AuthKey()))
8888
hlog("Use this key to authenticate other " + cliui.Code("wush") + " commands to this instance.")
89+
hlog("Visit the following link to connect via the browser:")
90+
fmt.Println("\t>", cliui.Code("https://wush.dev#"+r.ClientAuth().AuthKey()))
8991
} else {
9092
fmt.Println(cliui.Code(r.ClientAuth().AuthKey()))
9193
hlog("The auth key has been printed to stdout")

0 commit comments

Comments
 (0)