2023-08-08 23:58:45 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
// Package web provides the Tailscale client for web.
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-16 22:52:31 +00:00
|
|
|
"crypto/rand"
|
2023-08-18 23:46:48 +00:00
|
|
|
"embed"
|
2023-08-08 23:58:45 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-08-22 23:14:00 +00:00
|
|
|
"io/fs"
|
2023-08-08 23:58:45 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-08-10 17:58:59 +00:00
|
|
|
"net/http/httputil"
|
2023-08-08 23:58:45 +00:00
|
|
|
"net/netip"
|
|
|
|
"os"
|
2023-08-23 22:22:24 +00:00
|
|
|
"path/filepath"
|
2023-08-08 23:58:45 +00:00
|
|
|
"strings"
|
2023-08-24 20:24:57 +00:00
|
|
|
"sync"
|
2023-08-08 23:58:45 +00:00
|
|
|
|
2023-08-16 22:52:31 +00:00
|
|
|
"github.com/gorilla/csrf"
|
2023-08-08 23:58:45 +00:00
|
|
|
"tailscale.com/client/tailscale"
|
|
|
|
"tailscale.com/envknob"
|
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/ipn/ipnstate"
|
|
|
|
"tailscale.com/licenses"
|
|
|
|
"tailscale.com/net/netutil"
|
|
|
|
"tailscale.com/tailcfg"
|
2023-08-25 15:27:22 +00:00
|
|
|
"tailscale.com/util/httpm"
|
2023-08-22 23:14:00 +00:00
|
|
|
"tailscale.com/util/must"
|
2023-08-08 23:58:45 +00:00
|
|
|
"tailscale.com/version/distro"
|
|
|
|
)
|
|
|
|
|
2023-08-18 23:46:48 +00:00
|
|
|
// This contains all files needed to build the frontend assets.
|
|
|
|
// Because we assign this to the blank identifier, it does not actually embed the files.
|
|
|
|
// However, this does cause `go mod vendor` to include the files when vendoring the package.
|
|
|
|
// External packages that use the web client can `go mod vendor`, run `yarn build` to
|
2023-08-22 23:14:00 +00:00
|
|
|
// build the assets, then those asset bundles will be embedded.
|
2023-08-18 23:46:48 +00:00
|
|
|
//
|
2023-08-22 19:08:57 +00:00
|
|
|
//go:embed yarn.lock index.html *.js *.json src/*
|
2023-08-18 23:46:48 +00:00
|
|
|
var _ embed.FS
|
2023-08-08 23:58:45 +00:00
|
|
|
|
2023-08-22 23:14:00 +00:00
|
|
|
//go:embed build/*
|
2023-08-18 23:46:48 +00:00
|
|
|
var embeddedFS embed.FS
|
2023-08-08 23:58:45 +00:00
|
|
|
|
2023-08-22 23:14:00 +00:00
|
|
|
// staticfiles serves static files from the build directory.
|
|
|
|
var staticfiles http.Handler
|
2023-08-08 23:58:45 +00:00
|
|
|
|
2023-08-09 17:14:03 +00:00
|
|
|
// Server is the backend server for a Tailscale web client.
|
|
|
|
type Server struct {
|
2023-08-10 17:58:59 +00:00
|
|
|
lc *tailscale.LocalClient
|
|
|
|
|
|
|
|
devMode bool
|
|
|
|
devProxy *httputil.ReverseProxy // only filled when devMode is on
|
2023-08-16 22:52:31 +00:00
|
|
|
|
2023-08-23 22:22:24 +00:00
|
|
|
cgiMode bool
|
2023-08-24 19:56:09 +00:00
|
|
|
cgiPath string
|
2023-08-16 22:52:31 +00:00
|
|
|
apiHandler http.Handler // csrf-protected api handler
|
2023-08-24 20:24:57 +00:00
|
|
|
|
|
|
|
selfMu sync.Mutex // protects self field
|
|
|
|
// self is a cached NodeView of the active self node,
|
|
|
|
// refreshed by watching the IPN notification bus
|
|
|
|
// (see Server.watchSelf).
|
|
|
|
//
|
|
|
|
// self's hostname and Tailscale IP are used to verify
|
|
|
|
// that incoming requests to the web client api are coming
|
|
|
|
// from the web client frontend and not some other source.
|
|
|
|
// Particularly to protect against DNS rebinding attacks.
|
|
|
|
// self should not be used to fill data for frontend views.
|
|
|
|
self tailcfg.NodeView
|
2023-08-09 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 22:22:24 +00:00
|
|
|
// ServerOpts contains options for constructing a new Server.
|
|
|
|
type ServerOpts struct {
|
|
|
|
DevMode bool
|
|
|
|
|
|
|
|
// CGIMode indicates if the server is running as a CGI script.
|
|
|
|
CGIMode bool
|
|
|
|
|
2023-08-24 19:56:09 +00:00
|
|
|
// If running in CGIMode, CGIPath is the URL path prefix to the CGI script.
|
|
|
|
CGIPath string
|
|
|
|
|
2023-08-23 22:22:24 +00:00
|
|
|
// LocalClient is the tailscale.LocalClient to use for this web server.
|
|
|
|
// If nil, a new one will be created.
|
|
|
|
LocalClient *tailscale.LocalClient
|
|
|
|
}
|
|
|
|
|
2023-08-09 17:14:03 +00:00
|
|
|
// NewServer constructs a new Tailscale web client server.
|
2023-08-24 20:24:57 +00:00
|
|
|
// The provided context should live for the duration of the Server's lifetime.
|
|
|
|
func NewServer(ctx context.Context, opts ServerOpts) (s *Server, cleanup func()) {
|
2023-08-23 22:22:24 +00:00
|
|
|
if opts.LocalClient == nil {
|
|
|
|
opts.LocalClient = &tailscale.LocalClient{}
|
2023-08-09 17:14:03 +00:00
|
|
|
}
|
2023-08-10 17:58:59 +00:00
|
|
|
s = &Server{
|
2023-08-23 22:22:24 +00:00
|
|
|
devMode: opts.DevMode,
|
|
|
|
lc: opts.LocalClient,
|
|
|
|
cgiMode: opts.CGIMode,
|
2023-08-24 19:56:09 +00:00
|
|
|
cgiPath: opts.CGIPath,
|
2023-08-09 17:14:03 +00:00
|
|
|
}
|
2023-08-10 17:58:59 +00:00
|
|
|
cleanup = func() {}
|
|
|
|
if s.devMode {
|
|
|
|
cleanup = s.startDevServer()
|
|
|
|
s.addProxyToDevServer()
|
|
|
|
}
|
2023-08-24 20:24:57 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
defer wg.Wait()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
go s.watchSelf(ctx)
|
|
|
|
}()
|
|
|
|
|
2023-08-22 23:14:00 +00:00
|
|
|
// Create handler for "/api" requests with CSRF protection.
|
|
|
|
// We don't require secure cookies, since the web client is regularly used
|
|
|
|
// on network appliances that are served on local non-https URLs.
|
|
|
|
// The client is secured by limiting the interface it listens on,
|
|
|
|
// or by authenticating requests before they reach the web client.
|
|
|
|
csrfProtect := csrf.Protect(s.csrfKey(), csrf.Secure(false))
|
|
|
|
s.apiHandler = csrfProtect(http.HandlerFunc(s.serveAPI))
|
|
|
|
|
2023-08-22 17:15:08 +00:00
|
|
|
s.lc.IncrementCounter(context.Background(), "web_client_initialization", 1)
|
2023-08-10 17:58:59 +00:00
|
|
|
return s, cleanup
|
2023-08-09 17:14:03 +00:00
|
|
|
}
|
2023-08-08 23:58:45 +00:00
|
|
|
|
|
|
|
func init() {
|
2023-08-22 23:14:00 +00:00
|
|
|
buildFiles := must.Get(fs.Sub(embeddedFS, "build"))
|
|
|
|
staticfiles = http.FileServer(http.FS(buildFiles))
|
2023-08-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 20:24:57 +00:00
|
|
|
// watchSelf watches the IPN notification bus to refresh
|
|
|
|
// the Server's self node cache.
|
|
|
|
func (s *Server) watchSelf(ctx context.Context) {
|
|
|
|
watchCtx, cancelWatch := context.WithCancel(ctx)
|
|
|
|
defer cancelWatch()
|
|
|
|
|
|
|
|
watcher, err := s.lc.WatchIPNBus(watchCtx, ipn.NotifyInitialNetMap|ipn.NotifyNoPrivateKeys)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("lost connection to tailscaled: %v", err)
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err := watcher.Next()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("lost connection to tailscaled: %v", err)
|
|
|
|
}
|
|
|
|
if state := n.State; state != nil && *state == ipn.NeedsLogin {
|
|
|
|
s.updateSelf(tailcfg.NodeView{})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if n.NetMap == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.updateSelf(n.NetMap.SelfNode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateSelf grabs the lock and updates s.self.
|
|
|
|
// Then logs if anything changed.
|
|
|
|
func (s *Server) updateSelf(self tailcfg.NodeView) {
|
|
|
|
s.selfMu.Lock()
|
|
|
|
prev := s.self
|
|
|
|
s.self = self
|
|
|
|
s.selfMu.Unlock()
|
|
|
|
|
|
|
|
var old, new tailcfg.StableNodeID
|
|
|
|
if prev.Valid() {
|
|
|
|
old = prev.StableID()
|
|
|
|
}
|
|
|
|
if s.self.Valid() {
|
|
|
|
new = s.self.StableID()
|
|
|
|
}
|
|
|
|
if old != new {
|
|
|
|
if new.IsZero() {
|
|
|
|
log.Printf("self node logout")
|
|
|
|
} else {
|
|
|
|
log.Printf("self node login")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 19:46:51 +00:00
|
|
|
// ServeHTTP processes all requests for the Tailscale web client.
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2023-08-24 19:56:09 +00:00
|
|
|
handler := s.serve
|
|
|
|
|
|
|
|
// if running in cgi mode, strip the cgi path prefix
|
|
|
|
if s.cgiMode {
|
|
|
|
prefix := s.cgiPath
|
|
|
|
if prefix == "" {
|
|
|
|
switch distro.Get() {
|
|
|
|
case distro.Synology:
|
|
|
|
prefix = synologyPrefix
|
|
|
|
case distro.QNAP:
|
|
|
|
prefix = qnapPrefix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if prefix != "" {
|
|
|
|
handler = enforcePrefix(prefix, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handler(w, r)
|
2023-08-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 21:40:17 +00:00
|
|
|
// authorize checks if the request is authorized to access the web client for those platforms that support it.
|
|
|
|
func authorize(w http.ResponseWriter, r *http.Request) (handled bool) {
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/assets/") {
|
|
|
|
// don't require authorization for static assets
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch distro.Get() {
|
|
|
|
case distro.Synology:
|
|
|
|
return authorizeSynology(w, r)
|
|
|
|
case distro.QNAP:
|
|
|
|
return authorizeQNAP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-08-24 19:46:51 +00:00
|
|
|
func (s *Server) serve(w http.ResponseWriter, r *http.Request) {
|
2023-08-22 23:14:00 +00:00
|
|
|
switch {
|
|
|
|
case authorize(w, r):
|
|
|
|
// Authenticate and authorize the request for platforms that support it.
|
|
|
|
// Return if the request was processed.
|
2023-08-24 21:40:17 +00:00
|
|
|
return
|
2023-08-22 23:14:00 +00:00
|
|
|
case strings.HasPrefix(r.URL.Path, "/api/"):
|
|
|
|
// Pass API requests through to the API handler.
|
|
|
|
s.apiHandler.ServeHTTP(w, r)
|
2023-08-10 17:58:59 +00:00
|
|
|
return
|
2023-08-22 23:14:00 +00:00
|
|
|
case s.devMode:
|
|
|
|
// When in dev mode, proxy non-api requests to the Vite dev server.
|
|
|
|
s.devProxy.ServeHTTP(w, r)
|
2023-08-15 15:38:13 +00:00
|
|
|
return
|
|
|
|
default:
|
2023-08-22 23:14:00 +00:00
|
|
|
// Otherwise, serve static files from the embedded filesystem.
|
2023-08-22 17:15:08 +00:00
|
|
|
s.lc.IncrementCounter(context.Background(), "web_client_page_load", 1)
|
2023-08-22 23:14:00 +00:00
|
|
|
staticfiles.ServeHTTP(w, r)
|
2023-08-15 15:38:13 +00:00
|
|
|
return
|
2023-08-08 23:58:45 +00:00
|
|
|
}
|
2023-08-15 15:38:13 +00:00
|
|
|
}
|
2023-08-08 23:58:45 +00:00
|
|
|
|
2023-08-25 15:27:22 +00:00
|
|
|
// serveAPI serves requests for the web client api.
|
|
|
|
// It should only be called by Server.ServeHTTP, via Server.apiHandler,
|
|
|
|
// which protects the handler using gorilla csrf.
|
|
|
|
func (s *Server) serveAPI(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-CSRF-Token", csrf.Token(r))
|
|
|
|
path := strings.TrimPrefix(r.URL.Path, "/api")
|
|
|
|
switch path {
|
|
|
|
case "/data":
|
|
|
|
switch r.Method {
|
|
|
|
case httpm.GET:
|
|
|
|
s.serveGetNodeDataJSON(w, r)
|
|
|
|
case httpm.POST:
|
|
|
|
s.servePostNodeUpdate(w, r)
|
|
|
|
default:
|
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Error(w, "invalid endpoint", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:38:13 +00:00
|
|
|
type nodeData struct {
|
|
|
|
Profile tailcfg.UserProfile
|
|
|
|
Status string
|
|
|
|
DeviceName string
|
|
|
|
IP string
|
|
|
|
AdvertiseExitNode bool
|
|
|
|
AdvertiseRoutes string
|
|
|
|
LicensesURL string
|
|
|
|
TUNMode bool
|
|
|
|
IsSynology bool
|
|
|
|
DSMVersion int // 6 or 7, if IsSynology=true
|
|
|
|
IsUnraid bool
|
|
|
|
UnraidToken string
|
|
|
|
IPNVersion string
|
|
|
|
}
|
|
|
|
|
2023-08-24 19:46:51 +00:00
|
|
|
func (s *Server) getNodeData(ctx context.Context) (*nodeData, error) {
|
2023-08-10 03:11:31 +00:00
|
|
|
st, err := s.lc.Status(ctx)
|
2023-08-08 23:58:45 +00:00
|
|
|
if err != nil {
|
2023-08-15 15:38:13 +00:00
|
|
|
return nil, err
|
2023-08-08 23:58:45 +00:00
|
|
|
}
|
2023-08-09 17:14:03 +00:00
|
|
|
prefs, err := s.lc.GetPrefs(ctx)
|
2023-08-08 23:58:45 +00:00
|
|
|
if err != nil {
|
2023-08-15 15:38:13 +00:00
|
|
|
return nil, err
|
2023-08-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
profile := st.User[st.Self.UserID]
|
|
|
|
deviceName := strings.Split(st.Self.DNSName, ".")[0]
|
|
|
|
versionShort := strings.Split(st.Version, "-")[0]
|
2023-08-15 15:38:13 +00:00
|
|
|
data := &nodeData{
|
2023-08-24 19:46:51 +00:00
|
|
|
Profile: profile,
|
|
|
|
Status: st.BackendState,
|
|
|
|
DeviceName: deviceName,
|
|
|
|
LicensesURL: licenses.LicensesURL(),
|
|
|
|
TUNMode: st.TUN,
|
|
|
|
IsSynology: distro.Get() == distro.Synology || envknob.Bool("TS_FAKE_SYNOLOGY"),
|
|
|
|
DSMVersion: distro.DSMVersion(),
|
|
|
|
IsUnraid: distro.Get() == distro.Unraid,
|
|
|
|
UnraidToken: os.Getenv("UNRAID_CSRF_TOKEN"),
|
|
|
|
IPNVersion: versionShort,
|
2023-08-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
exitNodeRouteV4 := netip.MustParsePrefix("0.0.0.0/0")
|
|
|
|
exitNodeRouteV6 := netip.MustParsePrefix("::/0")
|
|
|
|
for _, r := range prefs.AdvertiseRoutes {
|
|
|
|
if r == exitNodeRouteV4 || r == exitNodeRouteV6 {
|
|
|
|
data.AdvertiseExitNode = true
|
|
|
|
} else {
|
|
|
|
if data.AdvertiseRoutes != "" {
|
|
|
|
data.AdvertiseRoutes += ","
|
|
|
|
}
|
|
|
|
data.AdvertiseRoutes += r.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(st.TailscaleIPs) != 0 {
|
|
|
|
data.IP = st.TailscaleIPs[0].String()
|
|
|
|
}
|
2023-08-15 15:38:13 +00:00
|
|
|
return data, nil
|
|
|
|
}
|
2023-08-08 23:58:45 +00:00
|
|
|
|
2023-08-24 19:46:51 +00:00
|
|
|
func (s *Server) serveGetNodeDataJSON(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, err := s.getNodeData(r.Context())
|
2023-08-15 15:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(*data); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeUpdate struct {
|
|
|
|
AdvertiseRoutes string
|
|
|
|
AdvertiseExitNode bool
|
|
|
|
Reauthenticate bool
|
|
|
|
ForceLogout bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) servePostNodeUpdate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
st, err := s.lc.Status(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var postData nodeUpdate
|
|
|
|
type mi map[string]any
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&postData); err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
json.NewEncoder(w).Encode(mi{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err := netutil.CalcAdvertiseRoutes(postData.AdvertiseRoutes, postData.AdvertiseExitNode)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
json.NewEncoder(w).Encode(mi{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mp := &ipn.MaskedPrefs{
|
|
|
|
AdvertiseRoutesSet: true,
|
|
|
|
WantRunningSet: true,
|
|
|
|
}
|
|
|
|
mp.Prefs.WantRunning = true
|
|
|
|
mp.Prefs.AdvertiseRoutes = routes
|
|
|
|
log.Printf("Doing edit: %v", mp.Pretty())
|
|
|
|
|
|
|
|
if _, err := s.lc.EditPrefs(r.Context(), mp); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
json.NewEncoder(w).Encode(mi{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
var reauth, logout bool
|
|
|
|
if postData.Reauthenticate {
|
|
|
|
reauth = true
|
|
|
|
}
|
|
|
|
if postData.ForceLogout {
|
|
|
|
logout = true
|
|
|
|
}
|
|
|
|
log.Printf("tailscaleUp(reauth=%v, logout=%v) ...", reauth, logout)
|
|
|
|
url, err := s.tailscaleUp(r.Context(), st, postData)
|
|
|
|
log.Printf("tailscaleUp = (URL %v, %v)", url != "", err)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
json.NewEncoder(w).Encode(mi{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if url != "" {
|
|
|
|
json.NewEncoder(w).Encode(mi{"url": url})
|
|
|
|
} else {
|
|
|
|
io.WriteString(w, "{}")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) tailscaleUp(ctx context.Context, st *ipnstate.Status, postData nodeUpdate) (authURL string, retErr error) {
|
2023-08-08 23:58:45 +00:00
|
|
|
if postData.ForceLogout {
|
2023-08-09 17:14:03 +00:00
|
|
|
if err := s.lc.Logout(ctx); err != nil {
|
2023-08-08 23:58:45 +00:00
|
|
|
return "", fmt.Errorf("Logout error: %w", err)
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
origAuthURL := st.AuthURL
|
|
|
|
isRunning := st.BackendState == ipn.Running.String()
|
|
|
|
|
|
|
|
forceReauth := postData.Reauthenticate
|
|
|
|
if !forceReauth {
|
|
|
|
if origAuthURL != "" {
|
|
|
|
return origAuthURL, nil
|
|
|
|
}
|
|
|
|
if isRunning {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// printAuthURL reports whether we should print out the
|
|
|
|
// provided auth URL from an IPN notify.
|
|
|
|
printAuthURL := func(url string) bool {
|
|
|
|
return url != origAuthURL
|
|
|
|
}
|
|
|
|
|
|
|
|
watchCtx, cancelWatch := context.WithCancel(ctx)
|
|
|
|
defer cancelWatch()
|
2023-08-09 17:14:03 +00:00
|
|
|
watcher, err := s.lc.WatchIPNBus(watchCtx, 0)
|
2023-08-08 23:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if !isRunning {
|
2023-08-09 17:14:03 +00:00
|
|
|
s.lc.Start(ctx, ipn.Options{})
|
2023-08-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
if forceReauth {
|
2023-08-09 17:14:03 +00:00
|
|
|
s.lc.StartLoginInteractive(ctx)
|
2023-08-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err := watcher.Next()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if n.ErrMessage != nil {
|
|
|
|
msg := *n.ErrMessage
|
|
|
|
return "", fmt.Errorf("backend error: %v", msg)
|
|
|
|
}
|
|
|
|
if url := n.BrowseToURL; url != nil && printAuthURL(*url) {
|
|
|
|
return *url, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 22:52:31 +00:00
|
|
|
|
2023-08-23 22:22:24 +00:00
|
|
|
// csrfKey returns a key that can be used for CSRF protection.
|
|
|
|
// If an error occurs during key creation, the error is logged and the active process terminated.
|
|
|
|
// If the server is running in CGI mode, the key is cached to disk and reused between requests.
|
|
|
|
// If an error occurs during key storage, the error is logged and the active process terminated.
|
|
|
|
func (s *Server) csrfKey() []byte {
|
|
|
|
var csrfFile string
|
|
|
|
|
|
|
|
// if running in CGI mode, try to read from disk, but ignore errors
|
|
|
|
if s.cgiMode {
|
|
|
|
confdir, err := os.UserConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
confdir = os.TempDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
csrfFile = filepath.Join(confdir, "tailscale", "web-csrf.key")
|
|
|
|
key, _ := os.ReadFile(csrfFile)
|
|
|
|
if len(key) == 32 {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new key
|
2023-08-16 22:52:31 +00:00
|
|
|
key := make([]byte, 32)
|
|
|
|
if _, err := rand.Read(key); err != nil {
|
|
|
|
log.Fatal("error generating CSRF key: %w", err)
|
|
|
|
}
|
2023-08-23 22:22:24 +00:00
|
|
|
|
|
|
|
// if running in CGI mode, try to write the newly created key to disk, and exit if it fails.
|
|
|
|
if s.cgiMode {
|
|
|
|
if err := os.Mkdir(filepath.Dir(csrfFile), 0700); err != nil && !os.IsExist(err) {
|
|
|
|
log.Fatalf("unable to store CSRF key: %v", err)
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(csrfFile, key, 0600); err != nil {
|
|
|
|
log.Fatalf("unable to store CSRF key: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 22:52:31 +00:00
|
|
|
return key
|
|
|
|
}
|
2023-08-24 19:56:09 +00:00
|
|
|
|
|
|
|
// enforcePrefix returns a HandlerFunc that enforces a given path prefix is used in requests,
|
|
|
|
// then strips it before invoking h.
|
|
|
|
// Unlike http.StripPrefix, it does not return a 404 if the prefix is not present.
|
|
|
|
// Instead, it returns a redirect to the prefix path.
|
|
|
|
func enforcePrefix(prefix string, h http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !strings.HasPrefix(r.URL.Path, prefix) {
|
|
|
|
http.Redirect(w, r, prefix, http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
2023-08-24 21:40:17 +00:00
|
|
|
prefix = strings.TrimSuffix(prefix, "/")
|
2023-08-24 19:56:09 +00:00
|
|
|
http.StripPrefix(prefix, h).ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|