forked from client9/ipcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappengine.go
72 lines (59 loc) · 1.37 KB
/
appengine.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
package ipcat
import (
"net"
"strings"
)
func lookupSPFRecord(name string, f func(dir string) error) error {
txt, err := net.LookupTXT(name)
if err != nil {
return err
}
for _, rec := range txt {
spf := strings.TrimPrefix(rec, "v=spf1 ")
if spf == rec {
continue
}
for _, dir := range strings.Split(spf, " ") {
if err := f(dir); err != nil {
return err
}
}
}
return nil
}
// DownloadAppEngine downloads and returns raw bytes of the Google App Engine ip
// range list
func DownloadAppEngine() ([]string, error) {
var ranges []string
if err := lookupSPFRecord("_cloud-netblocks.googleusercontent.com", func(dir string) error {
inc := strings.TrimPrefix(dir, "include:")
if dir == inc {
return nil
}
return lookupSPFRecord(inc, func(dir string) error {
if ip4 := strings.TrimPrefix(dir, "ip4:"); dir != ip4 {
ranges = append(ranges, ip4)
}
return nil
})
}); err != nil {
return nil, err
}
return ranges, nil
}
// UpdateAppEngine takes a raw data, parses it and updates the ipmap
func UpdateAppEngine(ipmap *IntervalSet, ranges []string) error {
const (
dcName = "Google App Engine"
dcURL = "https://cloud.google.com/appengine"
)
// delete all existing records
ipmap.DeleteByName(dcName)
for _, ipRange := range ranges {
err := ipmap.AddCIDR(ipRange, dcName, dcURL)
if err != nil {
return err
}
}
return nil
}