Added code to track aws/azure/gcp

This commit is contained in:
Ishan Jain 2023-07-29 20:56:34 +05:30
commit df3ebca4ad
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
5 changed files with 104981 additions and 0 deletions

74
aws/main.go Normal file
View File

@ -0,0 +1,74 @@
package main
import (
"encoding/json"
"fmt"
"math"
"net"
"net/http"
)
type Aws struct {
Prefixes []struct {
IPPrefix string `json:"ip_prefix"`
Region string `json:"region"`
Service string `json:"service"`
NetworkBorderGroup string `json:"network_border_group"`
} `json:"prefixes"`
Ipv6Prefixes []struct {
Ipv6Prefix string `json:"ipv6_prefix"`
Region string `json:"region"`
Service string `json:"service"`
NetworkBorderGroup string `json:"network_border_group"`
} `json:"ipv6_prefixes"`
}
func main() {
resp, err := http.Get("https://ip-ranges.amazonaws.com/ip-ranges.json")
if err != nil {
panic(err)
}
defer resp.Body.Close()
input := Aws{}
err = json.NewDecoder(resp.Body).Decode(&input)
if err != nil {
panic(err)
}
total := 0
smallestPrefix := 32
for _, l := range input.Prefixes {
_, cidr, err := net.ParseCIDR(l.IPPrefix)
if err != nil {
panic(err)
}
ones, bits := cidr.Mask.Size()
if ones < smallestPrefix {
smallestPrefix = ones
}
total += int(math.Pow(2.0, float64(bits-ones)))
}
fmt.Println("Total Ipv4 Address Count", total, "Smallest Ipv4 Prefix", smallestPrefix)
var totalIpv6 int64 = 0
smallestPrefix = 128
for _, l := range input.Ipv6Prefixes {
_, cidr, err := net.ParseCIDR(l.Ipv6Prefix)
if err != nil {
panic(err)
}
ones, bits := cidr.Mask.Size()
if ones < smallestPrefix {
smallestPrefix = ones
}
// Only count /64 blocks
bits = 64
totalIpv6 += int64(math.Pow(2.0, float64(bits-ones)))
}
fmt.Println("Total Ipv6 Address Count", totalIpv6, "Smallest Ipv6 Prefix", smallestPrefix)
}

File diff suppressed because it is too large Load Diff

83
azure/main.go Normal file
View File

@ -0,0 +1,83 @@
package main
import (
"encoding/json"
"fmt"
"math"
"net"
"os"
)
type Azure struct {
ChangeNumber int `json:"changeNumber"`
Cloud string `json:"cloud"`
Values []struct {
Name string `json:"name"`
ID string `json:"id"`
Properties struct {
ChangeNumber int `json:"changeNumber"`
Region string `json:"region"`
RegionID int `json:"regionId"`
Platform string `json:"platform"`
SystemService string `json:"systemService"`
AddressPrefixes []string `json:"addressPrefixes"`
NetworkFeatures []string `json:"networkFeatures"`
} `json:"properties"`
} `json:"values"`
}
func main() {
// https://www.microsoft.com/en-us/download/details.aspx?id=56519
f, err := os.Open("./ServiceTags_Public_20230724.json")
if err != nil {
panic(err)
}
defer f.Close()
input := Azure{}
err = json.NewDecoder(f).Decode(&input)
if err != nil {
panic(err)
}
totalIpv4 := 0
totalIpv6 := 0
smallestIpv4Prefix := 32
smallestIpv6Prefix := 128
for _, v := range input.Values {
for _, addr := range v.Properties.AddressPrefixes {
_, cidr, err := net.ParseCIDR(addr)
if err != nil {
panic(err)
}
ones, bits := cidr.Mask.Size()
if bits == 128 {
// Only track /64 blocks
bits = 64
totalIpv6 += int(math.Pow(2.0, float64(bits-ones)))
if ones < smallestIpv6Prefix {
smallestIpv6Prefix = ones
}
} else if bits == 32 {
totalIpv4 += int(math.Pow(2.0, float64(bits-ones)))
if ones < smallestIpv4Prefix {
smallestIpv4Prefix = ones
}
} else {
panic("unknown bits??")
}
}
}
fmt.Println("Total Ipv4 Address Count", totalIpv4, "Smallest Ipv4 Prefix", smallestIpv4Prefix)
fmt.Println("Total Ipv6 Address Count", totalIpv6, "Smallest Ipv6 Prefix", smallestIpv6Prefix)
}

76
gcp/main.go Normal file
View File

@ -0,0 +1,76 @@
package main
import (
"encoding/json"
"fmt"
"math"
"net"
"net/http"
)
type Gcp struct {
SyncToken string `json:"syncToken"`
CreationTime string `json:"creationTime"`
Prefixes []struct {
Ipv4Prefix string `json:"ipv4Prefix,omitempty"`
Service string `json:"service"`
Scope string `json:"scope"`
Ipv6Prefix string `json:"ipv6Prefix,omitempty"`
} `json:"prefixes"`
}
func main() {
resp, err := http.Get("https://www.gstatic.com/ipranges/cloud.json")
if err != nil {
panic(err)
}
defer resp.Body.Close()
input := Gcp{}
err = json.NewDecoder(resp.Body).Decode(&input)
if err != nil {
panic(err)
}
total := 0
smallestPrefix := 32
for _, l := range input.Prefixes {
if l.Ipv4Prefix == "" {
continue
}
_, cidr, err := net.ParseCIDR(l.Ipv4Prefix)
if err != nil {
panic(err)
}
ones, bits := cidr.Mask.Size()
if ones < smallestPrefix {
smallestPrefix = ones
}
total += int(math.Pow(2.0, float64(bits-ones)))
}
fmt.Println("Total Ipv4 Address Count", total, "Smallest Ipv4 Prefix", smallestPrefix)
var totalIpv6 int64 = 0
smallestPrefix = 128
for _, l := range input.Prefixes {
if l.Ipv6Prefix == "" {
continue
}
_, cidr, err := net.ParseCIDR(l.Ipv6Prefix)
if err != nil {
panic(err)
}
ones, bits := cidr.Mask.Size()
if ones < smallestPrefix {
smallestPrefix = ones
}
// Only count /64 blocks
bits = 64
totalIpv6 += int64(math.Pow(2.0, float64(bits-ones)))
}
fmt.Println("Total Ipv6 Address Count", totalIpv6, "Smallest Ipv6 Prefix", smallestPrefix)
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module cidr-check
go 1.20