-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (157 loc) · 3.69 KB
/
main.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bufio"
"encoding/json"
"fmt"
"net"
"os"
"strings"
"github.com/urfave/cli/v2"
)
const (
// default file to read when none is provided.
defaultFilename = "allowlist.txt"
)
func main() {
app := &cli.App{
Name: "drand-allowlist-parse",
Usage: "drand-allowlist-parse <allowlist.txt>",
Description: `
This tool parses a drand allow-list, stripping out comments and blank
lines, verifying that all CIDRs are in the correct format and outputting
the resulting list in the preferred form (CSV, JSON, Text), so that it can
be easily re-used.`,
Action: run,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "ips",
Usage: "list every individual IPs instead of CIDRs",
},
&cli.StringFlag{
Name: "type",
Usage: "IP type to produce the list for [ip4, ip6]",
Value: "ip4",
},
&cli.StringFlag{
Name: "format",
Value: "csv",
Usage: "format the list as: [csv, text, json]",
},
},
}
app.Run(os.Args)
}
func run(c *cli.Context) error {
filename := c.Args().First()
if filename == "" {
filename = defaultFilename
}
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
ipnets := []*net.IPNet{}
lineNumber := 0
scanner := bufio.NewScanner(f)
ipType := strings.ToLower(c.String("type"))
// Read every line.
for scanner.Scan() {
lineNumber++
line := scanner.Text()
// Get rid of spaces
line = strings.Replace(line, " ", "", -1)
// Skip comments and blank lines
if len(line) == 0 || line[0] == '#' || line[0] == '/' {
continue
}
// Parse the CIDR
ip, ipnet, err := net.ParseCIDR(line)
if err != nil {
msg := fmt.Sprintf("%s:%d: error parsing CIDR: %s",
filename, lineNumber, err)
return cli.Exit(msg, 1)
}
switch ipType {
case "ip4", "ipv4":
if ip.To4() == nil {
continue
}
case "ip6", "ipv6":
if ip.To4() != nil {
continue
}
}
// We only accept network addresses (i.e. 192.168.3.0/24 is
// valid, 192.168.3.5/24 is not).
if ip.String() != ipnet.IP.String() {
msg := fmt.Sprintf("%s:%d: %s is not a valid network: should probably be %s",
filename, lineNumber, line, ipnet)
return cli.Exit(msg, 1)
}
ipnets = append(ipnets, ipnet)
}
if err := scanner.Err(); err != nil {
return cli.Exit(err.Error(), 1)
}
results := []string{}
if c.Bool("ips") {
// List of
for _, ipnet := range ipnets {
results = append(results, ipsInNet(ipnet)...)
}
} else {
for _, ipnet := range ipnets {
results = append(results, ipnet.String())
}
}
switch c.String("format") {
case "csv":
formatCSV(results)
case "text":
formatText(results)
case "json":
formatJSON(results)
default:
return cli.Exit("format not supported", 1)
}
return nil
}
// mostly copied from stackoverflow, of course.
func ipsInNet(ipnet *net.IPNet) []string {
var ips []string
// Note the only purpose of Mask is to make a copy of the IP byte
// slice. In the original source it is use because it allows to
// provide a CIDR that uses an arbitrary ip in the network, so the
// mask provides the network IP.
for ip := ipnet.IP.Mask(ipnet.Mask); ipnet.Contains(ip); incIP(ip) {
ips = append(ips, ip.String())
}
// Remove network address and broadcast address
lenIPs := len(ips)
switch {
case lenIPs < 2:
return ips
default:
return ips[1 : len(ips)-1]
}
}
// Increases an IP address by one.
func incIP(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func formatCSV(results []string) {
fmt.Printf("%s", strings.Join(results, ","))
}
func formatText(results []string) {
fmt.Printf("%s", strings.Join(results, "\n"))
}
func formatJSON(results []string) {
j, _ := json.Marshal(results)
fmt.Printf("%s", string(j))
}