mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-15 05:50:27 +08:00
IP库阶段性提交(未完成)
This commit is contained in:
@@ -2,6 +2,36 @@
|
||||
|
||||
package iplibrary
|
||||
|
||||
type Country struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
}
|
||||
|
||||
type Province struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
}
|
||||
|
||||
type City struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
}
|
||||
|
||||
type Town struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
}
|
||||
|
||||
type Meta struct {
|
||||
Version int `json:"version"` // IP库版本
|
||||
Author string `json:"author"`
|
||||
@@ -11,29 +41,54 @@ type Meta struct {
|
||||
Towns []*Town `json:"towns"`
|
||||
Providers []*Provider `json:"providers"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
|
||||
countryMap map[int64]*Country // id => *Country
|
||||
provinceMap map[int64]*Province // id => *Province
|
||||
cityMap map[int64]*City // id => *City
|
||||
townMap map[int64]*Town // id => *Town
|
||||
providerMap map[int64]*Provider // id => *Provider
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
func (this *Meta) Init() {
|
||||
this.countryMap = map[int64]*Country{}
|
||||
this.provinceMap = map[int64]*Province{}
|
||||
this.cityMap = map[int64]*City{}
|
||||
this.townMap = map[int64]*Town{}
|
||||
this.providerMap = map[int64]*Provider{}
|
||||
|
||||
for _, country := range this.Countries {
|
||||
this.countryMap[country.Id] = country
|
||||
}
|
||||
for _, province := range this.Provinces {
|
||||
this.provinceMap[province.Id] = province
|
||||
}
|
||||
for _, city := range this.Cities {
|
||||
this.cityMap[city.Id] = city
|
||||
}
|
||||
for _, town := range this.Towns {
|
||||
this.townMap[town.Id] = town
|
||||
}
|
||||
for _, provider := range this.Providers {
|
||||
this.providerMap[provider.Id] = provider
|
||||
}
|
||||
}
|
||||
|
||||
type Country struct {
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
func (this *Meta) CountryWithId(countryId int64) *Country {
|
||||
return this.countryMap[countryId]
|
||||
}
|
||||
|
||||
type Province struct {
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
func (this *Meta) ProvinceWithId(provinceId int64) *Province {
|
||||
return this.provinceMap[provinceId]
|
||||
}
|
||||
|
||||
type City struct {
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
func (this *Meta) CityWithId(cityId int64) *City {
|
||||
return this.cityMap[cityId]
|
||||
}
|
||||
|
||||
type Town struct {
|
||||
Name string `json:"name"`
|
||||
Codes []string `json:"codes"`
|
||||
func (this *Meta) TownWithId(townId int64) *Town {
|
||||
return this.townMap[townId]
|
||||
}
|
||||
|
||||
func (this *Meta) ProviderWithId(providerId int64) *Provider {
|
||||
return this.providerMap[providerId]
|
||||
}
|
||||
|
||||
3
pkg/iplibrary/meta_test.go
Normal file
3
pkg/iplibrary/meta_test.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package iplibrary_test
|
||||
@@ -57,6 +57,7 @@ func (this *Reader) load(reader io.Reader) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
meta.Init()
|
||||
this.meta = meta
|
||||
|
||||
left, err := this.parse(dataBuf)
|
||||
@@ -132,6 +133,7 @@ func (this *Reader) Lookup(ip net.IP) *QueryResult {
|
||||
|
||||
return &QueryResult{
|
||||
item: resultItem,
|
||||
meta: this.meta,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,10 @@ func NewFileReader(path string) (*FileReader, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (this *FileReader) Meta() *Meta {
|
||||
return this.rawReader.meta
|
||||
}
|
||||
|
||||
func (this *FileReader) Lookup(ip net.IP) *QueryResult {
|
||||
return this.rawReader.Lookup(ip)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
package iplibrary_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
@@ -18,18 +20,29 @@ func TestNewFileReader(t *testing.T) {
|
||||
"127.0.0.1",
|
||||
"192.168.0.1",
|
||||
"192.168.0.150",
|
||||
"192.168.1.100",
|
||||
"192.168.2.100",
|
||||
"192.168.3.50",
|
||||
"192.168.0.150",
|
||||
"192.168.4.80",
|
||||
"8.8.8.8",
|
||||
"111.197.165.199",
|
||||
"175.178.206.125",
|
||||
} {
|
||||
var result = reader.Lookup(net.ParseIP(ip))
|
||||
if result.IsOk() {
|
||||
t.Log(ip+":", "countryId:", result.CountryId(), "provinceId:", result.ProvinceId(), "cityId:", result.CityId(), "provider:", result.ProviderId())
|
||||
var data = maps.Map{
|
||||
"countryId": result.CountryId(),
|
||||
"countryName": result.CountryName(),
|
||||
"provinceId": result.ProvinceId(),
|
||||
"provinceName": result.ProvinceName(),
|
||||
"cityId": result.CityId(),
|
||||
"cityName": result.CityName(),
|
||||
"townId": result.TownId(),
|
||||
"townName": result.TownName(),
|
||||
"providerId": result.ProviderId(),
|
||||
"providerName": result.ProviderName(),
|
||||
}
|
||||
dataJSON, err := json.MarshalIndent(data, "", " ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(ip, "=>", string(dataJSON))
|
||||
} else {
|
||||
t.Log(ip+":", "not found")
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package iplibrary
|
||||
|
||||
type QueryResult struct {
|
||||
item *ipItem
|
||||
meta *Meta
|
||||
}
|
||||
|
||||
func (this *QueryResult) IsOk() bool {
|
||||
@@ -17,6 +18,16 @@ func (this *QueryResult) CountryId() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (this *QueryResult) CountryName() string {
|
||||
if this.item.countryId > 0 {
|
||||
var country = this.meta.CountryWithId(this.item.countryId)
|
||||
if country != nil {
|
||||
return country.Name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (this *QueryResult) ProvinceId() int64 {
|
||||
if this.item != nil {
|
||||
return this.item.provinceId
|
||||
@@ -24,6 +35,16 @@ func (this *QueryResult) ProvinceId() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (this *QueryResult) ProvinceName() string {
|
||||
if this.item.provinceId > 0 {
|
||||
var province = this.meta.ProvinceWithId(this.item.provinceId)
|
||||
if province != nil {
|
||||
return province.Name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (this *QueryResult) CityId() int64 {
|
||||
if this.item != nil {
|
||||
return this.item.cityId
|
||||
@@ -31,6 +52,16 @@ func (this *QueryResult) CityId() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (this *QueryResult) CityName() string {
|
||||
if this.item.cityId > 0 {
|
||||
var city = this.meta.CityWithId(this.item.cityId)
|
||||
if city != nil {
|
||||
return city.Name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (this *QueryResult) TownId() int64 {
|
||||
if this.item != nil {
|
||||
return this.item.townId
|
||||
@@ -38,9 +69,29 @@ func (this *QueryResult) TownId() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (this *QueryResult) TownName() string {
|
||||
if this.item.townId > 0 {
|
||||
var town = this.meta.TownWithId(this.item.townId)
|
||||
if town != nil {
|
||||
return town.Name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (this *QueryResult) ProviderId() int64 {
|
||||
if this.item != nil {
|
||||
return this.item.providerId
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (this *QueryResult) ProviderName() string {
|
||||
if this.item.providerId > 0 {
|
||||
var provider = this.meta.ProviderWithId(this.item.providerId)
|
||||
if provider != nil {
|
||||
return provider.Name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user