Files
Jakob Borg 3f795834db feat: Peer-to-peer discovery (fixes #3388)
This implements a new peer-to-peer-based discovery protocol.

- The DHT is based on libp2p (of IPFS etc) and constitutes a new,
  separate Syncthing-specific network. It does not piggyback on any
  existing non-Syncthing nodes out there.
- It uses existing device IDs as the keys, and publishes discovery
  documents that include the device certificate and are signed by the
  same. This enables validation that the discovery data is _for_ the
  right device ID, as well as published _by_ that device.
- The DHT runs on the QUIC socket, so it shares UPnP/NAT-PMP whole
  punches and STUN discovery. (Unless the QUIC listener is disabled,
  then it runs on an ephemeral socket and can effectively do only
  queries and not participate as a full DHT node.)
- There is a hardcoded default pointing to three bootstrap nodes hosted
  in the usual manner.
- Currently it's strictly additive -- it runs separately and in
  parallell with the usual global discovery. Once proven and scaled, it
  will gradually take over the duties of the global discovery.
- There is a new command line tool `stdht` which just runs a DHT node to
  assist with discovery (the bootstrap nodes are this).

The diff is large and terrifying, but that's how it is.

Signed-off-by: Jakob Borg <[email protected]>
2026-11-05 22:43:18 +01:00

135 lines
3.1 KiB
Go

// Copyright (C) 2015 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package nat
import (
"fmt"
"log/slog"
"net"
"slices"
"strconv"
"sync"
"time"
)
type MappingChangeSubscriber func()
type Mapping struct {
protocol Protocol
ipVersion IPVersion
address Address
extAddresses map[string][]Address // NAT ID -> Address
expires time.Time
subscribers []MappingChangeSubscriber
mut sync.RWMutex
}
func (m *Mapping) setAddressLocked(id string, addresses []Address) {
slog.Info("New external port opened", "protocol", m.protocol, "external", addresses, "local", m.address, "gateway", id)
m.extAddresses[id] = addresses
}
func (m *Mapping) removeAddressLocked(id string) {
addresses, ok := m.extAddresses[id]
if ok {
slog.Info("Removing external open port", "protocol", m.protocol, "external", addresses, "gateway", id)
delete(m.extAddresses, id)
}
}
func (m *Mapping) clearAddresses() {
m.mut.Lock()
change := len(m.extAddresses) > 0
for id, addr := range m.extAddresses {
l.Debugf("Clearing mapping %s: ID: %s Address: %s", m, id, addr)
delete(m.extAddresses, id)
}
m.expires = time.Time{}
m.mut.Unlock()
if change {
m.notify()
}
}
func (m *Mapping) notify() {
m.mut.RLock()
subscribers := slices.Clone(m.subscribers)
m.mut.RUnlock()
for _, subscriber := range subscribers {
subscriber()
}
}
func (m *Mapping) Protocol() Protocol {
return m.protocol
}
func (m *Mapping) Address() Address {
return m.address
}
func (m *Mapping) ExternalAddresses() []Address {
m.mut.RLock()
addrs := make([]Address, 0, len(m.extAddresses))
for _, addr := range m.extAddresses {
addrs = append(addrs, addr...)
}
m.mut.RUnlock()
return addrs
}
func (m *Mapping) OnChanged(subscribed MappingChangeSubscriber) {
m.mut.Lock()
m.subscribers = append(m.subscribers, subscribed)
m.mut.Unlock()
}
func (m *Mapping) String() string {
return fmt.Sprintf("%s/%s", m.address, m.protocol)
}
func (m *Mapping) GoString() string {
return m.String()
}
// Checks if the mappings local IP address matches the IP address of the gateway
// For example, if we are explicitly listening on 192.168.0.12, there is no
// point trying to acquire a mapping on a gateway to which the local IP is
// 10.0.0.1. Fallback to true if any of the IPs is not there.
func (m *Mapping) validGateway(ip net.IP) bool {
if m.address.IP == nil || ip == nil || m.address.IP.IsUnspecified() || ip.IsUnspecified() {
return true
}
return m.address.IP.Equal(ip)
}
// Address is essentially net.TCPAddr yet is more general, and has a few helper
// methods which reduce boilerplate code.
type Address struct {
IP net.IP
Port int
}
func (a Address) Equal(b Address) bool {
return a.Port == b.Port && a.IP.Equal(b.IP)
}
func (a Address) String() string {
var ipStr string
if a.IP == nil {
ipStr = net.IPv4zero.String()
} else {
ipStr = a.IP.String()
}
return net.JoinHostPort(ipStr, strconv.Itoa(a.Port))
}
func (a Address) GoString() string {
return a.String()
}