gcpip

package module
v0.0.0-...-4454bd0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2025 License: MIT Imports: 2 Imported by: 1

README

gcpip: a Go package to check if an IP belongs to GCP

gcpip is a Go package that allows you to determine if an IP address belongs to GCP.

A cli tool is also included in cmd/gcpip for easily checking the status of an ip address.

Example:


func ExampleRange() {
	ip := netip.MustParseAddr("104.155.192.1")
	r := Range(ip)
	fmt.Println(r.Prefix)
	fmt.Println(r.Service)
	fmt.Println(r.Scope)
	// Output:
	// 104.155.192.0/19
	// Google Cloud
	// asia-east1
}

func ExampleIsGcpIP() {
	ips := []netip.Addr{
		netip.MustParseAddr("104.155.192.1"),
		netip.MustParseAddr("54.74.0.27"),
		netip.MustParseAddr("127.0.0.1"),
	}
	for _, ip := range ips {
		if IsGcpIP(ip) {
			fmt.Printf("%s is GCP\n", ip)
		} else {
			fmt.Printf("%s is NOT GCP\n", ip)
		}
	}
	// Output:
	// 104.155.192.1 is GCP
	// 54.74.0.27 is NOT GCP
	// 127.0.0.1 is NOT GCP
}

CLI:

$ $ gcpip 104.155.192.1
{
  "Prefix": "104.155.192.0/19",
  "Service": "Google Cloud",
  "Scope": "asia-east1"
}

Updating the ip ranges

To update the ip ranges run: go generate. This will fetch from https://www.gstatic.com/ipranges/cloud.json and regenerate the ips.gen.go file.

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsGcpIP

func IsGcpIP(ip netip.Addr) bool

IsGcpIP returns true if the ip address falls within one of the known GCP ip ranges.

Example
ips := []netip.Addr{
	netip.MustParseAddr("104.155.192.1"),
	netip.MustParseAddr("54.74.0.27"),
	netip.MustParseAddr("127.0.0.1"),
}
for _, ip := range ips {
	if IsGcpIP(ip) {
		fmt.Printf("%s is GCP\n", ip)
	} else {
		fmt.Printf("%s is NOT GCP\n", ip)
	}
}
Output:

104.155.192.1 is GCP
54.74.0.27 is NOT GCP
127.0.0.1 is NOT GCP

Types

type IPRange

type IPRange struct {
	Prefix  netip.Prefix
	Service string
	Scope   string
}

func Range

func Range(ip netip.Addr) *IPRange

Range returns the ip range and metadata an address falls within. If the IP is not an GCP IP address it returns nil

Example
ip := netip.MustParseAddr("104.155.192.1")
r := Range(ip)
fmt.Println(r.Prefix)
fmt.Println(r.Service)
fmt.Println(r.Scope)
Output:

104.155.192.0/19
Google Cloud
asia-east1

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL