Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions providers/cloudfront/cloudfront.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,67 @@
// Package ips contains a list of current cloud flare IP ranges
package cloudfront

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

// CFIPs is the CloudFlare Server IP list (this is checked on build).
func TrustedIPS() []string {
return []string{
"0.0.0.0/0",
"::/0",

// Found at https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/LocationsOfEdgeServers.html
url := "https://d7uri8nf7uskq.cloudfront.net/tools/list-cloudfront-ips"

resp, err := http.Get(url)
if err != nil {
fmt.Println("Error making the request:", err)
return []string{
"0.0.0.0/0",
"::/0",
}
}
defer resp.Body.Close() // Ensure the response body is closed

// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading the response body:", err)
return []string{
"0.0.0.0/0",
"::/0",
}
}
// Define a map to hold the JSON data
var data map[string][]string

// Parse the JSON response
err = json.Unmarshal(body, &data)
if err != nil {
fmt.Println("Error parsing the JSON:", err)
return []string{
"0.0.0.0/0",
"::/0",
}
}

// Extract the arrays
globalIPList, globalExists := data["CLOUDFRONT_GLOBAL_IP_LIST"]
regionalIPList, regionalExists := data["CLOUDFRONT_REGIONAL_EDGE_IP_LIST"]

if !globalExists && !regionalExists {
fmt.Println("Both keys are missing in the response")
return []string{
"0.0.0.0/0",
"::/0",
}
}

// Merge the arrays
mergedIPList := append(globalIPList, regionalIPList...)

return mergedIPList
}

const ClientIPHeaderName = "Cloudfront-Viewer-Address"