This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
aws.go
73 lines (62 loc) · 1.43 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
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"`
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"`
}
// 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 _, rec := range aws.Prefixes {
if rec.Service == "EC2" {
err := ipmap.AddCIDR(rec.IPPrefix, awsName, awsURL)
if err != nil {
return err
}
}
}
return nil
}