e613874de5
Second milestone of the Windows parity work (DESIGN-WINDOWS.md §13). WireGuard mesh client (signed, WSS-delivered): - wireguard_apply / wireguard_remove commands: Ed25519-verified, gated on a new wireguard capability. Linux applies via wg-quick up/down; Windows installs the peer config as a WireGuardTunnel service via wireguard.exe (/installtunnelservice, /uninstalltunnelservice) - state polling in the home monitor drives the blue tray icon and auto-VPN: connect when away from home + auto_vpn, disconnect on return (2m cooldown) - tray VPN toggle and the auto-VPN checkbox are now live; the preference persists to agent.yml (PersistAutoVPN) IAM on Windows (iam_windows.go): - allowed_login_groups -> net localgroup; ssh_keys -> per-profile authorized_keys + %ProgramData%\ssh\administrators_authorized_keys; revoke_users -> helper logs off all of the user's WTS sessions; sudo_rules logged as no direct equivalent Tray enrichment: - Open Config (opens agent.yml), Clear enrollment (re-enroll) menu items - set_auto_vpn persists; vpn_connect/vpn_disconnect/reinit/open_config commands handled by the daemon (tray_server.go) Packaging & release: - installer/windows/installer.iss: fully-offline Inno Setup bundle (agent, tray, helper, vendor-signed WireGuard MSI, OpenCredential CP, VC++ redist), /SILENT /SERVER_URL /JOIN_KEY parameters, SYSTEM service + HKLM Run tray autostart, Users-writable %ProgramData%\Theta42 for the IPC socket - .github/workflows/build-windows.yml: build + test, pinned vendor downloads, ISCC compile, Azure Trusted Signing (OIDC), SHA256SUMS, GH release attach, optional SSO resource publish - agent.yml.example documents auto_vpn, wireguard, service_name, desktop_helper, public_ip_detect Tests: - wireguard_apply/remove dispatch (allowed + capability-denied), PersistAutoVPN, ClearEnrollment; dispatch tests pin linuxPlatformOps with a temp WireGuard conf - end-to-end verified against a local mock SSO on Windows: join-key enrollment (token persisted, join key blanked), discovery/telemetry pushed, signed arbitrary_bash verified + executed via powershell -EncodedCommand; tray IPC socket binds %ProgramData%\Theta42; LDAP byte-pump binds 127.0.0.1:389; helper update swap verified Rebuilds all tracked dist binaries (v2.1.0).
243 lines
6.3 KiB
Go
243 lines
6.3 KiB
Go
package main
|
|
|
|
// Tray IPC server — runs inside the root daemon.
|
|
//
|
|
// Listens on /run/theta/tray.sock. Whenever the tray connects, it immediately
|
|
// gets the current status and then receives a push on every state change.
|
|
// Commands from the tray (auto-VPN toggle, connect/disconnect) come back over
|
|
// the same connection.
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
type trayServer struct {
|
|
mu sync.RWMutex
|
|
status TrayStatus
|
|
clients map[net.Conn]struct{}
|
|
}
|
|
|
|
var globalTrayServer = &trayServer{
|
|
clients: make(map[net.Conn]struct{}),
|
|
}
|
|
|
|
// Start begins listening on the tray socket. Call from main() as a goroutine.
|
|
func (ts *trayServer) Start() {
|
|
var l net.Listener
|
|
var boundPath string
|
|
var err error
|
|
|
|
for _, p := range TraySocketPaths {
|
|
os.Remove(p)
|
|
os.MkdirAll(filepath.Dir(p), 0755) //nolint:errcheck
|
|
l, err = net.Listen("unix", p)
|
|
if err == nil {
|
|
boundPath = p
|
|
os.Chmod(p, 0666) //nolint:errcheck
|
|
break
|
|
}
|
|
}
|
|
|
|
if err != nil || boundPath == "" {
|
|
log.Printf("[tray-ipc] cannot listen on tray socket: %v (tray icon disabled)", err)
|
|
return
|
|
}
|
|
|
|
log.Printf("[tray-ipc] listening on %s", boundPath)
|
|
for {
|
|
conn, err := l.Accept()
|
|
if err != nil {
|
|
log.Printf("[tray-ipc] accept error: %v", err)
|
|
return
|
|
}
|
|
go ts.handleConn(conn)
|
|
}
|
|
}
|
|
|
|
func (ts *trayServer) handleConn(conn net.Conn) {
|
|
ts.mu.Lock()
|
|
ts.clients[conn] = struct{}{}
|
|
// Send current status immediately on connect.
|
|
b, _ := encodeTrayStatus(ts.status)
|
|
conn.Write(b) //nolint:errcheck
|
|
ts.mu.Unlock()
|
|
|
|
defer func() {
|
|
ts.mu.Lock()
|
|
delete(ts.clients, conn)
|
|
ts.mu.Unlock()
|
|
conn.Close()
|
|
}()
|
|
|
|
scanner := bufio.NewScanner(conn)
|
|
for scanner.Scan() {
|
|
line := scanner.Bytes()
|
|
cmd, err := decodeTrayCommand(line)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
ts.handleCommand(cmd)
|
|
}
|
|
}
|
|
|
|
func (ts *trayServer) handleCommand(cmd TrayCommand) {
|
|
switch cmd.Command {
|
|
case "set_auto_vpn":
|
|
ts.mu.Lock()
|
|
ts.status.AutoVPN = cmd.Value
|
|
ts.mu.Unlock()
|
|
SetAutoVPN(cmd.Value)
|
|
if currentCM != nil {
|
|
if err := currentCM.PersistAutoVPN(cmd.Value); err != nil {
|
|
log.Printf("[tray-ipc] could not persist auto_vpn: %v", err)
|
|
}
|
|
}
|
|
log.Printf("[tray-ipc] auto_vpn set to %v", cmd.Value)
|
|
case "vpn_connect":
|
|
log.Printf("[tray-ipc] VPN connect requested")
|
|
if err := defaultPlatformOps.ConnectWireGuard(); err != nil {
|
|
log.Printf("[tray-ipc] VPN connect failed: %v", err)
|
|
}
|
|
case "vpn_disconnect":
|
|
log.Printf("[tray-ipc] VPN disconnect requested")
|
|
if err := defaultPlatformOps.DisconnectWireGuard(); err != nil {
|
|
log.Printf("[tray-ipc] VPN disconnect failed: %v", err)
|
|
}
|
|
case "reinit":
|
|
log.Printf("[tray-ipc] clearing enrollment (re-enroll requested)")
|
|
if currentCM != nil {
|
|
if err := currentCM.ClearEnrollment(); err != nil {
|
|
log.Printf("[tray-ipc] could not clear enrollment: %v", err)
|
|
}
|
|
}
|
|
case "open_config":
|
|
log.Printf("[tray-ipc] opening config %s", defaultConfigPath())
|
|
openInDefaultViewer(defaultConfigPath())
|
|
default:
|
|
log.Printf("[tray-ipc] unknown command: %q", cmd.Command)
|
|
}
|
|
}
|
|
|
|
// openInDefaultViewer opens a file/folder with the platform default handler.
|
|
func openInDefaultViewer(path string) {
|
|
if runtime.GOOS == "windows" {
|
|
// explorer /select,<path> opens the parent folder with the file
|
|
// selected. explorer is a GUI app, so no console window appears.
|
|
_ = exec.Command("explorer", "/select,"+path).Start()
|
|
return
|
|
}
|
|
_ = exec.Command("xdg-open", path).Start()
|
|
}
|
|
|
|
// Push broadcasts an updated status to all connected tray clients.
|
|
func (ts *trayServer) Push(status TrayStatus) {
|
|
ts.mu.Lock()
|
|
defer ts.mu.Unlock()
|
|
ts.status = status
|
|
if len(ts.clients) == 0 {
|
|
return
|
|
}
|
|
b, err := encodeTrayStatus(status)
|
|
if err != nil {
|
|
return
|
|
}
|
|
for conn := range ts.clients {
|
|
_, err := conn.Write(b)
|
|
if err != nil {
|
|
// Dead connection; handleConn will clean it up.
|
|
conn.Close()
|
|
}
|
|
}
|
|
}
|
|
|
|
// UpdateTrayStatus computes the current TrayColor from the known state
|
|
// and pushes it to all connected tray clients.
|
|
func UpdateTrayStatus(connected bool, agentPublicIP, homePublicIP string, vpnActive, autoVPN bool, siteName, serverURL string) {
|
|
color := ColorRed
|
|
statusText := "Not connected to directory"
|
|
isHome := false
|
|
|
|
if connected {
|
|
// Server URL is local (localhost, 127.0.0.1, LAN IP, or .local)
|
|
isLocalServer := strings.Contains(serverURL, "localhost") ||
|
|
strings.Contains(serverURL, "127.0.0.1") ||
|
|
strings.Contains(serverURL, ".local") ||
|
|
strings.Contains(serverURL, "192.168.") ||
|
|
strings.Contains(serverURL, "10.")
|
|
|
|
// On Home LAN if:
|
|
// 1) Both agent & home public IPs are known and match, OR
|
|
// 2) Connecting to a local/LAN SSO server, OR
|
|
// 3) homePublicIP is not yet set by directory (default to local home)
|
|
if (homePublicIP != "" && agentPublicIP != "" && agentPublicIP == homePublicIP) || isLocalServer || homePublicIP == "" {
|
|
isHome = true
|
|
}
|
|
|
|
if vpnActive {
|
|
color = ColorBlue
|
|
statusText = fmt.Sprintf("VPN active → %s", siteName)
|
|
} else if isHome {
|
|
color = ColorGreen
|
|
statusText = fmt.Sprintf("Home — %s", siteName)
|
|
} else {
|
|
color = ColorYellow
|
|
statusText = "Connected (away from home)"
|
|
}
|
|
}
|
|
|
|
globalTrayServer.Push(TrayStatus{
|
|
Color: color,
|
|
Connected: connected,
|
|
IsHome: isHome,
|
|
VPNActive: vpnActive,
|
|
AutoVPN: autoVPN,
|
|
SiteName: siteName,
|
|
AgentPublicIP: agentPublicIP,
|
|
HomePublicIP: homePublicIP,
|
|
StatusText: statusText,
|
|
})
|
|
}
|
|
|
|
// sendTrayCommand sends a single JSON command to the daemon from the tray process.
|
|
func sendTrayCommand(cmd TrayCommand) error {
|
|
conn, err := net.Dial("unix", TraySocket)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot connect to daemon IPC socket: %w", err)
|
|
}
|
|
defer conn.Close()
|
|
return json.NewEncoder(conn).Encode(cmd)
|
|
}
|
|
|
|
// receiveTrayStatus opens a persistent connection and calls cb on every status
|
|
// update. Blocks until the connection is lost. Call in a goroutine.
|
|
func receiveTrayStatus(cb func(TrayStatus)) error {
|
|
conn, err := net.Dial("unix", TraySocket)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot connect to daemon IPC socket: %w", err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
scanner := bufio.NewScanner(conn)
|
|
for scanner.Scan() {
|
|
s, err := decodeTrayStatus(scanner.Bytes())
|
|
if err == nil {
|
|
cb(s)
|
|
}
|
|
}
|
|
if err := scanner.Err(); err != nil && err != io.EOF {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|