mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-806] After panic, gracefully stop logs, and then exit immediately * [NOD-806] Convert non-kaspad applications to use the new spawn * [NOD-806] Fix disabled log at rpcclient * [NOD-806] Refactor HandlePanic * [NOD-806] Cancel Logger interface * [NOD-806] Remove redundant spawn checks from waitgroup_test.go * [NOD-806] Use caller subsystem when logging panics * [NOD-806] Fix go vet errors
100 lines
2.8 KiB
Go
100 lines
2.8 KiB
Go
// 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 (
|
|
"fmt"
|
|
mrand "math/rand"
|
|
"net"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/kaspanet/kaspad/config"
|
|
"github.com/kaspanet/kaspad/util/subnetworkid"
|
|
|
|
"github.com/kaspanet/kaspad/dagconfig"
|
|
"github.com/kaspanet/kaspad/wire"
|
|
)
|
|
|
|
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
|
|
|
|
// SubnetworkIDPrefixChar is the prefix of subnetworkID, when building a DNS seed request
|
|
SubnetworkIDPrefixChar byte = 'n'
|
|
|
|
// ServiceFlagPrefixChar is the prefix of service flag, when building a DNS seed request
|
|
ServiceFlagPrefixChar byte = 'x'
|
|
)
|
|
|
|
// 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.
|
|
func SeedFromDNS(dagParams *dagconfig.Params, reqServices wire.ServiceFlag, includeAllSubnetworks bool,
|
|
subnetworkID *subnetworkid.SubnetworkID, lookupFn LookupFunc, seedFn OnSeed) {
|
|
|
|
var dnsSeeds []string
|
|
mainConfig := config.ActiveConfig()
|
|
if mainConfig != nil && mainConfig.DNSSeed != "" {
|
|
dnsSeeds = []string{mainConfig.DNSSeed}
|
|
} else {
|
|
dnsSeeds = dagParams.DNSSeeds
|
|
}
|
|
|
|
for _, dnsseed := range dnsSeeds {
|
|
var host string
|
|
if reqServices == wire.SFNodeNetwork {
|
|
host = dnsseed
|
|
} else {
|
|
host = fmt.Sprintf("%c%x.%s", ServiceFlagPrefixChar, uint64(reqServices), dnsseed)
|
|
}
|
|
|
|
if !includeAllSubnetworks {
|
|
if subnetworkID != nil {
|
|
host = fmt.Sprintf("%c%s.%s", SubnetworkIDPrefixChar, subnetworkID, host)
|
|
} else {
|
|
host = fmt.Sprintf("%c.%s", SubnetworkIDPrefixChar, host)
|
|
}
|
|
}
|
|
|
|
spawn(func() {
|
|
randSource := mrand.New(mrand.NewSource(time.Now().UnixNano()))
|
|
|
|
seedpeers, err := lookupFn(host)
|
|
if err != nil {
|
|
log.Infof("DNS discovery failed on seed %s: %s", host, err)
|
|
return
|
|
}
|
|
numPeers := len(seedpeers)
|
|
|
|
log.Infof("%d addresses found from DNS seed %s", numPeers, host)
|
|
|
|
if numPeers == 0 {
|
|
return
|
|
}
|
|
addresses := make([]*wire.NetAddress, len(seedpeers))
|
|
// if this errors then we have *real* problems
|
|
intPort, _ := strconv.Atoi(dagParams.DefaultPort)
|
|
for i, peer := range seedpeers {
|
|
addresses[i] = wire.NewNetAddressTimestamp(
|
|
// seed 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))
|
|
}
|
|
|
|
seedFn(addresses)
|
|
})
|
|
}
|
|
}
|