-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support loading ip ranges via requests to trusted lists * renamed module to RealIP. Added Json test * create a single preset update routine * add support for cloudflare ip range refreshing
- Loading branch information
Showing
9 changed files
with
647 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package realip | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
type AwsIPRanges struct { | ||
SyncToken string `json:"syncToken"` | ||
CreateDate string `json:"createDate"` | ||
Prefixes []struct { | ||
IPPrefix string `json:"ip_prefix"` | ||
Region string `json:"region"` | ||
Service string `json:"service"` | ||
NetworkBorderGroup string `json:"network_border_group"` | ||
} `json:"prefixes"` | ||
} | ||
|
||
func LoadCloudFront() ([]*net.IPNet, error) { | ||
set := make([]*net.IPNet, 0) | ||
|
||
awsRanges, err := loadAwsIPRanges() | ||
if err != nil { | ||
return set, err | ||
} | ||
|
||
for i := 0; i < len(awsRanges.Prefixes); i++ { | ||
if awsRanges.Prefixes[i].Service == "CLOUDFRONT" { | ||
_, ipnet, err := net.ParseCIDR(awsRanges.Prefixes[i].IPPrefix) | ||
if err != nil { | ||
// ok to return a partial set as this will not be used if an error is returned | ||
return set, err | ||
} | ||
set = append(set, ipnet) | ||
} | ||
} | ||
|
||
return set, nil | ||
} | ||
|
||
func loadAwsIPRanges() (AwsIPRanges, error) { | ||
var awsRanges AwsIPRanges | ||
|
||
resp, err := http.DefaultClient.Get("https://ip-ranges.amazonaws.com/ip-ranges.json") | ||
if err != nil { | ||
return AwsIPRanges{}, err | ||
} | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&awsRanges) | ||
if err != nil { | ||
return AwsIPRanges{}, err | ||
} | ||
return awsRanges, nil | ||
} | ||
|
||
// help for updating the preset range | ||
func printRange() { | ||
|
||
awsRanges, err := loadAwsIPRanges() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for i := 0; i < len(awsRanges.Prefixes); i++ { | ||
if awsRanges.Prefixes[i].Service == "CLOUDFRONT" { | ||
fmt.Printf("MustParseCIDR(\"%s\"),\n", awsRanges.Prefixes[i].IPPrefix) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package realip | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestLoadAWS(t *testing.T) { | ||
printRange() | ||
t.Fail() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package realip | ||
|
||
import ( | ||
"bufio" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
func LoadCloudflare() ([]*net.IPNet, error) { | ||
set := make([]*net.IPNet, 0) | ||
|
||
loaded, err := loadList("https://www.cloudflare.com/ips-v4") | ||
if err != nil { | ||
return set, err | ||
} | ||
|
||
set = append(set, loaded...) | ||
|
||
loaded, err = loadList("https://www.cloudflare.com/ips-v6") | ||
if err != nil { | ||
return set, err | ||
} | ||
|
||
set = append(set, loaded...) | ||
|
||
return set, nil | ||
} | ||
|
||
func loadList(url string) ([]*net.IPNet, error) { | ||
set := make([]*net.IPNet, 0) | ||
|
||
resp, err := http.DefaultClient.Get(url) | ||
if err != nil { | ||
return set, err | ||
} | ||
|
||
scanner := bufio.NewScanner(resp.Body) | ||
for scanner.Scan() { | ||
_, ipnet, err := net.ParseCIDR(scanner.Text()) | ||
if err != nil { | ||
return set, err | ||
} | ||
set = append(set, ipnet) | ||
} | ||
|
||
return set, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.