-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcloudflare.go
More file actions
51 lines (40 loc) · 1.31 KB
/
cloudflare.go
File metadata and controls
51 lines (40 loc) · 1.31 KB
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
package fetchers
import (
"encoding/json"
"fmt"
"net/http"
)
// CloudflareFetcher implements the IPRangeFetcher interface for Cloudflare.
type CloudflareFetcher struct{}
func (f CloudflareFetcher) Name() string {
return "cloudflare"
}
func (f CloudflareFetcher) Description() string {
return "Fetches IP ranges used by Cloudflare services."
}
func (f CloudflareFetcher) FetchIPRanges() ([]string, error) {
const url = "https://api.cloudflare.com/client/v4/ips"
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch Cloudflare IP ranges: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("received non-200 status code from Cloudflare: %d", resp.StatusCode)
}
var result struct {
Result struct {
IPv4CIDRs []string `json:"ipv4_cidrs"`
IPv6CIDRs []string `json:"ipv6_cidrs"`
} `json:"result"`
Success bool `json:"success"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to unmarshal Cloudflare JSON: %v", err)
}
// Combine IPv4 and IPv6 ranges
ipRanges := make([]string, 0, len(result.Result.IPv4CIDRs)+len(result.Result.IPv6CIDRs))
ipRanges = append(ipRanges, result.Result.IPv4CIDRs...)
ipRanges = append(ipRanges, result.Result.IPv6CIDRs...)
return ipRanges, nil
}