Events to the frontend go through App.emit, and a test keeps their names in step with wails.ts. QEMU argument splitting, hidden console processes, the macOS release record, the running check and a few smaller helpers now exist once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
)
|
|
|
|
func (a *App) hostDNSConfigLocked() guesttools.HostDNSConfig {
|
|
return guesttools.HostDNSConfig{Mode: a.cfg.HostDNSMode, DoHURL: a.cfg.HostDNSDoHURL}
|
|
}
|
|
|
|
func (a *App) GetHostDNSConfig() guesttools.HostDNSConfig {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return a.hostDNSConfigLocked().WithDefaults()
|
|
}
|
|
|
|
func (a *App) SetHostDNSConfig(cfg guesttools.HostDNSConfig) error {
|
|
if err := cfg.Validate(); err != nil {
|
|
return err
|
|
}
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
prev := a.hostDNSConfigLocked()
|
|
a.cfg.HostDNSMode, a.cfg.HostDNSDoHURL = cfg.Mode, cfg.DoHURL
|
|
if err := a.saveConfigLocked(); err != nil {
|
|
a.cfg.HostDNSMode, a.cfg.HostDNSDoHURL = prev.Mode, prev.DoHURL
|
|
return err
|
|
}
|
|
if a.guestTools != nil {
|
|
a.guestTools.SetHostDNSConfig(cfg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *App) TestHostDNS(cfg guesttools.HostDNSConfig) (guesttools.HostDNSTestResult, error) {
|
|
if err := cfg.Validate(); err != nil {
|
|
return guesttools.HostDNSTestResult{}, err
|
|
}
|
|
return guesttools.TestHostDNSProxy(a.baseCtx(), cfg, "example.com")
|
|
}
|
|
|
|
type GuestToolsHostDNSStatus struct {
|
|
HostDNSEnabled bool `json:"host_dns_enabled"`
|
|
HostDNSActive bool `json:"host_dns_active"`
|
|
HostDNSReported bool `json:"host_dns_reported"`
|
|
HostDNSConfigStale bool `json:"host_dns_config_stale"`
|
|
}
|
|
|
|
func (a *App) GetGuestToolsHostDNS(name string) (GuestToolsHostDNSStatus, error) {
|
|
epoch := a.vmDeleteEpoch(name)
|
|
d, err := a.getVMDetail(name, epoch)
|
|
if err != nil {
|
|
return GuestToolsHostDNSStatus{}, err
|
|
}
|
|
unlock := a.lockGuestToolsVM(d.Name)
|
|
defer unlock()
|
|
if a.vmDeleteEpoch(d.Name) != epoch {
|
|
return GuestToolsHostDNSStatus{}, os.ErrNotExist
|
|
}
|
|
cfg, _, err := guesttools.ReadVMConfigStrict(d.Directory)
|
|
if err != nil {
|
|
return GuestToolsHostDNSStatus{}, err
|
|
}
|
|
status := GuestToolsHostDNSStatus{HostDNSEnabled: cfg.HostDNSEnabled}
|
|
srv, _, err := a.ensureGuestToolsBridge()
|
|
if err != nil {
|
|
return GuestToolsHostDNSStatus{}, err
|
|
}
|
|
live := srv.Status(d.Name, 45*time.Second)
|
|
status.HostDNSActive = live.HostDNSActive
|
|
status.HostDNSReported = live.HostDNSReported
|
|
status.HostDNSConfigStale = live.HostDNSConfigStale
|
|
return status, nil
|
|
}
|
|
|
|
func (a *App) SetGuestToolsHostDNS(name string, enabled bool) error {
|
|
if err := a.requireWritableDataRoot(); err != nil {
|
|
return err
|
|
}
|
|
epoch := a.vmDeleteEpoch(name)
|
|
d, err := a.getVMDetail(name, epoch)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.Directory == "" {
|
|
return fmt.Errorf("VM %s has no directory", name)
|
|
}
|
|
srv, port, err := a.ensureGuestToolsBridge()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
unlock := a.lockGuestToolsVM(d.Name)
|
|
defer unlock()
|
|
if a.vmDeleteEpoch(d.Name) != epoch {
|
|
return os.ErrNotExist
|
|
}
|
|
if err := requireGuestToolsVM(d.Directory); err != nil {
|
|
return err
|
|
}
|
|
if !enabled {
|
|
live := srv.Status(d.Name, 45*time.Second)
|
|
if live.HostDNSActive || live.HostDNSConfigStale {
|
|
return fmt.Errorf("remove the Host DNS proxy in macOS before disabling it")
|
|
}
|
|
}
|
|
cfg, err := guesttools.LoadOrCreateVMConfig(d.Directory, d.Name, a.vmSharedDir(d.Name), port)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cfg.HostDNSEnabled = enabled
|
|
if err := guesttools.SaveVMConfig(d.Directory, cfg); err != nil {
|
|
return err
|
|
}
|
|
if err := a.registerGuestToolsConfig(srv, d.Directory, cfg); err != nil {
|
|
return err
|
|
}
|
|
a.emitStateChanged()
|
|
return nil
|
|
}
|