2023-06-06 03:09:48 -06:00
|
|
|
package server
|
2022-03-03 16:01:31 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-18 11:51:37 -06:00
|
|
|
"encoding/json"
|
2022-03-03 16:01:31 -07:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-09-02 01:16:19 -06:00
|
|
|
"net/netip"
|
2022-03-05 12:04:31 -07:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2022-03-03 16:01:31 -07:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-06-06 03:09:48 -06:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-12-20 13:47:48 -07:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-03-03 16:01:31 -07:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/net/stun"
|
2022-03-05 12:04:31 -07:00
|
|
|
"tailscale.com/tailcfg"
|
2022-03-03 16:01:31 -07:00
|
|
|
"tailscale.com/types/key"
|
|
|
|
)
|
|
|
|
|
|
|
|
// fastStartHeader is the header (with value "1") that signals to the HTTP
|
|
|
|
// server that the DERP HTTP client does not want the HTTP 101 response
|
|
|
|
// headers and it will begin writing & reading the DERP protocol immediately
|
|
|
|
// following its HTTP request.
|
|
|
|
const fastStartHeader = "Derp-Fast-Start"
|
|
|
|
|
2022-03-05 11:30:30 -07:00
|
|
|
type DERPServer struct {
|
2023-06-06 03:09:48 -06:00
|
|
|
serverURL string
|
|
|
|
key key.NodePrivate
|
|
|
|
cfg *types.DERPConfig
|
2022-03-04 03:31:41 -07:00
|
|
|
tailscaleDERP *derp.Server
|
2022-03-03 16:01:31 -07:00
|
|
|
}
|
|
|
|
|
2023-06-06 03:09:48 -06:00
|
|
|
func NewDERPServer(
|
|
|
|
serverURL string,
|
|
|
|
derpKey key.NodePrivate,
|
|
|
|
cfg *types.DERPConfig,
|
|
|
|
) (*DERPServer, error) {
|
2022-06-20 04:32:13 -06:00
|
|
|
log.Trace().Caller().Msg("Creating new embedded DERP server")
|
2023-12-20 13:47:48 -07:00
|
|
|
server := derp.NewServer(derpKey, util.TSLogfWrapper()) // nolint // zerolinter complains
|
2022-03-06 09:35:54 -07:00
|
|
|
|
2023-06-06 03:09:48 -06:00
|
|
|
return &DERPServer{
|
|
|
|
serverURL: serverURL,
|
|
|
|
key: derpKey,
|
|
|
|
cfg: cfg,
|
|
|
|
tailscaleDERP: server,
|
|
|
|
}, nil
|
2022-03-05 12:04:31 -07:00
|
|
|
}
|
|
|
|
|
2023-06-06 03:09:48 -06:00
|
|
|
func (d *DERPServer) GenerateRegion() (tailcfg.DERPRegion, error) {
|
|
|
|
serverURL, err := url.Parse(d.serverURL)
|
2022-03-05 12:04:31 -07:00
|
|
|
if err != nil {
|
|
|
|
return tailcfg.DERPRegion{}, err
|
|
|
|
}
|
|
|
|
var host string
|
|
|
|
var port int
|
|
|
|
host, portStr, err := net.SplitHostPort(serverURL.Host)
|
|
|
|
if err != nil {
|
|
|
|
if serverURL.Scheme == "https" {
|
|
|
|
host = serverURL.Host
|
|
|
|
port = 443
|
|
|
|
} else {
|
|
|
|
host = serverURL.Host
|
|
|
|
port = 80
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
port, err = strconv.Atoi(portStr)
|
|
|
|
if err != nil {
|
|
|
|
return tailcfg.DERPRegion{}, err
|
|
|
|
}
|
|
|
|
}
|
2022-03-03 16:01:31 -07:00
|
|
|
|
2022-03-05 12:04:31 -07:00
|
|
|
localDERPregion := tailcfg.DERPRegion{
|
2023-06-06 03:09:48 -06:00
|
|
|
RegionID: d.cfg.ServerRegionID,
|
|
|
|
RegionCode: d.cfg.ServerRegionCode,
|
|
|
|
RegionName: d.cfg.ServerRegionName,
|
2022-03-05 12:04:31 -07:00
|
|
|
Avoid: false,
|
|
|
|
Nodes: []*tailcfg.DERPNode{
|
|
|
|
{
|
2023-06-06 03:09:48 -06:00
|
|
|
Name: fmt.Sprintf("%d", d.cfg.ServerRegionID),
|
|
|
|
RegionID: d.cfg.ServerRegionID,
|
2022-03-05 12:04:31 -07:00
|
|
|
HostName: host,
|
|
|
|
DERPPort: port,
|
2024-01-16 08:04:03 -07:00
|
|
|
IPv4: d.cfg.IPv4,
|
|
|
|
IPv6: d.cfg.IPv6,
|
2022-03-05 12:04:31 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-03-06 09:00:56 -07:00
|
|
|
|
2023-06-06 03:09:48 -06:00
|
|
|
_, portSTUNStr, err := net.SplitHostPort(d.cfg.STUNAddr)
|
2022-03-15 06:22:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return tailcfg.DERPRegion{}, err
|
|
|
|
}
|
|
|
|
portSTUN, err := strconv.Atoi(portSTUNStr)
|
|
|
|
if err != nil {
|
|
|
|
return tailcfg.DERPRegion{}, err
|
2022-03-06 09:00:56 -07:00
|
|
|
}
|
2022-03-15 06:22:25 -06:00
|
|
|
localDERPregion.Nodes[0].STUNPort = portSTUN
|
2022-03-06 09:00:56 -07:00
|
|
|
|
2022-06-20 04:32:13 -06:00
|
|
|
log.Info().Caller().Msgf("DERP region: %+v", localDERPregion)
|
2024-01-16 08:04:03 -07:00
|
|
|
log.Info().Caller().Msgf("DERP Nodes[0]: %+v", localDERPregion.Nodes[0])
|
2022-06-26 03:43:17 -06:00
|
|
|
|
2022-03-05 12:04:31 -07:00
|
|
|
return localDERPregion, nil
|
2022-03-03 16:01:31 -07:00
|
|
|
}
|
|
|
|
|
2023-06-06 03:09:48 -06:00
|
|
|
func (d *DERPServer) DERPHandler(
|
2022-06-26 03:55:37 -06:00
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
2022-06-18 10:41:42 -06:00
|
|
|
) {
|
2022-06-26 03:55:37 -06:00
|
|
|
log.Trace().Caller().Msgf("/derp request from %v", req.RemoteAddr)
|
2022-09-04 08:13:48 -06:00
|
|
|
upgrade := strings.ToLower(req.Header.Get("Upgrade"))
|
|
|
|
|
|
|
|
if upgrade != "websocket" && upgrade != "derp" {
|
|
|
|
if upgrade != "" {
|
|
|
|
log.Warn().
|
|
|
|
Caller().
|
|
|
|
Msg("No Upgrade header in DERP server request. If headscale is behind a reverse proxy, make sure it is configured to pass WebSockets through.")
|
2022-03-03 16:01:31 -07:00
|
|
|
}
|
2022-06-26 03:55:37 -06:00
|
|
|
writer.Header().Set("Content-Type", "text/plain")
|
|
|
|
writer.WriteHeader(http.StatusUpgradeRequired)
|
2022-06-26 04:21:35 -06:00
|
|
|
_, err := writer.Write([]byte("DERP requires connection upgrade"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2022-03-06 09:35:54 -07:00
|
|
|
|
2022-03-03 16:01:31 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-26 03:55:37 -06:00
|
|
|
fastStart := req.Header.Get(fastStartHeader) == "1"
|
2022-03-03 16:01:31 -07:00
|
|
|
|
2022-06-26 03:55:37 -06:00
|
|
|
hijacker, ok := writer.(http.Hijacker)
|
2022-03-03 16:01:31 -07:00
|
|
|
if !ok {
|
|
|
|
log.Error().Caller().Msg("DERP requires Hijacker interface from Gin")
|
2022-06-26 03:55:37 -06:00
|
|
|
writer.Header().Set("Content-Type", "text/plain")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
2022-06-26 04:21:35 -06:00
|
|
|
_, err := writer.Write([]byte("HTTP does not support general TCP support"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2022-03-06 09:25:21 -07:00
|
|
|
|
2022-03-03 16:01:31 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
netConn, conn, err := hijacker.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Caller().Err(err).Msgf("Hijack failed")
|
2022-06-26 03:55:37 -06:00
|
|
|
writer.Header().Set("Content-Type", "text/plain")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
2022-06-26 04:21:35 -06:00
|
|
|
_, err = writer.Write([]byte("HTTP does not support general TCP support"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2022-03-06 09:25:21 -07:00
|
|
|
|
2022-03-03 16:01:31 -07:00
|
|
|
return
|
|
|
|
}
|
2022-06-26 03:55:37 -06:00
|
|
|
log.Trace().Caller().Msgf("Hijacked connection from %v", req.RemoteAddr)
|
2022-03-03 16:01:31 -07:00
|
|
|
|
|
|
|
if !fastStart {
|
2023-06-06 03:09:48 -06:00
|
|
|
pubKey := d.key.Public()
|
2023-02-02 02:34:50 -07:00
|
|
|
pubKeyStr, _ := pubKey.MarshalText() //nolint
|
2022-03-03 16:01:31 -07:00
|
|
|
fmt.Fprintf(conn, "HTTP/1.1 101 Switching Protocols\r\n"+
|
|
|
|
"Upgrade: DERP\r\n"+
|
|
|
|
"Connection: Upgrade\r\n"+
|
|
|
|
"Derp-Version: %v\r\n"+
|
|
|
|
"Derp-Public-Key: %s\r\n\r\n",
|
|
|
|
derp.ProtocolVersion,
|
2023-02-02 02:34:50 -07:00
|
|
|
string(pubKeyStr))
|
2022-03-03 16:01:31 -07:00
|
|
|
}
|
|
|
|
|
2023-06-06 03:09:48 -06:00
|
|
|
d.tailscaleDERP.Accept(req.Context(), netConn, conn, netConn.RemoteAddr().String())
|
2022-03-03 16:01:31 -07:00
|
|
|
}
|
|
|
|
|
2022-03-05 11:30:30 -07:00
|
|
|
// DERPProbeHandler is the endpoint that js/wasm clients hit to measure
|
2022-03-03 16:01:31 -07:00
|
|
|
// DERP latency, since they can't do UDP STUN queries.
|
2023-06-06 03:09:48 -06:00
|
|
|
func DERPProbeHandler(
|
2022-06-26 03:43:17 -06:00
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
2022-06-18 10:41:42 -06:00
|
|
|
) {
|
2022-06-26 03:43:17 -06:00
|
|
|
switch req.Method {
|
2022-09-04 03:33:00 -06:00
|
|
|
case http.MethodHead, http.MethodGet:
|
2022-06-26 03:43:17 -06:00
|
|
|
writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
2022-03-03 16:01:31 -07:00
|
|
|
default:
|
2022-06-26 03:43:17 -06:00
|
|
|
writer.WriteHeader(http.StatusMethodNotAllowed)
|
2022-06-26 04:21:35 -06:00
|
|
|
_, err := writer.Write([]byte("bogus probe method"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2022-03-03 16:01:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 17:23:35 -07:00
|
|
|
// DERPBootstrapDNSHandler implements the /bootsrap-dns endpoint
|
|
|
|
// Described in https://github.com/tailscale/tailscale/issues/1405,
|
|
|
|
// this endpoint provides a way to help a client when it fails to start up
|
|
|
|
// because its DNS are broken.
|
|
|
|
// The initial implementation is here https://github.com/tailscale/tailscale/pull/1406
|
|
|
|
// They have a cache, but not clear if that is really necessary at Headscale, uh, scale.
|
2022-03-06 09:00:56 -07:00
|
|
|
// An example implementation is found here https://derp.tailscale.com/bootstrap-dns
|
2024-02-08 09:28:19 -07:00
|
|
|
// Coordination server is included automatically, since local DERP is using the same DNS Name in d.serverURL.
|
2023-06-06 03:09:48 -06:00
|
|
|
func DERPBootstrapDNSHandler(
|
|
|
|
derpMap *tailcfg.DERPMap,
|
|
|
|
) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(
|
|
|
|
writer http.ResponseWriter,
|
|
|
|
req *http.Request,
|
|
|
|
) {
|
|
|
|
dnsEntries := make(map[string][]net.IP)
|
|
|
|
|
|
|
|
resolvCtx, cancel := context.WithTimeout(req.Context(), time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
var resolver net.Resolver
|
|
|
|
for _, region := range derpMap.Regions {
|
|
|
|
for _, node := range region.Nodes { // we don't care if we override some nodes
|
|
|
|
addrs, err := resolver.LookupIP(resolvCtx, "ip", node.HostName)
|
|
|
|
if err != nil {
|
|
|
|
log.Trace().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msgf("bootstrap DNS lookup failed %q", node.HostName)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dnsEntries[node.HostName] = addrs
|
2022-03-05 17:23:35 -07:00
|
|
|
}
|
|
|
|
}
|
2023-06-06 03:09:48 -06:00
|
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
err := json.NewEncoder(writer).Encode(dnsEntries)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Caller().
|
|
|
|
Err(err).
|
|
|
|
Msg("Failed to write response")
|
|
|
|
}
|
2022-06-26 04:21:35 -06:00
|
|
|
}
|
2022-03-03 16:01:31 -07:00
|
|
|
}
|
|
|
|
|
2022-03-06 09:25:21 -07:00
|
|
|
// ServeSTUN starts a STUN server on the configured addr.
|
2023-06-06 03:09:48 -06:00
|
|
|
func (d *DERPServer) ServeSTUN() {
|
|
|
|
packetConn, err := net.ListenPacket("udp", d.cfg.STUNAddr)
|
2022-03-03 16:01:31 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Msgf("failed to open STUN listener: %v", err)
|
|
|
|
}
|
2022-03-06 09:00:56 -07:00
|
|
|
log.Info().Msgf("STUN server started at %s", packetConn.LocalAddr())
|
2022-03-06 09:35:54 -07:00
|
|
|
|
|
|
|
udpConn, ok := packetConn.(*net.UDPConn)
|
|
|
|
if !ok {
|
|
|
|
log.Fatal().Msg("STUN listener is not a UDP listener")
|
|
|
|
}
|
|
|
|
serverSTUNListener(context.Background(), udpConn)
|
2022-03-03 16:01:31 -07:00
|
|
|
}
|
|
|
|
|
2022-03-06 09:35:54 -07:00
|
|
|
func serverSTUNListener(ctx context.Context, packetConn *net.UDPConn) {
|
2022-03-03 16:01:31 -07:00
|
|
|
var buf [64 << 10]byte
|
|
|
|
var (
|
2022-03-06 09:35:54 -07:00
|
|
|
bytesRead int
|
|
|
|
udpAddr *net.UDPAddr
|
|
|
|
err error
|
2022-03-03 16:01:31 -07:00
|
|
|
)
|
|
|
|
for {
|
2022-03-06 09:35:54 -07:00
|
|
|
bytesRead, udpAddr, err = packetConn.ReadFromUDP(buf[:])
|
2022-03-03 16:01:31 -07:00
|
|
|
if err != nil {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-03-05 11:30:30 -07:00
|
|
|
log.Error().Caller().Err(err).Msgf("STUN ReadFrom")
|
2022-03-03 16:01:31 -07:00
|
|
|
time.Sleep(time.Second)
|
2022-03-06 09:35:54 -07:00
|
|
|
|
2022-03-03 16:01:31 -07:00
|
|
|
continue
|
|
|
|
}
|
2022-03-06 09:35:54 -07:00
|
|
|
log.Trace().Caller().Msgf("STUN request from %v", udpAddr)
|
|
|
|
pkt := buf[:bytesRead]
|
2022-03-03 16:01:31 -07:00
|
|
|
if !stun.Is(pkt) {
|
2022-03-08 04:11:51 -07:00
|
|
|
log.Trace().Caller().Msgf("UDP packet is not STUN")
|
|
|
|
|
2022-03-03 16:01:31 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
txid, err := stun.ParseBindingRequest(pkt)
|
|
|
|
if err != nil {
|
2022-03-08 04:11:51 -07:00
|
|
|
log.Trace().Caller().Err(err).Msgf("STUN parse error")
|
|
|
|
|
2022-03-03 16:01:31 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-02 01:16:19 -06:00
|
|
|
addr, _ := netip.AddrFromSlice(udpAddr.IP)
|
|
|
|
res := stun.Response(txid, netip.AddrPortFrom(addr, uint16(udpAddr.Port)))
|
2022-03-06 09:35:54 -07:00
|
|
|
_, err = packetConn.WriteTo(res, udpAddr)
|
|
|
|
if err != nil {
|
2022-03-08 04:11:51 -07:00
|
|
|
log.Trace().Caller().Err(err).Msgf("Issue writing to UDP")
|
|
|
|
|
2022-03-06 09:35:54 -07:00
|
|
|
continue
|
|
|
|
}
|
2022-03-03 16:01:31 -07:00
|
|
|
}
|
|
|
|
}
|