package main

import (
	"fmt"
	"log"
	"net"

	"tailscale.com/tsnet"
)

func startTailscale(dataDir string, nodeID string) (net.Listener, error) {
	s := &tsnet.Server{
		Hostname: nodeID,
		Dir:      dataDir,
		Logf:     log.Printf,
	}

	if err := s.Start(); err != nil {
		return nil, fmt.Errorf("tailscale start: %w", err)
	}

	ln, err := s.Listen("tcp", ":0")
	if err != nil {
		return nil, fmt.Errorf("tailscale listen: %w", err)
	}

	return ln, nil
}
