Skip to content

Commit

Permalink
golink: add flag to run using tailscaled instead of tsnet
Browse files Browse the repository at this point in the history
To support more deployment models.

Signed-off-by: Maisem Ali <maisem@tailscale.com>
  • Loading branch information
maisem committed Nov 15, 2023
1 parent 7f89e2e commit 483f110
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"tailscale.com/client/tailscale"
"tailscale.com/hostinfo"
"tailscale.com/ipn"
"tailscale.com/ipn/ipnstate"
"tailscale.com/tailcfg"
"tailscale.com/tsnet"
)
Expand All @@ -49,6 +50,7 @@ var (
hostname = flag.String("hostname", defaultHostname, "service name")
resolveFromBackup = flag.String("resolve-from-backup", "", "resolve a link from snapshot file and exit")
allowUnknownUsers = flag.Bool("allow-unknown-users", false, "allow unknown users to save links")
useTailscaled = flag.Bool("use-tailscaled", false, "use tailscaled instead of tsnet")
)

var stats struct {
Expand Down Expand Up @@ -154,6 +156,41 @@ func Run() error {
log.Fatal(http.ListenAndServe(*dev, serveHandler()))
}

if *useTailscaled {
localClient = &tailscale.LocalClient{}
var st *ipnstate.Status
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
st, err = localClient.StatusWithoutPeers(ctx)
cancel()
if err != nil {
return err
}
if st.BackendState == "Running" {
break
}
log.Printf("Waiting for backend to enter Running state; currently %s", st.BackendState)
time.Sleep(time.Second)
}

anySuccess := false
for _, ip := range st.TailscaleIPs {
l80, err := net.Listen("tcp", net.JoinHostPort(ip.String(), "80"))
if err != nil {
log.Printf("Listen(%s): %v", ip, err)
continue
}
anySuccess = true

go http.Serve(l80, serveHandler())
}
if !anySuccess {
return errors.New("unable to listen on any Tailscale IP")
}
log.Printf("Serving http://%s/ ...", strings.TrimSuffix(st.Self.DNSName, "."))
select {}
}

if *hostname == "" {
return errors.New("--hostname, if specified, cannot be empty")
}
Expand Down

0 comments on commit 483f110

Please sign in to comment.