Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cmd/cleanup/cleanup_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build !windows

package cleanup

import (
"errors"
"os"
"runtime"

"github.com/fosrl/cli/internal/logger"
"github.com/fosrl/cli/internal/olmdns"
"github.com/spf13/cobra"
)

func CleanupCmd() *cobra.Command {
interfaceName := olmdns.DefaultInterfaceName

cmd := &cobra.Command{
Use: "cleanup",
Short: "Clean up stale DNS configuration",
Long: `Remove stale DNS configuration left from an unclean shutdown.

This is useful if the client was killed while a tunnel was active and system DNS
was not restored. The same cleanup runs automatically before starting a connection.`,
RunE: func(cmd *cobra.Command, args []string) error {
if runtime.GOOS != "windows" && os.Geteuid() != 0 {
return errors.New("elevated permissions are required to clean up DNS configuration")
}

if err := olmdns.CleanupStaleState(interfaceName); err != nil {
logger.Error("Failed to clean up stale DNS configuration: %v", err)
return err
}

logger.Success("Stale DNS configuration cleaned up")
return nil
},
}

cmd.Flags().StringVar(&interfaceName, "interface-name", olmdns.DefaultInterfaceName, "WireGuard interface `name` used when the tunnel was active")

return cmd
}
9 changes: 9 additions & 0 deletions cmd/cleanup/cleanup_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build windows

package cleanup

import "github.com/spf13/cobra"

func CleanupCmd() *cobra.Command {
return nil
}
13 changes: 11 additions & 2 deletions cmd/down/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@ import (

"github.com/fosrl/cli/internal/config"
"github.com/fosrl/cli/internal/logger"
"github.com/fosrl/cli/internal/olmdns"
"github.com/fosrl/cli/internal/olm"
"github.com/fosrl/cli/internal/tui"
"github.com/spf13/cobra"
)

func ClientDownCmd() *cobra.Command {
interfaceName := olmdns.DefaultInterfaceName

cmd := &cobra.Command{
Use: "client",
Short: "Stop the client connection",
Long: "Stop the currently running client connection",
Run: func(cmd *cobra.Command, args []string) {
if err := clientDownMain(cmd); err != nil {
if err := clientDownMain(cmd, interfaceName); err != nil {
os.Exit(1)
}
},
}

cmd.Flags().StringVar(&interfaceName, "interface-name", olmdns.DefaultInterfaceName, "WireGuard interface `name` used when the tunnel was active")

return cmd
}

func clientDownMain(cmd *cobra.Command) error {
func clientDownMain(cmd *cobra.Command, interfaceName string) error {
cfg := config.ConfigFromContext(cmd.Context())

// Get socket path from config or use default
Expand Down Expand Up @@ -88,5 +93,9 @@ func clientDownMain(cmd *cobra.Command) error {
logger.Info("Client shutdown initiated: %s", exitResp.Status)
}

if err := olmdns.CleanupStaleState(interfaceName); err != nil {
logger.Warning("Failed to clean up stale DNS configuration: %v", err)
}

return nil
}
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/fosrl/cli/cmd/auth/login"
"github.com/fosrl/cli/cmd/auth/logout"
"github.com/fosrl/cli/cmd/authdaemon"
"github.com/fosrl/cli/cmd/cleanup"
"github.com/fosrl/cli/cmd/down"
"github.com/fosrl/cli/cmd/list"
"github.com/fosrl/cli/cmd/logs"
Expand Down Expand Up @@ -60,6 +61,9 @@ func RootCommand(initResources bool) (*cobra.Command, error) {
if downCmd := down.DownCmd(); downCmd != nil {
cmd.AddCommand(downCmd)
}
if cleanupCmd := cleanup.CleanupCmd(); cleanupCmd != nil {
cmd.AddCommand(cleanupCmd)
}
if logsCmd := logs.LogsCmd(); logsCmd != nil {
cmd.AddCommand(logsCmd)
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/up/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/fosrl/cli/internal/config"
"github.com/fosrl/cli/internal/fingerprint"
"github.com/fosrl/cli/internal/logger"
"github.com/fosrl/cli/internal/olmdns"
"github.com/fosrl/cli/internal/olm"
"github.com/fosrl/cli/internal/tui"
"github.com/fosrl/cli/internal/utils"
Expand Down Expand Up @@ -603,6 +604,11 @@ func clientUpMain(cmd *cobra.Command, opts *ClientUpCmdOpts, extraArgs []string)
}
}

// Clean up DNS left from an unclean shutdown before any network operations.
if err := olmdns.CleanupStaleState(opts.InterfaceName); err != nil {
logger.Warning("Failed to clean up stale DNS configuration: %v", err)
}

olm, err := olmpkg.Init(ctx, olmInitConfig)
if err != nil {
logger.Error("Error: failed to init olm: %v", err)
Expand Down
16 changes: 16 additions & 0 deletions internal/olmdns/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package olmdns

import (
override "github.com/fosrl/olm/dns/override"
)

const DefaultInterfaceName = "pangolin"

// CleanupStaleState removes DNS configuration left from an unclean shutdown
// (for example, killing the process while the tunnel was active).
func CleanupStaleState(interfaceName string) error {
if interfaceName == "" {
interfaceName = DefaultInterfaceName
}
return override.CleanupStaleState(interfaceName)
}
Loading