mirror of https://github.com/tgragnato/magnetico
feat: add validation for filter-nodes-cidrs during parsing
This commit is contained in:
parent
acb19137b5
commit
50e77170a1
|
|
@ -42,7 +42,7 @@ func (ir IndexingResult) PeerAddrs() []net.TCPAddr {
|
|||
return ir.peerAddrs
|
||||
}
|
||||
|
||||
func NewIndexingService(laddr string, interval time.Duration, maxNeighbors uint, eventHandlers IndexingServiceEventHandlers, bootstrapNodes []string, filterNodes []string) *IndexingService {
|
||||
func NewIndexingService(laddr string, interval time.Duration, maxNeighbors uint, eventHandlers IndexingServiceEventHandlers, bootstrapNodes []string, filterNodes []net.IPNet) *IndexingService {
|
||||
service := new(IndexingService)
|
||||
service.interval = interval
|
||||
service.protocol = NewProtocol(
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ func TestBasicIndexingService(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
is := NewIndexingService(tt.laddr, tt.interval, tt.maxNeighbors, tt.eventHandlers, []string{"dht.tgragnato.it"}, []string{})
|
||||
is := NewIndexingService(tt.laddr, tt.interval, tt.maxNeighbors, tt.eventHandlers, []string{"dht.tgragnato.it"}, []net.IPNet{})
|
||||
if is == nil {
|
||||
t.Error("NewIndexingService() = nil, wanted != nil")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,20 +12,11 @@ type routingTable struct {
|
|||
filterNodes []net.IPNet
|
||||
}
|
||||
|
||||
func newRoutingTable(maxNeighbors uint, filterNodes []string) *routingTable {
|
||||
filter := []net.IPNet{}
|
||||
for _, filterNode := range filterNodes {
|
||||
_, ipNet, err := net.ParseCIDR(filterNode)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
filter = append(filter, *ipNet)
|
||||
}
|
||||
|
||||
func newRoutingTable(maxNeighbors uint, filterNodes []net.IPNet) *routingTable {
|
||||
return &routingTable{
|
||||
nodes: make([]net.UDPAddr, 0, maxNeighbors),
|
||||
maxNeighbors: maxNeighbors,
|
||||
filterNodes: filter,
|
||||
filterNodes: filterNodes,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ type Manager struct {
|
|||
indexingServices []Service
|
||||
}
|
||||
|
||||
func NewManager(addrs []string, interval time.Duration, maxNeighbors uint, bootstrappingNodes []string, filterNodes []string) *Manager {
|
||||
func NewManager(addrs []string, interval time.Duration, maxNeighbors uint, bootstrappingNodes []string, filterNodes []net.IPNet) *Manager {
|
||||
manager := new(Manager)
|
||||
manager.output = make(chan Result, 20)
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ func TestChannelOutput(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
address := ManagerAddress + ":" + strconv.Itoa(rand.Intn(64511)+1024)
|
||||
manager := NewManager([]string{address}, time.Second, MaxNeighbours, []string{"dht.tgragnato.it"}, []string{})
|
||||
manager := NewManager([]string{address}, time.Second, MaxNeighbours, []string{"dht.tgragnato.it"}, []net.IPNet{})
|
||||
peerPort := rand.Intn(64511) + 1024
|
||||
|
||||
result := &TestResult{
|
||||
|
|
@ -62,7 +62,7 @@ func TestOnIndexingResult(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
address := ManagerAddress + ":" + strconv.Itoa(rand.Intn(64511)+1024)
|
||||
manager := NewManager([]string{address}, DefaultTimeOut, MaxNeighbours, []string{"dht.tgragnato.it"}, []string{})
|
||||
manager := NewManager([]string{address}, DefaultTimeOut, MaxNeighbours, []string{"dht.tgragnato.it"}, []net.IPNet{})
|
||||
|
||||
result := mainline.IndexingResult{}
|
||||
outputChan := make(chan Result, ChanSize)
|
||||
|
|
|
|||
20
main.go
20
main.go
|
|
@ -10,6 +10,7 @@ import (
|
|||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
|
@ -35,7 +36,7 @@ var opFlags struct {
|
|||
|
||||
LeechMaxN int
|
||||
BootstrappingNodes []string
|
||||
FilterNodesCIDRs []string
|
||||
FilterNodesCIDRs []net.IPNet
|
||||
|
||||
Addr string
|
||||
|
||||
|
|
@ -186,7 +187,22 @@ func parseFlags() error {
|
|||
|
||||
mainline.DefaultThrottleRate = int(cmdF.MaxRPS)
|
||||
opFlags.BootstrappingNodes = cmdF.BootstrappingNodes
|
||||
opFlags.FilterNodesCIDRs = cmdF.FilterNodesCIDRs
|
||||
|
||||
opFlags.FilterNodesCIDRs = []net.IPNet{}
|
||||
for _, cidr := range cmdF.FilterNodesCIDRs {
|
||||
if cidr == "" {
|
||||
continue
|
||||
}
|
||||
if _, ipnet, err := net.ParseCIDR(cidr); err == nil {
|
||||
opFlags.FilterNodesCIDRs = append(opFlags.FilterNodesCIDRs, *ipnet)
|
||||
} else {
|
||||
log.Fatalf("Error while parsing CIDR %s: %s", cidr, err.Error())
|
||||
}
|
||||
}
|
||||
if len(opFlags.FilterNodesCIDRs) != 0 && reflect.DeepEqual(cmdF.BootstrappingNodes, []string{"dht.tgragnato.it"}) {
|
||||
log.Printf("%v\n", cmdF.FilterNodesCIDRs[0])
|
||||
log.Fatalln("You should specify your own internal bootstrapping nodes in filter mode.")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -12,22 +12,12 @@ type infoHashes struct {
|
|||
filterPeers []net.IPNet
|
||||
}
|
||||
|
||||
func newInfoHashes(maxNLeeches int, filterPeers []string) *infoHashes {
|
||||
filter := []net.IPNet{}
|
||||
for _, filterPeer := range filterPeers {
|
||||
_, ipNet, err := net.ParseCIDR(filterPeer)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
filter = append(filter, *ipNet)
|
||||
}
|
||||
|
||||
ih := &infoHashes{
|
||||
func newInfoHashes(maxNLeeches int, filterPeers []net.IPNet) *infoHashes {
|
||||
return &infoHashes{
|
||||
infoHashes: make(map[[20]byte][]net.TCPAddr),
|
||||
maxNLeeches: maxNLeeches,
|
||||
filterPeers: filter,
|
||||
filterPeers: filterPeers,
|
||||
}
|
||||
return ih
|
||||
}
|
||||
|
||||
func (ih *infoHashes) isAllowed(peer net.TCPAddr) bool {
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ type Sink struct {
|
|||
termination chan interface{}
|
||||
}
|
||||
|
||||
func NewSink(deadline time.Duration, maxNLeeches int, filterNodes []string) *Sink {
|
||||
func NewSink(deadline time.Duration, maxNLeeches int, filterNodes []net.IPNet) *Sink {
|
||||
ms := new(Sink)
|
||||
|
||||
ms.PeerID = randomID()
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import (
|
|||
func TestSink_NewSink(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sink := NewSink(time.Second, 10, []string{})
|
||||
sink := NewSink(time.Second, 10, []net.IPNet{})
|
||||
if sink == nil ||
|
||||
len(sink.PeerID) != 20 ||
|
||||
sink.deadline != time.Second ||
|
||||
|
|
@ -37,7 +37,7 @@ func (tr *TestResult) PeerAddrs() []net.TCPAddr {
|
|||
func TestSink_Sink(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sink := NewSink(time.Minute, 2, []string{})
|
||||
sink := NewSink(time.Minute, 2, []net.IPNet{})
|
||||
testResult := &TestResult{
|
||||
infoHash: [20]byte{255},
|
||||
peerAddrs: []net.TCPAddr{{IP: net.ParseIP("1.0.0.1"), Port: 443}},
|
||||
|
|
@ -51,7 +51,7 @@ func TestSink_Sink(t *testing.T) {
|
|||
func TestSink_Terminate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sink := NewSink(time.Minute, 1, []string{})
|
||||
sink := NewSink(time.Minute, 1, []net.IPNet{})
|
||||
sink.Terminate()
|
||||
|
||||
if !sink.terminated {
|
||||
|
|
@ -68,7 +68,7 @@ func TestSink_Drain(t *testing.T) {
|
|||
}
|
||||
}()
|
||||
|
||||
sink := NewSink(time.Minute, 1, []string{})
|
||||
sink := NewSink(time.Minute, 1, []net.IPNet{})
|
||||
sink.Terminate()
|
||||
sink.Drain()
|
||||
}
|
||||
|
|
@ -76,7 +76,7 @@ func TestSink_Drain(t *testing.T) {
|
|||
func TestFlush(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sink := NewSink(time.Minute, 1, []string{})
|
||||
sink := NewSink(time.Minute, 1, []net.IPNet{})
|
||||
testMetadata := Metadata{
|
||||
InfoHash: []byte{1, 2, 3, 4, 5, 6},
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue