2023-10-11 18:35:22 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
//go:build !ios && !android
|
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-10-26 18:39:20 +00:00
|
|
|
"net"
|
2023-10-11 18:35:22 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"tailscale.com/client/tailscale"
|
|
|
|
"tailscale.com/client/web"
|
2023-10-26 18:39:20 +00:00
|
|
|
"tailscale.com/net/netutil"
|
2023-10-31 18:41:39 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2023-10-11 18:35:22 +00:00
|
|
|
)
|
|
|
|
|
2023-10-31 18:56:20 +00:00
|
|
|
// webClient holds state for the web interface for managing
|
2023-10-11 18:35:22 +00:00
|
|
|
// this tailscale instance. The web interface is not used by
|
|
|
|
// default, but initialized by calling LocalBackend.WebOrInit.
|
2023-10-31 18:56:20 +00:00
|
|
|
type webClient struct {
|
|
|
|
server *web.Server // or nil, initialized lazily
|
2023-10-11 18:35:22 +00:00
|
|
|
|
|
|
|
// lc optionally specifies a LocalClient to use to connect
|
|
|
|
// to the localapi for this tailscaled instance.
|
|
|
|
// If nil, a default is used.
|
|
|
|
lc *tailscale.LocalClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWebLocalClient sets the b.web.lc function.
|
|
|
|
// If lc is provided as nil, b.web.lc is cleared out.
|
|
|
|
func (b *LocalBackend) SetWebLocalClient(lc *tailscale.LocalClient) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2023-10-31 18:56:20 +00:00
|
|
|
b.webClient.lc = lc
|
2023-10-11 18:35:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 18:56:20 +00:00
|
|
|
// WebClientInit initializes the web interface for managing this
|
|
|
|
// tailscaled instance.
|
|
|
|
// If the web interface is already running, WebClientInit is a no-op.
|
|
|
|
func (b *LocalBackend) WebClientInit() (err error) {
|
2023-10-31 18:41:39 +00:00
|
|
|
if !hasCapability(b.netMap, tailcfg.CapabilityPreviewWebClient) {
|
|
|
|
return errors.New("web client not enabled for this device")
|
2023-10-11 18:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2023-10-31 18:56:20 +00:00
|
|
|
if b.webClient.server != nil {
|
2023-10-11 18:35:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-31 18:56:20 +00:00
|
|
|
b.logf("WebClientInit: initializing web ui")
|
|
|
|
if b.webClient.server, err = web.NewServer(web.ServerOpts{
|
2023-10-11 18:35:22 +00:00
|
|
|
// TODO(sonia): allow passing back dev mode flag
|
2023-10-31 18:56:20 +00:00
|
|
|
LocalClient: b.webClient.lc,
|
2023-10-11 18:35:22 +00:00
|
|
|
Logf: b.logf,
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("web.NewServer: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-10-31 18:56:20 +00:00
|
|
|
b.logf("WebClientInit: started web ui")
|
2023-10-11 18:35:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-31 18:56:20 +00:00
|
|
|
// WebClientShutdown shuts down any running b.webClient servers and
|
|
|
|
// clears out b.webClient state (besides the b.webClient.lc field,
|
|
|
|
// which is left untouched because required for future web startups).
|
|
|
|
// WebClientShutdown obtains the b.mu lock.
|
|
|
|
func (b *LocalBackend) WebClientShutdown() {
|
2023-10-11 18:35:22 +00:00
|
|
|
b.mu.Lock()
|
2023-10-31 18:56:20 +00:00
|
|
|
server := b.webClient.server
|
|
|
|
b.webClient.server = nil
|
2023-10-11 18:35:22 +00:00
|
|
|
b.mu.Unlock() // release lock before shutdown
|
2023-10-31 18:56:20 +00:00
|
|
|
if server != nil {
|
|
|
|
server.Shutdown()
|
2023-10-11 18:35:22 +00:00
|
|
|
}
|
2023-10-31 18:56:20 +00:00
|
|
|
b.logf("WebClientShutdown: shut down web ui")
|
2023-10-11 18:35:22 +00:00
|
|
|
}
|
2023-10-26 18:39:20 +00:00
|
|
|
|
|
|
|
// handleWebClientConn serves web client requests.
|
|
|
|
func (b *LocalBackend) handleWebClientConn(c net.Conn) error {
|
2023-10-31 18:56:20 +00:00
|
|
|
if err := b.WebClientInit(); err != nil {
|
2023-10-26 18:39:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-10-31 18:56:20 +00:00
|
|
|
s := http.Server{Handler: b.webClient.server}
|
2023-10-26 18:39:20 +00:00
|
|
|
return s.Serve(netutil.NewOneConnListener(c, nil))
|
|
|
|
}
|