Default DNS seeding (broken for now, use --seed override).
This commit is contained in:
parent
60ea0ebb76
commit
af55e865f8
41
btc-crawl.go
41
btc-crawl.go
|
@ -1,21 +1,46 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/jessevdk/go-flags"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Taken from: https://github.com/bitcoin/bitcoin/blob/89d72f3d9b6e7ef051ad1439f266b809f348229b/src/chainparams.cpp#L143
|
||||||
|
var defaultDnsSeeds = []string{
|
||||||
|
"seed.bitcoin.sipa.be",
|
||||||
|
"dnsseed.bluematt.me",
|
||||||
|
"dnsseed.bitcoin.dashjr.org",
|
||||||
|
"seed.bitcoinstats.com",
|
||||||
|
"seed.bitnodes.io",
|
||||||
|
"bitseed.xf2.org",
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Unhardcode these:
|
||||||
|
var userAgent string = "/btc-crawl:0.0.1"
|
||||||
|
var lastBlock int32 = 0
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
Verbose []bool `short:"v" long:"verbose" description:"Show verbose logging."`
|
||||||
|
Seed []string `short:"s" long:"seed" description:"Override which seeds to use." default-mask:"<bitcoind DNS seeds>"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// TODO: Parse args.
|
// TODO: Parse args.
|
||||||
// TODO: Export to a reasonable format.
|
options := Options{}
|
||||||
// TODO: Use proper logger for logging.
|
_, err := flags.Parse(&options)
|
||||||
var seedNodes []string = []string{"85.214.251.25:8333", "62.75.216.13:8333"}
|
|
||||||
client := NewDefaultClient()
|
|
||||||
crawler := NewCrawler(client, seedNodes, 10)
|
|
||||||
|
|
||||||
done, err := crawler.Start()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
<-done
|
seedNodes := options.Seed
|
||||||
|
|
||||||
|
// TODO: Export to a reasonable format.
|
||||||
|
// TODO: Use proper logger for logging.
|
||||||
|
if len(seedNodes) == 0 {
|
||||||
|
seedNodes = GetSeedsFromDNS(defaultDnsSeeds)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := NewClient(userAgent, lastBlock)
|
||||||
|
crawler := NewCrawler(client, seedNodes, 10)
|
||||||
|
crawler.Start()
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,6 @@ import (
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Unhardcode these:
|
|
||||||
var userAgent string = "/btc-crawl:0.0.1"
|
|
||||||
var lastBlock int32 = 0
|
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
btcnet btcwire.BitcoinNet // Bitcoin Network
|
btcnet btcwire.BitcoinNet // Bitcoin Network
|
||||||
pver uint32 // Protocl Version
|
pver uint32 // Protocl Version
|
||||||
|
@ -15,7 +11,7 @@ type Client struct {
|
||||||
lastBlock int32
|
lastBlock int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultClient() *Client {
|
func NewClient(userAgent string, lastBlock int32) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
btcnet: btcwire.MainNet,
|
btcnet: btcwire.MainNet,
|
||||||
pver: btcwire.ProtocolVersion,
|
pver: btcwire.ProtocolVersion,
|
||||||
|
|
|
@ -115,8 +115,7 @@ func (c *Crawler) addAddress(address string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Crawler) Start() (chan struct{}, error) {
|
func (c *Crawler) Start() {
|
||||||
done := make(chan struct{}, 1)
|
|
||||||
numWorkers := 0
|
numWorkers := 0
|
||||||
numGood := 0
|
numGood := 0
|
||||||
|
|
||||||
|
@ -159,8 +158,7 @@ func (c *Crawler) Start() (chan struct{}, error) {
|
||||||
|
|
||||||
if len(c.queue) == 0 && numWorkers == 0 {
|
if len(c.queue) == 0 && numWorkers == 0 {
|
||||||
log.Printf("Done.")
|
log.Printf("Done.")
|
||||||
done <- struct{}{}
|
return
|
||||||
return done, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
<-c.workers
|
<-c.workers
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/conformal/btcwire"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetSeedsFromDNS(dnsSeeds []string) []string {
|
||||||
|
wait := sync.WaitGroup{}
|
||||||
|
results := make(chan []net.IP)
|
||||||
|
|
||||||
|
for _, address := range dnsSeeds {
|
||||||
|
wait.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wait.Done()
|
||||||
|
ips, err := net.LookupIP(address)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to resolve %s: %v", address, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
results <- ips
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wait.Wait()
|
||||||
|
close(results)
|
||||||
|
}()
|
||||||
|
|
||||||
|
seeds := []string{}
|
||||||
|
for ips := range results {
|
||||||
|
for _, ip := range ips {
|
||||||
|
seeds = append(seeds, net.JoinHostPort(ip.String(), btcwire.MainPort))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Resolved %d seed nodes from %d DNS seeds.", len(seeds), len(dnsSeeds))
|
||||||
|
|
||||||
|
// Note that this will likely include duplicate seeds. The crawler deduplicates them.
|
||||||
|
return seeds
|
||||||
|
}
|
Loading…
Reference in New Issue