2022-03-03 18:01:31 -05:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-03-05 14:04:31 -05:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2022-03-03 18:01:31 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/net/stun"
|
2022-03-05 14:04:31 -05:00
|
|
|
"tailscale.com/tailcfg"
|
2022-03-03 18:01:31 -05: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 13:30:30 -05:00
|
|
|
type DERPServer struct {
|
2022-03-04 05:31:41 -05:00
|
|
|
tailscaleDERP *derp.Server
|
2022-03-05 14:04:31 -05:00
|
|
|
region tailcfg.DERPRegion
|
2022-03-03 18:01:31 -05:00
|
|
|
}
|
|
|
|
|
2022-03-05 13:30:30 -05:00
|
|
|
func (h *Headscale) NewDERPServer() (*DERPServer, error) {
|
2022-03-06 11:35:54 -05:00
|
|
|
server := derp.NewServer(key.NodePrivate(*h.privateKey), log.Info().Msgf)
|
2022-03-05 14:04:31 -05:00
|
|
|
region, err := h.generateRegionLocalDERP()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-06 11:35:54 -05:00
|
|
|
|
|
|
|
return &DERPServer{server, region}, nil
|
2022-03-05 14:04:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Headscale) generateRegionLocalDERP() (tailcfg.DERPRegion, error) {
|
|
|
|
serverURL, err := url.Parse(h.cfg.ServerURL)
|
|
|
|
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 18:01:31 -05:00
|
|
|
|
2022-03-05 14:04:31 -05:00
|
|
|
localDERPregion := tailcfg.DERPRegion{
|
2022-03-06 11:25:21 -05:00
|
|
|
RegionID: h.cfg.DERP.ServerRegionID,
|
|
|
|
RegionCode: h.cfg.DERP.ServerRegionCode,
|
|
|
|
RegionName: h.cfg.DERP.ServerRegionName,
|
2022-03-05 14:04:31 -05:00
|
|
|
Avoid: false,
|
|
|
|
Nodes: []*tailcfg.DERPNode{
|
|
|
|
{
|
2022-03-06 11:25:21 -05:00
|
|
|
Name: fmt.Sprintf("%d", h.cfg.DERP.ServerRegionID),
|
|
|
|
RegionID: h.cfg.DERP.ServerRegionID,
|
2022-03-05 14:04:31 -05:00
|
|
|
HostName: host,
|
|
|
|
DERPPort: port,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-03-06 11:00:56 -05:00
|
|
|
|
|
|
|
if h.cfg.DERP.STUNEnabled {
|
|
|
|
_, portStr, err := net.SplitHostPort(h.cfg.DERP.STUNAddr)
|
|
|
|
if err != nil {
|
|
|
|
return tailcfg.DERPRegion{}, err
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(portStr)
|
|
|
|
if err != nil {
|
|
|
|
return tailcfg.DERPRegion{}, err
|
|
|
|
}
|
|
|
|
localDERPregion.Nodes[0].STUNPort = port
|
|
|
|
}
|
|
|
|
|
2022-03-05 14:04:31 -05:00
|
|
|
return localDERPregion, nil
|
2022-03-03 18:01:31 -05:00
|
|
|
}
|
|
|
|
|
2022-03-05 13:30:30 -05:00
|
|
|
func (h *Headscale) DERPHandler(ctx *gin.Context) {
|
|
|
|
log.Trace().Caller().Msgf("/derp request from %v", ctx.ClientIP())
|
2022-03-03 18:01:31 -05:00
|
|
|
up := strings.ToLower(ctx.Request.Header.Get("Upgrade"))
|
|
|
|
if up != "websocket" && up != "derp" {
|
|
|
|
if up != "" {
|
|
|
|
log.Warn().Caller().Msgf("Weird websockets connection upgrade: %q", up)
|
|
|
|
}
|
|
|
|
ctx.String(http.StatusUpgradeRequired, "DERP requires connection upgrade")
|
2022-03-06 11:35:54 -05:00
|
|
|
|
2022-03-03 18:01:31 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fastStart := ctx.Request.Header.Get(fastStartHeader) == "1"
|
|
|
|
|
|
|
|
hijacker, ok := ctx.Writer.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
log.Error().Caller().Msg("DERP requires Hijacker interface from Gin")
|
|
|
|
ctx.String(http.StatusInternalServerError, "HTTP does not support general TCP support")
|
2022-03-06 11:25:21 -05:00
|
|
|
|
2022-03-03 18:01:31 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
netConn, conn, err := hijacker.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Caller().Err(err).Msgf("Hijack failed")
|
|
|
|
ctx.String(http.StatusInternalServerError, "HTTP does not support general TCP support")
|
2022-03-06 11:25:21 -05:00
|
|
|
|
2022-03-03 18:01:31 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fastStart {
|
|
|
|
pubKey := h.privateKey.Public()
|
2022-03-06 11:35:54 -05:00
|
|
|
pubKeyStr := pubKey.UntypedHexString() // nolint
|
2022-03-03 18:01:31 -05: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,
|
2022-03-06 11:35:54 -05:00
|
|
|
pubKeyStr)
|
2022-03-03 18:01:31 -05:00
|
|
|
}
|
|
|
|
|
2022-03-05 13:30:30 -05:00
|
|
|
h.DERPServer.tailscaleDERP.Accept(netConn, conn, netConn.RemoteAddr().String())
|
2022-03-03 18:01:31 -05:00
|
|
|
}
|
|
|
|
|
2022-03-05 13:30:30 -05:00
|
|
|
// DERPProbeHandler is the endpoint that js/wasm clients hit to measure
|
2022-03-03 18:01:31 -05:00
|
|
|
// DERP latency, since they can't do UDP STUN queries.
|
2022-03-05 13:30:30 -05:00
|
|
|
func (h *Headscale) DERPProbeHandler(ctx *gin.Context) {
|
2022-03-03 18:01:31 -05:00
|
|
|
switch ctx.Request.Method {
|
|
|
|
case "HEAD", "GET":
|
|
|
|
ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
default:
|
|
|
|
ctx.String(http.StatusMethodNotAllowed, "bogus probe method")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 19:23:35 -05: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 11:00:56 -05:00
|
|
|
// An example implementation is found here https://derp.tailscale.com/bootstrap-dns
|
2022-03-05 13:30:30 -05:00
|
|
|
func (h *Headscale) DERPBootstrapDNSHandler(ctx *gin.Context) {
|
2022-03-05 19:23:35 -05:00
|
|
|
dnsEntries := make(map[string][]net.IP)
|
|
|
|
|
|
|
|
resolvCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
var r net.Resolver
|
|
|
|
for _, region := range h.DERPMap.Regions {
|
|
|
|
for _, node := range region.Nodes { // we don't care if we override some nodes
|
|
|
|
addrs, err := r.LookupIP(resolvCtx, "ip", node.HostName)
|
|
|
|
if err != nil {
|
|
|
|
log.Trace().Caller().Err(err).Msgf("bootstrap DNS lookup failed %q", node.HostName)
|
2022-03-06 11:35:54 -05:00
|
|
|
|
2022-03-05 19:23:35 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
dnsEntries[node.HostName] = addrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, dnsEntries)
|
2022-03-03 18:01:31 -05:00
|
|
|
}
|
|
|
|
|
2022-03-06 11:25:21 -05:00
|
|
|
// ServeSTUN starts a STUN server on the configured addr.
|
2022-03-03 18:01:31 -05:00
|
|
|
func (h *Headscale) ServeSTUN() {
|
2022-03-06 11:00:56 -05:00
|
|
|
packetConn, err := net.ListenPacket("udp", h.cfg.DERP.STUNAddr)
|
2022-03-03 18:01:31 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Msgf("failed to open STUN listener: %v", err)
|
|
|
|
}
|
2022-03-06 11:00:56 -05:00
|
|
|
log.Info().Msgf("STUN server started at %s", packetConn.LocalAddr())
|
2022-03-06 11:35:54 -05: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 18:01:31 -05:00
|
|
|
}
|
|
|
|
|
2022-03-06 11:35:54 -05:00
|
|
|
func serverSTUNListener(ctx context.Context, packetConn *net.UDPConn) {
|
2022-03-03 18:01:31 -05:00
|
|
|
var buf [64 << 10]byte
|
|
|
|
var (
|
2022-03-06 11:35:54 -05:00
|
|
|
bytesRead int
|
|
|
|
udpAddr *net.UDPAddr
|
|
|
|
err error
|
2022-03-03 18:01:31 -05:00
|
|
|
)
|
|
|
|
for {
|
2022-03-06 11:35:54 -05:00
|
|
|
bytesRead, udpAddr, err = packetConn.ReadFromUDP(buf[:])
|
2022-03-03 18:01:31 -05:00
|
|
|
if err != nil {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-03-05 13:30:30 -05:00
|
|
|
log.Error().Caller().Err(err).Msgf("STUN ReadFrom")
|
2022-03-03 18:01:31 -05:00
|
|
|
time.Sleep(time.Second)
|
2022-03-06 11:35:54 -05:00
|
|
|
|
2022-03-03 18:01:31 -05:00
|
|
|
continue
|
|
|
|
}
|
2022-03-06 11:35:54 -05:00
|
|
|
log.Trace().Caller().Msgf("STUN request from %v", udpAddr)
|
|
|
|
pkt := buf[:bytesRead]
|
2022-03-03 18:01:31 -05:00
|
|
|
if !stun.Is(pkt) {
|
2022-03-08 06:11:51 -05:00
|
|
|
log.Trace().Caller().Msgf("UDP packet is not STUN")
|
|
|
|
|
2022-03-03 18:01:31 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
txid, err := stun.ParseBindingRequest(pkt)
|
|
|
|
if err != nil {
|
2022-03-08 06:11:51 -05:00
|
|
|
log.Trace().Caller().Err(err).Msgf("STUN parse error")
|
|
|
|
|
2022-03-03 18:01:31 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-06 11:35:54 -05:00
|
|
|
res := stun.Response(txid, udpAddr.IP, uint16(udpAddr.Port))
|
|
|
|
_, err = packetConn.WriteTo(res, udpAddr)
|
|
|
|
if err != nil {
|
2022-03-08 06:11:51 -05:00
|
|
|
log.Trace().Caller().Err(err).Msgf("Issue writing to UDP")
|
|
|
|
|
2022-03-06 11:35:54 -05:00
|
|
|
continue
|
|
|
|
}
|
2022-03-03 18:01:31 -05:00
|
|
|
}
|
|
|
|
}
|