Signed-off-by: Jakob Borg <[email protected]>
This commit is contained in:
Jakob Borg committed 2026-11-19 11:48:07 +01:00
1 parent 61bfc7e75f
commit 792f8ce938
4 files changed
+52 -27

No files matched your search

+1
View File
@@ -383,6 +383,7 @@ jobs:
done
env:
CGO_ENABLED: "1"
TAGS: "nolibp2p sqlite_omit_load_extension sqlite_dbstat"
- name: Archive artifacts
uses: actions/upload-artifact@v4
+9 -11
View File
@@ -20,7 +20,6 @@ import (
"github.com/thejerf/suture/v4"
"github.com/syncthing/syncthing/internal/db"
"github.com/syncthing/syncthing/internal/dht"
"github.com/syncthing/syncthing/internal/slogutil"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/connections/registry"
@@ -252,7 +251,7 @@ func (m *manager) CommitConfiguration(_, to config.Configuration) (handled bool)
toIdentities[ipv6Identity(to.Options.LocalAnnMCAddr)] = struct{}{}
}
if to.Options.P2PDiscoveryEnabled {
if p2pDiscoveryAvailable && to.Options.P2PDiscoveryEnabled {
toIdentities[p2pIdentity()] = struct{}{}
}
@@ -308,16 +307,10 @@ func (m *manager) CommitConfiguration(_, to config.Configuration) (handled bool)
}
}
if to.Options.P2PDiscoveryEnabled {
if p2pDiscoveryAvailable && to.Options.P2PDiscoveryEnabled {
ident := p2pIdentity()
if _, ok := m.finders[ident]; !ok {
listenAddresses := p2pQUICListenAddresses(to.Options.ListenAddresses())
bootstrapPeers := to.Options.P2PDiscoveryBootstrapPeers()
dht, err := NewPeerToPeerDiscovery(m.cert, m.addressLister, m.evLogger, m.kv, dht.Options{
UDP: m.registry.UDP,
ListenAddresses: listenAddresses,
BootstrapPeers: bootstrapPeers,
})
dht, err := NewPeerToPeerDiscovery(m.cert, m.addressLister, m.evLogger, m.kv, m.registry.UDP, to.Options)
if err != nil {
slog.Warn("Failed to initialize peer-to-peer discovery", slogutil.Error(err))
} else {
@@ -332,10 +325,15 @@ func (m *manager) CommitConfiguration(_, to config.Configuration) (handled bool)
// P2PStats returns statistics about the currently running peer-to-peer
// discovery.
func (m *manager) P2PStats() P2PStats {
type p2pImpl interface {
Mode() string
Peers() int
}
m.mut.RLock()
defer m.mut.RUnlock()
p2p, ok := m.finders[p2pIdentity()].Finder.(*PeerToPeerDiscovery)
p2p, ok := m.finders[p2pIdentity()].Finder.(p2pImpl)
if !ok {
return P2PStats{}
}
+17 -16
View File
@@ -4,6 +4,8 @@
// 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/.
//go:build !nolibp2p
package discover
import (
@@ -25,13 +27,15 @@ import (
"github.com/syncthing/syncthing/internal/db"
"github.com/syncthing/syncthing/internal/dht"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/connections/registry"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/protocol"
)
const (
p2pIdentityKey = "dht-identity"
p2pIdentityLifetime = 7 * 24 * time.Hour
p2pDiscoveryAvailable = true
p2pIdentityKey = "dht-identity"
p2pIdentityLifetime = 7 * 24 * time.Hour
)
type PeerToPeerDiscovery struct {
@@ -48,28 +52,25 @@ type PeerToPeerDiscovery struct {
var _ FinderService = (*PeerToPeerDiscovery)(nil)
func NewPeerToPeerDiscovery(cert tls.Certificate, addresses AddressLister, evLogger events.Logger, kv db.KV, opts dht.Options) (*PeerToPeerDiscovery, error) {
func NewPeerToPeerDiscovery(cert tls.Certificate, addresses AddressLister, evLogger events.Logger, kv db.KV, udp *registry.UDPReuseManager, cfg config.OptionsConfiguration) (Finder, error) {
privateKey, ok := cert.PrivateKey.(crypto.Signer)
if !ok {
return nil, errors.New("TLS private key must implement crypto.Signer")
}
if opts.ValueDatastore == nil {
opts.ValueDatastore = dht.NewDatastore(db.NewTyped(kv, "dht-values"))
identity, err := loadDHTIdentity(db.NewMiscDB(kv), time.Now())
if err != nil {
return nil, fmt.Errorf("loading DHT identity: %w", err)
}
if opts.RoutingDatastore == nil {
opts.RoutingDatastore = dht.NewDatastore(db.NewTyped(kv, "dht-routing"))
opts := dht.Options{
UDP: udp,
ListenAddresses: p2pQUICListenAddresses(cfg.ListenAddresses()),
BootstrapPeers: cfg.P2PDiscoveryBootstrapPeers(),
ValueDatastore: dht.NewDatastore(db.NewTyped(kv, "dht-values")),
RoutingDatastore: dht.NewDatastore(db.NewTyped(kv, "dht-routing")),
Identity: identity,
}
if opts.Identity == nil {
identity, err := loadDHTIdentity(db.NewMiscDB(kv), time.Now())
if err != nil {
return nil, fmt.Errorf("loading DHT identity: %w", err)
}
opts.Identity = identity
}
dht, err := dht.NewSyncthingDHT(opts)
if err != nil {
return nil, err
+25
View File
@@ -0,0 +1,25 @@
// 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/.
//go:build nolibp2p
package discover
import (
"crypto/tls"
"errors"
"github.com/syncthing/syncthing/internal/db"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/connections/registry"
"github.com/syncthing/syncthing/lib/events"
)
const p2pDiscoveryAvailable = false
func NewPeerToPeerDiscovery(tls.Certificate, AddressLister, events.Logger, db.KV, *registry.UDPReuseManager, config.OptionsConfiguration) (Finder, error) {
return nil, errors.New("peer-to-peer discovery is disabled in this build")
}