Skip to content

Commit

Permalink
feat: add monitoring to tailscale proxyproviders
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidapaulopt committed Dec 11, 2024
1 parent a75c941 commit c20124a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/proxyproviders/tailscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (c *Client) NewProxy(config *proxyconfig.Config) (proxyproviders.Proxy, err
}

return &Proxy{
log: c.log,
config: config,
tsServer: tserver,
}, nil
Expand Down
70 changes: 65 additions & 5 deletions internal/proxyproviders/tailscale/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,83 @@ package tailscale
import (
"context"
"net"

"tailscale.com/tsnet"
"strings"

"github.com/almeidapaulopt/tsdproxy/internal/proxyconfig"

"github.com/rs/zerolog"
"tailscale.com/client/tailscale"
"tailscale.com/ipn"
"tailscale.com/ipn/ipnstate"
"tailscale.com/tsnet"
)

// Proxy struct implements proxyconfig.Proxy.
type Proxy struct {
log zerolog.Logger
config *proxyconfig.Config
tsServer *tsnet.Server
status *ipnstate.Status
lc *tailscale.LocalClient
}

// Start method implements proxyconfig.Proxy Start method.
func (p *Proxy) Start() error {
// TODO: ADD status monitoring
_, err := p.tsServer.Up(context.Background())
return err
var err error
ctx := context.Background()

p.lc, err = p.tsServer.LocalClient()
if err != nil {
return err
}

go p.watchStatus(ctx)

_, err = p.tsServer.Up(ctx)
if err != nil {
return err
}

return nil
}

func (p *Proxy) GetURL() string {
if p.status == nil {
return ""
}

// TODO: should be configurable and not force to https
return "https://" + strings.TrimRight(p.status.Self.DNSName, ".")
}

func (p *Proxy) watchStatus(ctx context.Context) {
watcher, err := p.lc.WatchIPNBus(ctx, ipn.NotifyInitialState)
if err != nil {
p.log.Error().Err(err).Msg("tailscale.watchStatus")
return
}
defer watcher.Close()

for {
n, err := watcher.Next()
if err != nil {
p.log.Error().Err(err).Msg("tailscale.watchStatus")
return
}
if n.ErrMessage != nil {
p.log.Error().Err(err).Msg("tailscale.watchStatus: backend")
return
}
if s := n.State; s != nil {
status, err := p.lc.Status(ctx)
if err != nil {

p.log.Error().Err(err).Msg("tailscale.watchStatus")
return
}
p.status = status
}
}
}

// Close method implements proxyconfig.Proxy Close method.
Expand Down

0 comments on commit c20124a

Please sign in to comment.