mirror of
https://github.com/tailscale/tailscale.git
synced 2025-05-06 07:37:38 +00:00
wgengine/magicsock: move legacy endpoint creation into legacy.go.
Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
parent
f873da5b16
commit
0ad109f63d
@ -24,6 +24,51 @@ import (
|
|||||||
|
|
||||||
var errNoDestinations = errors.New("magicsock: no destinations")
|
var errNoDestinations = errors.New("magicsock: no destinations")
|
||||||
|
|
||||||
|
func (c *Conn) createLegacyEndpointLocked(pk key.Public, addrs string) (conn.Endpoint, error) {
|
||||||
|
a := &addrSet{
|
||||||
|
Logf: c.logf,
|
||||||
|
publicKey: pk,
|
||||||
|
curAddr: -1,
|
||||||
|
}
|
||||||
|
|
||||||
|
if addrs != "" {
|
||||||
|
for _, ep := range strings.Split(addrs, ",") {
|
||||||
|
ipp, err := netaddr.ParseIPPort(ep)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("bogus address %q", ep)
|
||||||
|
}
|
||||||
|
a.ipPorts = append(a.ipPorts, ipp)
|
||||||
|
a.addrs = append(a.addrs, *ipp.UDPAddr())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this endpoint is being updated, remember its old set of
|
||||||
|
// endpoints so we can remove any (from c.addrsByUDP) that are
|
||||||
|
// not in the new set.
|
||||||
|
var oldIPP []netaddr.IPPort
|
||||||
|
if preva, ok := c.addrsByKey[pk]; ok {
|
||||||
|
oldIPP = preva.ipPorts
|
||||||
|
}
|
||||||
|
c.addrsByKey[pk] = a
|
||||||
|
|
||||||
|
// Add entries to c.addrsByUDP.
|
||||||
|
for _, ipp := range a.ipPorts {
|
||||||
|
if ipp.IP == derpMagicIPAddr {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c.addrsByUDP[ipp] = a
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove previous c.addrsByUDP entries that are no longer in the new set.
|
||||||
|
for _, ipp := range oldIPP {
|
||||||
|
if ipp.IP != derpMagicIPAddr && c.addrsByUDP[ipp] != a {
|
||||||
|
delete(c.addrsByUDP, ipp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return a, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Conn) findLegacyEndpointLocked(ipp netaddr.IPPort, addr *net.UDPAddr) conn.Endpoint {
|
func (c *Conn) findLegacyEndpointLocked(ipp netaddr.IPPort, addr *net.UDPAddr) conn.Endpoint {
|
||||||
// Pre-disco: look up their addrSet.
|
// Pre-disco: look up their addrSet.
|
||||||
if as, ok := c.addrsByUDP[ipp]; ok {
|
if as, ok := c.addrsByUDP[ipp]; ok {
|
||||||
|
@ -2520,69 +2520,28 @@ func (c *Conn) CreateEndpoint(pubKey [32]byte, addrs string) (conn.Endpoint, err
|
|||||||
pk := key.Public(pubKey)
|
pk := key.Public(pubKey)
|
||||||
c.logf("magicsock: CreateEndpoint: key=%s: %s", pk.ShortString(), derpStr(addrs))
|
c.logf("magicsock: CreateEndpoint: key=%s: %s", pk.ShortString(), derpStr(addrs))
|
||||||
|
|
||||||
if strings.HasSuffix(addrs, controlclient.EndpointDiscoSuffix) {
|
if !strings.HasSuffix(addrs, controlclient.EndpointDiscoSuffix) {
|
||||||
discoHex := strings.TrimSuffix(addrs, controlclient.EndpointDiscoSuffix)
|
return c.createLegacyEndpointLocked(pk, addrs)
|
||||||
discoKey, err := key.NewPublicFromHexMem(mem.S(discoHex))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("magicsock: invalid discokey endpoint %q for %v: %w", addrs, pk.ShortString(), err)
|
|
||||||
}
|
|
||||||
de := &discoEndpoint{
|
|
||||||
c: c,
|
|
||||||
publicKey: tailcfg.NodeKey(pk), // peer public key (for WireGuard + DERP)
|
|
||||||
discoKey: tailcfg.DiscoKey(discoKey), // for discovery mesages
|
|
||||||
discoShort: tailcfg.DiscoKey(discoKey).ShortString(),
|
|
||||||
wgEndpointHostPort: addrs,
|
|
||||||
sentPing: map[stun.TxID]sentPing{},
|
|
||||||
endpointState: map[netaddr.IPPort]*endpointState{},
|
|
||||||
}
|
|
||||||
de.initFakeUDPAddr()
|
|
||||||
de.updateFromNode(c.nodeOfDisco[de.discoKey])
|
|
||||||
c.endpointOfDisco[de.discoKey] = de
|
|
||||||
return de, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
a := &addrSet{
|
discoHex := strings.TrimSuffix(addrs, controlclient.EndpointDiscoSuffix)
|
||||||
Logf: c.logf,
|
discoKey, err := key.NewPublicFromHexMem(mem.S(discoHex))
|
||||||
publicKey: pk,
|
if err != nil {
|
||||||
curAddr: -1,
|
return nil, fmt.Errorf("magicsock: invalid discokey endpoint %q for %v: %w", addrs, pk.ShortString(), err)
|
||||||
}
|
}
|
||||||
|
de := &discoEndpoint{
|
||||||
if addrs != "" {
|
c: c,
|
||||||
for _, ep := range strings.Split(addrs, ",") {
|
publicKey: tailcfg.NodeKey(pk), // peer public key (for WireGuard + DERP)
|
||||||
ipp, err := netaddr.ParseIPPort(ep)
|
discoKey: tailcfg.DiscoKey(discoKey), // for discovery mesages
|
||||||
if err != nil {
|
discoShort: tailcfg.DiscoKey(discoKey).ShortString(),
|
||||||
return nil, fmt.Errorf("bogus address %q", ep)
|
wgEndpointHostPort: addrs,
|
||||||
}
|
sentPing: map[stun.TxID]sentPing{},
|
||||||
a.ipPorts = append(a.ipPorts, ipp)
|
endpointState: map[netaddr.IPPort]*endpointState{},
|
||||||
a.addrs = append(a.addrs, *ipp.UDPAddr())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
de.initFakeUDPAddr()
|
||||||
// If this endpoint is being updated, remember its old set of
|
de.updateFromNode(c.nodeOfDisco[de.discoKey])
|
||||||
// endpoints so we can remove any (from c.addrsByUDP) that are
|
c.endpointOfDisco[de.discoKey] = de
|
||||||
// not in the new set.
|
return de, nil
|
||||||
var oldIPP []netaddr.IPPort
|
|
||||||
if preva, ok := c.addrsByKey[pk]; ok {
|
|
||||||
oldIPP = preva.ipPorts
|
|
||||||
}
|
|
||||||
c.addrsByKey[pk] = a
|
|
||||||
|
|
||||||
// Add entries to c.addrsByUDP.
|
|
||||||
for _, ipp := range a.ipPorts {
|
|
||||||
if ipp.IP == derpMagicIPAddr {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
c.addrsByUDP[ipp] = a
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove previous c.addrsByUDP entries that are no longer in the new set.
|
|
||||||
for _, ipp := range oldIPP {
|
|
||||||
if ipp.IP != derpMagicIPAddr && c.addrsByUDP[ipp] != a {
|
|
||||||
delete(c.addrsByUDP, ipp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return a, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RebindingUDPConn is a UDP socket that can be re-bound.
|
// RebindingUDPConn is a UDP socket that can be re-bound.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user