ipfinder

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2023 License: MIT Imports: 7 Imported by: 1

README

ipfinder

Go utility package for finding your local and public IP addresses.

Run go get hermannm.dev/ipfinder to add it to your project!

Usage

import (
	"context"
	"fmt"

	"hermannm.dev/ipfinder"
)

func main() {
	publicIP, err := ipfinder.FindPublicIP(context.Background())
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("Public IP: %s\n", publicIP.String())

	localIPs, err := ipfinder.FindLocalIPs()
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("Local IPs:")
	for _, ip := range localIPs {
		fmt.Printf("%s (interface %s)\n", ip.Address.String(), ip.NetworkInterface.Name)
	}
}

Documentation

Overview

Package ipfinder provides functions for finding your local and public IP addresses.

Index

Constants

This section is empty.

Variables

View Source
var DefaultPublicIPAPIs = []string{
	"https://api.ipify.org/",
	"https://ip.seeip.org/",
}

DefaultPublicIPAPIs are the default URLs used to find your public IP. These have almost guaranteed uptime, and no usage limit.

Functions

func FindPublicIP

func FindPublicIP(ctx context.Context, apiURLs ...string) (net.IP, error)

FindPublicIP queries the given API URLs for your public IP, and returns the first to respond. If no URLs are given, it uses the ones in DefaultPublicIPAPIs. It expects the given APIs to return an IP only, in plain-text.

It errors if all API calls fail, or if the given context cancels before a result is received.

Types

type LocalIP added in v0.2.0

type LocalIP struct {
	Address          net.IP
	NetworkInterface net.Interface
}

func FindLocalIPs

func FindLocalIPs() ([]LocalIP, error)

FindLocalIPs goes through network interfaces on your computer to find your local IP addresses. It returns a list of the found addresses, along with their associated network interface.

Jump to

Keyboard shortcuts

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