From bab57783e2d62e6ff963ec6106b8ab0381520a42 Mon Sep 17 00:00:00 2001 From: miloschwartz Date: Tue, 17 Nov 2026 15:31:46 -0500 Subject: [PATCH 1/2] add companion --- cmd/companion/companion_desktop.go | 2 +- cmd/companion/companion_other.go | 2 +- cmd/up/client/client.go | 10 +++++ go.mod | 4 +- go.sum | 4 -- internal/companion/accounts_hint_desktop.go | 2 +- internal/companion/accounts_hint_other.go | 2 +- ...rovider_desktop.go => desktop_provider.go} | 43 +++++++++++-------- internal/companion/provider_darwin.go | 19 +++++++- internal/companion/provider_linux.go | 7 ++- internal/companion/provider_windows.go | 13 +++--- internal/companion/secrets_darwin.go | 38 ++++++++++++++++ internal/companion/secrets_desktop.go | 14 +++--- internal/companion/secrets_windows.go | 2 + internal/companion/version.go | 2 +- internal/config/config.go | 6 +++ internal/olm/companion_darwin.go | 7 +++ 17 files changed, 133 insertions(+), 44 deletions(-) rename internal/companion/{provider_desktop.go => desktop_provider.go} (68%) create mode 100644 internal/companion/secrets_darwin.go create mode 100644 internal/olm/companion_darwin.go diff --git a/cmd/companion/companion_desktop.go b/cmd/companion/companion_desktop.go index 18c7228..e7a637f 100644 --- a/cmd/companion/companion_desktop.go +++ b/cmd/companion/companion_desktop.go @@ -1,4 +1,4 @@ -//go:build windows || linux +//go:build windows || linux || darwin package companioncmd diff --git a/cmd/companion/companion_other.go b/cmd/companion/companion_other.go index c49bb45..87d0ba7 100644 --- a/cmd/companion/companion_other.go +++ b/cmd/companion/companion_other.go @@ -1,4 +1,4 @@ -//go:build !windows && !linux +//go:build !windows && !linux && !darwin package companioncmd diff --git a/cmd/up/client/client.go b/cmd/up/client/client.go index c246cf7..7b5f268 100644 --- a/cmd/up/client/client.go +++ b/cmd/up/client/client.go @@ -17,6 +17,7 @@ import ( "time" "github.com/fosrl/cli/internal/api" + "github.com/fosrl/cli/internal/companion" "github.com/fosrl/cli/internal/config" "github.com/fosrl/cli/internal/fingerprint" "github.com/fosrl/cli/internal/logger" @@ -396,6 +397,15 @@ func clientUpMain(cmd *cobra.Command, opts *ClientUpCmdOpts, extraArgs []string) opts.MatchDomains = cfg.GetStringSlice("up.match_domains_dns") } + if opts.ID == "" && opts.Secret == "" { + if state := companion.StateFromContext(cmd.Context()); state.Enabled { + err := fmt.Errorf("companion mode is enabled, so connect from %s", state.ProviderName) + logger.Error("Error: %v", err) + logger.Info("To run the tunnel from the CLI, pass --id and --secret, or run 'pangolin companion disable'.") + return err + } + } + // Check if a client is already running olmClient := olm.NewClient("") if olmClient.IsRunning() { diff --git a/go.mod b/go.mod index 7578d74..9a0a9fe 100644 --- a/go.mod +++ b/go.mod @@ -132,6 +132,6 @@ require ( // If changes to Olm or Newt are required, use these // replace directives during development. // -//replace github.com/fosrl/olm => ../olm +replace github.com/fosrl/olm => ../olm -//replace github.com/fosrl/newt => ../newt +replace github.com/fosrl/newt => ../newt diff --git a/go.sum b/go.sum index 1851c36..a159923 100644 --- a/go.sum +++ b/go.sum @@ -74,10 +74,6 @@ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc= github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE= -github.com/fosrl/newt v1.20.0 h1:c56gsdw1xGFZxcax2fYUEz4qKVnCxkmnBMjQj2qAbic= -github.com/fosrl/newt v1.20.0/go.mod h1:2chBJ4TyLOmRqligDe3M5HXxpIWh1MWP3JS1fHSIMSM= -github.com/fosrl/olm v1.10.2 h1:pj72Fu+b+fzydOv4QZRi+CpeM/iLKL+5jyrk1+oqlVY= -github.com/fosrl/olm v1.10.2/go.mod h1:EBZ9lHq5cbHxIP3J9WyL3w8jlkso8qWKI9vIalOMdWE= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho= diff --git a/internal/companion/accounts_hint_desktop.go b/internal/companion/accounts_hint_desktop.go index 93ed2f4..f9ecd3c 100644 --- a/internal/companion/accounts_hint_desktop.go +++ b/internal/companion/accounts_hint_desktop.go @@ -1,4 +1,4 @@ -//go:build windows || linux +//go:build windows || linux || darwin package companion diff --git a/internal/companion/accounts_hint_other.go b/internal/companion/accounts_hint_other.go index ba8bb76..bdaba7b 100644 --- a/internal/companion/accounts_hint_other.go +++ b/internal/companion/accounts_hint_other.go @@ -1,4 +1,4 @@ -//go:build !windows && !linux +//go:build !windows && !linux && !darwin package companion diff --git a/internal/companion/provider_desktop.go b/internal/companion/desktop_provider.go similarity index 68% rename from internal/companion/provider_desktop.go rename to internal/companion/desktop_provider.go index a1e1715..54748ab 100644 --- a/internal/companion/provider_desktop.go +++ b/internal/companion/desktop_provider.go @@ -1,4 +1,4 @@ -//go:build windows || linux +//go:build windows || linux || darwin package companion @@ -14,11 +14,15 @@ import ( const accountsFileName = "accounts.json" -// desktopProvider reads the Pangolin desktop client's session, on Windows and -// Linux alike: account details from its accounts.json, and secrets from its -// manager service, which only the manager can read. +// desktopProvider loads the session of the Pangolin desktop app (Windows, +// Linux, or macOS). Account details live in an accounts.json of the same +// shape; secrets use the framed protocol in secrets_desktop.go. Platforms +// differ only in where the data lives and how secrets are fetched. type desktopProvider struct { - secrets SecretsClient + defaultDataDir string + // secrets returns the client that fetches secrets for the app whose + // data is in dataDir. + secrets func(dataDir string) SecretsClient } type desktopAccountsFile struct { @@ -39,8 +43,8 @@ func (desktopProvider) Name() string { return olm.CompanionAgentName } -func (desktopProvider) DefaultDataDir() string { - return defaultDesktopDataDir() +func (p desktopProvider) DefaultDataDir() string { + return p.defaultDataDir } func (p desktopProvider) LoadSession(dataDir string) (*Session, error) { @@ -65,9 +69,10 @@ func (p desktopProvider) LoadSession(dataDir string) (*Session, error) { return nil, nil } + secretsClient := p.secrets(dataDir) accounts := make(map[string]config.Account, len(file.Accounts)) - for userID, appAccount := range file.Accounts { - secrets, err := p.secrets.GetUserSecrets(userID) + for userID, desktopAccount := range file.Accounts { + secrets, err := secretsClient.GetUserSecrets(userID) if err != nil { logger.Debug("companion: failed to load secrets for %s: %v", userID, err) continue @@ -76,7 +81,7 @@ func (p desktopProvider) LoadSession(dataDir string) (*Session, error) { continue } - account := mapDesktopAccount(appAccount, secrets) + account := mapDesktopAccount(desktopAccount, secrets) accounts[userID] = account } @@ -107,20 +112,20 @@ func (p desktopProvider) LoadSession(dataDir string) (*Session, error) { return session, nil } -func mapDesktopAccount(app desktopAccount, secrets UserSecrets) config.Account { +func mapDesktopAccount(src desktopAccount, secrets UserSecrets) config.Account { account := config.Account{ - UserID: app.UserID, - Host: app.Hostname, - Email: app.Email, - OrgID: app.OrgID, + UserID: src.UserID, + Host: src.Hostname, + Email: src.Email, + OrgID: src.OrgID, SessionToken: secrets.SessionToken, } - if app.Username != "" { - username := app.Username + if src.Username != "" { + username := src.Username account.Username = &username } - if app.Name != "" { - name := app.Name + if src.Name != "" { + name := src.Name account.Name = &name } if secrets.OlmID != "" || secrets.OlmSecret != "" { diff --git a/internal/companion/provider_darwin.go b/internal/companion/provider_darwin.go index f173e9e..cb18b61 100644 --- a/internal/companion/provider_darwin.go +++ b/internal/companion/provider_darwin.go @@ -2,7 +2,22 @@ package companion -// macOS companion mode is not implemented yet. Return nil so the CLI uses standalone auth. +import ( + "path/filepath" + + "github.com/fosrl/cli/internal/config" +) + +const darwinAppName = "Pangolin" + func platformProvider() Provider { - return nil + // The original user's home, so the CLI under sudo still finds their app. + dataDir := "" + if home, err := config.UserHomeDir(); err == nil { + dataDir = filepath.Join(home, "Library", "Application Support", darwinAppName) + } + return desktopProvider{ + defaultDataDir: dataDir, + secrets: newSocketSecretsClient, + } } diff --git a/internal/companion/provider_linux.go b/internal/companion/provider_linux.go index 89d8434..4f84dbf 100644 --- a/internal/companion/provider_linux.go +++ b/internal/companion/provider_linux.go @@ -39,7 +39,12 @@ func platformProvider() Provider { if !desktopAppInstalled() { return nil } - return desktopProvider{secrets: newSocketSecretsClient()} + return desktopProvider{ + defaultDataDir: defaultDesktopDataDir(), + secrets: func(string) SecretsClient { + return newSocketSecretsClient() + }, + } } func desktopAppInstalled() bool { diff --git a/internal/companion/provider_windows.go b/internal/companion/provider_windows.go index 31db43a..2ab8589 100644 --- a/internal/companion/provider_windows.go +++ b/internal/companion/provider_windows.go @@ -9,14 +9,15 @@ import ( const windowsAppName = "Pangolin" -func defaultDesktopDataDir() string { +func platformProvider() Provider { appData := os.Getenv("LOCALAPPDATA") if appData == "" { appData = os.Getenv("APPDATA") } - return filepath.Join(appData, windowsAppName) -} - -func platformProvider() Provider { - return desktopProvider{secrets: newPipeSecretsClient()} + return desktopProvider{ + defaultDataDir: filepath.Join(appData, windowsAppName), + secrets: func(string) SecretsClient { + return newPipeSecretsClient() + }, + } } diff --git a/internal/companion/secrets_darwin.go b/internal/companion/secrets_darwin.go new file mode 100644 index 0000000..c9b9cf4 --- /dev/null +++ b/internal/companion/secrets_darwin.go @@ -0,0 +1,38 @@ +//go:build darwin + +package companion + +import ( + "fmt" + "net" + "path/filepath" + "time" +) + +// cliSecretsSocketName is the Unix socket Pangolin for macOS listens on, next +// to its accounts.json. The app checks that the caller runs as the same user +// (or root, for the CLI under sudo). +const cliSecretsSocketName = "cli.sock" + +const cliSecretsTimeout = 3 * time.Second + +// socketSecretsClient asks the running Pangolin macOS app for secrets, which +// it keeps in its own Keychain items. +type socketSecretsClient struct { + socketPath string +} + +func newSocketSecretsClient(dataDir string) SecretsClient { + return &socketSecretsClient{socketPath: filepath.Join(dataDir, cliSecretsSocketName)} +} + +func (c *socketSecretsClient) GetUserSecrets(userID string) (UserSecrets, error) { + conn, err := net.DialTimeout("unix", c.socketPath, cliSecretsTimeout) + if err != nil { + return UserSecrets{}, fmt.Errorf("connect to desktop app secrets socket: %w", err) + } + defer conn.Close() + _ = conn.SetDeadline(time.Now().Add(cliSecretsTimeout)) + + return requestSecrets(conn, userID) +} diff --git a/internal/companion/secrets_desktop.go b/internal/companion/secrets_desktop.go index 6acc308..5919ae1 100644 --- a/internal/companion/secrets_desktop.go +++ b/internal/companion/secrets_desktop.go @@ -1,4 +1,4 @@ -//go:build windows || linux +//go:build windows || linux || darwin package companion @@ -9,26 +9,30 @@ import ( "io" ) +// The desktop app serves secrets with a small framed protocol, the same on +// every platform: the request is a little-endian uint32 length and the +// Pangolin user id; the reply is a uint32 status, a uint32 length and a +// payload (secrets JSON, or an error message). Only the transport differs. + const ( secretsStatusOK uint32 = 0 secretsStatusNotFound uint32 = 1 secretsStatusError uint32 = 2 ) -// UserSecrets matches the desktop client manager's secret store JSON shape. +// UserSecrets matches the desktop app's secret store JSON shape. type UserSecrets struct { SessionToken string `json:"sessionToken,omitempty"` OlmID string `json:"olmId,omitempty"` OlmSecret string `json:"olmSecret,omitempty"` } -// SecretsClient fetches user secrets from the desktop client's manager service. +// SecretsClient fetches user secrets from the desktop app. type SecretsClient interface { GetUserSecrets(userID string) (UserSecrets, error) } -// requestSecrets sends a secrets request for userID on a connection to the -// manager and reads the reply. The framing is the same on every platform. +// requestSecrets sends one secrets request on conn and decodes the reply. func requestSecrets(conn io.ReadWriter, userID string) (UserSecrets, error) { userIDBytes := []byte(userID) if err := binary.Write(conn, binary.LittleEndian, uint32(len(userIDBytes))); err != nil { diff --git a/internal/companion/secrets_windows.go b/internal/companion/secrets_windows.go index bcc9122..caeb4f2 100644 --- a/internal/companion/secrets_windows.go +++ b/internal/companion/secrets_windows.go @@ -11,6 +11,8 @@ import ( const cliSecretsPipePath = `\\.\pipe\pangolin-manager-cli-secrets` +// pipeSecretsClient asks the Pangolin Manager service for secrets over its +// named pipe. type pipeSecretsClient struct { pipePath string } diff --git a/internal/companion/version.go b/internal/companion/version.go index d24d69b..49ad60d 100644 --- a/internal/companion/version.go +++ b/internal/companion/version.go @@ -4,7 +4,7 @@ import "runtime" const ( windowsMinDesktopAppVersion = "0.11.0" - darwinMinDesktopAppVersion = "" + darwinMinDesktopAppVersion = "0.12.3" // Every Pangolin Linux client release supports companion mode. linuxMinDesktopAppVersion = "" ) diff --git a/internal/config/config.go b/internal/config/config.go index 4eb5428..dcfc9e0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -604,6 +604,12 @@ func GetPangolinConfigDir() (string, error) { return pangolinDir, nil } +// UserHomeDir returns the home directory of the user who invoked the CLI, +// even under sudo. See userHomeDir. +func UserHomeDir() (string, error) { + return userHomeDir() +} + // userHomeDir returns the home directory of the original user // (the user who invoked the command, not the effective user when running with sudo). // This ensures that config files work both with and without sudo. diff --git a/internal/olm/companion_darwin.go b/internal/olm/companion_darwin.go new file mode 100644 index 0000000..4673444 --- /dev/null +++ b/internal/olm/companion_darwin.go @@ -0,0 +1,7 @@ +//go:build darwin + +package olm + +// CompanionAgentName is the OLM agent identifier used by Pangolin for macOS. +// Must match the agent the macOS app's packet tunnel passes to OLM. +const CompanionAgentName = "Pangolin macOS" From e48fda3c1879d62538b15327abb7a3c8e8231451 Mon Sep 17 00:00:00 2001 From: miloschwartz Date: Thu, 19 Nov 2026 16:40:55 -0500 Subject: [PATCH 2/2] add companion notice --- go.mod | 4 ++-- go.sum | 4 ++++ internal/companion/version.go | 2 +- internal/notice/notices.go | 16 +++++++++++++--- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index a5eefe9..5bfdfb0 100644 --- a/go.mod +++ b/go.mod @@ -132,6 +132,6 @@ require ( // If changes to Olm or Newt are required, use these // replace directives during development. // -replace github.com/fosrl/olm => ../olm +// replace github.com/fosrl/olm => ../olm -replace github.com/fosrl/newt => ../newt +// replace github.com/fosrl/newt => ../newt diff --git a/go.sum b/go.sum index ba90abb..c641cf0 100644 --- a/go.sum +++ b/go.sum @@ -74,6 +74,10 @@ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc= github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE= +github.com/fosrl/newt v1.20.1 h1:2wLcU+8CHJNoEq24yKTuIzZUK6+7/r3Bk/ovAkQRA6c= +github.com/fosrl/newt v1.20.1/go.mod h1:8Cs6g5OUvcPGjQXzMhJtpIR3oeo8u4EsegHmlUPY+/g= +github.com/fosrl/olm v1.10.2 h1:pj72Fu+b+fzydOv4QZRi+CpeM/iLKL+5jyrk1+oqlVY= +github.com/fosrl/olm v1.10.2/go.mod h1:EBZ9lHq5cbHxIP3J9WyL3w8jlkso8qWKI9vIalOMdWE= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho= diff --git a/internal/companion/version.go b/internal/companion/version.go index 49ad60d..89fb6b2 100644 --- a/internal/companion/version.go +++ b/internal/companion/version.go @@ -4,7 +4,7 @@ import "runtime" const ( windowsMinDesktopAppVersion = "0.11.0" - darwinMinDesktopAppVersion = "0.12.3" + darwinMinDesktopAppVersion = "0.14.0" // Every Pangolin Linux client release supports companion mode. linuxMinDesktopAppVersion = "" ) diff --git a/internal/notice/notices.go b/internal/notice/notices.go index 38ca3b8..b990240 100644 --- a/internal/notice/notices.go +++ b/internal/notice/notices.go @@ -8,9 +8,10 @@ import ( ) const ( - companionModeIntroID = "companion-mode-intro-windows-v1" - companionModeIntroLinuxID = "companion-mode-intro-linux-v1" - desktopClientLinuxID = "desktop-client-available-linux-v1" + companionModeIntroID = "companion-mode-intro-windows-v1" + companionModeIntroDarwinID = "companion-mode-intro-darwin-v1" + companionModeIntroLinuxID = "companion-mode-intro-linux-v1" + desktopClientLinuxID = "desktop-client-available-linux-v1" ) const downloadsURL = "https://pangolin.net/downloads" @@ -25,6 +26,15 @@ var registeredNotices = []Notice{ }, Lines: companionModeIntroLines, }, + { + ID: companionModeIntroDarwinID, + MinVersion: "0.23.0", + MaxVersion: "0.23.0", + Condition: func(cfg *config.Config) bool { + return runtime.GOOS == "darwin" && companion.PlatformAvailable() + }, + Lines: companionModeIntroLines, + }, { // Not tied to a CLI version: Linux offers companion mode only once // the desktop client is installed, which can happen at any time.