-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
219 lines (182 loc) · 4.83 KB
/
main.go
File metadata and controls
219 lines (182 loc) · 4.83 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"flag"
"fmt"
"net"
"os"
"strings"
"github.com/miekg/dns"
)
type SPFRecord struct {
IP4 []string
IP6 []string
Includes []string
}
func main() {
var (
ip4List stringSlice
ip6List stringSlice
includeList stringSlice
tags bool
)
flag.Var(&ip4List, "ip4", "IPv4 addresses to include (can be specified multiple times)")
flag.Var(&ip6List, "ip6", "IPv6 addresses to include (can be specified multiple times)")
flag.Var(&includeList, "include", "Domain names to include SPF records from (can be specified multiple times)")
flag.BoolVar(&tags, "tags", false, "Add ip4 or ip6 tag to each IP address")
flag.Parse()
if len(includeList) == 0 && len(ip4List) == 0 && len(ip6List) == 0 {
fmt.Fprintln(os.Stderr, "Error: At least one -ip4, -ip6, or -include argument is required")
flag.Usage()
os.Exit(1)
}
ips, err := flattenSPF(ip4List, ip6List, includeList)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
for _, ip := range ips {
if tags {
tag := "ip6"
if net.ParseIP(strings.Split(ip, "/")[0]).To4() != nil {
tag = "ip4"
}
fmt.Printf("%s:%s\n", tag, ip)
} else {
fmt.Println(ip)
}
}
}
func flattenSPF(ip4List, ip6List, includeList []string) ([]string, error) {
var allIPs []string
allIPs = append(allIPs, ip4List...)
allIPs = append(allIPs, ip6List...)
visited := make(map[string]bool)
for _, domain := range includeList {
ips, err := resolveDomain(domain, visited)
if err != nil {
return nil, fmt.Errorf("failed to resolve include domain %s: %w", domain, err)
}
allIPs = append(allIPs, ips...)
}
uniqueIPs := deduplicateIPs(allIPs)
return uniqueIPs, nil
}
func resolveDomain(domain string, visited map[string]bool) ([]string, error) {
domain = strings.ToLower(domain)
if visited[domain] {
return nil, nil
}
visited[domain] = true
spfRecord, err := getSPFRecord(domain)
if err != nil {
return nil, err
}
var ips []string
ips = append(ips, spfRecord.IP4...)
ips = append(ips, spfRecord.IP6...)
for _, includeDomain := range spfRecord.Includes {
includeIPs, err := resolveDomain(includeDomain, visited)
if err != nil {
return nil, fmt.Errorf("failed to resolve include %s: %w", includeDomain, err)
}
ips = append(ips, includeIPs...)
}
return ips, nil
}
func getSPFRecord(domain string) (*SPFRecord, error) {
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(domain), dns.TypeTXT)
m.RecursionDesired = true
m.SetEdns0(4096, false)
r, _, err := c.Exchange(m, getDNSResolver())
if err != nil {
return nil, fmt.Errorf("DNS query failed: %w", err)
}
if r.Rcode != dns.RcodeSuccess {
return nil, fmt.Errorf("DNS query returned error code: %s", dns.RcodeToString[r.Rcode])
}
var spfTxt string
for _, ans := range r.Answer {
if txt, ok := ans.(*dns.TXT); ok {
// Concatenate all strings in the TXT record to build the complete record
fullTxt := strings.Join(txt.Txt, "")
if strings.HasPrefix(strings.ToLower(fullTxt), "v=spf1") {
spfTxt = strings.ToLower(fullTxt)
break
}
}
}
if spfTxt == "" {
return nil, fmt.Errorf("no SPF record found for domain %s", domain)
}
return parseSPFRecord(spfTxt)
}
func parseSPFRecord(spf string) (*SPFRecord, error) {
record := &SPFRecord{
IP4: []string{},
IP6: []string{},
Includes: []string{},
}
parts := strings.Fields(spf)
if len(parts) == 0 || !strings.HasPrefix(parts[0], "v=spf1") {
return nil, fmt.Errorf("invalid SPF record: %s", spf)
}
for _, part := range parts[1:] {
if strings.HasPrefix(part, "ip4:") {
ip := strings.TrimPrefix(part, "ip4:")
if isValidIP(ip, 4) {
record.IP4 = append(record.IP4, ip)
}
} else if strings.HasPrefix(part, "ip6:") {
ip := strings.TrimPrefix(part, "ip6:")
if isValidIP(ip, 6) {
record.IP6 = append(record.IP6, ip)
}
} else if strings.HasPrefix(part, "include:") {
domain := strings.TrimPrefix(part, "include:")
if domain != "" {
record.Includes = append(record.Includes, domain)
}
}
}
return record, nil
}
func isValidIP(ip string, version int) bool {
if strings.Contains(ip, "/") {
ip = strings.Split(ip, "/")[0]
}
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return false
}
if version == 4 {
return parsedIP.To4() != nil
}
return parsedIP.To4() == nil && strings.Contains(ip, ":")
}
func deduplicateIPs(ips []string) []string {
seen := make(map[string]bool)
var result []string
for _, ip := range ips {
if !seen[ip] {
seen[ip] = true
result = append(result, ip)
}
}
return result
}
func getDNSResolver() string {
if resolver := os.Getenv("DNS_RESOLVER"); resolver != "" {
return resolver
}
return "127.0.0.1:53"
}
type stringSlice []string
func (s *stringSlice) String() string {
return strings.Join(*s, ",")
}
func (s *stringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}