Skip to content

Commit

Permalink
Ignore nodes that don't have a global unicast ip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbakker committed Jan 21, 2024
1 parent 383f7c3 commit 7246b80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 15 additions & 3 deletions internal/crawler/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ func (c *Crawler) Run(ctx context.Context, bsNodes []*dht.Node) error {
slog.String("addr", bsNode.Addr().String()),
)

if !isGlobalUnicast(bsNode.IP) {
logger.Warn("Node ip is not a global unicast address")
continue
}

if _, err := c.repo.TrackDHTNode(ctx, bsNode); err != nil {
logger.Error("Unable to track bootstrap node", slog.Any("err", err))
continue
Expand Down Expand Up @@ -462,6 +467,16 @@ func (c *Crawler) handleSendNodesPacket(ctx context.Context, node *dht.Node, pac
if bytes.Equal(packetNode.PublicKey[:], c.ident.PublicKey[:]) {
continue
}

logger := c.logger.With(slog.String("public_key", packetNode.PublicKey.String()),
slog.String("net", packetNode.Type.Net()),
slog.String("addr", packetNode.Addr().String()))

if !isGlobalUnicast(packetNode.IP) {
logger.Warn("Node ip is not a global unicast address")
continue
}

found, err := c.repo.HasNodeByPublicKey(ctx, packetNode.PublicKey)
if err != nil {
return fmt.Errorf("check whether node is known: %w", err)
Expand All @@ -470,9 +485,6 @@ func (c *Crawler) handleSendNodesPacket(ctx context.Context, node *dht.Node, pac
continue
}

logger := c.logger.With(slog.String("public_key", packetNode.PublicKey.String()),
slog.String("net", packetNode.Type.Net()),
slog.String("addr", packetNode.Addr().String()))
logger.Info("Tracking new node")

if _, err := c.repo.TrackDHTNode(ctx, packetNode); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/crawler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crawler

import (
"encoding/binary"
"net"

"github.com/alexbakker/tox4go/dht"
)
Expand All @@ -22,3 +23,12 @@ func getPublicKeyGenerator(n int) func() *dht.PublicKey {
return &res
}
}

func isGlobalUnicast(ip net.IP) bool {
return !ip.IsUnspecified() &&
!ip.IsLoopback() &&
!ip.IsPrivate() &&
!ip.IsMulticast() &&
!ip.IsLinkLocalUnicast() &&
!ip.IsLinkLocalMulticast()
}

0 comments on commit 7246b80

Please sign in to comment.