2016-05-12 11:06:07 +02:00
|
|
|
// Copyright (c) 2016 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package connmgr
|
|
|
|
|
|
|
|
import (
|
2016-10-22 02:36:38 +02:00
|
|
|
"fmt"
|
2016-05-12 11:06:07 +02:00
|
|
|
mrand "math/rand"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-10-15 07:45:32 +02:00
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
|
|
|
"github.com/lbryio/lbcd/wire"
|
2016-05-12 11:06:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// These constants are used by the DNS seed code to pick a random last
|
|
|
|
// seen time.
|
|
|
|
secondsIn3Days int32 = 24 * 60 * 60 * 3
|
|
|
|
secondsIn4Days int32 = 24 * 60 * 60 * 4
|
|
|
|
)
|
|
|
|
|
|
|
|
// OnSeed is the signature of the callback function which is invoked when DNS
|
|
|
|
// seeding is succesfull.
|
|
|
|
type OnSeed func(addrs []*wire.NetAddress)
|
|
|
|
|
|
|
|
// LookupFunc is the signature of the DNS lookup function.
|
|
|
|
type LookupFunc func(string) ([]net.IP, error)
|
|
|
|
|
|
|
|
// SeedFromDNS uses DNS seeding to populate the address manager with peers.
|
2016-10-22 02:36:38 +02:00
|
|
|
func SeedFromDNS(chainParams *chaincfg.Params, reqServices wire.ServiceFlag,
|
|
|
|
lookupFn LookupFunc, seedFn OnSeed) {
|
|
|
|
|
|
|
|
for _, dnsseed := range chainParams.DNSSeeds {
|
|
|
|
var host string
|
|
|
|
if !dnsseed.HasFiltering || reqServices == wire.SFNodeNetwork {
|
|
|
|
host = dnsseed.Host
|
|
|
|
} else {
|
|
|
|
host = fmt.Sprintf("x%x.%s", uint64(reqServices), dnsseed.Host)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(host string) {
|
2016-05-12 11:06:07 +02:00
|
|
|
randSource := mrand.New(mrand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2016-10-22 02:36:38 +02:00
|
|
|
seedpeers, err := lookupFn(host)
|
2016-05-12 11:06:07 +02:00
|
|
|
if err != nil {
|
2016-10-22 02:36:38 +02:00
|
|
|
log.Infof("DNS discovery failed on seed %s: %v", host, err)
|
2016-05-12 11:06:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
numPeers := len(seedpeers)
|
|
|
|
|
2016-10-22 02:36:38 +02:00
|
|
|
log.Infof("%d addresses found from DNS seed %s", numPeers, host)
|
2016-05-12 11:06:07 +02:00
|
|
|
|
|
|
|
if numPeers == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addresses := make([]*wire.NetAddress, len(seedpeers))
|
|
|
|
// if this errors then we have *real* problems
|
|
|
|
intPort, _ := strconv.Atoi(chainParams.DefaultPort)
|
|
|
|
for i, peer := range seedpeers {
|
2016-12-06 20:48:02 +01:00
|
|
|
addresses[i] = wire.NewNetAddressTimestamp(
|
|
|
|
// bitcoind seeds with addresses from
|
|
|
|
// a time randomly selected between 3
|
|
|
|
// and 7 days ago.
|
|
|
|
time.Now().Add(-1*time.Second*time.Duration(secondsIn3Days+
|
|
|
|
randSource.Int31n(secondsIn4Days))),
|
|
|
|
0, peer, uint16(intPort))
|
2016-05-12 11:06:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
seedFn(addresses)
|
2016-10-22 02:36:38 +02:00
|
|
|
}(host)
|
2016-05-12 11:06:07 +02:00
|
|
|
}
|
|
|
|
}
|