mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-03 21:50:14 +08:00
初步完成新版IP库
This commit is contained in:
@@ -4,10 +4,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
@@ -794,20 +793,14 @@ func (this *HTTPFirewallPolicyService) CheckHTTPFirewallPolicyIPStatus(ctx conte
|
||||
}
|
||||
|
||||
// 检查封禁的地区和省份
|
||||
info, err := iplibrary.SharedLibrary.Lookup(req.Ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if info != nil {
|
||||
var info = iplibrary.LookupIP(req.Ip)
|
||||
if info != nil && info.IsOk() {
|
||||
if firewallPolicy.Inbound != nil &&
|
||||
firewallPolicy.Inbound.IsOn &&
|
||||
firewallPolicy.Inbound.Region != nil &&
|
||||
firewallPolicy.Inbound.Region.IsOn {
|
||||
// 检查封禁的地区
|
||||
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithNameCacheable(tx, info.Country)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var countryId = info.CountryId()
|
||||
if countryId > 0 && lists.ContainsInt64(firewallPolicy.Inbound.Region.DenyCountryIds, countryId) {
|
||||
return &pb.CheckHTTPFirewallPolicyIPStatusResponse{
|
||||
IsOk: true,
|
||||
@@ -818,7 +811,7 @@ func (this *HTTPFirewallPolicyService) CheckHTTPFirewallPolicyIPStatus(ctx conte
|
||||
IpItem: nil,
|
||||
RegionCountry: &pb.RegionCountry{
|
||||
Id: countryId,
|
||||
Name: info.Country,
|
||||
Name: info.CountryName(),
|
||||
},
|
||||
RegionProvince: nil,
|
||||
}, nil
|
||||
@@ -826,10 +819,7 @@ func (this *HTTPFirewallPolicyService) CheckHTTPFirewallPolicyIPStatus(ctx conte
|
||||
|
||||
// 检查封禁的省份
|
||||
if countryId > 0 {
|
||||
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(tx, countryId, info.Province)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var provinceId = info.ProvinceId()
|
||||
if provinceId > 0 && lists.ContainsInt64(firewallPolicy.Inbound.Region.DenyProvinceIds, provinceId) {
|
||||
return &pb.CheckHTTPFirewallPolicyIPStatusResponse{
|
||||
IsOk: true,
|
||||
@@ -840,11 +830,11 @@ func (this *HTTPFirewallPolicyService) CheckHTTPFirewallPolicyIPStatus(ctx conte
|
||||
IpItem: nil,
|
||||
RegionCountry: &pb.RegionCountry{
|
||||
Id: countryId,
|
||||
Name: info.Country,
|
||||
Name: info.CountryName(),
|
||||
},
|
||||
RegionProvince: &pb.RegionProvince{
|
||||
Id: provinceId,
|
||||
Name: info.Province,
|
||||
Name: info.ProvinceName(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -3,9 +3,8 @@ package services
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/iplibrary"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
@@ -183,34 +182,19 @@ func (this *IPLibraryService) LookupIPRegion(ctx context.Context, req *pb.Lookup
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := iplibrary.SharedLibrary.Lookup(req.Ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result == nil {
|
||||
var result = iplibrary.LookupIP(req.Ip)
|
||||
if result == nil || !result.IsOk() {
|
||||
return &pb.LookupIPRegionResponse{IpRegion: nil}, nil
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
countryId, err := regions.SharedRegionCountryDAO.FindCountryIdWithNameCacheable(tx, result.Country)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
provinceId, err := regions.SharedRegionProvinceDAO.FindProvinceIdWithNameCacheable(tx, countryId, result.Province)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.LookupIPRegionResponse{IpRegion: &pb.IPRegion{
|
||||
Country: result.Country,
|
||||
Region: result.Region,
|
||||
Province: result.Province,
|
||||
City: result.City,
|
||||
Isp: result.ISP,
|
||||
CountryId: countryId,
|
||||
ProvinceId: provinceId,
|
||||
Country: result.CountryName(),
|
||||
Region: "",
|
||||
Province: result.ProvinceName(),
|
||||
City: result.CityName(),
|
||||
Isp: result.ProviderName(),
|
||||
CountryId: result.CountryId(),
|
||||
ProvinceId: result.ProvinceId(),
|
||||
Summary: result.Summary(),
|
||||
}}, nil
|
||||
}
|
||||
@@ -223,20 +207,17 @@ func (this *IPLibraryService) LookupIPRegions(ctx context.Context, req *pb.Looku
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := map[string]*pb.IPRegion{}
|
||||
var 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 {
|
||||
var info = iplibrary.LookupIP(ip)
|
||||
if info != nil && info.IsOk() {
|
||||
result[ip] = &pb.IPRegion{
|
||||
Country: info.Country,
|
||||
Region: info.Region,
|
||||
Province: info.Province,
|
||||
City: info.City,
|
||||
Isp: info.ISP,
|
||||
Country: info.CountryName(),
|
||||
Region: "",
|
||||
Province: info.ProvinceName(),
|
||||
City: info.CityName(),
|
||||
Isp: info.ProviderName(),
|
||||
Summary: info.Summary(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,71 @@ type IPLibraryFileService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// FindAllFinishedIPLibraryFiles 查找所有已完成的IP库文件
|
||||
func (this *IPLibraryFileService) FindAllFinishedIPLibraryFiles(ctx context.Context, req *pb.FindAllFinishedIPLibraryFilesRequest) (*pb.FindAllFinishedIPLibraryFilesResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
libraryFiles, err := models.SharedIPLibraryFileDAO.FindAllFinishedLibraryFiles(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbLibraryFiles = []*pb.IPLibraryFile{}
|
||||
for _, libraryFile := range libraryFiles {
|
||||
var pbCountryNames = libraryFile.DecodeCountries()
|
||||
var pbProviderNames = libraryFile.DecodeProviders()
|
||||
|
||||
var pbProvinces = []*pb.IPLibraryFile_Province{}
|
||||
for _, province := range libraryFile.DecodeProvinces() {
|
||||
pbProvinces = append(pbProvinces, &pb.IPLibraryFile_Province{
|
||||
CountryName: province[0],
|
||||
ProvinceName: province[1],
|
||||
})
|
||||
}
|
||||
|
||||
var pbCities = []*pb.IPLibraryFile_City{}
|
||||
for _, city := range libraryFile.DecodeCities() {
|
||||
pbCities = append(pbCities, &pb.IPLibraryFile_City{
|
||||
CountryName: city[0],
|
||||
ProvinceName: city[1],
|
||||
CityName: city[2],
|
||||
})
|
||||
}
|
||||
|
||||
var pbTowns = []*pb.IPLibraryFile_Town{}
|
||||
for _, town := range libraryFile.DecodeTowns() {
|
||||
pbTowns = append(pbTowns, &pb.IPLibraryFile_Town{
|
||||
CountryName: town[0],
|
||||
ProvinceName: town[1],
|
||||
CityName: town[2],
|
||||
TownName: town[3],
|
||||
})
|
||||
}
|
||||
|
||||
pbLibraryFiles = append(pbLibraryFiles, &pb.IPLibraryFile{
|
||||
Id: int64(libraryFile.Id),
|
||||
Name: libraryFile.Name,
|
||||
FileId: int64(libraryFile.FileId),
|
||||
IsFinished: libraryFile.IsFinished,
|
||||
CreatedAt: int64(libraryFile.CreatedAt),
|
||||
GeneratedFileId: int64(libraryFile.GeneratedFileId),
|
||||
GeneratedAt: int64(libraryFile.GeneratedAt),
|
||||
CountryNames: pbCountryNames,
|
||||
Provinces: pbProvinces,
|
||||
Cities: pbCities,
|
||||
Towns: pbTowns,
|
||||
ProviderNames: pbProviderNames,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.FindAllFinishedIPLibraryFilesResponse{
|
||||
IpLibraryFiles: pbLibraryFiles,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FindAllUnfinishedIPLibraryFiles 查找所有未完成的IP库文件
|
||||
func (this *IPLibraryFileService) FindAllUnfinishedIPLibraryFiles(ctx context.Context, req *pb.FindAllUnfinishedIPLibraryFilesRequest) (*pb.FindAllUnfinishedIPLibraryFilesResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
@@ -64,6 +129,7 @@ func (this *IPLibraryFileService) FindAllUnfinishedIPLibraryFiles(ctx context.Co
|
||||
|
||||
pbLibraryFiles = append(pbLibraryFiles, &pb.IPLibraryFile{
|
||||
Id: int64(libraryFile.Id),
|
||||
Name: libraryFile.Name,
|
||||
FileId: int64(libraryFile.FileId),
|
||||
IsFinished: libraryFile.IsFinished,
|
||||
CreatedAt: int64(libraryFile.CreatedAt),
|
||||
@@ -130,15 +196,19 @@ func (this *IPLibraryFileService) FindIPLibraryFile(ctx context.Context, req *pb
|
||||
|
||||
return &pb.FindIPLibraryFileResponse{
|
||||
IpLibraryFile: &pb.IPLibraryFile{
|
||||
Id: int64(libraryFile.Id),
|
||||
FileId: int64(libraryFile.FileId),
|
||||
IsFinished: libraryFile.IsFinished,
|
||||
CreatedAt: int64(libraryFile.CreatedAt),
|
||||
CountryNames: pbCountryNames,
|
||||
Provinces: pbProvinces,
|
||||
Cities: pbCities,
|
||||
Towns: pbTowns,
|
||||
ProviderNames: pbProviderNames,
|
||||
Id: int64(libraryFile.Id),
|
||||
Name: libraryFile.Name,
|
||||
Template: libraryFile.Template,
|
||||
EmptyValues: libraryFile.DecodeEmptyValues(),
|
||||
FileId: int64(libraryFile.FileId),
|
||||
IsFinished: libraryFile.IsFinished,
|
||||
CreatedAt: int64(libraryFile.CreatedAt),
|
||||
GeneratedFileId: int64(libraryFile.GeneratedFileId),
|
||||
CountryNames: pbCountryNames,
|
||||
Provinces: pbProvinces,
|
||||
Cities: pbCities,
|
||||
Towns: pbTowns,
|
||||
ProviderNames: pbProviderNames,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -182,7 +252,7 @@ func (this *IPLibraryFileService) CreateIPLibraryFile(ctx context.Context, req *
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
libraryFileId, err := models.SharedIPLibraryFileDAO.CreateLibraryFile(tx, req.Template, req.EmptyValues, req.FileId, countries, provinces, cities, towns, providers)
|
||||
libraryFileId, err := models.SharedIPLibraryFileDAO.CreateLibraryFile(tx, req.Name, req.Template, req.EmptyValues, req.FileId, countries, provinces, cities, towns, providers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -481,6 +551,9 @@ func (this *IPLibraryFileService) CheckTownsWithIPLibraryFileId(ctx context.Cont
|
||||
}
|
||||
}
|
||||
cityMap[cityKey] = cityId
|
||||
if cityId > 0 {
|
||||
cityIds = append(cityIds, cityId)
|
||||
}
|
||||
}
|
||||
|
||||
// town
|
||||
@@ -607,3 +680,34 @@ func (this *IPLibraryFileService) GenerateIPLibraryFile(ctx context.Context, req
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// UpdateIPLibraryFileFinished 设置某个IP库为已完成
|
||||
func (this *IPLibraryFileService) UpdateIPLibraryFileFinished(ctx context.Context, req *pb.UpdateIPLibraryFileFinishedRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = models.SharedIPLibraryFileDAO.UpdateLibraryFileIsFinished(tx, req.IpLibraryFileId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// DeleteIPLibraryFile 删除IP库文件
|
||||
func (this *IPLibraryFileService) DeleteIPLibraryFile(ctx context.Context, req *pb.DeleteIPLibraryFileRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = models.SharedIPLibraryFileDAO.DisableIPLibraryFile(tx, req.IpLibraryFileId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user