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
- func GetIPLocation(ip string) (string, string, string, error)
- func GetIPLocationCN(ip string) (string, string, string, error)
- func GetIPLocationGlobal(ip string) (string, string, string, error)
- func GetRealAddressByIP(ip string) string
- func IsIP(s string) bool
- func IsInternalIP(ipStr string) bool
- func IsIpInCIDRList(ip net.IP, cidrList []string) bool
- func IsPrivateIP(ip net.IP) bool
- func ParseIP(s string) net.IP
- type IPGeolocationResponse
- type IPLocationResponse
Constants ¶
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.
const LocalNetwork = LOCAL_NETWORK
LocalNetwork is the display label for internal/private client IPs.
Variables ¶
This section is empty.
Functions ¶
func GetIPLocation ¶
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 ¶
GetIPLocationCN forces the domestic (China) API for lookup.
func GetIPLocationGlobal ¶
GetIPLocationGlobal forces the international API for lookup.
func GetRealAddressByIP ¶
GetRealAddressByIP returns a single display string for the IP location.
func IsInternalIP ¶
IsInternalIP returns true if ipStr is an internal/private address.
func IsIpInCIDRList ¶
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 ¶
IsPrivateIP returns true if ip is in a private/loopback/link-local range.
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 ¶
IPLocationResponse IP 地理位置查询响应(pconline 格式)