Compare commits
14 Commits
Author | SHA1 | Date |
---|---|---|
李光春 | 803502b68a | 1 year ago |
李光春 | e7336ba3ce | 1 year ago |
李光春 | db75469df6 | 2 years ago |
李光春 | 18b8be2356 | 2 years ago |
李光春 | 33e41cb5b1 | 2 years ago |
李光春 | 07c723310f | 2 years ago |
李光春 | a24d5383fb | 2 years ago |
李光春 | 8b0cd6dfee | 2 years ago |
李光春 | bb3ce86430 | 2 years ago |
李光春 | e92e32634c | 2 years ago |
李光春 | 96906f189e | 2 years ago |
李光春 | 5cb7732037 | 2 years ago |
李光春 | 36b08917cb | 2 years ago |
李光春 | 73395b29a9 | 2 years ago |
@ -0,0 +1,39 @@
|
||||
package goip
|
||||
|
||||
import (
|
||||
"go.dtapp.net/goip/geoip"
|
||||
"go.dtapp.net/goip/ip2region"
|
||||
"go.dtapp.net/goip/ip2region_v2"
|
||||
"go.dtapp.net/goip/ipv6wry"
|
||||
"go.dtapp.net/goip/qqwry"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
ip2regionV2Client *ip2region_v2.Client
|
||||
ip2regionClient *ip2region.Client
|
||||
qqwryClient *qqwry.Client
|
||||
geoIpClient *geoip.Client
|
||||
ipv6wryClient *ipv6wry.Client
|
||||
}
|
||||
|
||||
// NewIp 实例化
|
||||
func NewIp() *Client {
|
||||
|
||||
c := &Client{}
|
||||
|
||||
c.ip2regionV2Client, _ = ip2region_v2.New()
|
||||
|
||||
c.ip2regionClient = ip2region.New()
|
||||
|
||||
c.qqwryClient = qqwry.New()
|
||||
|
||||
c.geoIpClient, _ = geoip.New()
|
||||
|
||||
c.ipv6wryClient = ipv6wry.New()
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) Close() {
|
||||
c.geoIpClient.Close()
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package goip
|
||||
|
||||
const Version = "1.0.27"
|
||||
const Version = "1.0.40"
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 66 MiB |
Binary file not shown.
@ -0,0 +1,52 @@
|
||||
package geoip
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
)
|
||||
|
||||
//go:embed GeoLite2-ASN.mmdb
|
||||
var asnBuff []byte
|
||||
|
||||
//go:embed GeoLite2-City.mmdb
|
||||
var cityBuff []byte
|
||||
|
||||
//go:embed GeoLite2-Country.mmdb
|
||||
var countryBuff []byte
|
||||
|
||||
type Client struct {
|
||||
asnDb *geoip2.Reader
|
||||
cityDb *geoip2.Reader
|
||||
countryDb *geoip2.Reader
|
||||
}
|
||||
|
||||
func New() (*Client, error) {
|
||||
|
||||
var err error
|
||||
c := &Client{}
|
||||
|
||||
c.asnDb, err = geoip2.FromBytes(asnBuff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.cityDb, err = geoip2.FromBytes(cityBuff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.countryDb, err = geoip2.FromBytes(countryBuff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
func (c *Client) Close() {
|
||||
|
||||
c.asnDb.Close()
|
||||
c.cityDb.Close()
|
||||
c.countryDb.Close()
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package geoip
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func OnlineDownload(downloadUrl string, downloadName string) {
|
||||
resp, err := http.Get(downloadUrl)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
err = ioutil.WriteFile("./"+downloadName, body, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Printf("已下载最新 ip2region.xdb 数据库 %s ", "./"+downloadName)
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package geoip
|
||||
|
||||
import (
|
||||
"go.dtapp.net/gostring"
|
||||
)
|
||||
|
||||
var licenseKey = "" // 许可证密钥
|
||||
|
||||
func GetGeoLite2AsnDownloadUrl(licenseKey string) string {
|
||||
return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=YOUR_LICENSE_KEY&suffix=tar.gz", "YOUR_LICENSE_KEY", licenseKey)
|
||||
}
|
||||
|
||||
//func GetGeoLite2AsnCsvDownloadUrl(licenseKey string) string {
|
||||
// return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN-CSV&license_key=YOUR_LICENSE_KEY&suffix=zip", "YOUR_LICENSE_KEY", licenseKey)
|
||||
//}
|
||||
|
||||
func GetGeoLite2CityDownloadUrl(licenseKey string) string {
|
||||
return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=YOUR_LICENSE_KEY&suffix=tar.gz", "YOUR_LICENSE_KEY", licenseKey)
|
||||
}
|
||||
|
||||
//func GetGeoLite2CityCsvDownloadUrl(licenseKey string) string {
|
||||
// return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City-CSV&license_key=YOUR_LICENSE_KEY&suffix=zip", "YOUR_LICENSE_KEY", licenseKey)
|
||||
//}
|
||||
|
||||
func GetGeoLite2CountryDownloadUrl(licenseKey string) string {
|
||||
return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=YOUR_LICENSE_KEY&suffix=tar.gz", "YOUR_LICENSE_KEY", licenseKey)
|
||||
}
|
||||
|
||||
//func GetGeoLite2CountryCsvDownloadUrl(licenseKey string) string {
|
||||
// return gostring.Replace("https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country-CSV&license_key=YOUR_LICENSE_KEY&suffix=zip", "YOUR_LICENSE_KEY", licenseKey)
|
||||
//}
|
@ -0,0 +1,66 @@
|
||||
package geoip
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"net"
|
||||
)
|
||||
|
||||
// QueryCityResult 返回
|
||||
type QueryCityResult struct {
|
||||
Ip string `json:"ip,omitempty"` // ip
|
||||
Continent struct {
|
||||
Code string `json:"code,omitempty"` // 大陆代码
|
||||
Name string `json:"name,omitempty"` // 大陆名称
|
||||
} `json:"continent,omitempty"`
|
||||
Country struct {
|
||||
Code string `json:"code,omitempty"` // 国家代码
|
||||
Name string `json:"name,omitempty"` // 国家名称
|
||||
} `json:"country,omitempty"`
|
||||
Province struct {
|
||||
Code string `json:"code,omitempty"` // 省份代码
|
||||
Name string `json:"name,omitempty"` // 省份名称
|
||||
} `json:"province,omitempty"`
|
||||
City struct {
|
||||
Name string `json:"name,omitempty"` // 城市名称
|
||||
} `json:"city,omitempty"`
|
||||
Location struct {
|
||||
TimeZone string `json:"time_zone,omitempty"` // 位置时区
|
||||
Latitude float64 `json:"latitude,omitempty"` // 坐标纬度
|
||||
Longitude float64 `json:"longitude,omitempty"` // 坐标经度
|
||||
} `json:"location,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) QueryCity(ipAddress net.IP) (result QueryCityResult, err error) {
|
||||
|
||||
record, err := c.cityDb.City(ipAddress)
|
||||
if err != nil {
|
||||
return QueryCityResult{}, err
|
||||
}
|
||||
|
||||
// ip
|
||||
result.Ip = ipAddress.String()
|
||||
|
||||
// 大陆
|
||||
result.Continent.Code = record.Continent.Code
|
||||
result.Continent.Name = record.Continent.Names["zh-CN"]
|
||||
|
||||
// 国家
|
||||
result.Country.Code = record.Country.IsoCode
|
||||
result.Country.Name = record.Country.Names["zh-CN"]
|
||||
|
||||
// 省份
|
||||
if len(record.Subdivisions) > 0 {
|
||||
result.Province.Code = record.Subdivisions[0].IsoCode
|
||||
result.Province.Name = record.Subdivisions[0].Names["zh-CN"]
|
||||
}
|
||||
|
||||
// 城市
|
||||
result.City.Name = record.City.Names["zh-CN"]
|
||||
|
||||
// 位置
|
||||
result.Location.TimeZone = record.Location.TimeZone
|
||||
result.Location.Latitude = record.Location.Latitude
|
||||
result.Location.Longitude = record.Location.Longitude
|
||||
|
||||
return result, err
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
package goip
|
||||
|
||||
import (
|
||||
"go.dtapp.net/goip/ip2region"
|
||||
v4 "go.dtapp.net/goip/v4"
|
||||
v6 "go.dtapp.net/goip/v6"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
V4Region ip2region.Ip2Region // IPV4
|
||||
V4db v4.Pointer // IPV4
|
||||
V6db v6.Pointer // IPV6
|
||||
}
|
||||
|
||||
// NewIp 实例化
|
||||
func NewIp() *Client {
|
||||
app := &Client{}
|
||||
v4Num := app.V4db.InitIPV4Data()
|
||||
log.Printf("IPV4 库加载完成 共加载:%d 条 IP 记录\n", v4Num)
|
||||
v6Num := app.V6db.InitIPV4Data()
|
||||
log.Printf("IPV6 库加载完成 共加载:%d 条 IP 记录\n", v6Num)
|
||||
return app
|
||||
}
|
||||
|
||||
func (c *Client) Ipv4(ip string) (res v4.Result, resInfo ip2region.IpInfo) {
|
||||
res = c.V4db.Find(ip)
|
||||
resInfo, _ = c.V4Region.MemorySearch(ip)
|
||||
return res, resInfo
|
||||
}
|
||||
|
||||
func (c *Client) Ipv6(ip string) (res v6.Result) {
|
||||
res = c.V6db.Find(ip)
|
||||
return res
|
||||
}
|
||||
|
||||
func (c *Client) isIpv4OrIpv6(ip string) string {
|
||||
if len(ip) < 7 {
|
||||
return ""
|
||||
}
|
||||
arrIpv4 := strings.Split(ip, ".")
|
||||
if len(arrIpv4) == 4 {
|
||||
//. 判断IPv4
|
||||
for _, val := range arrIpv4 {
|
||||
if !c.CheckIpv4(val) {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return ipv4
|
||||
}
|
||||
arrIpv6 := strings.Split(ip, ":")
|
||||
if len(arrIpv6) == 8 {
|
||||
// 判断Ipv6
|
||||
for _, val := range arrIpv6 {
|
||||
if !c.CheckIpv6(val) {
|
||||
return "Neither"
|
||||
}
|
||||
}
|
||||
return ipv6
|
||||
}
|
||||
return ""
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package ip2region
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"go.dtapp.net/gostring"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
IndexBlockLength = 12
|
||||
)
|
||||
|
||||
//go:embed ip2region.db
|
||||
var dbBuff []byte
|
||||
|
||||
type Client struct {
|
||||
// db file handler
|
||||
dbFileHandler *os.File
|
||||
|
||||
//header block info
|
||||
|
||||
headerSip []int64
|
||||
headerPtr []int64
|
||||
headerLen int64
|
||||
|
||||
// super block index info
|
||||
firstIndexPtr int64
|
||||
lastIndexPtr int64
|
||||
totalBlocks int64
|
||||
|
||||
// for memory mode only
|
||||
// the original db binary string
|
||||
|
||||
dbFile string
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
|
||||
c := &Client{}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// 获取Ip信息
|
||||
func getIpInfo(ipStr string, cityId int64, line []byte) (result QueryResult) {
|
||||
|
||||
lineSlice := strings.Split(string(line), "|")
|
||||
length := len(lineSlice)
|
||||
result.CityId = cityId
|
||||
if length < 5 {
|
||||
for i := 0; i <= 5-length; i++ {
|
||||
lineSlice = append(lineSlice, "")
|
||||
}
|
||||
}
|
||||
|
||||
if lineSlice[0] != "0" {
|
||||
result.Country = gostring.SpaceAndLineBreak(lineSlice[0])
|
||||
}
|
||||
if lineSlice[1] != "0" {
|
||||
result.Region = gostring.SpaceAndLineBreak(lineSlice[1])
|
||||
}
|
||||
if lineSlice[2] != "0" {
|
||||
result.Province = gostring.SpaceAndLineBreak(lineSlice[2])
|
||||
}
|
||||
if lineSlice[3] != "0" {
|
||||
result.City = gostring.SpaceAndLineBreak(lineSlice[3])
|
||||
}
|
||||
if lineSlice[4] != "0" {
|
||||
result.Isp = gostring.SpaceAndLineBreak(lineSlice[4])
|
||||
}
|
||||
|
||||
result.Ip = ipStr
|
||||
return result
|
||||
}
|
||||
|
||||
func getLong(b []byte, offset int64) int64 {
|
||||
|
||||
val := int64(b[offset]) |
|
||||
int64(b[offset+1])<<8 |
|
||||
int64(b[offset+2])<<16 |
|
||||
int64(b[offset+3])<<24
|
||||
|
||||
return val
|
||||
|
||||
}
|
||||
|
||||
func ip2long(IpStr string) (int64, error) {
|
||||
bits := strings.Split(IpStr, ".")
|
||||
if len(bits) != 4 {
|
||||
return 0, errors.New("ip format error")
|
||||
}
|
||||
|
||||
var sum int64
|
||||
for i, n := range bits {
|
||||
bit, _ := strconv.ParseInt(n, 10, 64)
|
||||
sum += bit << uint(24-8*i)
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package ip2region_v2
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed ip2region.xdb
|
||||
var cBuff []byte
|
||||
|
||||
type Client struct {
|
||||
db *Searcher
|
||||
}
|
||||
|
||||
func New() (*Client, error) {
|
||||
|
||||
var err error
|
||||
c := &Client{}
|
||||
|
||||
// 1、从 dbPath 加载整个 xdb 到内存
|
||||
|
||||
// 2、用全局的 cBuff 创建完全基于内存的查询对象。
|
||||
c.db, err = NewWithBuffer(cBuff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, err
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ip2region_v2
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func OnlineDownload() {
|
||||
resp, err := http.Get("https://ghproxy.com/?q=https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.xdb?raw=true")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
err = ioutil.WriteFile("./ip2region.xdb", body, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Printf("已下载最新 ip2region.xdb 数据库 %s ", "./ip2region.xdb")
|
||||
}
|
Binary file not shown.
@ -0,0 +1,52 @@
|
||||
package ip2region_v2
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"go.dtapp.net/gostring"
|
||||
"net"
|
||||
)
|
||||
|
||||
// QueryResult 返回
|
||||
type QueryResult struct {
|
||||
Ip string `json:"ip,omitempty"` // ip
|
||||
Country string `json:"country,omitempty"` // 国家
|
||||
Province string `json:"province,omitempty"` // 省份
|
||||
City string `json:"city,omitempty"` // 城市
|
||||
Operator string `json:"operator,omitempty"` // 运营商
|
||||
}
|
||||
|
||||
func (c *Client) Query(ipAddress net.IP) (result QueryResult, err error) {
|
||||
|
||||
// 备注:并发使用,用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。
|
||||
|
||||
str, err := c.db.SearchByStr(ipAddress.String())
|
||||
if err != nil {
|
||||
return QueryResult{}, err
|
||||
}
|
||||
|
||||
split := gostring.Split(str, "|")
|
||||
if len(split) <= 0 {
|
||||
return QueryResult{}, err
|
||||
}
|
||||
|
||||
result.Ip = ipAddress.String()
|
||||
|
||||
result.Country = split[0]
|
||||
if result.Country == "0" {
|
||||
result.Country = ""
|
||||
}
|
||||
result.Province = split[2]
|
||||
if result.Province == "0" {
|
||||
result.Province = ""
|
||||
}
|
||||
result.City = split[3]
|
||||
if result.City == "0" {
|
||||
result.City = ""
|
||||
}
|
||||
result.Operator = split[4]
|
||||
if result.Operator == "0" {
|
||||
result.Operator = ""
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
@ -0,0 +1,240 @@
|
||||
package ip2region_v2
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
HeaderInfoLength = 256
|
||||
VectorIndexRows = 256
|
||||
VectorIndexCols = 256
|
||||
VectorIndexSize = 8
|
||||
SegmentIndexBlockSize = 14
|
||||
)
|
||||
|
||||
// --- Index policy define
|
||||
|
||||
type IndexPolicy int
|
||||
|
||||
const (
|
||||
VectorIndexPolicy IndexPolicy = 1
|
||||
BTreeIndexPolicy IndexPolicy = 2
|
||||
)
|
||||
|
||||
func (i IndexPolicy) String() string {
|
||||
switch i {
|
||||
case VectorIndexPolicy:
|
||||
return "VectorIndex"
|
||||
case BTreeIndexPolicy:
|
||||
return "BtreeIndex"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// --- Header define
|
||||
|
||||
type Header struct {
|
||||
// data []byte
|
||||
Version uint16
|
||||
IndexPolicy IndexPolicy
|
||||
CreatedAt uint32
|
||||
StartIndexPtr uint32
|
||||
EndIndexPtr uint32
|
||||
}
|
||||
|
||||
func NewHeader(input []byte) (*Header, error) {
|
||||
if len(input) < 16 {
|
||||
return nil, fmt.Errorf("invalid input buffer")
|
||||
}
|
||||
|
||||
return &Header{
|
||||
Version: binary.LittleEndian.Uint16(input),
|
||||
IndexPolicy: IndexPolicy(binary.LittleEndian.Uint16(input[2:])),
|
||||
CreatedAt: binary.LittleEndian.Uint32(input[4:]),
|
||||
StartIndexPtr: binary.LittleEndian.Uint32(input[8:]),
|
||||
EndIndexPtr: binary.LittleEndian.Uint32(input[12:]),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// --- searcher implementation
|
||||
|
||||
type Searcher struct {
|
||||
handle *os.File
|
||||
|
||||
// header info
|
||||
header *Header
|
||||
ioCount int
|
||||
|
||||
// use it only when this feature enabled.
|
||||
// Preload the vector index will reduce the number of IO operations
|
||||
// thus speedup the search process
|
||||
vectorIndex []byte
|
||||
|
||||
// content buffer.
|
||||
// running with the whole xdb file cached
|
||||
contentBuff []byte
|
||||
}
|
||||
|
||||
func baseNew(dbFile string, vIndex []byte, cBuff []byte) (*Searcher, error) {
|
||||
var err error
|
||||
|
||||
// content buff first
|
||||
if cBuff != nil {
|
||||
return &Searcher{
|
||||
vectorIndex: nil,
|
||||
contentBuff: cBuff,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// open the xdb binary file
|
||||
handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Searcher{
|
||||
handle: handle,
|
||||
vectorIndex: vIndex,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewWithFileOnly(dbFile string) (*Searcher, error) {
|
||||
return baseNew(dbFile, nil, nil)
|
||||
}
|
||||
|
||||
func NewWithVectorIndex(dbFile string, vIndex []byte) (*Searcher, error) {
|
||||
return baseNew(dbFile, vIndex, nil)
|
||||
}
|
||||
|
||||
func NewWithBuffer(cBuff []byte) (*Searcher, error) {
|
||||
return baseNew("", nil, cBuff)
|
||||
}
|
||||
|
||||
func (s *Searcher) Close() {
|
||||
if s.handle != nil {
|
||||
err := s.handle.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetIOCount return the global io count for the last search
|
||||
func (s *Searcher) GetIOCount() int {
|
||||
return s.ioCount
|
||||
}
|
||||
|
||||
// SearchByStr find the region for the specified ip string
|
||||
func (s *Searcher) SearchByStr(str string) (string, error) {
|
||||
ip, err := CheckIP(str)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return s.Search(ip)
|
||||
}
|
||||
|
||||
// Search find the region for the specified long ip
|
||||
func (s *Searcher) Search(ip uint32) (string, error) {
|
||||
// reset the global ioCount
|
||||
s.ioCount = 0
|
||||
|
||||
// locate the segment index block based on the vector index
|
||||
var il0 = (ip >> 24) & 0xFF
|
||||
var il1 = (ip >> 16) & 0xFF
|
||||
var idx = il0*VectorIndexCols*VectorIndexSize + il1*VectorIndexSize
|
||||
var sPtr, ePtr = uint32(0), uint32(0)
|
||||
if s.vectorIndex != nil {
|
||||
sPtr = binary.LittleEndian.Uint32(s.vectorIndex[idx:])
|
||||
ePtr = binary.LittleEndian.Uint32(s.vectorIndex[idx+4:])
|
||||
} else if s.contentBuff != nil {
|
||||
sPtr = binary.LittleEndian.Uint32(s.contentBuff[HeaderInfoLength+idx:])
|
||||
ePtr = binary.LittleEndian.Uint32(s.contentBuff[HeaderInfoLength+idx+4:])
|
||||
} else {
|
||||
// read the vector index block
|
||||
var buff = make([]byte, VectorIndexSize)
|
||||
err := s.read(int64(HeaderInfoLength+idx), buff)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read vector index block at %d: %w", HeaderInfoLength+idx, err)
|
||||
}
|
||||
|
||||
sPtr = binary.LittleEndian.Uint32(buff)
|
||||
ePtr = binary.LittleEndian.Uint32(buff[4:])
|
||||
}
|
||||
|
||||
// fmt.Printf("sPtr=%d, ePtr=%d", sPtr, ePtr)
|
||||
|
||||
// binary search the segment index to get the region
|
||||
var dataLen, dataPtr = 0, uint32(0)
|
||||
var buff = make([]byte, SegmentIndexBlockSize)
|
||||
var l, h = 0, int((ePtr - sPtr) / SegmentIndexBlockSize)
|
||||
for l <= h {
|
||||
m := (l + h) >> 1
|
||||
p := sPtr + uint32(m*SegmentIndexBlockSize)
|
||||
err := s.read(int64(p), buff)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read segment index at %d: %w", p, err)
|
||||
}
|
||||
|
||||
// decode the data step by step to reduce the unnecessary operations
|
||||
sip := binary.LittleEndian.Uint32(buff)
|
||||
if ip < sip {
|
||||
h = m - 1
|
||||
} else {
|
||||
eip := binary.LittleEndian.Uint32(buff[4:])
|
||||
if ip > eip {
|
||||
l = m + 1
|
||||
} else {
|
||||
dataLen = int(binary.LittleEndian.Uint16(buff[8:]))
|
||||
dataPtr = binary.LittleEndian.Uint32(buff[10:])
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//fmt.Printf("dataLen: %d, dataPtr: %d", dataLen, dataPtr)
|
||||
if dataLen == 0 {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// load and return the region data
|
||||
var regionBuff = make([]byte, dataLen)
|
||||
err := s.read(int64(dataPtr), regionBuff)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read region at %d: %w", dataPtr, err)
|
||||
}
|
||||
|
||||
return string(regionBuff), nil
|
||||
}
|
||||
|
||||
// do the data read operation based on the setting.
|
||||
// content buffer first or will read from the file.
|
||||
// this operation will invoke the Seek for file based read.
|
||||
func (s *Searcher) read(offset int64, buff []byte) error {
|
||||
if s.contentBuff != nil {
|
||||
cLen := copy(buff, s.contentBuff[offset:])
|
||||
if cLen != len(buff) {
|
||||
return fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
|
||||
}
|
||||
} else {
|
||||
_, err := s.handle.Seek(offset, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("seek to %d: %w", offset, err)
|
||||
}
|
||||
|
||||
s.ioCount++
|
||||
rLen, err := s.handle.Read(buff)
|
||||
if err != nil {
|
||||
return fmt.Errorf("handle read: %w", err)
|
||||
}
|
||||
|
||||
if rLen != len(buff) {
|
||||
return fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -0,0 +1,165 @@
|
||||
package ip2region_v2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var shiftIndex = []int{24, 16, 8, 0}
|
||||
|
||||
func CheckIP(ip string) (uint32, error) {
|
||||
var ps = strings.Split(ip, ".")
|
||||
if len(ps) != 4 {
|
||||
return 0, fmt.Errorf("invalid ip address `%s`", ip)
|
||||
}
|
||||
|
||||
var val = uint32(0)
|
||||
for i, s := range ps {
|
||||
d, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("the %dth part `%s` is not an integer", i, s)
|
||||
}
|
||||
|
||||
if d < 0 || d > 255 {
|
||||
return 0, fmt.Errorf("the %dth part `%s` should be an integer bettween 0 and 255", i, s)
|
||||
}
|
||||
|
||||
val |= uint32(d) << shiftIndex[i]
|
||||
}
|
||||
|
||||
// convert the ip to integer
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func Long2IP(ip uint32) string {
|
||||
return fmt.Sprintf("%d.%d.%d.%d", (ip>>24)&0xFF, (ip>>16)&0xFF, (ip>>8)&0xFF, ip&0xFF)
|
||||
}
|
||||
|
||||
func MidIP(sip uint32, eip uint32) uint32 {
|
||||
return uint32((uint64(sip) + uint64(eip)) >> 1)
|
||||
}
|
||||
|
||||
// LoadHeader load the header info from the specified handle
|
||||
func LoadHeader(handle *os.File) (*Header, error) {
|
||||
_, err := handle.Seek(0, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("seek to the header: %w", err)
|
||||
}
|
||||
|
||||
var buff = make([]byte, HeaderInfoLength)
|
||||
rLen, err := handle.Read(buff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if rLen != len(buff) {
|
||||
return nil, fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
|
||||
}
|
||||
|
||||
return NewHeader(buff)
|
||||
}
|
||||
|
||||
// LoadHeaderFromFile load header info from the specified db file path
|
||||
func LoadHeaderFromFile(dbFile string) (*Header, error) {
|
||||
handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open xdb file `%s`: %w", dbFile, err)
|
||||
}
|
||||
|
||||
header, err := LoadHeader(handle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_ = handle.Close()
|
||||
return header, nil
|
||||
}
|
||||
|
||||
// LoadHeaderFromBuff wrap the header info from the content buffer
|
||||
func LoadHeaderFromBuff(cBuff []byte) (*Header, error) {
|
||||
return NewHeader(cBuff[0:256])
|
||||
}
|
||||
|
||||
// LoadVectorIndex util function to load the vector index from the specified file handle
|
||||
func LoadVectorIndex(handle *os.File) ([]byte, error) {
|
||||
// load all the vector index block
|
||||
_, err := handle.Seek(HeaderInfoLength, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("seek to vector index: %w", err)
|
||||
}
|
||||
|
||||
var buff = make([]byte, VectorIndexRows*VectorIndexCols*VectorIndexSize)
|
||||
rLen, err := handle.Read(buff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if rLen != len(buff) {
|
||||
return nil, fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
|
||||
}
|
||||
|
||||
return buff, nil
|
||||
}
|
||||
|
||||
// LoadVectorIndexFromFile load vector index from a specified file path
|
||||
func LoadVectorIndexFromFile(dbFile string) ([]byte, error) {
|
||||
handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open xdb file `%s`: %w", dbFile, err)
|
||||
}
|
||||
|
||||
vIndex, err := LoadVectorIndex(handle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_ = handle.Close()
|
||||
return vIndex, nil
|
||||
}
|
||||
|
||||
// LoadContent load the whole xdb content from the specified file handle
|
||||
func LoadContent(handle *os.File) ([]byte, error) {
|
||||
// get file size
|
||||
fi, err := handle.Stat()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("stat: %w", err)
|
||||
}
|
||||
|
||||
size := fi.Size()
|
||||
|
||||
// seek to the head of the file
|
||||
_, err = handle.Seek(0, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("seek to get xdb file length: %w", err)
|
||||
}
|
||||
|
||||
var buff = make([]byte, size)
|
||||
rLen, err := handle.Read(buff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if rLen != len(buff) {
|
||||
return nil, fmt.Errorf("incomplete read: readed bytes should be %d", len(buff))
|
||||
}
|
||||
|
||||
return buff, nil
|
||||
}
|
||||
|
||||
// LoadContentFromFile load the whole xdb content from the specified db file path
|
||||
func LoadContentFromFile(dbFile string) ([]byte, error) {
|
||||
handle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open xdb file `%s`: %w", dbFile, err)
|
||||
}
|
||||
|
||||
cBuff, err := LoadContent(handle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_ = handle.Close()
|
||||
return cBuff, nil
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
package ipv6wry
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
)
|
||||
|
||||
var (
|
||||
header []byte
|
||||
country []byte
|
||||
area []byte
|
||||
v6ip uint64
|
||||
offset uint32
|
||||
start uint32
|
||||
end uint32
|
||||
)
|
||||
|
||||
//go:embed ipv6wry.db
|
||||
var datBuff []byte
|
||||
|
||||
type Client struct {
|
||||
Offset uint32
|
||||
ItemLen uint32
|
||||
IndexLen uint32
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
|
||||
c := &Client{}
|
||||
|
||||
buf := datBuff[0:8]
|
||||
start := binary.LittleEndian.Uint32(buf[:4])
|
||||
end := binary.LittleEndian.Uint32(buf[4:])
|
||||
|
||||
num := int64((end-start)/7 + 1)
|
||||
log.Printf("ipv6wry.db 共加载:%d 条ip记录\n", num)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// ReadData 从文件中读取数据
|
||||
func (c *Client) readData(length uint32) (rs []byte) {
|
||||
end := c.Offset + length
|
||||
dataNum := uint32(len(datBuff))
|
||||
if c.Offset > dataNum {
|
||||
return nil
|
||||
}
|
||||
|
||||
if end > dataNum {
|
||||
end = dataNum
|
||||
}
|
||||
rs = datBuff[c.Offset:end]
|
||||
c.Offset = end
|
||||
return rs
|
||||
}
|
||||
|
||||
func (c *Client) getAddr() ([]byte, []byte) {
|
||||
mode := c.readData(1)[0]
|
||||
if mode == 0x01 {
|
||||
// [IP][0x01][国家和地区信息的绝对偏移地址]
|
||||
c.Offset = byteToUInt32(c.readData(3))
|
||||
return c.getAddr()
|
||||
}
|
||||
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
|
||||
_offset := c.Offset - 1
|
||||
c1 := c.readArea(_offset)
|
||||
if mode == 0x02 {
|
||||
c.Offset = 4 + _offset
|
||||
} else {
|
||||
c.Offset = _offset + uint32(1+len(c1))
|
||||
}
|
||||
c2 := c.readArea(c.Offset)
|
||||
return c1, c2
|
||||
}
|
||||
|
||||
func (c *Client) readArea(offset uint32) []byte {
|
||||
c.Offset = offset
|
||||
mode := c.readData(1)[0]
|
||||
if mode == 0x01 || mode == 0x02 {
|
||||
return c.readArea(byteToUInt32(c.readData(3)))
|
||||
}
|
||||
c.Offset = offset
|
||||
return c.readString()
|
||||
}
|
||||
|
||||
func (c *Client) readString() []byte {
|
||||
data := make([]byte, 0)
|
||||
for {
|
||||
buf := c.readData(1)
|
||||
if buf[0] == 0 {
|
||||
break
|
||||
}
|
||||
data = append(data, buf[0])
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func (c *Client) searchIndex(ip uint64) uint32 {
|
||||
|
||||
c.ItemLen = 8
|
||||
c.IndexLen = 11
|
||||
|
||||
header = datBuff[8:24]
|
||||
start = binary.LittleEndian.Uint32(header[8:])
|
||||
counts := binary.LittleEndian.Uint32(header[:8])
|
||||
end = start + counts*c.IndexLen
|
||||
|
||||
buf := make([]byte, c.IndexLen)
|
||||
|
||||
for {
|
||||
mid := start + c.IndexLen*(((end-start)/c.IndexLen)>>1)
|
||||
buf = datBuff[mid : mid+c.IndexLen]
|
||||
_ip := binary.LittleEndian.Uint64(buf[:c.ItemLen])
|
||||
|
||||
if end-start == c.IndexLen {
|
||||
if ip >= binary.LittleEndian.Uint64(datBuff[end:end+c.ItemLen]) {
|
||||
buf = datBuff[end : end+c.IndexLen]
|
||||
}
|
||||
return byteToUInt32(buf[c.ItemLen:])
|
||||
}
|
||||
|
||||
if _ip > ip {
|
||||
end = mid
|
||||
} else if _ip < ip {
|
||||
start = mid
|
||||
} else if _ip == ip {
|
||||
return byteToUInt32(buf[c.ItemLen:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func byteToUInt32(data []byte) uint32 {
|
||||
i := uint32(data[0]) & 0xff
|
||||
i |= (uint32(data[1]) << 8) & 0xff00
|
||||
i |= (uint32(data[2]) << 16) & 0xff0000
|
||||
return i
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package ipv6wry
|
||||
|
||||
import (
|
||||
"go.dtapp.net/gostring"
|
||||
"math/big"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// QueryResult 返回
|
||||
type QueryResult struct {
|
||||
Ip string `json:"ip,omitempty"` // ip
|
||||
Country string `json:"country,omitempty"` // 国家
|
||||
Province string `json:"province,omitempty"` // 省份
|
||||
City string `json:"city,omitempty"` // 城市
|
||||
Area string `json:"area,omitempty"` // 区域
|
||||
Isp string `json:"isp,omitempty"` // 运营商
|
||||
}
|
||||
|
||||
// Query ip地址查询对应归属地信息
|
||||
func (c *Client) Query(ipAddress net.IP) (result QueryResult) {
|
||||
|
||||
result.Ip = ipAddress.String()
|
||||
|
||||
c.Offset = 0
|
||||
|
||||
tp := big.NewInt(0)
|
||||
op := big.NewInt(0)
|
||||
tp.SetBytes(ipAddress.To16())
|
||||
op.SetString("18446744073709551616", 10)
|
||||
op.Div(tp, op)
|
||||
tp.SetString("FFFFFFFFFFFFFFFF", 16)
|
||||
op.And(op, tp)
|
||||
|
||||
v6ip = op.Uint64()
|
||||
offset = c.searchIndex(v6ip)
|
||||
c.Offset = offset
|
||||
|
||||
country, area = c.getAddr()
|
||||
|
||||
// 解析地区数据
|
||||
info := strings.Split(string(country), "\t")
|
||||
if len(info) > 0 {
|
||||
i := 1
|
||||
for {
|
||||
if i > len(info) {
|
||||
break
|
||||
}
|
||||
switch i {
|
||||
case 1:
|
||||
result.Country = info[i-1]
|
||||
result.Country = gostring.SpaceAndLineBreak(result.Country)
|
||||
case 2:
|
||||
result.Province = info[i-1]
|
||||
result.Province = gostring.SpaceAndLineBreak(result.Province)
|
||||
case 3:
|
||||
result.City = info[i-1]
|
||||
result.City = gostring.SpaceAndLineBreak(result.City)
|
||||
case 4:
|
||||
result.Area = info[i-1]
|
||||
result.Area = gostring.SpaceAndLineBreak(result.Area)
|
||||
}
|
||||
i++ // 自增
|
||||
}
|
||||
} else {
|
||||
result.Country = string(country)
|
||||
result.Country = gostring.SpaceAndLineBreak(result.Country)
|
||||
}
|
||||
// 运营商
|
||||
result.Isp = string(area)
|
||||
|
||||
// Delete ZX (防止不相关的信息产生干扰)
|
||||
if result.Isp == "ZX" || result.Isp == "" {
|
||||
result.Isp = ""
|
||||
} else {
|
||||
result.Isp = " " + result.Isp
|
||||
}
|
||||
|
||||
result.Isp = gostring.SpaceAndLineBreak(result.Isp)
|
||||
|
||||
return result
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package goip
|
||||
|
||||
import (
|
||||
"go.dtapp.net/gostring"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ipv4 = "IPV4"
|
||||
ipv6 = "IPV6"
|
||||
)
|
||||
|
||||
func (c *Client) isIpv4OrIpv6(ip string) string {
|
||||
if len(ip) < 7 {
|
||||
return ""
|
||||
}
|
||||
arrIpv4 := strings.Split(ip, ".")
|
||||
if len(arrIpv4) == 4 {
|
||||
//. 判断IPv4
|
||||
for _, val := range arrIpv4 {
|
||||
if !c.CheckIpv4(val) {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return ipv4
|
||||
}
|
||||
arrIpv6 := strings.Split(ip, ":")
|
||||
if len(arrIpv6) == 8 {
|
||||
// 判断Ipv6
|
||||
for _, val := range arrIpv6 {
|
||||
if !c.CheckIpv6(val) {
|
||||
return "Neither"
|
||||
}
|
||||
}
|
||||
return ipv6
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// IsIp 是否ip
|
||||
func IsIp(ipStr string) string {
|
||||
|
||||
if ipStr == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// ipv4
|
||||
if gostring.Contains(ipStr, "/32") {
|
||||
cidr, _, _ := net.ParseCIDR(ipStr)
|
||||
if cidr != nil {
|
||||
return cidr.String()
|
||||
}
|
||||
}
|
||||
|
||||
// ipv6
|
||||
if gostring.Contains(ipStr, "/128") {
|
||||
cidr, _, _ := net.ParseCIDR(ipStr)
|
||||
if cidr != nil {
|
||||
return cidr.String()
|
||||
}
|
||||
}
|
||||
|
||||
// 解析
|
||||
result := net.ParseIP(ipStr).String()
|
||||
if result != "<nil>" {
|
||||
return result
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// IsIpConsistent 两个ip是否一致
|
||||
func IsIpConsistent(ipStr1, ipStr2 string) bool {
|
||||
|
||||
ip1Result := IsIp(ipStr1)
|
||||
ip2Result := IsIp(ipStr2)
|
||||
|
||||
if ip1Result != "" && ip2Result != "" {
|
||||
if ip1Result == ip2Result {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
package qqwry
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
)
|
||||
|
||||
var (
|
||||
header []byte
|
||||
country []byte
|
||||
area []byte
|
||||
offset uint32
|
||||
start uint32
|
||||
end uint32
|
||||
)
|
||||
|
||||
//go:embed qqwry.dat
|
||||
var datBuff []byte
|
||||
|
||||
type Client struct {
|
||||
Offset uint32
|
||||
ItemLen uint32
|
||||
IndexLen uint32
|
||||
}
|
||||
|
||||
func New() *Client {
|
||||
|
||||
c := &Client{}
|
||||
|
||||
buf := datBuff[0:8]
|
||||
start := binary.LittleEndian.Uint32(buf[:4])
|
||||
end := binary.LittleEndian.Uint32(buf[4:])
|
||||
|
||||
num := int64((end-start)/7 + 1)
|
||||
log.Printf("qqwry.dat 共加载:%d 条ip记录\n", num)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// ReadData 从文件中读取数据
|
||||
func (c *Client) readData(length uint32) (rs []byte) {
|
||||
end := c.Offset + length
|
||||
dataNum := uint32(len(datBuff))
|
||||
if c.Offset > dataNum {
|
||||
return nil
|
||||
}
|
||||
|
||||
if end > dataNum {
|
||||
end = dataNum
|
||||
}
|
||||
rs = datBuff[c.Offset:end]
|
||||
c.Offset = end
|
||||
return rs
|
||||
}
|
||||
|
||||
// 获取地址信息
|
||||
func (c *Client) getAddr() ([]byte, []byte) {
|
||||
mode := c.readData(1)[0]
|
||||
if mode == 0x01 {
|
||||
// [IP][0x01][国家和地区信息的绝对偏移地址]
|
||||
c.Offset = byteToUInt32(c.readData(3))
|
||||
return c.getAddr()
|
||||
}
|
||||
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
|
||||
_offset := c.Offset - 1
|
||||
c1 := c.readArea(_offset)
|
||||
if mode == 0x02 {
|
||||
c.Offset = 4 + _offset
|
||||
} else {
|
||||
c.Offset = _offset + uint32(1+len(c1))
|
||||
}
|
||||
c2 := c.readArea(c.Offset)
|
||||
return c1, c2
|
||||
}
|
||||
|
||||
// 读取区
|
||||
func (c *Client) readArea(offset uint32) []byte {
|
||||
c.Offset = offset
|
||||
mode := c.readData(1)[0]
|
||||
if mode == 0x01 || mode == 0x02 {
|
||||
return c.readArea(byteToUInt32(c.readData(3)))
|
||||
}
|
||||
c.Offset = offset
|
||||
return c.readString()
|
||||
}
|
||||
|
||||
// 读取字符串
|
||||
func (c *Client) readString() []byte {
|
||||
data := make([]byte, 0)
|
||||
for {
|
||||
buf := c.readData(1)
|
||||
if buf[0] == 0 {
|
||||
break
|
||||
}
|
||||
data = append(data, buf[0])
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// 搜索索引
|
||||
func (c *Client) searchIndex(ip uint32) uint32 {
|
||||
c.ItemLen = 4
|
||||
c.IndexLen = 7
|
||||
header = datBuff[0:8]
|
||||
start = binary.LittleEndian.Uint32(header[:4])
|
||||
end = binary.LittleEndian.Uint32(header[4:])
|
||||
|
||||
buf := make([]byte, c.IndexLen)
|
||||
|
||||
for {
|
||||
mid := start + c.IndexLen*(((end-start)/c.IndexLen)>>1)
|
||||
buf = datBuff[mid : mid+c.IndexLen]
|
||||
_ip := binary.LittleEndian.Uint32(buf[:c.ItemLen])
|
||||
|
||||
if end-start == c.IndexLen {
|
||||
if ip >= binary.LittleEndian.Uint32(datBuff[end:end+c.ItemLen]) {
|
||||
buf = datBuff[end : end+c.IndexLen]
|
||||
}
|
||||
return byteToUInt32(buf[c.ItemLen:])
|
||||
}
|
||||
|
||||
if _ip > ip {
|
||||
end = mid
|
||||
} else if _ip < ip {
|
||||
start = mid
|
||||
} else if _ip == ip {
|
||||
return byteToUInt32(buf[c.ItemLen:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 字节转UInt32
|
||||
func byteToUInt32(data []byte) uint32 {
|
||||
i := uint32(data[0]) & 0xff
|
||||
i |= (uint32(data[1]) << 8) & 0xff00
|
||||
i |= (uint32(data[2]) << 16) & 0xff0000
|
||||
return i
|
||||
}
|
Binary file not shown.
@ -0,0 +1,53 @@
|
||||
package qqwry
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"go.dtapp.net/gostring"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"net"
|
||||
)
|
||||
|
||||
// QueryResult 返回
|
||||
type QueryResult struct {
|
||||
Ip string `json:"ip,omitempty"` // ip
|
||||
Country string `json:"country,omitempty"` // 国家或地区
|
||||
Area string `json:"area,omitempty"` // 区域
|
||||
}
|
||||
|
||||
// Query ip地址查询对应归属地信息
|
||||
func (c *Client) Query(ipAddress net.IP) (result QueryResult, err error) {
|
||||
|
||||
c.Offset = 0
|
||||
|
||||
// 偏移
|
||||
offset = c.searchIndex(binary.BigEndian.Uint32(ipAddress.To4()))
|
||||
if offset <= 0 {
|
||||
return QueryResult{}, errors.New("搜索失败")
|
||||
}
|
||||
|
||||
result.Ip = ipAddress.String()
|
||||
|
||||
c.Offset = offset + c.ItemLen
|
||||
|
||||
country, area = c.getAddr()
|
||||
|
||||
enc := simplifiedchinese.GBK.NewDecoder()
|
||||
|
||||
result.Country, _ = enc.String(string(country))
|
||||
|
||||
result.Country = gostring.SpaceAndLineBreak(result.Country)
|
||||
|
||||
result.Area, _ = enc.String(string(area))
|
||||
|
||||
// Delete CZ88.NET (防止不相关的信息产生干扰)
|
||||
if result.Area == " CZ88.NET" || result.Area == "" {
|
||||
result.Area = ""
|
||||
} else {
|
||||
result.Area = " " + result.Area
|
||||
}
|
||||
|
||||
result.Area = gostring.SpaceAndLineBreak(result.Area)
|
||||
|
||||
return result, nil
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package goip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go.dtapp.net/goip/geoip"
|
||||
"go.dtapp.net/goip/ip2region"
|
||||
"go.dtapp.net/goip/ip2region_v2"
|
||||
"go.dtapp.net/goip/ipv6wry"
|
||||
"go.dtapp.net/goip/qqwry"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
QueryIncorrect = errors.New("ip地址不正确")
|
||||
)
|
||||
|
||||
// QueryQqWry 纯真IP库
|
||||
// https://www.cz88.net/
|
||||
func (c *Client) QueryQqWry(ipAddress net.IP) (result qqwry.QueryResult, err error) {
|
||||
if ipAddress.To4() == nil {
|
||||
return result, QueryIncorrect
|
||||
}
|
||||
|
||||
query, err := c.qqwryClient.Query(ipAddress)
|
||||
if err != nil {
|
||||
return qqwry.QueryResult{}, err
|
||||
}
|
||||
|
||||
return query, err
|
||||
}
|
||||
|
||||
// QueryIp2Region ip2region
|
||||
// https://github.com/lionsoul2014/ip2region
|
||||
func (c *Client) QueryIp2Region(ipAddress net.IP) (result ip2region.QueryResult, err error) {
|
||||
if ipAddress.To4() == nil {
|
||||
return result, QueryIncorrect
|
||||
}
|
||||
|
||||
query, err := c.ip2regionClient.Query(ipAddress)
|
||||
if err != nil {
|
||||
return ip2region.QueryResult{}, err
|
||||
}
|
||||
|
||||
return query, err
|
||||
}
|
||||
|
||||
// QueryIp2RegionV2 ip2region
|
||||
// https://github.com/lionsoul2014/ip2region
|
||||
func (c *Client) QueryIp2RegionV2(ipAddress net.IP) (result ip2region_v2.QueryResult, err error) {
|
||||
if ipAddress.To4() == nil {
|
||||
return result, QueryIncorrect
|
||||
}
|
||||
|
||||
query, err := c.ip2regionV2Client.Query(ipAddress)
|
||||
if err != nil {
|
||||
return ip2region_v2.QueryResult{}, 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
|
||||
}
|
||||
|
||||
// QueryIpv6wry ip2region
|
||||
// https://ip.zxinc.org
|
||||
func (c *Client) QueryIpv6wry(ipAddress net.IP) (result ipv6wry.QueryResult, err error) {
|
||||
if ipAddress.To16() == nil {
|
||||
return result, QueryIncorrect
|
||||
}
|
||||
|
||||
query := c.ipv6wryClient.Query(ipAddress)
|
||||
|
||||
return query, nil
|
||||
}
|
@ -1,201 +0,0 @@
|
||||
package v4
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"go.dtapp.net/gostring"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
header []byte
|
||||
country []byte
|
||||
area []byte
|
||||
offset uint32
|
||||
start uint32
|
||||
end uint32
|
||||
)
|
||||
|
||||
//go:embed qqwry.dat
|
||||
var dat []byte
|
||||
|
||||
type Pointer struct {
|
||||
Offset uint32
|
||||
ItemLen uint32
|
||||
IndexLen uint32
|
||||
}
|
||||
|
||||
// Result 返回
|
||||
type Result struct {
|
||||
IP string `json:"ip,omitempty"` // 输入的ip地址
|
||||
Country string `json:"country,omitempty"` // 国家或地区
|
||||
Area string `json:"area,omitempty"` // 区域
|
||||
}
|
||||
|
||||
// InitIPV4Data 加载
|
||||
func (q *Pointer) InitIPV4Data() int64 {
|
||||
buf := dat[0:8]
|
||||
start := binary.LittleEndian.Uint32(buf[:4])
|
||||
end := binary.LittleEndian.Uint32(buf[4:])
|
||||
|
||||
return int64((end-start)/7 + 1)
|
||||
}
|
||||
|
||||
// ReadData 从文件中读取数据
|
||||
func (q *Pointer) readData(length uint32) (rs []byte) {
|
||||
end := q.Offset + length
|
||||
dataNum := uint32(len(dat))
|
||||
if q.Offset > dataNum {
|
||||
return nil
|
||||
}
|
||||
|
||||
if end > dataNum {
|
||||
end = dataNum
|
||||
}
|
||||
rs = dat[q.Offset:end]
|
||||
q.Offset = end
|
||||
return rs
|
||||
}
|
||||
|
||||
// Find ip地址查询对应归属地信息
|
||||
func (q *Pointer) Find(ip string) (res Result) {
|
||||
|
||||
// 赋值
|
||||
res.IP = ip
|
||||
if net.ParseIP("61.241.55.180").To4() == nil {
|
||||
// 不是ip地址
|
||||
return res
|
||||
}
|
||||
|
||||
q.Offset = 0
|
||||
|
||||
// 偏移
|
||||
offset = q.searchIndex(binary.BigEndian.Uint32(net.ParseIP(ip).To4()))
|
||||
if offset <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
q.Offset = offset + q.ItemLen
|
||||
|
||||
country, area = q.getAddr()
|
||||
|
||||
enc := simplifiedchinese.GBK.NewDecoder()
|
||||
|
||||
res.Country, _ = enc.String(string(country))
|
||||
res.Country = gostring.SpaceAndLineBreak(res.Country)
|
||||
|
||||
res.Area, _ = enc.String(string(area))
|
||||
|
||||
// Delete CZ88.NET (防止不相关的信息产生干扰)
|
||||
if res.Area == " CZ88.NET" || res.Area == "" {
|
||||
res.Area = ""
|
||||
} else {
|
||||
res.Area = " " + res.Area
|
||||
}
|
||||
|
||||
res.Area = gostring.SpaceAndLineBreak(res.Area)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 获取地址信息
|
||||
func (q *Pointer) getAddr() ([]byte, []byte) {
|
||||
mode := q.readData(1)[0]
|
||||
if mode == 0x01 {
|
||||
// [IP][0x01][国家和地区信息的绝对偏移地址]
|
||||
q.Offset = byteToUInt32(q.readData(3))
|
||||
return q.getAddr()
|
||||
}
|
||||
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
|
||||
_offset := q.Offset - 1
|
||||
c1 := q.readArea(_offset)
|
||||
if mode == 0x02 {
|
||||
q.Offset = 4 + _offset
|
||||
} else {
|
||||
q.Offset = _offset + uint32(1+len(c1))
|
||||
}
|
||||
c2 := q.readArea(q.Offset)
|
||||
return c1, c2
|
||||
}
|
||||
|
||||
// 读取区
|
||||
func (q *Pointer) readArea(offset uint32) []byte {
|
||||
q.Offset = offset
|
||||
mode := q.readData(1)[0]
|
||||
if mode == 0x01 || mode == 0x02 {
|
||||
return q.readArea(byteToUInt32(q.readData(3)))
|
||||
}
|
||||
q.Offset = offset
|
||||
return q.readString()
|
||||
}
|
||||
|
||||
// 读取字符串
|
||||
func (q *Pointer) readString() []byte {
|
||||
data := make([]byte, 0)
|
||||
for {
|
||||
buf := q.readData(1)
|
||||
if buf[0] == 0 {
|
||||
break
|
||||
}
|
||||
data = append(data, buf[0])
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// 搜索索引
|
||||
func (q *Pointer) searchIndex(ip uint32) uint32 {
|
||||
q.ItemLen = 4
|
||||
q.IndexLen = 7
|
||||
header = dat[0:8]
|
||||
start = binary.LittleEndian.Uint32(header[:4])
|
||||
end = binary.LittleEndian.Uint32(header[4:])
|
||||
|
||||
buf := make([]byte, q.IndexLen)
|
||||
|
||||
for {
|
||||
mid := start + q.IndexLen*(((end-start)/q.IndexLen)>>1)
|
||||
buf = dat[mid : mid+q.IndexLen]
|
||||
_ip := binary.LittleEndian.Uint32(buf[:q.ItemLen])
|
||||
|
||||
if end-start == q.IndexLen {
|
||||
if ip >= binary.LittleEndian.Uint32(dat[end:end+q.ItemLen]) {
|
||||
buf = dat[end : end+q.IndexLen]
|
||||
}
|
||||
return byteToUInt32(buf[q.ItemLen:])
|
||||
}
|
||||
|
||||
if _ip > ip {
|
||||
end = mid
|
||||
} else if _ip < ip {
|
||||
start = mid
|
||||
} else if _ip == ip {
|
||||
return byteToUInt32(buf[q.ItemLen:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 字节转UInt32
|
||||
func byteToUInt32(data []byte) uint32 {
|
||||
i := uint32(data[0]) & 0xff
|
||||
i |= (uint32(data[1]) << 8) & 0xff00
|
||||
i |= (uint32(data[2]) << 16) & 0xff0000
|
||||
return i
|
||||
}
|
||||
|
||||
// OnlineDownload 在线下载
|
||||
func (q *Pointer) OnlineDownload() (err error) {
|
||||
tmpData, err := getOnline()
|
||||
if err != nil {
|
||||
return errors.New("下载失败")
|
||||
}
|
||||
if err := ioutil.WriteFile("./qqwry.dat", tmpData, 0644); err == nil {
|
||||
log.Printf("已下载最新 纯真 IPv4数据库 %s ", "./qqwry.dat")
|
||||
} else {
|
||||
return errors.New("保存失败")
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,226 +0,0 @@
|
||||
package v6
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"go.dtapp.net/gostring"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/big"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
header []byte
|
||||
country []byte
|
||||
area []byte
|
||||
v6ip uint64
|
||||
offset uint32
|
||||
start uint32
|
||||
end uint32
|
||||
)
|
||||
|
||||
type Result struct {
|
||||
IP string `json:"ip,omitempty"` // 输入的ip地址
|
||||
Country string `json:"country,omitempty"` // 国家
|
||||
Province string `json:"province,omitempty"` // 省份
|
||||
City string `json:"city,omitempty"` // 城市
|
||||
Area string `json:"area,omitempty"` // 区域
|
||||
Isp string `json:"isp,omitempty"` // 运营商
|
||||
}
|
||||
|
||||
//go:embed ipv6wry.db
|
||||
var dat []byte
|
||||
|
||||
type Pointer struct {
|
||||
Offset uint32
|
||||
ItemLen uint32
|
||||
IndexLen uint32
|
||||
}
|
||||
|
||||
// InitIPV4Data 加载
|
||||
func (q *Pointer) InitIPV4Data() int64 {
|
||||
buf := dat[0:8]
|
||||
start := binary.LittleEndian.Uint32(buf[:4])
|
||||
end := binary.LittleEndian.Uint32(buf[4:])
|
||||
|
||||
return int64((end-start)/7 + 1)
|
||||
}
|
||||
|
||||
// ReadData 从文件中读取数据
|
||||
func (q *Pointer) readData(length uint32) (rs []byte) {
|
||||
end := q.Offset + length
|
||||
dataNum := uint32(len(dat))
|
||||
if q.Offset > dataNum {
|
||||
return nil
|
||||
}
|
||||
|
||||
if end > dataNum {
|
||||
end = dataNum
|
||||
}
|
||||
rs = dat[q.Offset:end]
|
||||
q.Offset = end
|
||||
return rs
|
||||
}
|
||||
|
||||
// Find ip地址查询对应归属地信息
|
||||
func (q *Pointer) Find(ip string) (res Result) {
|
||||
|
||||
res = Result{}
|
||||
res.IP = ip
|
||||
q.Offset = 0
|
||||
|
||||
tp := big.NewInt(0)
|
||||
op := big.NewInt(0)
|
||||
tp.SetBytes(net.ParseIP(ip).To16())
|
||||
op.SetString("18446744073709551616", 10)
|
||||
op.Div(tp, op)
|
||||
tp.SetString("FFFFFFFFFFFFFFFF", 16)
|
||||
op.And(op, tp)
|
||||
|
||||
v6ip = op.Uint64()
|
||||
offset = q.searchIndex(v6ip)
|
||||
q.Offset = offset
|
||||
|
||||
country, area = q.getAddr()
|
||||
|
||||
// 解析地区数据
|
||||
info := strings.Split(string(country), "\t")
|
||||
if len(info) > 0 {
|
||||
i := 1
|
||||
for {
|
||||
if i > len(info) {
|
||||
break
|
||||
}
|
||||
switch i {
|
||||
case 1:
|
||||
res.Country = info[i-1]
|
||||
res.Country = gostring.SpaceAndLineBreak(res.Country)
|
||||
case 2:
|
||||
res.Province = info[i-1]
|
||||
res.Province = gostring.SpaceAndLineBreak(res.Province)
|
||||
case 3:
|
||||
res.City = info[i-1]
|
||||
res.City = gostring.SpaceAndLineBreak(res.City)
|
||||
case 4:
|
||||
res.Area = info[i-1]
|
||||
res.Area = gostring.SpaceAndLineBreak(res.Area)
|
||||
}
|
||||
i++ // 自增
|
||||
}
|
||||
} else {
|
||||
res.Country = string(country)
|
||||
res.Country = gostring.SpaceAndLineBreak(res.Country)
|
||||
}
|
||||
// 运营商
|
||||
res.Isp = string(area)
|
||||
|
||||
// Delete ZX (防止不相关的信息产生干扰)
|
||||
if res.Isp == "ZX" || res.Isp == "" {
|
||||
res.Isp = ""
|
||||
} else {
|
||||
res.Isp = " " + res.Isp
|
||||
}
|
||||
|
||||
res.Isp = gostring.SpaceAndLineBreak(res.Isp)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (q *Pointer) getAddr() ([]byte, []byte) {
|
||||
mode := q.readData(1)[0]
|
||||
if mode == 0x01 {
|
||||
// [IP][0x01][国家和地区信息的绝对偏移地址]
|
||||
q.Offset = byteToUInt32(q.readData(3))
|
||||
return q.getAddr()
|
||||
}
|
||||
// [IP][0x02][信息的绝对偏移][...] or [IP][国家][...]
|
||||
_offset := q.Offset - 1
|
||||
c1 := q.readArea(_offset)
|
||||
if mode == 0x02 {
|
||||
q.Offset = 4 + _offset
|
||||
} else {
|
||||
q.Offset = _offset + uint32(1+len(c1))
|
||||
}
|
||||
c2 := q.readArea(q.Offset)
|
||||
return c1, c2
|
||||
}
|
||||
|
||||
func (q *Pointer) readArea(offset uint32) []byte {
|
||||
q.Offset = offset
|
||||
mode := q.readData(1)[0]
|
||||
if mode == 0x01 || mode == 0x02 {
|
||||
return q.readArea(byteToUInt32(q.readData(3)))
|
||||
}
|
||||
q.Offset = offset
|
||||
return q.readString()
|
||||
}
|
||||
|
||||
func (q *Pointer) readString() []byte {
|
||||
data := make([]byte, 0)
|
||||
for {
|
||||
buf := q.readData(1)
|
||||
if buf[0] == 0 {
|
||||
break
|
||||
}
|
||||
data = append(data, buf[0])
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func (q *Pointer) searchIndex(ip uint64) uint32 {
|
||||
|
||||
q.ItemLen = 8
|
||||
q.IndexLen = 11
|
||||
|
||||
header = dat[8:24]
|
||||
start = binary.LittleEndian.Uint32(header[8:])
|
||||
counts := binary.LittleEndian.Uint32(header[:8])
|
||||
end = start + counts*q.IndexLen
|
||||
|
||||
buf := make([]byte, q.IndexLen)
|
||||
|
||||
for {
|
||||
mid := start + q.IndexLen*(((end-start)/q.IndexLen)>>1)
|
||||
buf = dat[mid : mid+q.IndexLen]
|
||||
_ip := binary.LittleEndian.Uint64(buf[:q.ItemLen])
|
||||
|
||||
if end-start == q.IndexLen {
|
||||
if ip >= binary.LittleEndian.Uint64(dat[end:end+q.ItemLen]) {
|
||||
buf = dat[end : end+q.IndexLen]
|
||||
}
|
||||
return byteToUInt32(buf[q.ItemLen:])
|
||||
}
|
||||
|
||||
if _ip > ip {
|
||||
end = mid
|
||||
} else if _ip < ip {
|
||||
start = mid
|
||||
} else if _ip == ip {
|
||||
return byteToUInt32(buf[q.ItemLen:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func byteToUInt32(data []byte) uint32 {
|
||||
i := uint32(data[0]) & 0xff
|
||||
i |= (uint32(data[1]) << 8) & 0xff00
|
||||
i |= (uint32(data[2]) << 16) & 0xff0000
|
||||
return i
|
||||
}
|
||||
|
||||
// OnlineDownload 在线下载
|
||||
func (q *Pointer) OnlineDownload() (err error) {
|
||||
tmpData, err := getOnline()
|
||||
if err != nil {
|
||||
return errors.New("下载失败")
|
||||
}
|
||||
if err := ioutil.WriteFile("./ipv6wry.db", tmpData, 0644); err == nil {
|
||||
log.Printf("已下载最新 ZX IPv6数据库 %s ", "./ipv6wry.db")
|
||||
} else {
|
||||
return errors.New("保存失败")
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in new issue