mirror of
https://github.com/tailscale/tailscale.git
synced 2026-09-14 14:59:57 -04:00
Package tailcfg defines the types and constants used by the Tailscale protocol, but since everything is all in one package, it’s difficult to sift through the docs: https://pkg.go.dev/tailscale.com/tailcfg We define and enumerate capabilities as string constants for tailcfg.NodeCapability and tailcfg.PeerCapability. This PR extracts them into their own packages: - tailcfg.CapabilityFileSharing becomes nodecap.FileSharing - tailcfg.NodeAttrOnlyTCP443 becomes nodecap.OnlyTCP443 - tailcfg.PeerCapabilityTaildrive becomes peercap.Taildrive We originally intended for CapabilityFoo to grant an entitlement or permission for Foo, and for NodeAttrBar to configure Bar in the nodeAttrs section of the policy file. However, there was no technical enforcement of this convention, so new capabilities have used the NodeAttr prefix regardless of meaning. Therefore, this PR unifies tailcfg.CapabilityFoo and tailcfg.NodeAttrBar into a single package as nodecap.Foo and nodecap.Bar. Ran `go fix -inline ./...` and committed the changes that replaced uses of the tailcfg aliases with the authoritative ones. Updates #20259 Change-Id: Ieb7e7e6c8247c39faf42fdf15c68cdc7c621c730 Signed-off-by: Simon Law <sfllaw@tailscale.com>
159 lines
3.9 KiB
Go
159 lines
3.9 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package helloserver implements the HTTP server behind hello.ts.net.
|
|
package helloserver
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"embed"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"tailscale.com/client/local"
|
|
"tailscale.com/client/tailscale/apitype"
|
|
"tailscale.com/tailcfg"
|
|
"tailscale.com/tailcfg/nodecap"
|
|
)
|
|
|
|
//go:embed hello.tmpl.html
|
|
var embeddedTemplate string
|
|
|
|
//go:embed static/*
|
|
var staticFiles embed.FS
|
|
|
|
var staticHandler = http.FileServerFS(staticFiles)
|
|
|
|
var tmpl = template.Must(template.New("home").Parse(embeddedTemplate))
|
|
|
|
// Server is an HTTP server for hello.ts.net.
|
|
//
|
|
// The zero value is not valid; populate at least one of HTTPAddr or HTTPSAddr
|
|
// before calling Run.
|
|
type Server struct {
|
|
// HTTPAddr is the address to run an HTTP server on, or empty for none.
|
|
HTTPAddr string
|
|
|
|
// HTTPSAddr is the address to run an HTTPS server on, or empty for none.
|
|
HTTPSAddr string
|
|
|
|
// LocalClient is used to look up the identity of incoming requests and
|
|
// to obtain TLS certificates. If nil, the zero value of local.Client is
|
|
// used.
|
|
LocalClient *local.Client
|
|
}
|
|
|
|
func (s *Server) localClient() *local.Client {
|
|
if s.LocalClient != nil {
|
|
return s.LocalClient
|
|
}
|
|
return &local.Client{}
|
|
}
|
|
|
|
// Run starts the configured HTTP and HTTPS servers and blocks until one of
|
|
// them returns an error.
|
|
func (s *Server) Run() error {
|
|
errc := make(chan error, 1)
|
|
if s.HTTPAddr != "" {
|
|
log.Printf("running HTTP server on %s", s.HTTPAddr)
|
|
go func() {
|
|
errc <- http.ListenAndServe(s.HTTPAddr, s)
|
|
}()
|
|
}
|
|
if s.HTTPSAddr != "" {
|
|
log.Printf("running HTTPS server on %s", s.HTTPSAddr)
|
|
go func() {
|
|
hs := &http.Server{
|
|
Addr: s.HTTPSAddr,
|
|
Handler: s,
|
|
TLSConfig: &tls.Config{
|
|
GetCertificate: s.localClient().GetCertificate,
|
|
},
|
|
IdleTimeout: 30 * time.Second,
|
|
ReadHeaderTimeout: 20 * time.Second,
|
|
MaxHeaderBytes: 10 << 10,
|
|
}
|
|
errc <- hs.ListenAndServeTLS("", "")
|
|
}()
|
|
}
|
|
return <-errc
|
|
}
|
|
|
|
type tmplData struct {
|
|
DisplayName string // "Foo Barberson"
|
|
LoginName string // "foo@bar.com"
|
|
ProfilePicURL string // "https://..."
|
|
MachineName string // "imac5k"
|
|
MachineOS string // "Linux"
|
|
IP string // "100.2.3.4"
|
|
}
|
|
|
|
func tailscaleIP(who *apitype.WhoIsResponse) string {
|
|
if who == nil {
|
|
return ""
|
|
}
|
|
vals, err := tailcfg.UnmarshalNodeCapJSON[string](who.Node.CapMap, nodecap.NativeIPV4)
|
|
if err == nil && len(vals) > 0 {
|
|
return vals[0]
|
|
}
|
|
for _, nodeIP := range who.Node.Addresses {
|
|
if nodeIP.Addr().Is4() && nodeIP.IsSingleIP() {
|
|
return nodeIP.Addr().String()
|
|
}
|
|
}
|
|
for _, nodeIP := range who.Node.Addresses {
|
|
if nodeIP.IsSingleIP() {
|
|
return nodeIP.Addr().String()
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ServeHTTP implements http.Handler.
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if r.TLS == nil && s.HTTPSAddr != "" {
|
|
host := r.Host
|
|
if strings.Contains(r.Host, "100.101.102.103") {
|
|
host = "hello.ts.net"
|
|
}
|
|
http.Redirect(w, r, "https://"+host, http.StatusFound)
|
|
return
|
|
}
|
|
|
|
if strings.HasPrefix(r.RequestURI, "/static/") {
|
|
staticHandler.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
if r.RequestURI != "/" {
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
return
|
|
}
|
|
|
|
who, err := s.localClient().WhoIs(r.Context(), r.RemoteAddr)
|
|
if err != nil {
|
|
log.Printf("whois(%q) error: %v", r.RemoteAddr, err)
|
|
http.Error(w, "Your Tailscale works, but we failed to look you up.", 500)
|
|
return
|
|
}
|
|
data := tmplData{
|
|
DisplayName: who.UserProfile.DisplayName,
|
|
LoginName: who.UserProfile.LoginName,
|
|
ProfilePicURL: who.UserProfile.ProfilePicURL,
|
|
MachineName: firstLabel(who.Node.ComputedName),
|
|
MachineOS: who.Node.Hostinfo.OS(),
|
|
IP: tailscaleIP(who),
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
tmpl.Execute(w, data)
|
|
}
|
|
|
|
// firstLabel returns s up until the first period, if any.
|
|
func firstLabel(s string) string {
|
|
s, _, _ = strings.Cut(s, ".")
|
|
return s
|
|
}
|