From 9c2bfd5ff4b6e145eb7f9c880d067f8ace74c2b5 Mon Sep 17 00:00:00 2001 From: Paulo Almeida Date: Wed, 23 Oct 2024 11:01:23 +0100 Subject: [PATCH] refactor: add a tailscale package --- internal/proxymanager/proxymanager.go | 13 +++++--- internal/proxymanager/tailscale.go | 16 --------- internal/tailscale/tailscale.go | 48 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 20 deletions(-) delete mode 100644 internal/proxymanager/tailscale.go create mode 100644 internal/tailscale/tailscale.go diff --git a/internal/proxymanager/proxymanager.go b/internal/proxymanager/proxymanager.go index f9c4f1c..b3eec63 100644 --- a/internal/proxymanager/proxymanager.go +++ b/internal/proxymanager/proxymanager.go @@ -11,7 +11,6 @@ import ( "sync" "github.com/rs/zerolog/log" - "tailscale.com/tsnet" ctypes "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/events" @@ -20,6 +19,7 @@ import ( "github.com/almeidapaulopt/tsdproxy/internal/containers" "github.com/almeidapaulopt/tsdproxy/internal/core" + "github.com/almeidapaulopt/tsdproxy/internal/tailscale" ) type ProxyManager struct { @@ -31,7 +31,7 @@ type ProxyManager struct { } type Proxy struct { - TsServer *tsnet.Server + TsServer *tailscale.TsNetServer reverseProxy *httputil.ReverseProxy container *containers.Container URL *url.URL @@ -128,11 +128,16 @@ func (pm *ProxyManager) SetupProxy(ctx context.Context, containerID string) { reverseProxy := httputil.NewSingleHostReverseProxy(targetURL) // Create the tsnet server - server := pm.GetTsNetServer(proxyURL.Hostname()) + server := tailscale.NewTsNetServer(proxyURL.Hostname(), pm.config, pm.Log) defer server.Close() + if err := server.Start(ctx); err != nil { + pm.Log.Error().Err(err).Str("containerID", containerID).Msg("Error starting server") + return + } + // Create the TLS listener - ln, err := server.ListenTLS("tcp", ":443") + ln, err := server.TsServer.ListenTLS("tcp", ":443") if err != nil { pm.Log.Error().Err(err).Str("containerID", containerID).Msg("Error listening on TLS") return diff --git a/internal/proxymanager/tailscale.go b/internal/proxymanager/tailscale.go deleted file mode 100644 index db68381..0000000 --- a/internal/proxymanager/tailscale.go +++ /dev/null @@ -1,16 +0,0 @@ -package proxymanager - -import ( - "path/filepath" - - "tailscale.com/tsnet" -) - -func (pm *ProxyManager) GetTsNetServer(hostname string) *tsnet.Server { - return &tsnet.Server{ - Hostname: hostname, - AuthKey: pm.config.AuthKey, - Dir: filepath.Join(pm.config.DataDir, hostname), - Ephemeral: true, - } -} diff --git a/internal/tailscale/tailscale.go b/internal/tailscale/tailscale.go new file mode 100644 index 0000000..06c2d47 --- /dev/null +++ b/internal/tailscale/tailscale.go @@ -0,0 +1,48 @@ +package tailscale + +import ( + "context" + "fmt" + "path/filepath" + + "github.com/almeidapaulopt/tsdproxy/internal/core" + "tailscale.com/tsnet" +) + +type TsNetServer struct { + TsServer *tsnet.Server +} + +func NewTsNetServer(hostname string, config *core.Config, logger *core.Logger) *TsNetServer { + return &TsNetServer{ + &tsnet.Server{ + Hostname: hostname, + AuthKey: config.AuthKey, + Dir: filepath.Join(config.DataDir, hostname), + Ephemeral: true, + Logf: func(format string, args ...any) { + logger.Info().Msgf(format, args...) + }, + UserLogf: func(format string, args ...any) { + logger.Info().Msgf(format, args...) + }, + }, + } +} + +func (tn *TsNetServer) Close() error { + return tn.TsServer.Close() +} + +func (tn *TsNetServer) Start(ctx context.Context) error { + if err := tn.TsServer.Start(); err != nil { + return fmt.Errorf("error starting server: %w", err) + } + + // Wait for tailscale to come up... + if _, err := tn.TsServer.Up(ctx); err != nil { + return fmt.Errorf("error to come up server: %w", err) + } + + return tn.TsServer.Start() +}