Skip to content

Commit

Permalink
cache aws ip ranges to file
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed May 16, 2023
1 parent 1351aa7 commit 294a345
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion check/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// defaultHttpClient is reused by checks that make HTTP requests.
var defaultHttpClient = newHttpClient(&http.Client{Timeout: 10 * time.Second})
var defaultHttpClient = newHttpClient(&http.Client{Timeout: 5 * time.Second})

type httpClient struct {
client *http.Client
Expand Down
21 changes: 18 additions & 3 deletions check/isonaws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net"
"os"

"github.com/jreisinger/checkip"
)
Expand Down Expand Up @@ -46,10 +47,24 @@ func IsOnAWS(ipaddr net.IP) (checkip.Result, error) {
NetworkBorderGroup string `json:"network_border_group"`
} `json:"prefixes"`
}{}
apiUrl := "https://ip-ranges.amazonaws.com/ip-ranges.json"
if err := defaultHttpClient.GetJson(apiUrl, map[string]string{}, map[string]string{}, &resp); err != nil {
return result, newCheckError(err)

filename, err := getDbFilesPath("aws-ip-ranges.json")
if err != nil {
return result, err
}
if err := updateFile(filename, "https://ip-ranges.amazonaws.com/ip-ranges.json", ""); err != nil {
return result, err
}

f, err := os.Open(filename)
if err != nil {
return result, err
}
dec := json.NewDecoder(f)
if err := dec.Decode(&resp); err != nil {
return result, err
}

var a awsIpRanges
for _, prefix := range resp.Prefixes {
_, network, err := net.ParseCIDR(prefix.IpPrefix)
Expand Down

0 comments on commit 294a345

Please sign in to comment.