You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
goip/query.go

78 lines
1.8 KiB

package goip
import (
"errors"
"go.dtapp.net/goip/geoip"
"go.dtapp.net/goip/ip2region_v2"
"net"
)
var (
QueryIncorrect = errors.New("ip地址不正确")
)
// QueryQqWryResult 返回
type QueryQqWryResult struct {
Ip string `json:"ip,omitempty"` // 查询的ip地址
Country string `json:"country,omitempty"` // 国家或地区
Area string `json:"area,omitempty"` // 区域
}
// QueryQqWry 纯真IP库
// https://www.cz88.net/
func (c *Client) QueryQqWry(ipAddress net.IP) (result QueryQqWryResult, err error) {
if ipAddress.To4() == nil {
return result, QueryIncorrect
}
resp := c.V4db.Query(ipAddress)
return QueryQqWryResult{
Ip: resp.IP,
Country: resp.Country,
Area: resp.Area,
}, nil
}
// QueryIp2Region ip2region
// https://github.com/lionsoul2014/ip2region
func (c *Client) QueryIp2Region(ipAddress net.IP) (result QueryQqWryResult, err error) {
if ipAddress.To4() == nil {
return result, QueryIncorrect
}
resp := c.V4db.Query(ipAddress)
return QueryQqWryResult{
Ip: resp.IP,
Country: resp.Country,
Area: resp.Area,
}, nil
}
// QueryIp2RegionV2 ip2region
// https://github.com/lionsoul2014/ip2region
func (c *Client) QueryIp2RegionV2(ipAddress net.IP) (result ip2region_v2.Result, err error) {
if ipAddress.To4() == nil {
return result, QueryIncorrect
}
query, err := c.ip2regionV2Client.Query(ipAddress)
if err != nil {
return ip2region_v2.Result{}, err
}
return query, nil
}
// QueryGeoIp ip2region
// https://www.maxmind.com/
func (c *Client) QueryGeoIp(ipAddress net.IP) (result geoip.QueryCityResult, err error) {
if ipAddress.String() == "<nil>" {
return result, QueryIncorrect
}
query, err := c.geoIpClient.QueryCity(ipAddress)
if err != nil {
return geoip.QueryCityResult{}, err
}
return query, nil
}