forked from client9/ipcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
89 lines (76 loc) · 2.03 KB
/
aws.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
package ipcat
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
var (
awsDownload = "https://ip-ranges.amazonaws.com/ip-ranges.json"
)
// AWSPrefix is AWS prefix in their IP ranges file
type AWSPrefix struct {
IPPrefix string `json:"ip_prefix"`
IPv6Prefix string `json:"ipv6_prefix"`
Region string `json:"region"`
Service string `json:"service"`
}
// AWS is main record for AWS IP info
type AWS struct {
SyncToken string `json:"syncToken"`
CreateDate string `json:"createDate"`
Prefixes []AWSPrefix `json:"prefixes"`
IPv6Prefixes []AWSPrefix `json:"ipv6_prefixes"`
}
// DownloadAWS downloads the latest AWS IP ranges list
func DownloadAWS() ([]byte, error) {
resp, err := http.Get(awsDownload)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Failed to download AWS ranges: status code %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body.Close()
return body, nil
}
// UpdateAWS parses the AWS IP json file and updates the interval set
func UpdateAWS(ipmap *IntervalSet, body []byte) error {
const (
awsName = "Amazon AWS"
awsURL = "http://www.amazon.com/aws/"
)
aws := AWS{}
err := json.Unmarshal(body, &aws)
if err != nil {
return err
}
// delete all existing records
ipmap.DeleteByName(awsName)
// and add back
for _, prefixList := range []*[]AWSPrefix{&aws.Prefixes, &aws.IPv6Prefixes} {
for _, rec := range *prefixList {
if rec.Service != "AMAZON" {
// Service is the subset of IP address ranges. Specify AMAZON to get
// all IP address ranges (for example, the ranges in the EC2 subset
// are also in the AMAZON subset). Note that some IP address ranges
// are only in the AMAZON subset.
// <https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html>
continue
}
prefix := rec.IPPrefix
if prefix == "" {
prefix = rec.IPv6Prefix
}
err := ipmap.AddCIDR(prefix, awsName, awsURL)
if err != nil {
return err
}
}
}
return nil
}