From c2283cd4eb592ba18e29277d25f0ace2ce9a45f9 Mon Sep 17 00:00:00 2001 From: Gitau Gakwa <50299997+gitaugakwa@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:14:07 +0300 Subject: [PATCH 1/3] Update cloudfront.go --- providers/cloudfront/cloudfront.go | 48 ++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/providers/cloudfront/cloudfront.go b/providers/cloudfront/cloudfront.go index 0e884d7..14cb590 100644 --- a/providers/cloudfront/cloudfront.go +++ b/providers/cloudfront/cloudfront.go @@ -1,12 +1,54 @@ // 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", + + 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 } + 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 + } + // 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 + } + + // 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 + } + + // Merge the arrays + mergedIPList := append(globalIPList, regionalIPList...) + + return mergedIPList, nil } const ClientIPHeaderName = "Cloudfront-Viewer-Address" From a26b1e2bf20aceb0426c8c6994231e0fec0b7af5 Mon Sep 17 00:00:00 2001 From: Gitau Gakwa <50299997+gitaugakwa@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:24:29 +0300 Subject: [PATCH 2/3] Update cloudfront.go --- providers/cloudfront/cloudfront.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/providers/cloudfront/cloudfront.go b/providers/cloudfront/cloudfront.go index 14cb590..e3a37c8 100644 --- a/providers/cloudfront/cloudfront.go +++ b/providers/cloudfront/cloudfront.go @@ -16,7 +16,10 @@ func TrustedIPS() []string { resp, err := http.Get(url) if err != nil { fmt.Println("Error making the request:", err) - return + return []string{ + "0.0.0.0/0", + "::/0", + } } defer resp.Body.Close() // Ensure the response body is closed @@ -24,7 +27,10 @@ func TrustedIPS() []string { body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading the response body:", err) - return + return []string{ + "0.0.0.0/0", + "::/0", + } } // Define a map to hold the JSON data var data map[string][]string @@ -33,7 +39,10 @@ func TrustedIPS() []string { err = json.Unmarshal(body, &data) if err != nil { fmt.Println("Error parsing the JSON:", err) - return + return []string{ + "0.0.0.0/0", + "::/0", + } } // Extract the arrays @@ -42,13 +51,16 @@ func TrustedIPS() []string { if !globalExists && !regionalExists { fmt.Println("Both keys are missing in the response") - return + return []string{ + "0.0.0.0/0", + "::/0", + } } // Merge the arrays mergedIPList := append(globalIPList, regionalIPList...) - - return mergedIPList, nil + + return mergedIPList } const ClientIPHeaderName = "Cloudfront-Viewer-Address" From d204dbd9410ecd71ff3899c7cecd828d9013be54 Mon Sep 17 00:00:00 2001 From: Gitau Gakwa <50299997+gitaugakwa@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:26:26 +0300 Subject: [PATCH 3/3] Update cloudfront.go --- providers/cloudfront/cloudfront.go | 1 + 1 file changed, 1 insertion(+) diff --git a/providers/cloudfront/cloudfront.go b/providers/cloudfront/cloudfront.go index e3a37c8..ed8f6ef 100644 --- a/providers/cloudfront/cloudfront.go +++ b/providers/cloudfront/cloudfront.go @@ -11,6 +11,7 @@ import ( // CFIPs is the CloudFlare Server IP list (this is checked on build). func TrustedIPS() []string { + // 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)