geoip

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package geoip provides IP geolocation lookup utilities.

It automatically selects between a domestic (China) API (whois.pconline.com.cn) and an international API (ip-api.com) based on the IP address, and also exposes explicit CN/Global variants.

Quick start

country, city, location, err := geoip.GetIPLocation("8.8.8.8")
// → "United States", "Mountain View", "Mountain View, United States", nil

// Force domestic API (more accurate for China IPs)
country, city, location, _ := geoip.GetIPLocationCN("112.0.0.1")

// Just the display string
addr := geoip.GetRealAddressByIP("8.8.8.8")

Index

Constants

View Source
const (
	PCONLINE_IP_URL = "http://whois.pconline.com.cn/ipJson.jsp"
	IP_API_URL      = "http://ip-api.com/json/"
	UNKNOWN         = "Unknown"
	INTERNAL_IP     = "内网IP"
	LOCAL_NETWORK   = "Local Network"
)

Constants for unknown/internal labels.

View Source
const LocalNetwork = LOCAL_NETWORK

LocalNetwork is the display label for internal/private client IPs.

Variables

This section is empty.

Functions

func GetIPLocation

func GetIPLocation(ip string) (string, string, string, error)

GetIPLocation resolves the geographic location of an IP address. It automatically selects a domestic (China) or international API. Returns country, city, full location string, and error.

func GetIPLocationCN

func GetIPLocationCN(ip string) (string, string, string, error)

GetIPLocationCN forces the domestic (China) API for lookup.

func GetIPLocationGlobal

func GetIPLocationGlobal(ip string) (string, string, string, error)

GetIPLocationGlobal forces the international API for lookup.

func GetRealAddressByIP

func GetRealAddressByIP(ip string) string

GetRealAddressByIP returns a single display string for the IP location.

func IsIP

func IsIP(s string) bool

IsIP returns true if s is a valid IP address.

func IsInternalIP

func IsInternalIP(ipStr string) bool

IsInternalIP returns true if ipStr is an internal/private address.

func IsIpInCIDRList

func IsIpInCIDRList(ip net.IP, cidrList []string) bool

IsIpInCIDRList checks whether ip is contained in any of the given CIDR entries. Entries that are not valid CIDR notation are treated as individual IPs.

func IsPrivateIP

func IsPrivateIP(ip net.IP) bool

IsPrivateIP returns true if ip is in a private/loopback/link-local range.

func ParseIP

func ParseIP(s string) net.IP

ParseIP parses s into a net.IP, returning nil on failure.

Types

type IPGeolocationResponse

type IPGeolocationResponse struct {
	Country     string  `json:"country"`
	CountryCode string  `json:"countryCode"`
	RegionName  string  `json:"regionName"`
	City        string  `json:"city"`
	Lat         float64 `json:"lat"`
	Lon         float64 `json:"lon"`
	Timezone    string  `json:"timezone"`
	ISP         string  `json:"isp"`
	Org         string  `json:"org"`
	AS          string  `json:"as"`
	Query       string  `json:"query"`
	Status      string  `json:"status"`
	Message     string  `json:"message"`
}

IPGeolocationResponse IP 地理位置 API 响应(ip-api 格式)

type IPLocationResponse

type IPLocationResponse struct {
	Pro  string `json:"pro"`
	City string `json:"city"`
}

IPLocationResponse IP 地理位置查询响应(pconline 格式)

Jump to

Keyboard shortcuts

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