Skip to content

golink: add flag to run using tailscaled instead of tsnet #95

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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