2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-03-12 21:14:48 +00:00
|
|
|
|
|
|
|
// Package stuntest provides a STUN test server.
|
|
|
|
package stuntest
|
|
|
|
|
|
|
|
import (
|
2020-07-10 21:26:04 +00:00
|
|
|
"context"
|
2023-04-15 20:08:16 +00:00
|
|
|
"errors"
|
2020-05-17 16:51:38 +00:00
|
|
|
"fmt"
|
2020-03-12 21:14:48 +00:00
|
|
|
"net"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2020-05-17 16:51:38 +00:00
|
|
|
"strconv"
|
2020-03-12 21:14:48 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2023-04-15 20:08:16 +00:00
|
|
|
"tailscale.com/net/netaddr"
|
2020-05-25 16:15:50 +00:00
|
|
|
"tailscale.com/net/stun"
|
2020-05-17 16:51:38 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2020-07-10 21:26:04 +00:00
|
|
|
"tailscale.com/types/nettype"
|
2020-03-12 21:14:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type stunStats struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
readIPv4 int
|
|
|
|
readIPv6 int
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:49:45 +00:00
|
|
|
func Serve(t testing.TB) (addr *net.UDPAddr, cleanupFn func()) {
|
2020-07-10 21:26:04 +00:00
|
|
|
return ServeWithPacketListener(t, nettype.Std{})
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:49:45 +00:00
|
|
|
func ServeWithPacketListener(t testing.TB, ln nettype.PacketListener) (addr *net.UDPAddr, cleanupFn func()) {
|
2020-03-12 21:14:48 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// TODO(crawshaw): use stats to test re-STUN logic
|
|
|
|
var stats stunStats
|
|
|
|
|
2020-07-10 21:26:04 +00:00
|
|
|
pc, err := ln.ListenPacket(context.Background(), "udp4", ":0")
|
2020-03-12 21:14:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open STUN listener: %v", err)
|
|
|
|
}
|
2020-07-10 21:26:04 +00:00
|
|
|
addr = pc.LocalAddr().(*net.UDPAddr)
|
|
|
|
if len(addr.IP) == 0 || addr.IP.IsUnspecified() {
|
|
|
|
addr.IP = net.ParseIP("127.0.0.1")
|
2020-05-17 16:51:38 +00:00
|
|
|
}
|
2020-03-12 21:14:48 +00:00
|
|
|
doneCh := make(chan struct{})
|
2023-04-15 20:08:16 +00:00
|
|
|
go runSTUN(t, pc.(nettype.PacketConn), &stats, doneCh)
|
2020-05-17 16:51:38 +00:00
|
|
|
return addr, func() {
|
2020-03-12 21:14:48 +00:00
|
|
|
pc.Close()
|
|
|
|
<-doneCh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 20:08:16 +00:00
|
|
|
func runSTUN(t testing.TB, pc nettype.PacketConn, stats *stunStats, done chan<- struct{}) {
|
2020-03-12 21:14:48 +00:00
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
var buf [64 << 10]byte
|
|
|
|
for {
|
2023-04-15 20:08:16 +00:00
|
|
|
n, src, err := pc.ReadFromUDPAddrPort(buf[:])
|
2020-03-12 21:14:48 +00:00
|
|
|
if err != nil {
|
2023-04-15 20:08:16 +00:00
|
|
|
if errors.Is(err, net.ErrClosed) {
|
2020-03-12 21:14:48 +00:00
|
|
|
t.Logf("STUN server shutdown")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2023-04-15 20:08:16 +00:00
|
|
|
src = netaddr.Unmap(src)
|
2020-03-12 21:14:48 +00:00
|
|
|
pkt := buf[:n]
|
|
|
|
if !stun.Is(pkt) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
txid, err := stun.ParseBindingRequest(pkt)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.mu.Lock()
|
2023-04-15 20:08:16 +00:00
|
|
|
if src.Addr().Is4() {
|
2020-03-12 21:14:48 +00:00
|
|
|
stats.readIPv4++
|
|
|
|
} else {
|
|
|
|
stats.readIPv6++
|
|
|
|
}
|
|
|
|
stats.mu.Unlock()
|
|
|
|
|
2023-04-15 20:08:16 +00:00
|
|
|
res := stun.Response(txid, src)
|
|
|
|
if _, err := pc.WriteToUDPAddrPort(res, src); err != nil {
|
2020-03-12 21:14:48 +00:00
|
|
|
t.Logf("STUN server write failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 16:51:38 +00:00
|
|
|
|
|
|
|
func DERPMapOf(stun ...string) *tailcfg.DERPMap {
|
|
|
|
m := &tailcfg.DERPMap{
|
|
|
|
Regions: map[int]*tailcfg.DERPRegion{},
|
|
|
|
}
|
|
|
|
for i, hostPortStr := range stun {
|
|
|
|
regionID := i + 1
|
|
|
|
host, portStr, err := net.SplitHostPort(hostPortStr)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("bogus STUN hostport: %q", hostPortStr))
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(portStr)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("bogus port %q in %q", portStr, hostPortStr))
|
|
|
|
}
|
|
|
|
var ipv4, ipv6 string
|
2022-07-26 03:55:44 +00:00
|
|
|
ip, err := netip.ParseAddr(host)
|
2020-05-17 16:51:38 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("bogus non-IP STUN host %q in %q", host, hostPortStr))
|
|
|
|
}
|
|
|
|
if ip.Is4() {
|
|
|
|
ipv4 = host
|
|
|
|
ipv6 = "none"
|
|
|
|
}
|
|
|
|
if ip.Is6() {
|
|
|
|
ipv6 = host
|
|
|
|
ipv4 = "none"
|
|
|
|
}
|
|
|
|
node := &tailcfg.DERPNode{
|
2024-05-05 18:57:47 +00:00
|
|
|
Name: fmt.Sprint(regionID) + "a",
|
|
|
|
RegionID: regionID,
|
|
|
|
HostName: fmt.Sprintf("d%d%s", regionID, tailcfg.DotInvalid),
|
|
|
|
IPv4: ipv4,
|
|
|
|
IPv6: ipv6,
|
|
|
|
STUNPort: port,
|
|
|
|
STUNOnly: true,
|
|
|
|
STUNTestIP: host,
|
2020-05-17 16:51:38 +00:00
|
|
|
}
|
|
|
|
m.Regions[regionID] = &tailcfg.DERPRegion{
|
|
|
|
RegionID: regionID,
|
|
|
|
Nodes: []*tailcfg.DERPNode{node},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|