增加批量查询IP信息API

This commit is contained in:
刘祥超
2021-01-13 17:00:29 +08:00
parent 42e0103b86
commit 2a0bc1f338
2 changed files with 53 additions and 2 deletions

View File

@@ -1,5 +1,10 @@
package iplibrary package iplibrary
import (
"github.com/iwind/TeaGo/lists"
"strings"
)
type Result struct { type Result struct {
CityId int64 CityId int64
Country string Country string
@@ -8,3 +13,17 @@ type Result struct {
City string City string
ISP string ISP string
} }
func (this *Result) Summary() string {
pieces := []string{}
if len(this.Country) > 0 {
pieces = append(pieces, this.Country)
}
if len(this.Province) > 0 && !lists.ContainsString(pieces, this.Province) {
pieces = append(pieces, this.Province)
}
if len(this.City) > 0 && !lists.ContainsString(pieces, this.City) && !lists.ContainsString(pieces, strings.TrimSuffix(this.Province, "市")) {
pieces = append(pieces, this.City)
}
return strings.Join(pieces, " ")
}

View File

@@ -187,7 +187,7 @@ func (this *IPLibraryService) LookupIPRegion(ctx context.Context, req *pb.Lookup
return nil, err return nil, err
} }
if result == nil { if result == nil {
return &pb.LookupIPRegionResponse{Region: nil}, nil return &pb.LookupIPRegionResponse{IpRegion: nil}, nil
} }
tx := this.NullTx() tx := this.NullTx()
@@ -202,7 +202,7 @@ func (this *IPLibraryService) LookupIPRegion(ctx context.Context, req *pb.Lookup
return nil, err return nil, err
} }
return &pb.LookupIPRegionResponse{Region: &pb.IPRegion{ return &pb.LookupIPRegionResponse{IpRegion: &pb.IPRegion{
Country: result.Country, Country: result.Country,
Region: result.Region, Region: result.Region,
Province: result.Province, Province: result.Province,
@@ -210,5 +210,37 @@ func (this *IPLibraryService) LookupIPRegion(ctx context.Context, req *pb.Lookup
Isp: result.ISP, Isp: result.ISP,
CountryId: countryId, CountryId: countryId,
ProvinceId: provinceId, ProvinceId: provinceId,
Summary: result.Summary(),
}}, nil }}, nil
} }
// 查询一组IP信息
func (this *IPLibraryService) LookupIPRegions(ctx context.Context, req *pb.LookupIPRegionsRequest) (*pb.LookupIPRegionsResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
result := map[string]*pb.IPRegion{}
if len(req.IpList) > 0 {
for _, ip := range req.IpList {
info, err := iplibrary.SharedLibrary.Lookup(ip)
if err != nil {
return nil, err
}
if info != nil {
result[ip] = &pb.IPRegion{
Country: info.Country,
Region: info.Region,
Province: info.Province,
City: info.City,
Isp: info.ISP,
Summary: info.Summary(),
}
}
}
}
return &pb.LookupIPRegionsResponse{IpRegionMap: result}, nil
}