Skip to content

Commit

Permalink
refactor: add a tailscale package
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidapaulopt committed Oct 23, 2024
1 parent f3e2781 commit 9c2bfd5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
13 changes: 9 additions & 4 deletions internal/proxymanager/proxymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions internal/proxymanager/tailscale.go

This file was deleted.

48 changes: 48 additions & 0 deletions internal/tailscale/tailscale.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 9c2bfd5

Please sign in to comment.