all: convert from []wgcfg.Endpoint to string

This eliminates a dependency on wgcfg.Endpoint,
as part of the effort to eliminate our wireguard-go fork.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
Josh Bleecher Snyder 2021-01-13 17:10:41 -08:00 committed by Josh Bleecher Snyder
parent 9abcb18061
commit 654b5f1570
11 changed files with 52 additions and 64 deletions

View File

@ -27,7 +27,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
github.com/tailscale/wireguard-go/tai64n from github.com/tailscale/wireguard-go/device+ github.com/tailscale/wireguard-go/tai64n from github.com/tailscale/wireguard-go/device+
💣 github.com/tailscale/wireguard-go/tun from github.com/tailscale/wireguard-go/device+ 💣 github.com/tailscale/wireguard-go/tun from github.com/tailscale/wireguard-go/device+
W 💣 github.com/tailscale/wireguard-go/tun/wintun from github.com/tailscale/wireguard-go/tun+ W 💣 github.com/tailscale/wireguard-go/tun/wintun from github.com/tailscale/wireguard-go/tun+
github.com/tailscale/wireguard-go/wgcfg from github.com/tailscale/wireguard-go/conn+ github.com/tailscale/wireguard-go/wgcfg from github.com/tailscale/wireguard-go/device+
github.com/tcnksm/go-httpstat from tailscale.com/net/netcheck github.com/tcnksm/go-httpstat from tailscale.com/net/netcheck
github.com/toqueteos/webbrowser from tailscale.com/cmd/tailscale/cli github.com/toqueteos/webbrowser from tailscale.com/cmd/tailscale/cli
💣 go4.org/intern from inet.af/netaddr 💣 go4.org/intern from inet.af/netaddr

View File

@ -31,7 +31,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
github.com/tailscale/wireguard-go/tai64n from github.com/tailscale/wireguard-go/device+ github.com/tailscale/wireguard-go/tai64n from github.com/tailscale/wireguard-go/device+
💣 github.com/tailscale/wireguard-go/tun from github.com/tailscale/wireguard-go/device+ 💣 github.com/tailscale/wireguard-go/tun from github.com/tailscale/wireguard-go/device+
W 💣 github.com/tailscale/wireguard-go/tun/wintun from github.com/tailscale/wireguard-go/tun+ W 💣 github.com/tailscale/wireguard-go/tun/wintun from github.com/tailscale/wireguard-go/tun+
github.com/tailscale/wireguard-go/wgcfg from github.com/tailscale/wireguard-go/conn+ github.com/tailscale/wireguard-go/wgcfg from github.com/tailscale/wireguard-go/device+
github.com/tcnksm/go-httpstat from tailscale.com/net/netcheck github.com/tcnksm/go-httpstat from tailscale.com/net/netcheck
💣 go4.org/intern from inet.af/netaddr 💣 go4.org/intern from inet.af/netaddr
💣 go4.org/mem from tailscale.com/control/controlclient+ 💣 go4.org/mem from tailscale.com/control/controlclient+

View File

@ -298,7 +298,7 @@ func (nm *NetworkMap) WGCfg(logf logger.Logf, flags WGConfigFlags) (*wgcfg.Confi
if err := appendEndpoint(cpeer, fmt.Sprintf("%x%s", peer.DiscoKey[:], EndpointDiscoSuffix)); err != nil { if err := appendEndpoint(cpeer, fmt.Sprintf("%x%s", peer.DiscoKey[:], EndpointDiscoSuffix)); err != nil {
return nil, err return nil, err
} }
cpeer.Endpoints = []wgcfg.Endpoint{{Host: fmt.Sprintf("%x.disco.tailscale", peer.DiscoKey[:]), Port: 12345}} cpeer.Endpoints = fmt.Sprintf("%x.disco.tailscale:12345", peer.DiscoKey[:])
} else { } else {
if err := appendEndpoint(cpeer, peer.DERP); err != nil { if err := appendEndpoint(cpeer, peer.DERP); err != nil {
return nil, err return nil, err
@ -349,15 +349,18 @@ func appendEndpoint(peer *wgcfg.Peer, epStr string) error {
if epStr == "" { if epStr == "" {
return nil return nil
} }
host, port, err := net.SplitHostPort(epStr) _, port, err := net.SplitHostPort(epStr)
if err != nil { if err != nil {
return fmt.Errorf("malformed endpoint %q for peer %v", epStr, peer.PublicKey.ShortString()) return fmt.Errorf("malformed endpoint %q for peer %v", epStr, peer.PublicKey.ShortString())
} }
port16, err := strconv.ParseUint(port, 10, 16) _, err = strconv.ParseUint(port, 10, 16)
if err != nil { if err != nil {
return fmt.Errorf("invalid port in endpoint %q for peer %v", epStr, peer.PublicKey.ShortString()) return fmt.Errorf("invalid port in endpoint %q for peer %v", epStr, peer.PublicKey.ShortString())
} }
peer.Endpoints = append(peer.Endpoints, wgcfg.Endpoint{Host: host, Port: uint16(port16)}) if peer.Endpoints != "" {
peer.Endpoints += ","
}
peer.Endpoints += epStr
return nil return nil
} }

2
go.mod
View File

@ -24,7 +24,7 @@ require (
github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3 github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3
github.com/peterbourgon/ff/v2 v2.0.0 github.com/peterbourgon/ff/v2 v2.0.0
github.com/tailscale/depaware v0.0.0-20201214215404-77d1e9757027 github.com/tailscale/depaware v0.0.0-20201214215404-77d1e9757027
github.com/tailscale/wireguard-go v0.0.0-20210113223737-a6213b5eaf98 github.com/tailscale/wireguard-go v0.0.0-20210114205708-a1377e83f551
github.com/tcnksm/go-httpstat v0.2.0 github.com/tcnksm/go-httpstat v0.2.0
github.com/toqueteos/webbrowser v1.2.0 github.com/toqueteos/webbrowser v1.2.0
go4.org/mem v0.0.0-20201119185036-c04c5a6ff174 go4.org/mem v0.0.0-20201119185036-c04c5a6ff174

View File

@ -41,12 +41,7 @@ func getVal() []interface{} {
ListenPort: 5, ListenPort: 5,
Peers: []wgcfg.Peer{ Peers: []wgcfg.Peer{
{ {
Endpoints: []wgcfg.Endpoint{ Endpoints: "foo:5",
{
Host: "foo",
Port: 5,
},
},
}, },
}, },
}, },

View File

@ -567,24 +567,18 @@ func (as *addrSet) populatePeerStatus(ps *ipnstate.PeerStatus) {
} }
} }
func (a *addrSet) Addrs() []wgcfg.Endpoint { func (a *addrSet) Addrs() string {
var eps []wgcfg.Endpoint var addrs []string
for _, addr := range a.addrs { for _, addr := range a.addrs {
eps = append(eps, wgcfg.Endpoint{ addrs = append(addrs, addr.String())
Host: addr.IP.String(),
Port: uint16(addr.Port),
})
} }
a.mu.Lock() a.mu.Lock()
defer a.mu.Unlock() defer a.mu.Unlock()
if a.roamAddr != nil { if a.roamAddr != nil {
eps = append(eps, wgcfg.Endpoint{ addrs = append(addrs, a.roamAddr.String())
Host: a.roamAddr.IP.String(),
Port: uint16(a.roamAddr.Port),
})
} }
return eps return strings.Join(addrs, ",")
} }
// Message types copied from wireguard-go/device/noise-protocol.go // Message types copied from wireguard-go/device/noise-protocol.go

View File

@ -28,7 +28,6 @@
"time" "time"
"github.com/tailscale/wireguard-go/conn" "github.com/tailscale/wireguard-go/conn"
"github.com/tailscale/wireguard-go/wgcfg"
"go4.org/mem" "go4.org/mem"
"golang.org/x/crypto/nacl/box" "golang.org/x/crypto/nacl/box"
"golang.org/x/time/rate" "golang.org/x/time/rate"
@ -2977,19 +2976,11 @@ func (de *discoEndpoint) String() string {
return fmt.Sprintf("magicsock.discoEndpoint{%v, %v}", de.publicKey.ShortString(), de.discoShort) return fmt.Sprintf("magicsock.discoEndpoint{%v, %v}", de.publicKey.ShortString(), de.discoShort)
} }
func (de *discoEndpoint) Addrs() []wgcfg.Endpoint { func (de *discoEndpoint) Addrs() string {
// This has to be the same string that was passed to // This has to be the same string that was passed to
// CreateEndpoint, otherwise Reconfig will end up recreating // CreateEndpoint, otherwise Reconfig will end up recreating
// Endpoints and losing state over time. // Endpoints and losing state over time.
host, portStr, err := net.SplitHostPort(de.wgEndpointHostPort) return de.wgEndpointHostPort
if err != nil {
panic(err)
}
port, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
panic(err)
}
return []wgcfg.Endpoint{{Host: host, Port: uint16(port)}}
} }
func (de *discoEndpoint) ClearSrc() {} func (de *discoEndpoint) ClearSrc() {}

View File

@ -483,12 +483,9 @@ func makeConfigs(t *testing.T, addrs []netaddr.IPPort) []wgcfg.Config {
continue continue
} }
peer := wgcfg.Peer{ peer := wgcfg.Peer{
PublicKey: privKeys[peerNum].Public(), PublicKey: privKeys[peerNum].Public(),
AllowedIPs: addresses[peerNum], AllowedIPs: addresses[peerNum],
Endpoints: []wgcfg.Endpoint{{ Endpoints: addr.String(),
Host: addr.IP.String(),
Port: addr.Port,
}},
PersistentKeepalive: 25, PersistentKeepalive: 25,
} }
cfg.Peers = append(cfg.Peers, peer) cfg.Peers = append(cfg.Peers, peer)
@ -1140,12 +1137,12 @@ func testTwoDevicePing(t *testing.T, d *devices) {
}) })
// Add DERP relay. // Add DERP relay.
derpEp := wgcfg.Endpoint{Host: "127.3.3.40", Port: 1} derpEp := "127.3.3.40:1"
ep0 := cfgs[0].Peers[0].Endpoints ep0 := cfgs[0].Peers[0].Endpoints
ep0 = append([]wgcfg.Endpoint{derpEp}, ep0...) ep0 = derpEp + "," + ep0
cfgs[0].Peers[0].Endpoints = ep0 cfgs[0].Peers[0].Endpoints = ep0
ep1 := cfgs[1].Peers[0].Endpoints ep1 := cfgs[1].Peers[0].Endpoints
ep1 = append([]wgcfg.Endpoint{derpEp}, ep1...) ep1 = derpEp + "," + ep1
cfgs[1].Peers[0].Endpoints = ep1 cfgs[1].Peers[0].Endpoints = ep1
if err := m1.dev.Reconfig(&cfgs[0]); err != nil { if err := m1.dev.Reconfig(&cfgs[0]); err != nil {
t.Fatal(err) t.Fatal(err)
@ -1161,8 +1158,8 @@ func testTwoDevicePing(t *testing.T, d *devices) {
}) })
// Disable real route. // Disable real route.
cfgs[0].Peers[0].Endpoints = []wgcfg.Endpoint{derpEp} cfgs[0].Peers[0].Endpoints = derpEp
cfgs[1].Peers[0].Endpoints = []wgcfg.Endpoint{derpEp} cfgs[1].Peers[0].Endpoints = derpEp
if err := m1.dev.Reconfig(&cfgs[0]); err != nil { if err := m1.dev.Reconfig(&cfgs[0]); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -674,10 +674,15 @@ func isTrimmablePeer(p *wgcfg.Peer, numPeers int) bool {
if forceFullWireguardConfig(numPeers) { if forceFullWireguardConfig(numPeers) {
return false return false
} }
if len(p.Endpoints) != 1 { if !isSingleEndpoint(p.Endpoints) {
return false return false
} }
if !strings.HasSuffix(p.Endpoints[0].Host, ".disco.tailscale") {
host, _, err := net.SplitHostPort(p.Endpoints)
if err != nil {
return false
}
if !strings.HasSuffix(host, ".disco.tailscale") {
return false return false
} }
@ -741,11 +746,14 @@ func (e *userspaceEngine) isActiveSince(dk tailcfg.DiscoKey, ip netaddr.IP, t ti
// Host of form "<64-hex-digits>.disco.tailscale". If invariant is violated, // Host of form "<64-hex-digits>.disco.tailscale". If invariant is violated,
// we return the zero value. // we return the zero value.
func discoKeyFromPeer(p *wgcfg.Peer) tailcfg.DiscoKey { func discoKeyFromPeer(p *wgcfg.Peer) tailcfg.DiscoKey {
host := p.Endpoints[0].Host if len(p.Endpoints) < 64 {
if len(host) < 64 {
return tailcfg.DiscoKey{} return tailcfg.DiscoKey{}
} }
k, err := key.NewPublicFromHexMem(mem.S(host[:64])) host, rest := p.Endpoints[:64], p.Endpoints[64:]
if !strings.HasPrefix(rest, ".disco.tailscale") {
return tailcfg.DiscoKey{}
}
k, err := key.NewPublicFromHexMem(mem.S(host))
if err != nil { if err != nil {
return tailcfg.DiscoKey{} return tailcfg.DiscoKey{}
} }
@ -946,21 +954,21 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config)
// and a second time with it. // and a second time with it.
discoChanged := make(map[key.Public]bool) discoChanged := make(map[key.Public]bool)
{ {
prevEP := make(map[key.Public]wgcfg.Endpoint) prevEP := make(map[key.Public]string)
for i := range e.lastCfgFull.Peers { for i := range e.lastCfgFull.Peers {
if p := &e.lastCfgFull.Peers[i]; len(p.Endpoints) == 1 { if p := &e.lastCfgFull.Peers[i]; isSingleEndpoint(p.Endpoints) {
prevEP[key.Public(p.PublicKey)] = p.Endpoints[0] prevEP[key.Public(p.PublicKey)] = p.Endpoints
} }
} }
for i := range cfg.Peers { for i := range cfg.Peers {
p := &cfg.Peers[i] p := &cfg.Peers[i]
if len(p.Endpoints) != 1 { if !isSingleEndpoint(p.Endpoints) {
continue continue
} }
pub := key.Public(p.PublicKey) pub := key.Public(p.PublicKey)
if old, ok := prevEP[pub]; ok && old != p.Endpoints[0] { if old, ok := prevEP[pub]; ok && old != p.Endpoints {
discoChanged[pub] = true discoChanged[pub] = true
e.logf("wgengine: Reconfig: %s changed from %s to %s", pub.ShortString(), &old, &p.Endpoints[0]) e.logf("wgengine: Reconfig: %s changed from %q to %q", pub.ShortString(), old, p.Endpoints)
} }
} }
} }
@ -1005,6 +1013,11 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config)
return nil return nil
} }
// isSingleEndpoint reports whether endpoints contains exactly one host:port pair.
func isSingleEndpoint(s string) bool {
return s != "" && !strings.Contains(s, ",")
}
func (e *userspaceEngine) GetFilter() *filter.Filter { func (e *userspaceEngine) GetFilter() *filter.Filter {
return e.tundev.GetFilter() return e.tundev.GetFilter()
} }

View File

@ -103,12 +103,7 @@ func TestUserspaceEngineReconfig(t *testing.T) {
AllowedIPs: []netaddr.IPPrefix{ AllowedIPs: []netaddr.IPPrefix{
{IP: netaddr.IPv4(100, 100, 99, 1), Bits: 32}, {IP: netaddr.IPv4(100, 100, 99, 1), Bits: 32},
}, },
Endpoints: []wgcfg.Endpoint{ Endpoints: discoHex + ".disco.tailscale:12345",
{
Host: discoHex + ".disco.tailscale",
Port: 12345,
},
},
}, },
}, },
} }

View File

@ -36,7 +36,7 @@ type PeerStatus struct {
// TODO(bradfitz): remove this, subset of ipnstate? Need to migrate users. // TODO(bradfitz): remove this, subset of ipnstate? Need to migrate users.
type Status struct { type Status struct {
Peers []PeerStatus Peers []PeerStatus
LocalAddrs []string // TODO(crawshaw): []wgcfg.Endpoint? LocalAddrs []string // the set of possible endpoints for the magic conn
DERPs int // number of active DERP connections DERPs int // number of active DERP connections
} }