f267c430d9
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>
104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
//go:build windows
|
|
|
|
// Package desktop holds the small Windows shell integrations that do not
|
|
// belong to any one feature: opening a URL in the default browser, and the
|
|
// HKCU Run key that starts ipswap with Windows.
|
|
package desktop
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows/registry"
|
|
|
|
"gitea.apointless.space/bsncubed/ipswap/internal/config"
|
|
)
|
|
|
|
var (
|
|
shell32 = syscall.NewLazyDLL("shell32.dll")
|
|
shellExecuteW = shell32.NewProc("ShellExecuteW")
|
|
)
|
|
|
|
const runKeyPath = `Software\Microsoft\Windows\CurrentVersion\Run`
|
|
|
|
// OpenURL opens a URL in the default browser.
|
|
//
|
|
// ShellExecuteW rather than `cmd /c start`: no console window to suppress and
|
|
// no quoting rules to get wrong around a URL that already contains "?" and "&".
|
|
func OpenURL(url string) error {
|
|
verb, err := syscall.UTF16PtrFromString("open")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
target, err := syscall.UTF16PtrFromString(url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ret, _, callErr := shellExecuteW.Call(
|
|
0,
|
|
uintptr(unsafe.Pointer(verb)),
|
|
uintptr(unsafe.Pointer(target)),
|
|
0, 0, 1, // no args, no cwd, SW_SHOWNORMAL
|
|
)
|
|
if ret <= 32 {
|
|
return fmt.Errorf("could not open %s (ShellExecuteW returned %d: %v)", url, ret, callErr)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetRunAtLogin adds or removes ipswap from the current user's Run key.
|
|
//
|
|
// HKCU rather than HKLM deliberately: HKLM needs elevation, and ipswap is
|
|
// designed to run unelevated. It also keeps the setting per-user on a shared
|
|
// support laptop.
|
|
func SetRunAtLogin(enabled bool) error {
|
|
key, _, err := registry.CreateKey(registry.CURRENT_USER, runKeyPath, registry.SET_VALUE)
|
|
if err != nil {
|
|
return fmt.Errorf("opening the Run key: %w", err)
|
|
}
|
|
defer key.Close()
|
|
|
|
if !enabled {
|
|
err := key.DeleteValue(config.AppName)
|
|
if err != nil && err != registry.ErrNotExist {
|
|
return fmt.Errorf("removing the Run key value: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return fmt.Errorf("locating the running executable: %w", err)
|
|
}
|
|
exe, err = filepath.Abs(exe)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Quoted: the path routinely contains spaces (the install lives under
|
|
// %APPDATA%, and a user profile can have a space in its name) and the Run
|
|
// key value is a command line, not a path.
|
|
//
|
|
// --background is what stops a browser tab appearing at every login. A
|
|
// launch from the shortcut has no such flag and does open the editor.
|
|
if err := key.SetStringValue(config.AppName, `"`+exe+`" --background`); err != nil {
|
|
return fmt.Errorf("writing the Run key value: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// RunAtLogin reports whether the Run key entry is present.
|
|
func RunAtLogin() bool {
|
|
key, err := registry.OpenKey(registry.CURRENT_USER, runKeyPath, registry.QUERY_VALUE)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer key.Close()
|
|
|
|
v, _, err := key.GetStringValue(config.AppName)
|
|
return err == nil && v != ""
|
|
}
|