forked from zmap/zgrab2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocklist.go
More file actions
49 lines (45 loc) · 1.37 KB
/
blocklist.go
File metadata and controls
49 lines (45 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package zgrab2
import (
"fmt"
"net"
"os"
"strings"
"github.com/censys/cidranger"
)
func stripComments(line, commentDelimiter string) string {
// Remove comments from the line
if idx := strings.Index(line, commentDelimiter); idx != -1 {
return line[:idx]
}
return line
}
// readBlocklist reads a blocklist file that contains CIDR ranges, IPs, or IP ranges
// It returns a path-compressed trie of CIDR ranges that can be used to check if an IP address is in the blocklist
func readBlocklist(fileName string) (cidranger.Ranger, error) {
data, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("failed to read blocklist file: %w", err)
}
strData := string(data)
// Remove comments and empty lines
cidrStrings := make([]string, 0)
for _, line := range strings.Split(strData, "\n") {
line = stripComments(line, "#")
line = strings.TrimSpace(line) // Trim whitespace
if len(line) > 0 {
cidrStrings = append(cidrStrings, line)
}
}
var cidrs []net.IPNet
cidrs, err = extractCIDRRanges(cidrStrings)
if err != nil {
return nil, fmt.Errorf("failed to convert blacklist into CIDRs: %w", err)
}
ranger := cidranger.NewPCTrieRanger()
for _, cidr := range cidrs {
if err = ranger.Insert(cidranger.NewBasicRangerEntry(cidr)); err != nil {
return nil, fmt.Errorf("failed to insert CIDR (%s) into ranger: %w", cidr.String(), err)
}
}
return ranger, nil
}