Add installer, launch-to-browser, and switching from the web GUI
Three features plus the design-system swap. Installer. Inno-compile.iss builds a per-user installer into %APPDATA%\ipswap with PrivilegesRequired=lowest, so it never shows a UAC prompt. That matches the app's asInvoker manifest, and it is also what lets the in-app updater replace the .exe later without elevation — a Program Files install could not. It offers a desktop shortcut and an optional start-with-Windows entry, and reuses the app's own single-instance mutex as AppMutex so setup notices a running copy, since a running .exe cannot be overwritten. Uninstall leaves presets.json, config.json and the log in place. Launching. Opening ipswap opens the editor in a browser, starting the tray first if it is not already up; if it is, the running copy opens the browser and the second process exits. internal/instance does this with a loopback control listener whose port and token live in a mode-600 session.json, plus a session-local named mutex on Windows to settle a launch race. A stale session file from a crash is detected by a failed call and treated as "no primary", so it can never wedge startup. The Run-key entry now passes --background, because a browser tab at every login is not wanted. Switching from the browser. New endpoints for active state, apply preview and apply. The browser confirms against the same before/after text the tray's MessageBox shows, built from a live read at prompt time. Apply requires the session token in a header like every other mutation — it changes the machine's network, so it is not a weaker case than editing a preset. A netsh refusal for lack of elevation comes back as 409 with a flag, so the page can point at "Relaunch as administrator" instead of showing a generic failure. To avoid two copies of that sequence, internal/switcher now owns everything between "the user said yes" and "the adapter changed", and both front ends call it. It serialises applies: two interleaved netsh sequences on one adapter would leave it matching neither preset, and there are now two ways to start one. CSS. apointless.css is vendored from bsncubed/css and left untouched; the previous file was the boarding-pass stylesheet and its class names did not match this markup. ipswap.css adds only what the design system does not ship — page chrome, tables, modals, stat tiles, a success alert — on its tokens, so re-pulling apointless.css restyles the app. Not verified: the installer is uncompiled (no Inno Setup or wine on this box) and the UI is not visually rendered (headless Firefox hangs here). Both are checked as far as the tooling allows — assets and endpoints serve, and every DOM id the JS touches exists in the markup. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
"embed"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -30,6 +31,7 @@ import (
|
||||
"gitea.apointless.space/bsncubed/ipswap/internal/config"
|
||||
"gitea.apointless.space/bsncubed/ipswap/internal/netcfg"
|
||||
"gitea.apointless.space/bsncubed/ipswap/internal/preset"
|
||||
"gitea.apointless.space/bsncubed/ipswap/internal/switcher"
|
||||
)
|
||||
|
||||
//go:embed web
|
||||
@@ -47,6 +49,7 @@ const (
|
||||
// Server hosts the editor.
|
||||
type Server struct {
|
||||
store *preset.Store
|
||||
sw *switcher.Switcher
|
||||
mgr netcfg.Manager
|
||||
paths config.Paths
|
||||
|
||||
@@ -55,6 +58,9 @@ type Server struct {
|
||||
onSettingsChange func(config.Settings)
|
||||
// onPresetsChange asks the tray to rebuild its menu.
|
||||
onPresetsChange func()
|
||||
// onApplied tells the tray to re-read the active preset after the browser
|
||||
// switched one, so the check mark and tooltip do not go stale.
|
||||
onApplied func()
|
||||
|
||||
mu sync.Mutex
|
||||
settings config.Settings
|
||||
@@ -66,8 +72,8 @@ type Server struct {
|
||||
}
|
||||
|
||||
// New builds a server. It does not listen until Start is called.
|
||||
func New(store *preset.Store, mgr netcfg.Manager, paths config.Paths, settings config.Settings) *Server {
|
||||
return &Server{store: store, mgr: mgr, paths: paths, settings: settings}
|
||||
func New(store *preset.Store, sw *switcher.Switcher, mgr netcfg.Manager, paths config.Paths, settings config.Settings) *Server {
|
||||
return &Server{store: store, sw: sw, mgr: mgr, paths: paths, settings: settings}
|
||||
}
|
||||
|
||||
// OnSettingsChange registers a callback fired after settings are saved.
|
||||
@@ -76,6 +82,9 @@ func (s *Server) OnSettingsChange(f func(config.Settings)) { s.onSettingsChange
|
||||
// OnPresetsChange registers a callback fired after any preset mutation.
|
||||
func (s *Server) OnPresetsChange(f func()) { s.onPresetsChange = f }
|
||||
|
||||
// OnApplied registers a callback fired after a successful apply.
|
||||
func (s *Server) OnApplied(f func()) { s.onApplied = f }
|
||||
|
||||
// URL returns the address to open, including the session token. It is empty
|
||||
// when the server is not running.
|
||||
func (s *Server) URL() string {
|
||||
@@ -228,6 +237,13 @@ func (s *Server) routes() http.Handler {
|
||||
mux.HandleFunc("PUT /api/presets/{id}", s.handlePutPreset)
|
||||
mux.HandleFunc("DELETE /api/presets/{id}", s.handleDeletePreset)
|
||||
|
||||
// Switching from the browser. The preview is a separate GET so the
|
||||
// confirmation the user sees is built from a live read, exactly like the
|
||||
// tray's MessageBox, rather than from whatever the list happened to say.
|
||||
mux.HandleFunc("GET /api/active", s.handleActive)
|
||||
mux.HandleFunc("GET /api/presets/{id}/preview", s.handlePreview)
|
||||
mux.HandleFunc("POST /api/presets/{id}/apply", s.handleApply)
|
||||
|
||||
mux.HandleFunc("GET /api/adapters", s.handleAdapters)
|
||||
mux.HandleFunc("GET /api/settings", s.handleGetSettings)
|
||||
mux.HandleFunc("PUT /api/settings", s.handlePutSettings)
|
||||
@@ -327,6 +343,62 @@ func (s *Server) handleDeletePreset(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
||||
}
|
||||
|
||||
// handleActive reports which preset is currently applied, so the editor can
|
||||
// mark it and so a switch initiated there updates without a reload.
|
||||
func (s *Server) handleActive(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, s.sw.Active())
|
||||
}
|
||||
|
||||
// handlePreview returns the before/after the browser confirms against.
|
||||
//
|
||||
// The tray blocks on a native MessageBox before applying. The browser cannot,
|
||||
// so it gets the same diff as data and confirms in the page. Same safety net,
|
||||
// same live read — only the widget differs.
|
||||
func (s *Server) handlePreview(w http.ResponseWriter, r *http.Request) {
|
||||
p, live, err := s.sw.Preview(r.PathValue("id"))
|
||||
if err != nil {
|
||||
if errors.Is(err, switcher.ErrNotFound) {
|
||||
writeErr(w, http.StatusNotFound, err)
|
||||
return
|
||||
}
|
||||
writeErr(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"preset": p,
|
||||
"current": live,
|
||||
"text": netcfg.ConfirmText(p, live),
|
||||
"matches": netcfg.Matches(p, live),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) handleApply(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
|
||||
if err := s.sw.Apply(id); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, switcher.ErrNotFound):
|
||||
writeErr(w, http.StatusNotFound, err)
|
||||
case errors.Is(err, netcfg.ErrElevationRequired):
|
||||
// 409 rather than 500: the request was fine, the process just is
|
||||
// not elevated. The page turns this into the relaunch hint.
|
||||
writeJSON(w, http.StatusConflict, map[string]any{
|
||||
"error": err.Error(),
|
||||
"elevation_required": true,
|
||||
})
|
||||
default:
|
||||
writeErr(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if s.onApplied != nil {
|
||||
s.onApplied()
|
||||
}
|
||||
writeJSON(w, http.StatusOK, s.sw.Active())
|
||||
}
|
||||
|
||||
func (s *Server) handleAdapters(w http.ResponseWriter, r *http.Request) {
|
||||
adapters, err := s.mgr.Adapters()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user