tailscale/tsnet/example/web-client/web-client.go
Sonia Appasamy 89953b015b ipn/ipnlocal,client/web: add web client to tailscaled
Allows for serving the web interface from tailscaled, with the
ability to start and stop the server via localapi endpoints
(/web/start and /web/stop).

This will be used to run the new full management web client,
which will only be accessible over Tailscale (with an extra auth
check step over noise) from the daemon. This switch also allows
us to run the web interface as a long-lived service in environments
where the CLI version is restricted to CGI, allowing us to manage
certain auth state in memory.

ipn/ipnlocal/web is stubbed out in ipn/ipnlocal/web_stub for
ios builds to satisfy ios restriction from adding "text/template"
and "html/template" dependencies.

Updates tailscale/corp#14335

Signed-off-by: Sonia Appasamy <sonia@tailscale.com>
2023-10-31 13:15:07 -04:00

48 lines
933 B
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// The web-client command demonstrates serving the Tailscale web client over tsnet.
package main
import (
"flag"
"log"
"net/http"
"tailscale.com/client/web"
"tailscale.com/tsnet"
)
var (
addr = flag.String("addr", "localhost:8060", "address of Tailscale web client")
devMode = flag.Bool("dev", false, "run web client in dev mode")
)
func main() {
flag.Parse()
s := new(tsnet.Server)
defer s.Close()
lc, err := s.LocalClient()
if err != nil {
log.Fatal(err)
}
// Serve the Tailscale web client.
ws, err := web.NewServer(web.ServerOpts{
DevMode: *devMode,
LocalClient: lc,
})
if err != nil {
log.Fatal(err)
}
defer ws.Shutdown()
log.Printf("Serving Tailscale web client on http://%s", *addr)
if err := http.ListenAndServe(*addr, ws); err != nil {
if err != http.ErrServerClosed {
log.Fatal(err)
}
}
}