-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws.go
141 lines (122 loc) · 3.57 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
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
package ccidr
import (
"fmt"
_ "github.com/runoncloud/ccidr/pkg/statik"
"github.com/thoas/go-funk"
"github.com/tidwall/gjson"
"io/ioutil"
"log"
"net/http"
"sort"
"strings"
"time"
)
const (
Amazon = "AMAZON"
AmazonJsonUrl = "https://ip-ranges.amazonaws.com/ip-ranges.json"
)
type AWS struct {
isRemote bool
}
var (
awsJSON string
)
func (a AWS) ListRegions() []string {
var regions []string
json := getAWSJsonString(a.isRemote)
queryResult := gjson.Get(json, "prefixes.#.region")
for _, region := range queryResult.Array() {
regions = append(regions, region.String())
}
sort.Strings(regions)
return funk.UniqString(regions)
}
func (a AWS) ListServices() []string {
var services []string
json := getAWSJsonString(a.isRemote)
queryResult := gjson.Get(json, "prefixes.#.service")
for _, service := range queryResult.Array() {
services = append(services, service.String())
}
sort.Strings(services)
return funk.UniqString(services)
}
func (a AWS) ListServicesByRegion(region string) []string {
var services []string
json := getAWSJsonString(a.isRemote)
queryResult := gjson.Get(json,
fmt.Sprintf("prefixes.#(region==%s)#.service", region))
for _, service := range queryResult.Array() {
services = append(services, service.String())
}
sort.Strings(services)
return funk.UniqString(services)
}
func (a AWS) ListAddressPrefixes() []string {
return a.ListAddressPrefixesByService(Amazon)
}
func (a AWS) ListAddressPrefixesByRegion(region string) []string {
var addressPrefixes []string
json := getAWSJsonString(a.isRemote)
cidrQueryResult := gjson.Get(json, fmt.Sprintf("prefixes.#(region==%s)#.ip_prefix", region))
for _, addressPrefix := range cidrQueryResult.Array() {
addressPrefixes = append(addressPrefixes, addressPrefix.String())
}
sort.Strings(addressPrefixes)
return funk.UniqString(addressPrefixes)
}
func (a AWS) ListAddressPrefixesByService(service string) []string {
var addressPrefixes []string
json := getAWSJsonString(a.isRemote)
cidrQueryResult := gjson.Get(json, fmt.Sprintf("prefixes.#(service==%s)#.ip_prefix", service))
for _, addressPrefix := range cidrQueryResult.Array() {
addressPrefixes = append(addressPrefixes, addressPrefix.String())
}
sort.Strings(addressPrefixes)
return funk.UniqString(addressPrefixes)
}
func (a AWS) ListAddressPrefixesByServiceAndRegion(service string, region string) []string {
var addressPrefixes []string
json := getAWSJsonString(a.isRemote)
cidrServiceQueryResult := gjson.Get(json, fmt.Sprintf("prefixes.#(service==%s)#.ip_prefix", service))
cidrRegionQueryResult := gjson.Get(json, fmt.Sprintf("prefixes.#(region==%s)#.ip_prefix", region))
for _, addressPrefix := range cidrServiceQueryResult.Array() {
if strings.Contains(cidrRegionQueryResult.String(), addressPrefix.String()) {
addressPrefixes = append(addressPrefixes, addressPrefix.String())
}
}
sort.Strings(addressPrefixes)
return funk.UniqString(addressPrefixes)
}
func getAWSJsonString(isRemote bool) string {
if awsJSON == "" {
if isRemote {
awsJSON = getAWSRemoteJsonString()
} else {
awsJSON = GetJsonString("/aws.json")
}
}
return awsJSON
}
func getAWSRemoteJsonString() string {
if awsJSON == "" {
client := http.Client{Timeout: time.Second * 10}
req, err := http.NewRequest(http.MethodGet, AmazonJsonUrl, nil)
if err != nil {
log.Fatal(err)
}
res, getErr := client.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
if res.Body != nil {
defer res.Body.Close()
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
awsJSON = string(body)
}
return awsJSON
}