awsip

package module
v0.0.0-...-a0bb1f0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 2 Imported by: 0

README

awsip: a Go package to check if an IP belongs to AWS

awsip is a Go package that allows you to determine if an IP address belongs to AWS.

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

Example:

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

func ExampleRange() {
	ip := netip.MustParseAddr("54.74.0.27")
	r := Range(ip)
	fmt.Println(r.Prefix)
	fmt.Println(r.NetworkBorderGroup)
	fmt.Println(r.Region)
	fmt.Println(r.Services)
	// Output:
	// 54.74.0.0/15
	// eu-west-1
	// eu-west-1
	// [AMAZON EC2]
}

CLI:

$ ./awsip 54.74.0.27
{
  "Prefix": "54.74.0.0/15",
  "NetworkBorderGroup": "eu-west-1",
  "Region": "eu-west-1",
  "Services": [
    "AMAZON",
    "EC2"
  ]
}

Updating the ip ranges

To update the ip ranges run: go generate. This will fetch from https://ip-ranges.amazonaws.com/ip-ranges.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 IsAwsIP

func IsAwsIP(ip netip.Addr) bool

IsAwsIP returns true if the ip address falls within one of the known AWS ip ranges.

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

54.74.0.27 is AWS
127.0.0.1 is NOT AWS

Types

type IPRange

type IPRange struct {
	Prefix             netip.Prefix
	NetworkBorderGroup string
	Region             string
	Services           []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 AWS IP address it returns nil

Example
ip := netip.MustParseAddr("54.74.0.27")
r := Range(ip)
fmt.Println(r.Prefix)
fmt.Println(r.NetworkBorderGroup)
fmt.Println(r.Region)
fmt.Println(r.Services)
Output:

54.74.0.0/15
eu-west-1
eu-west-1
[AMAZON EC2]

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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