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

107 lines
3.0 KiB
Go

// Copyright (C) 2026 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 dht
import (
"context"
ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
"github.com/syncthing/syncthing/internal/db"
)
// datastore provides database-backed storage for the DHT values and
// routing table.
type datastore struct {
store *db.Typed
}
// NewDatastore provides database-backed storage for DHT values or routing
// data.
func NewDatastore(store *db.Typed) ds.Batching {
return &datastore{store: store}
}
func (s *datastore) Put(ctx context.Context, key ds.Key, value []byte) error {
result := "success"
err := s.store.PutBytes(key.String(), value)
if err != nil {
result = "error"
}
metricDatastoreOperationsTotal.WithLabelValues(s.store.Prefix(), "put", result).Inc()
return err
}
func (s *datastore) Get(ctx context.Context, key ds.Key) ([]byte, error) {
value, ok, err := s.store.Bytes(key.String())
if err != nil {
metricDatastoreOperationsTotal.WithLabelValues(s.store.Prefix(), "get", "error").Inc()
return nil, err
}
if !ok {
metricDatastoreOperationsTotal.WithLabelValues(s.store.Prefix(), "get", "notfound").Inc()
return nil, ds.ErrNotFound
}
metricDatastoreOperationsTotal.WithLabelValues(s.store.Prefix(), "get", "success").Inc()
return value, nil
}
func (s *datastore) Has(ctx context.Context, key ds.Key) (bool, error) {
return ds.GetBackedHas(ctx, s, key)
}
func (s *datastore) GetSize(ctx context.Context, key ds.Key) (int, error) {
return ds.GetBackedSize(ctx, s, key)
}
func (s *datastore) Delete(ctx context.Context, key ds.Key) error {
result := "success"
err := s.store.Delete(key.String())
if err != nil {
result = "error"
}
metricDatastoreOperationsTotal.WithLabelValues(s.store.Prefix(), "delete", result).Inc()
return err
}
func (s *datastore) Query(ctx context.Context, q query.Query) (query.Results, error) {
var entries []query.Entry
it, errFn := s.store.KeyValues()
for kv := range it {
if err := ctx.Err(); err != nil {
metricDatastoreOperationsTotal.WithLabelValues(s.store.Prefix(), "query", "error").Inc()
return nil, err
}
e := query.Entry{Key: kv.Key, Size: len(kv.Value)}
if !q.KeysOnly {
e.Value = kv.Value
}
entries = append(entries, e)
}
if err := errFn(); err != nil {
metricDatastoreOperationsTotal.WithLabelValues(s.store.Prefix(), "query", "error").Inc()
return nil, err
}
metricDatastoreOperationsTotal.WithLabelValues(s.store.Prefix(), "query", "success").Inc()
metricDatastoreItems.WithLabelValues(s.store.Prefix()).Set(float64(len(entries)))
return query.NaiveQueryApply(q, query.ResultsWithEntries(q, entries)), nil
}
func (s *datastore) Sync(ctx context.Context, prefix ds.Key) error {
return nil
}
func (s *datastore) Close() error {
return nil
}
func (s *datastore) Batch(context.Context) (ds.Batch, error) {
return ds.NewBasicBatch(s), nil
}