Skip to content

Commit

Permalink
feat: improve ibm ranges (#4)
Browse files Browse the repository at this point in the history
- Ibm ranges are now correctly fetched
  • Loading branch information
nohehf authored Oct 1, 2024
1 parent 6bf1322 commit a62a39f
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 147 deletions.
61 changes: 51 additions & 10 deletions internal/source/source_ibm.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,73 @@
package source

import (
"encoding/json"

"github.com/Escape-Technologies/cloudfinder/internal/log"
"github.com/Escape-Technologies/cloudfinder/pkg/provider"
)

type Ibm struct{}

const ibmFileURL = "https://raw.githubusercontent.com/devanshbatham/ip2cloud/main/data/ibm.txt"
// This url is sourced from: https://ibm.biz/cidr-calculator, itself referenced in the official docs: https://cloud.ibm.com/docs/cloud-infrastructure?topic=cloud-infrastructure-ibm-cloud-ip-ranges
const ibmFileURL = "https://raw.githubusercontent.com/dprosper/cidr-calculator/main/data/datacenters.json"

type cat []*struct {
CdirBlocks []string `json:"cidr_blocks"`
}

type ibmJSON struct {
DataCenters []struct {
Public cat `json:"front_end_public_network"`
LoadBalancers cat `json:"load_balancers_ips"`
Service cat `json:"service_network"`
FileBlock cat `json:"file_block"`
Icons cat `json:"icos"`
AdvMon cat `json:"advmon"`
RheLs cat `json:"rhe_ls"`
Ims cat `json:"ims"`
} `json:"data_centers"`
}

func (a Ibm) GetProvider() provider.Provider {
return provider.Ibm
}

func (a Ibm) GetIPRanges() []*IPRange {
log.Info("Using static Ibm ip ranges")
log.Info("Fetching ibm ranges")

ranges := make([]*IPRange, 0)
ibmRanges, err := LoadTextURLToRange(ibmFileURL)
j := &ibmJSON{}
err := LoadFileURLToJSON(ibmFileURL, j)
if err != nil {
log.Fatal("Failed to load text url to range for IBM", err)
log.Fatal("Failed to load json from url for IBM", err)
}
for _, cdir := range ibmRanges {
network, cat := ParseCIDR(cdir)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
})

// Much nesting lol
for _, d := range j.DataCenters {
// convert to map to iterate over fields easily
b, _ := json.Marshal(d)
m := map[string]cat{}
err = json.Unmarshal(b, &m)
if err != nil {
log.Fatal("Failed to load json from url for IBM", err)
}

for _, c := range m {
for _, cdirs := range c {
for _, cdir := range cdirs.CdirBlocks {
network, cat := ParseCIDR(cdir)
if isPrivateNetwork(network) {
continue
}
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
})
}
}
}
}

return ranges
}
21 changes: 21 additions & 0 deletions internal/source/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,24 @@ func LoadTextURLToRange(url string) ([]string, error) {
}
return strings.Split(string(body), "\n"), nil
}

func isPrivateNetwork(n *net.IPNet) bool {
// Source: https://en.wikipedia.org/wiki/Private_network
for _, cdir := range []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
"100.64.0.0/10",
"fc00::/7",
} {
_, pn, err := net.ParseCIDR(cdir)
if err != nil {
panic(err)
}

if pn.Contains(n.IP) {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion internal/static/hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52b8c3cf76f519279f8d1592e351bfbc0a099bc2f8e787a16df3065097f9962a
56bb287f7dbb22a8ba1ccad5e16fe86e8bd3632de2a056b37ed939cd3692ab7f
Binary file modified internal/static/ipv4.gob
Binary file not shown.
Binary file modified internal/static/ipv6.gob
Binary file not shown.
Loading

0 comments on commit a62a39f

Please sign in to comment.