Created
July 8, 2019 01:43
-
-
Save daidokoro/ddb44c91113550ccc04d94a10a2b9fbb to your computer and use it in GitHub Desktop.
List Google Cloud Platform IP Address Ranges - Golang
This file contains hidden or 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 package contains a simple Go code | |
for acquiring the IP Address Ranges utilised by Google Cloud Platform. | |
This process is based on documentation provided by Google | |
see: https://cloud.google.com/compute/docs/faq#find_ip_range | |
Author: Shaun Remekie | |
Email: [email protected] | |
*/ | |
package main | |
import ( | |
"fmt" | |
"log" | |
"net" | |
"strings" | |
) | |
const ( | |
gcpLookupAddr = "_cloud-netblocks.googleusercontent.com" | |
) | |
// GoogleCloudIPR - type used to store ip ranges | |
type GoogleCloudIPR []string | |
// lookup - recursively lookup and read TXT records to | |
// identify google cloud IP address ranges | |
func (g *GoogleCloudIPR) lookup(addr string) error { | |
records, err := net.LookupTXT(addr) | |
if err != nil { | |
return err | |
} | |
for _, txt := range records { | |
for _, r := range strings.Split(txt, " ") { | |
switch { | |
case strings.HasPrefix(r, "include:"): | |
if err := g.lookup(strings.ReplaceAll(r, "include:", "")); err != nil { | |
return err | |
} | |
case strings.HasPrefix(r, "ip4:"): | |
*g = append(*g, strings.ReplaceAll(r, "ip4:", "")) | |
default: | |
continue | |
} | |
} | |
} | |
return nil | |
} | |
func main() { | |
gcp := GoogleCloudIPR{} | |
if err := gcp.lookup(gcpLookupAddr); err != nil { | |
log.Fatalf("failed lookup: %s", err) | |
} | |
for _, ip := range gcp { | |
fmt.Println(ip) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment