WIP: fixes to avoid build errors

This commit is contained in:
Gustavo Valverde 2024-02-27 11:35:11 +00:00
parent c9a566fcb4
commit 5a374b3c89
6 changed files with 54 additions and 50 deletions

View File

@ -8,7 +8,7 @@ import (
"strconv"
"time"
"github.com/caddyserver/caddy"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"

View File

@ -4,7 +4,7 @@ import (
"testing"
"time"
"github.com/caddyserver/caddy"
"github.com/coredns/caddy"
"github.com/zcashfoundation/dnsseeder/zcash/network"
)

View File

@ -11,13 +11,13 @@ import (
)
type Address struct {
netaddr *wire.NetAddress
netaddr *wire.NetAddressV2
lastUpdate time.Time
}
func (a *Address) String() string {
portString := strconv.Itoa(int(a.netaddr.Port))
return net.JoinHostPort(a.netaddr.IP.String(), portString)
return net.JoinHostPort(a.netaddr.Addr.String(), portString)
}
func (a *Address) asPeerKey() PeerKey {
@ -35,7 +35,7 @@ func (a *Address) fromPeerKey(s PeerKey) (*Address, error) {
return nil, err
}
na := wire.NewNetAddressTimestamp(
na := wire.NetAddressV2FromBytes(
time.Now(),
0,
net.ParseIP(host),
@ -47,13 +47,13 @@ func (a *Address) fromPeerKey(s PeerKey) (*Address, error) {
return a, nil
}
func (a *Address) asNetAddress() *wire.NetAddress {
func (a *Address) asNetAddress() *wire.NetAddressV2 {
newNA := *a.netaddr
newNA.Timestamp = a.lastUpdate
return &newNA
}
func (a *Address) fromNetAddress(na *wire.NetAddress) (*Address, error) {
func (a *Address) fromNetAddress(na *wire.NetAddressV2) (*Address, error) {
a.netaddr = na
a.lastUpdate = na.Timestamp
return a, nil
@ -222,6 +222,12 @@ func (bk *AddressBook) waitForAddresses(n int, done chan struct{}) {
return
}
// Validate is an address is an IPv4 address.
func (bk *AddressBook) Validate(s string) bool {
ip := net.ParseIP(s)
return ip.To4() != nil
}
// GetAddressList returns a slice of n valid addresses in random order.
// If there aren't enough known addresses, it returns as many as we have.
func (bk *AddressBook) shuffleAddressList(n int, v6 bool, defaultPort string) []net.IP {
@ -236,12 +242,12 @@ func (bk *AddressBook) shuffleAddressList(n int, v6 bool, defaultPort string) []
continue
}
if v6 && v.netaddr.IP.To4() != nil {
if v6 && bk.Validate(v.netaddr.Addr.String()) {
// skip IPv4 addresses if we're asked for v6
continue
}
if !v6 && v.netaddr.IP.To4() == nil {
if !v6 && !bk.Validate(v.netaddr.Addr.String()) {
// skip IPv6 addresses if we're asked for v4
continue
}
@ -253,7 +259,7 @@ func (bk *AddressBook) shuffleAddressList(n int, v6 bool, defaultPort string) []
continue
}
resp = append(resp, v.netaddr.IP)
resp = append(resp, net.IP(v.netaddr.Addr.String()))
}
mrand.Seed(time.Now().UnixNano())

View File

@ -446,7 +446,7 @@ func (s *Seeder) RefreshAddresses(disconnect bool) {
next := <-refreshQueue
na := next.netaddr
ipString := na.IP.String()
ipString := na.Addr.String()
portString := strconv.Itoa(int(na.Port))
// Don't care about the peer individually, just that we can connect.
@ -454,7 +454,7 @@ func (s *Seeder) RefreshAddresses(disconnect bool) {
if err != nil {
if err != ErrRepeatConnection {
s.logger.Printf("Peer %s:%d unusable on refresh. Error: %s", na.IP, na.Port, err)
s.logger.Printf("Peer %s:%d unusable on refresh. Error: %s", na.Addr, na.Port, err)
// Blacklist the peer. We might try to connect again later.
// This would deadlock if enqueueAddrs still holds the RLock,
// hence the awkward channel allocation above.
@ -467,7 +467,7 @@ func (s *Seeder) RefreshAddresses(disconnect bool) {
s.DisconnectPeer(next.asPeerKey())
}
s.logger.Printf("Validated %s", na.IP)
s.logger.Printf("Validated %s", na.Addr)
}
wg.Done()
}()
@ -503,7 +503,7 @@ func (s *Seeder) RetryBlacklist() {
next := <-blacklistQueue
na := next.netaddr
ip := na.IP.String()
ip := na.Addr.String()
port := strconv.Itoa(int(na.Port))
// Call internal connect directly to avoid being blocked

View File

@ -54,8 +54,8 @@ func (s *Seeder) onAddr(p *peer.Peer, msg *wire.MsgAddr) {
// By checking if we know them before adding to the queue, we create
// the end condition for the crawler thread: it will time out after
// not processing any new addresses.
if s.addrBook.IsKnown(peerKeyFromNA(na)) {
s.logger.Printf("Already knew about %s:%d", na.IP, na.Port)
if s.addrBook.IsKnown(peerKeyFromNAV2(na)) {
s.logger.Printf("Already knew about %s:%d", na.Addr.String(), na.Port)
continue
}
_, denied := DeniedPorts[na.Port]
@ -71,26 +71,24 @@ func (s *Seeder) onAddr(p *peer.Peer, msg *wire.MsgAddr) {
// If other address type is received, the address is ignored.
func (s *Seeder) onAddrV2(p *peer.Peer, msg *wire.MsgAddrV2) {
if len(msg.AddrList) == 0 {
s.logger.Printf("Got empty addrv2 message from peer %s. Disconnecting.", p.Addr())
s.logger.Printf("Got empty addr message from peer %s. Disconnecting.", p.Addr())
s.DisconnectPeer(peerKeyFromPeer(p))
return
}
s.logger.Printf("Got %d addrv2s from peer %s", len(msg.AddrList), p.Addr())
s.logger.Printf("Got %d addrs from peer %s", len(msg.AddrList), p.Addr())
for _, na := range msg.AddrList {
if na.NetworkID == wire.NIIPV4 || na.NetworkID == wire.NIIPV6 {
// By checking if we know them before adding to the queue, we create
// the end condition for the crawler thread: it will time out after
// not processing any new addresses.
if s.addrBook.IsKnown(peerKeyFromNAV2(na)) {
s.logger.Printf("Already knew about %s:%d", na.IP, na.Port)
s.logger.Printf("Already knew about %s:%d", na.Addr.String(), na.Port)
continue
}
_, denied := DeniedPorts[na.Port]
if !denied {
s.addrQueue <- &na.NetAddress
}
s.addrQueue <- na
}
}
}

View File

@ -70,9 +70,9 @@ func startMockLoop() error {
if atomic.LoadUint32(&useAddrV2) != 0 {
cache := make([]*wire.NetAddressV2, 0, 1)
addrv2_1 := wire.NewNetAddressV2NetAddress(addr)
addrv2_2 := wire.NewNetAddressV2NetAddress(addr2)
addrv2_3 := wire.NewNetAddressV2NetAddress(addr3)
addrv2_1 := wire.NetAddressV2FromBytes(addr.Timestamp, addr.Services, addr.IP.To16(), addr.Port)
addrv2_2 := wire.NetAddressV2FromBytes(addr2.Timestamp, addr2.Services, addr2.IP.To16(), addr2.Port)
addrv2_3 := wire.NetAddressV2FromBytes(addr3.Timestamp, addr3.Services, addr3.IP.To16(), addr3.Port)
cache = append(cache, addrv2_1, addrv2_2, addrv2_3)
_, err := p.PushAddrV2Msg(cache)
if err != nil {