ipapi

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

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

Go to latest
Published: May 19, 2025 License: MIT Imports: 7 Imported by: 0

README

ipapi

ipapi is a simple, dependency free and lightweight Go package to fetch public IP address information (IPv4 and IPv6) of the current machine or any arbitrary IP address using third-party APIs

Features

  • 🔍 Get your machine's public IPv4 and IPv6 addresses separately.
  • 🌐 Lookup detailed geolocation and network info for any IP address.
  • ⚡ Uses two fast APIs: ipify.org for IP fetching, and ipquery.io for metadata.
  • 📦 Simple to use, easy to integrate.

Installation

go get github.com/ali-hasehmi/ipapi

Usage Examples

find my IPs
package main

import (
    "github.com/ali-hasehmi/ipapi"
    "log"
)

func main(){
    ipv4, err := ipapi.QueryOwnIPv4()
    if err != nil {
        log.Fatalln("error finding ipv4:", err)
    }
    ipv6, err := ipapi.QueryOwnIPv6()
    if err != nil {
        log.Fatalln("error finding ipv4:", err)
    }
    if ipv4 == "" {
        ipv4 = "none"
    }
    if ipv6 == ""{
        ipv6= "none"
    }
    log.Printf("your ipv4: %v\n",ipv4)
    log.Printf("your ipv6: %v\n",ipv6)
}
Find Everything About my machine IPs
package main

import (
	"encoding/json"
	"log"

	"github.com/ali-hasehmi/ipapi"
)

func main() {
	info, err := ipapi.QueryOwnIPInfo()
	if err != nil {
		log.Fatal(err)
	}
	b, err := json.MarshalIndent(info, "", "\t")
	if err != nil {
		log.Fatalln(info)
	}
	log.Println(string(b))
}
Find Everything About any IP
package main

import (
	"encoding/json"
	"log"

	"github.com/ali-hasehmi/ipapi"
)

func main() {
	info, err := ipapi.QueryIPInfo("1.1.1.1")
	if err != nil {
		log.Fatalln(err)
	}
	b, err := json.MarshalIndent(info, "", "\t")
	if err != nil {
		log.Fatalln(info)
	}
	log.Println(string(b))
}

Acknoledgments

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QueryOwnIPv4

func QueryOwnIPv4() (string, error)

Find this machine's public ipv4 returns empty string if this machine doesn't have ipv4

func QueryOwnIPv6

func QueryOwnIPv6() (string, error)

Find this machine's public ipv6 returns empty string if this machine doesn't have ipv6

Types

type IPInfo

type IPInfo struct {
	IPv4     string        `json:"ipv4"`
	IPv6     string        `json:"ipv6"`
	ISP      *ISPInfo      `json:"isp,omitempty"`
	Location *LocationInfo `json:"location,omitempty"`
	Risk     *RiskInfo     `json:"risk,omitempty"`
}

IPInfo represents all the information returned by the API.

func QueryIPInfo

func QueryIPInfo(ip string) (*IPInfo, error)

Query a specific IP(v4 or v6) for details

func QueryOwnIPInfo

func QueryOwnIPInfo() (*IPInfo, error)

Fetch Both IPv4 and IPv6 of this machine and their relevant details

type ISPInfo

type ISPInfo struct {
	ASN string `json:"asn,omitempty"`
	Org string `json:"org,omitempty"`
	ISP string `json:"isp,omitempty"`
}

ISPInfo represents information about the ISP of an IP address.

type LocationInfo

type LocationInfo struct {
	Country     string  `json:"country,omitempty"`
	CountryCode string  `json:"country_code,omitempty"`
	City        string  `json:"city,omitempty"`
	State       string  `json:"state,omitempty"`
	ZipCode     string  `json:"zipcode,omitempty"`
	Latitude    float64 `json:"latitude,omitempty"`
	Longitude   float64 `json:"longitude,omitempty"`
	Timezone    string  `json:"timezone,omitempty"`
	Localtime   string  `json:"localtime,omitempty"`
}

LocationInfo represents geographical information about an IP address.

type RiskInfo

type RiskInfo struct {
	IsMobile     bool `json:"is_mobile,omitempty"`
	IsVPN        bool `json:"is_vpn,omitempty"`
	IsTor        bool `json:"is_tor,omitempty"`
	IsProxy      bool `json:"is_proxy,omitempty"`
	IsDatacenter bool `json:"is_datacenter,omitempty"`
	RiskScore    int  `json:"risk_score,omitempty"`
}

RiskInfo represents risk information about an IP address.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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