2020-03-12 21:14:48 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package stuntest provides a STUN test server.
|
|
|
|
package stuntest
|
|
|
|
|
|
|
|
import (
|
2020-05-17 17:51:38 +01:00
|
|
|
"fmt"
|
2020-03-12 21:14:48 +00:00
|
|
|
"net"
|
2020-05-17 17:51:38 +01:00
|
|
|
"strconv"
|
2020-03-12 21:14:48 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2020-05-17 17:51:38 +01:00
|
|
|
"inet.af/netaddr"
|
2020-03-12 21:14:48 +00:00
|
|
|
"tailscale.com/stun"
|
2020-05-17 17:51:38 +01:00
|
|
|
"tailscale.com/tailcfg"
|
2020-03-12 21:14:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type stunStats struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
readIPv4 int
|
|
|
|
readIPv6 int
|
|
|
|
}
|
|
|
|
|
2020-05-17 17:51:38 +01:00
|
|
|
func Serve(t *testing.T) (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
|
|
|
|
|
|
|
|
pc, err := net.ListenPacket("udp4", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open STUN listener: %v", err)
|
|
|
|
}
|
2020-05-17 17:51:38 +01:00
|
|
|
addr = &net.UDPAddr{
|
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Port: pc.LocalAddr().(*net.UDPAddr).Port,
|
|
|
|
}
|
2020-03-12 21:14:48 +00:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go runSTUN(t, pc, &stats, doneCh)
|
2020-05-17 17:51:38 +01:00
|
|
|
return addr, func() {
|
2020-03-12 21:14:48 +00:00
|
|
|
pc.Close()
|
|
|
|
<-doneCh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runSTUN(t *testing.T, pc net.PacketConn, stats *stunStats, done chan<- struct{}) {
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
var buf [64 << 10]byte
|
|
|
|
for {
|
|
|
|
n, addr, err := pc.ReadFrom(buf[:])
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "closed network connection") {
|
|
|
|
t.Logf("STUN server shutdown")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ua := addr.(*net.UDPAddr)
|
|
|
|
pkt := buf[:n]
|
|
|
|
if !stun.Is(pkt) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
txid, err := stun.ParseBindingRequest(pkt)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.mu.Lock()
|
|
|
|
if ua.IP.To4() != nil {
|
|
|
|
stats.readIPv4++
|
|
|
|
} else {
|
|
|
|
stats.readIPv6++
|
|
|
|
}
|
|
|
|
stats.mu.Unlock()
|
|
|
|
|
|
|
|
res := stun.Response(txid, ua.IP, uint16(ua.Port))
|
|
|
|
if _, err := pc.WriteTo(res, addr); err != nil {
|
|
|
|
t.Logf("STUN server write failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 17:51:38 +01: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
|
|
|
|
ip, err := netaddr.ParseIP(host)
|
|
|
|
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{
|
|
|
|
Name: fmt.Sprint(regionID) + "a",
|
|
|
|
RegionID: regionID,
|
|
|
|
HostName: fmt.Sprintf("d%d.invalid", regionID),
|
|
|
|
IPv4: ipv4,
|
|
|
|
IPv6: ipv6,
|
|
|
|
STUNPort: port,
|
|
|
|
STUNOnly: true,
|
|
|
|
}
|
|
|
|
m.Regions[regionID] = &tailcfg.DERPRegion{
|
|
|
|
RegionID: regionID,
|
|
|
|
Nodes: []*tailcfg.DERPNode{node},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|