Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tailscale network tools #1589

Prev Previous commit
Next Next commit
Add comments
bkneis committed Jan 29, 2025
commit 1a770e15b444cec61b8b332b363ccc450534395a
7 changes: 4 additions & 3 deletions cmd/ts/metrics.go
Original file line number Diff line number Diff line change
@@ -45,28 +45,29 @@ func NewMetricsCmd() *cobra.Command {

func (cmd *MetricsCmd) Run(_ *cobra.Command, _ []string) error {
ctx := context.Background()
// Create network
tsNet := tailscale.NewTSNet(&tailscale.TSNetConfig{
AccessKey: cmd.AccessKey,
Host: tailscale.RemoveProtocol(cmd.PlatformHost),
Hostname: cmd.NetworkHostname,
})

// Run tailscale up and wait until we have a connected client
done := make(chan bool)

go func() {
err := tsNet.Start(ctx, done)
bkneis marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("cannot start tsNet server: %v", err)
}
}()

<-done

// Get tailscale API client
localClient, err := tsNet.LocalClient()
if err != nil {
return fmt.Errorf("cannot get local client: %w", err)
}

// Print metrics
out, err := localClient.DaemonMetrics(ctx)
if err != nil {
return err
6 changes: 3 additions & 3 deletions cmd/ts/netcheck.go
Original file line number Diff line number Diff line change
@@ -50,23 +50,23 @@ func NewNetcheckCmd() *cobra.Command {

func (cmd *NetcheckCmd) Run(_ *cobra.Command, _ []string) error {
ctx := context.Background()
bkneis marked this conversation as resolved.
Show resolved Hide resolved
// Create network
tsNet := tailscale.NewTSNet(&tailscale.TSNetConfig{
AccessKey: cmd.AccessKey,
Host: tailscale.RemoveProtocol(cmd.PlatformHost),
Hostname: cmd.NetworkHostname,
})

// Run tailscale up and wait until we have a connected client
done := make(chan bool)

go func() {
err := tsNet.Start(ctx, done)
bkneis marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("cannot start tsNet server: %v", err)
}
}()

<-done

// Get tailscale API client
localClient, err := tsNet.LocalClient()
if err != nil {
return fmt.Errorf("cannot get local client: %w", err)
10 changes: 5 additions & 5 deletions cmd/ts/ping.go
Original file line number Diff line number Diff line change
@@ -57,26 +57,26 @@ func NewPingCmd() *cobra.Command {
),
RunE: func(cobraCmd *cobra.Command, args []string) error {
ctx := cobraCmd.Context()
// Create network
tsNet := tailscale.NewTSNet(&tailscale.TSNetConfig{
AccessKey: cmd.AccessKey,
Host: tailscale.RemoveProtocol(cmd.PlatformHost),
Hostname: cmd.NetworkHostname,
})

// Run tailscale up and wait until we have a connected client
done := make(chan bool)

go func() {
err := tsNet.Start(ctx, done)
if err != nil {
log.Fatalf("cannot start tsNet server: %v", err)
}
}()
<-done

time.Sleep(5 * time.Second)

// Get tailscale API client
localClient, err := tsNet.LocalClient()
if err != nil {
return fmt.Errorf("cannot create local client: %w", err)
return fmt.Errorf("cannot get local client: %w", err)
}
return cmd.runPing(ctx, args, localClient)
},
6 changes: 4 additions & 2 deletions cmd/ts/status.go
Original file line number Diff line number Diff line change
@@ -52,12 +52,13 @@ func NewStatusCmd() *cobra.Command {
return errors.New("unexpected non-flag arguments to 'tailscale status'")
}

// Create network
tsNet := tailscale.NewTSNet(&tailscale.TSNetConfig{
AccessKey: cmd.AccessKey,
Host: tailscale.RemoveProtocol(cmd.PlatformHost),
Hostname: cmd.NetworkHostname,
})

// Run tailscale up and wait until we have a connected client
done := make(chan bool)
go func() {
err := tsNet.Start(ctx, done)
@@ -67,9 +68,10 @@ func NewStatusCmd() *cobra.Command {
}()
<-done

// Get tailscale API client
localClient, err := tsNet.LocalClient()
if err != nil {
return fmt.Errorf("cannot create local client: %w", err)
return fmt.Errorf("cannot get local client: %w", err)
}
return cmd.runStatus(ctx, localClient)
},
6 changes: 3 additions & 3 deletions pkg/tailscale/tsnet.go
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ func NewTSNet(config *TSNetConfig) TSNet {
}
}

// Start starts the TSNet server and binds port handlers
// Start runs tailscale up and binds port handlers
func (t *tsNet) Start(ctx context.Context, done chan bool) error {
if t.config.AccessKey == "" || t.config.Host == "" {
return fmt.Errorf("access key or host cannot be empty")
@@ -126,15 +126,15 @@ func (t *tsNet) Stop() {
// Dial allows dialing to a specific address via Tailscale
func (t *tsNet) Dial(ctx context.Context, network, addr string) (net.Conn, error) {
if t.tsServer == nil {
return nil, fmt.Errorf("Tailscale server is not running")
return nil, fmt.Errorf("tailscale server is not running")
}
return t.tsServer.Dial(ctx, network, addr)
}

// LocalClient returns the tailscale API client to the caller
func (t *tsNet) LocalClient() (*tailscale.LocalClient, error) {
if t.tsServer == nil {
return nil, fmt.Errorf("Tailscale server is not running")
return nil, fmt.Errorf("tailscale server is not running")
}
return t.tsServer.LocalClient()
}