|
|
|
@ -8,6 +8,23 @@ import (
|
|
|
|
|
"github.com/dtapps/go-library/utils/goip/qqwry"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ClientConfig struct {
|
|
|
|
|
Ip2regionPath string
|
|
|
|
|
Ip2regionByte []byte
|
|
|
|
|
Ip2regionV2Path string
|
|
|
|
|
Ip2regionV2Byte []byte
|
|
|
|
|
QqwryPath string
|
|
|
|
|
QqwryByte []byte
|
|
|
|
|
Ipv6wryPath string
|
|
|
|
|
Ipv6wryByte []byte
|
|
|
|
|
GeoipAsnPath string
|
|
|
|
|
GeoipAsnByte []byte
|
|
|
|
|
GeoipCityPath string
|
|
|
|
|
GeoipCityByte []byte
|
|
|
|
|
GeoipCountryPath string
|
|
|
|
|
GeoipCountryByte []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
|
ip2regionV2Client *ip2region_v2.Client
|
|
|
|
|
ip2regionClient *ip2region.Client
|
|
|
|
@ -17,21 +34,72 @@ type Client struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewIp 实例化
|
|
|
|
|
func NewIp() *Client {
|
|
|
|
|
func NewIp(config ClientConfig) (*Client, error) {
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
c := &Client{}
|
|
|
|
|
|
|
|
|
|
c.ip2regionV2Client, _ = ip2region_v2.New()
|
|
|
|
|
if config.Ip2regionV2Path == "" {
|
|
|
|
|
c.ip2regionV2Client, err = ip2region_v2.NewBuff(config.Ip2regionV2Byte)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.ip2regionV2Client, err = ip2region_v2.New(config.Ip2regionV2Path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.ip2regionClient = ip2region.New()
|
|
|
|
|
if config.Ip2regionPath == "" {
|
|
|
|
|
c.ip2regionClient, err = ip2region.NewBuff(config.Ip2regionByte)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.ip2regionClient, err = ip2region.New(config.Ip2regionPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.qqwryClient = qqwry.New()
|
|
|
|
|
if config.QqwryPath == "" {
|
|
|
|
|
c.qqwryClient, err = qqwry.NewBuff(config.QqwryByte)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.qqwryClient, err = qqwry.New(config.QqwryPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.geoIpClient, _ = geoip.New()
|
|
|
|
|
if config.GeoipAsnPath == "" || config.GeoipCityPath == "" || config.GeoipCountryPath == "" {
|
|
|
|
|
c.geoIpClient, err = geoip.NewBuff(config.GeoipAsnByte, config.GeoipCityByte, config.GeoipCountryByte)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.geoIpClient, err = geoip.New(config.GeoipAsnPath, config.GeoipCityPath, config.GeoipCountryPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.ipv6wryClient = ipv6wry.New()
|
|
|
|
|
if config.Ipv6wryPath == "" {
|
|
|
|
|
c.ipv6wryClient, err = ipv6wry.NewBuff(config.Ipv6wryByte)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.ipv6wryClient, err = ipv6wry.New(config.Ipv6wryPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c
|
|
|
|
|
return c, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) Close() {
|
|
|
|
|