Skip to content

Commit 8d8034f

Browse files
authored
Merge pull request #15269 from karalabe/puppeth-dumb-ip-filtering
cmd/puppeth: use dumb textual IP filtering
2 parents fd0e7b1 + b45cc0c commit 8d8034f

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

cmd/puppeth/wizard.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,10 @@ func (w *wizard) readJSON() string {
302302
}
303303

304304
// readIPAddress reads a single line from stdin, trimming if from spaces and
305-
// converts it to a network IP address.
306-
func (w *wizard) readIPAddress() net.IP {
305+
// returning it if it's convertible to an IP address. The reason for keeping
306+
// the user input format instead of returning a Go net.IP is to match with
307+
// weird formats used by ethstats, which compares IPs textually, not by value.
308+
func (w *wizard) readIPAddress() string {
307309
for {
308310
// Read the IP address from the user
309311
fmt.Printf("> ")
@@ -312,14 +314,13 @@ func (w *wizard) readIPAddress() net.IP {
312314
log.Crit("Failed to read user input", "err", err)
313315
}
314316
if text = strings.TrimSpace(text); text == "" {
315-
return nil
317+
return ""
316318
}
317319
// Make sure it looks ok and return it if so
318-
ip := net.ParseIP(text)
319-
if ip == nil {
320+
if ip := net.ParseIP(text); ip == nil {
320321
log.Error("Invalid IP address, please retry")
321322
continue
322323
}
323-
return ip
324+
return text
324325
}
325326
}

cmd/puppeth/wizard_ethstats.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"fmt"
21+
"sort"
2122

2223
"github.com/ethereum/go-ethereum/log"
2324
)
@@ -64,17 +65,37 @@ func (w *wizard) deployEthstats() {
6465
fmt.Println()
6566
fmt.Printf("Keep existing IP %v blacklist (y/n)? (default = yes)\n", infos.banned)
6667
if w.readDefaultString("y") != "y" {
67-
infos.banned = nil
68-
68+
// The user might want to clear the entire list, although generally probably not
69+
fmt.Println()
70+
fmt.Printf("Clear out blacklist and start over (y/n)? (default = no)\n")
71+
if w.readDefaultString("n") != "n" {
72+
infos.banned = nil
73+
}
74+
// Offer the user to explicitly add/remove certain IP addresses
75+
fmt.Println()
76+
fmt.Println("Which additional IP addresses should be blacklisted?")
77+
for {
78+
if ip := w.readIPAddress(); ip != "" {
79+
infos.banned = append(infos.banned, ip)
80+
continue
81+
}
82+
break
83+
}
6984
fmt.Println()
70-
fmt.Println("Which IP addresses should be blacklisted?")
85+
fmt.Println("Which IP addresses should not be blacklisted?")
7186
for {
72-
if ip := w.readIPAddress(); ip != nil {
73-
infos.banned = append(infos.banned, ip.String())
87+
if ip := w.readIPAddress(); ip != "" {
88+
for i, addr := range infos.banned {
89+
if ip == addr {
90+
infos.banned = append(infos.banned[:i], infos.banned[i+1:]...)
91+
break
92+
}
93+
}
7494
continue
7595
}
7696
break
7797
}
98+
sort.Strings(infos.banned)
7899
}
79100
// Try to deploy the ethstats server on the host
80101
trusted := make([]string, 0, len(w.servers))

0 commit comments

Comments
 (0)