feat: add LAN access configuration for desktop app, allowing users to enable public API access with corresponding UI updates and translations
This commit is contained in:
+25
-3
@@ -7,9 +7,11 @@ import (
|
||||
|
||||
// App holds Wails-bound state for the desktop shell.
|
||||
type App struct {
|
||||
ctx context.Context
|
||||
backendURL string
|
||||
shutdownCh chan struct{}
|
||||
ctx context.Context
|
||||
backendURL string
|
||||
apiLanBaseURL string
|
||||
listenPublic bool
|
||||
shutdownCh chan struct{}
|
||||
}
|
||||
|
||||
// NewApp creates a new App application struct.
|
||||
@@ -46,3 +48,23 @@ func (a *App) GetDesktopHTTPPortSetting() int {
|
||||
func (a *App) SetDesktopHTTPPortSetting(port int) error {
|
||||
return SaveDesktopHTTPPortPreference(port)
|
||||
}
|
||||
|
||||
// GetDesktopHTTPBindPublicSetting returns whether API listens on all interfaces (0.0.0.0).
|
||||
func (a *App) GetDesktopHTTPBindPublicSetting() bool {
|
||||
return LoadDesktopHTTPBindPublic()
|
||||
}
|
||||
|
||||
// SetDesktopHTTPBindPublicSetting saves LAN/public listen preference. Restart the app for it to take effect.
|
||||
func (a *App) SetDesktopHTTPBindPublicSetting(v bool) error {
|
||||
return SaveDesktopHTTPBindPublicPreference(v)
|
||||
}
|
||||
|
||||
// GetAPILanBaseURL returns a suggested base URL for other devices on the LAN (…/api/v1), or empty if not in bind-public mode or IP detection failed.
|
||||
func (a *App) GetAPILanBaseURL() string {
|
||||
return a.apiLanBaseURL
|
||||
}
|
||||
|
||||
// GetDesktopListenPublicActive is true when this session’s API server is listening on all interfaces (runtime), not the saved preference.
|
||||
func (a *App) GetDesktopListenPublicActive() bool {
|
||||
return a.listenPublic
|
||||
}
|
||||
|
||||
+80
-7
@@ -222,10 +222,17 @@ func main() {
|
||||
return fmt.Errorf("failed to start server: %v", err)
|
||||
}
|
||||
|
||||
actualAddr := listener.Addr().(*net.TCPAddr).String()
|
||||
|
||||
// Store the backend URL so Wails can load it
|
||||
app.backendURL = fmt.Sprintf("http://%s", actualAddr)
|
||||
tcpAddr := listener.Addr().(*net.TCPAddr)
|
||||
port := tcpAddr.Port
|
||||
app.listenPublic = LoadDesktopHTTPBindPublic()
|
||||
// Reverse proxy and webview API calls always use loopback; avoid 0.0.0.0 / [::] as dial target.
|
||||
app.backendURL = fmt.Sprintf("http://127.0.0.1:%d", port)
|
||||
app.apiLanBaseURL = ""
|
||||
if app.listenPublic {
|
||||
if ip := desktopPreferredLANIPv4(); ip != nil {
|
||||
app.apiLanBaseURL = fmt.Sprintf("http://%s:%d/api/v1", ip.String(), port)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle graceful shutdown from Wails OnShutdown hook
|
||||
go func() {
|
||||
@@ -254,7 +261,7 @@ func main() {
|
||||
app.shutdownCh <- struct{}{} // trigger shutdown
|
||||
}()
|
||||
|
||||
logger.Infof(context.Background(), "Server is running at %s", actualAddr)
|
||||
logger.Infof(context.Background(), "Server is running at %s (proxy -> %s)", tcpAddr.String(), app.backendURL)
|
||||
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
||||
return fmt.Errorf("server error: %v", err)
|
||||
}
|
||||
@@ -334,6 +341,10 @@ func main() {
|
||||
inject := fmt.Sprintf(`try{window.__WEKNORA_API_BASE__=%s}catch(e){}`, strconv.Quote(apiRoot))
|
||||
wailsruntime.WindowExecJS(ctx, inject)
|
||||
}
|
||||
if lan := strings.TrimSpace(app.apiLanBaseURL); lan != "" {
|
||||
injectLan := fmt.Sprintf(`try{window.__WEKNORA_API_LAN_BASE__=%s}catch(e){}`, strconv.Quote(lan))
|
||||
wailsruntime.WindowExecJS(ctx, injectLan)
|
||||
}
|
||||
},
|
||||
OnShutdown: app.shutdown,
|
||||
Bind: []interface{}{
|
||||
@@ -411,9 +422,12 @@ func resolveDesktopDataPath(rawPath, defaultRelativePath, appSupportDir string)
|
||||
}
|
||||
|
||||
// desktopBackendListenAddr returns the TCP address for the embedded Gin server (Wails desktop).
|
||||
// Always binds loopback 127.0.0.1. Uses saved UI preference (desktop-prefs.json) when set, else ephemeral port :0.
|
||||
// Binds 127.0.0.1 by default, or 0.0.0.0 when http_bind_public is set in desktop-prefs.json.
|
||||
func desktopBackendListenAddr() string {
|
||||
const host = "127.0.0.1"
|
||||
host := "127.0.0.1"
|
||||
if LoadDesktopHTTPBindPublic() {
|
||||
host = "0.0.0.0"
|
||||
}
|
||||
pref := LoadDesktopPrefsHTTPPort()
|
||||
if pref >= 1 && pref <= 65535 {
|
||||
return net.JoinHostPort(host, strconv.Itoa(pref))
|
||||
@@ -421,6 +435,65 @@ func desktopBackendListenAddr() string {
|
||||
return net.JoinHostPort(host, "0")
|
||||
}
|
||||
|
||||
// desktopPreferredLANIPv4 picks a non-loopback IPv4 for “other devices on this network” URL hints.
|
||||
func desktopPreferredLANIPv4() net.IP {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err == nil {
|
||||
for _, iface := range ifaces {
|
||||
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
||||
continue
|
||||
}
|
||||
addrs, aerr := iface.Addrs()
|
||||
if aerr != nil {
|
||||
continue
|
||||
}
|
||||
for _, a := range addrs {
|
||||
ipNet, ok := a.(*net.IPNet)
|
||||
if !ok || ipNet.IP == nil {
|
||||
continue
|
||||
}
|
||||
ip4 := ipNet.IP.To4()
|
||||
if ip4 == nil || ip4.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
if ip4.IsPrivate() {
|
||||
return ip4
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, iface := range ifaces {
|
||||
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
||||
continue
|
||||
}
|
||||
addrs, aerr := iface.Addrs()
|
||||
if aerr != nil {
|
||||
continue
|
||||
}
|
||||
for _, a := range addrs {
|
||||
ipNet, ok := a.(*net.IPNet)
|
||||
if !ok || ipNet.IP == nil {
|
||||
continue
|
||||
}
|
||||
ip4 := ipNet.IP.To4()
|
||||
if ip4 == nil || ip4.IsLoopback() || ip4.IsUnspecified() {
|
||||
continue
|
||||
}
|
||||
return ip4
|
||||
}
|
||||
}
|
||||
}
|
||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer conn.Close()
|
||||
la, ok := conn.LocalAddr().(*net.UDPAddr)
|
||||
if !ok || la.IP == nil {
|
||||
return nil
|
||||
}
|
||||
return la.IP.To4()
|
||||
}
|
||||
|
||||
func migrateLegacyDesktopData(resourcesDir, targetDataDir string) {
|
||||
legacyDataDir := filepath.Join(resourcesDir, "data")
|
||||
if info, err := os.Stat(legacyDataDir); err != nil || !info.IsDir() {
|
||||
|
||||
+40
-18
@@ -10,7 +10,8 @@ import (
|
||||
const desktopPrefsFileName = "desktop-prefs.json"
|
||||
|
||||
type desktopPrefs struct {
|
||||
HTTPPort int `json:"http_port"`
|
||||
HTTPPort int `json:"http_port"`
|
||||
HTTPBindPublic bool `json:"http_bind_public"`
|
||||
}
|
||||
|
||||
func desktopPrefsDir() (string, error) {
|
||||
@@ -33,24 +34,45 @@ func desktopPrefsFilePath() (string, error) {
|
||||
return filepath.Join(dir, desktopPrefsFileName), nil
|
||||
}
|
||||
|
||||
// LoadDesktopPrefsHTTPPort returns http_port from prefs file, or 0 if unset / invalid (ephemeral port on each launch).
|
||||
func LoadDesktopPrefsHTTPPort() int {
|
||||
func loadDesktopPrefs() desktopPrefs {
|
||||
path, err := desktopPrefsFilePath()
|
||||
if err != nil {
|
||||
return 0
|
||||
return desktopPrefs{}
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return 0
|
||||
return desktopPrefs{}
|
||||
}
|
||||
var p desktopPrefs
|
||||
if json.Unmarshal(data, &p) != nil {
|
||||
return 0
|
||||
return desktopPrefs{}
|
||||
}
|
||||
if p.HTTPPort < 0 || p.HTTPPort > 65535 {
|
||||
return 0
|
||||
p.HTTPPort = 0
|
||||
}
|
||||
return p.HTTPPort
|
||||
return p
|
||||
}
|
||||
|
||||
func saveDesktopPrefs(p desktopPrefs) error {
|
||||
path, err := desktopPrefsFilePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(p, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, 0o600)
|
||||
}
|
||||
|
||||
// LoadDesktopPrefsHTTPPort returns http_port from prefs file, or 0 if unset / invalid (ephemeral port on each launch).
|
||||
func LoadDesktopPrefsHTTPPort() int {
|
||||
return loadDesktopPrefs().HTTPPort
|
||||
}
|
||||
|
||||
// LoadDesktopHTTPBindPublic returns whether the embedded API server should listen on all interfaces (0.0.0.0).
|
||||
func LoadDesktopHTTPBindPublic() bool {
|
||||
return loadDesktopPrefs().HTTPBindPublic
|
||||
}
|
||||
|
||||
// SaveDesktopHTTPPortPreference persists listen port preference. port 0 means use a random free port on each launch.
|
||||
@@ -58,14 +80,14 @@ func SaveDesktopHTTPPortPreference(port int) error {
|
||||
if port < 0 || port > 65535 {
|
||||
return fmt.Errorf("invalid port")
|
||||
}
|
||||
path, err := desktopPrefsFilePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := desktopPrefs{HTTPPort: port}
|
||||
data, err := json.MarshalIndent(p, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, 0o600)
|
||||
cur := loadDesktopPrefs()
|
||||
cur.HTTPPort = port
|
||||
return saveDesktopPrefs(cur)
|
||||
}
|
||||
|
||||
// SaveDesktopHTTPBindPublicPreference persists whether to listen on 0.0.0.0 for LAN/public access.
|
||||
func SaveDesktopHTTPBindPublicPreference(v bool) error {
|
||||
cur := loadDesktopPrefs()
|
||||
cur.HTTPBindPublic = v
|
||||
return saveDesktopPrefs(cur)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user