-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgcloud.go
69 lines (58 loc) · 1.75 KB
/
gcloud.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
package fetchers
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// GCloudFetcher implements the IPRangeFetcher interface for GCP IP ranges.
type GCloudFetcher struct{}
func (f GCloudFetcher) Name() string {
return "GCloud"
}
func (f GCloudFetcher) Description() string {
return "Fetches IP ranges for Google Cloud Platform (GCP) services."
}
func (f GCloudFetcher) FetchIPRanges() ([]string, error) {
// Fetch all GCP IP ranges
return fetchGCloudIPRanges()
}
// GCloudIPRanges represents the structure of the GCP IP ranges JSON file.
type GCloudIPRanges struct {
SyncToken string `json:"syncToken"`
CreationTime string `json:"creationTime"`
Prefixes []struct {
IPv4Prefix string `json:"ipv4Prefix"`
IPv6Prefix string `json:"ipv6Prefix"`
Service string `json:"service"`
Scope string `json:"scope"`
} `json:"prefixes"`
}
// fetchGCloudIPRanges fetches and parses the GCP IP ranges JSON file.
func fetchGCloudIPRanges() ([]string, error) {
url := "https://www.gstatic.com/ipranges/cloud.json"
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch GCP IP ranges from %s: %v", url, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body from %s: %v", url, err)
}
var ipRanges GCloudIPRanges
if err := json.Unmarshal(body, &ipRanges); err != nil {
return nil, fmt.Errorf("failed to unmarshal GCP IP ranges JSON: %v", err)
}
// Extract all IP ranges (both IPv4 and IPv6)
var ranges []string
for _, prefix := range ipRanges.Prefixes {
if prefix.IPv4Prefix != "" {
ranges = append(ranges, prefix.IPv4Prefix)
}
if prefix.IPv6Prefix != "" {
ranges = append(ranges, prefix.IPv6Prefix)
}
}
return ranges, nil
}