Files
EdgeAPI/internal/rpc/services/service_ip_library.go

237 lines
6.3 KiB
Go
Raw Normal View History

2020-11-04 15:51:32 +08:00
package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
2022-08-21 20:38:34 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
2020-11-04 15:51:32 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
2021-07-11 18:05:57 +08:00
// IPLibraryService IP库服务
2020-11-04 15:51:32 +08:00
type IPLibraryService struct {
2020-11-24 15:02:44 +08:00
BaseService
2020-11-04 15:51:32 +08:00
}
2021-07-11 18:05:57 +08:00
// CreateIPLibrary 创建IP库
2020-11-04 15:51:32 +08:00
func (this *IPLibraryService) CreateIPLibrary(ctx context.Context, req *pb.CreateIPLibraryRequest) (*pb.CreateIPLibraryResponse, error) {
// 校验请求
2021-07-11 18:05:57 +08:00
_, _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
2022-07-22 15:05:30 +08:00
var tx = this.NullTx()
ipLibraryId, err := models.SharedIPLibraryDAO.CreateIPLibrary(tx, req.Type, req.FileId)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
return &pb.CreateIPLibraryResponse{
IpLibraryId: ipLibraryId,
}, nil
}
2021-07-11 18:05:57 +08:00
// FindEnabledIPLibrary 查找单个IP库
2020-11-04 15:51:32 +08:00
func (this *IPLibraryService) FindEnabledIPLibrary(ctx context.Context, req *pb.FindEnabledIPLibraryRequest) (*pb.FindEnabledIPLibraryResponse, error) {
// 校验请求
2021-07-11 18:05:57 +08:00
_, _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
2022-07-22 15:05:30 +08:00
var tx = this.NullTx()
ipLibrary, err := models.SharedIPLibraryDAO.FindEnabledIPLibrary(tx, req.IpLibraryId)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
if ipLibrary == nil {
return &pb.FindEnabledIPLibraryResponse{IpLibrary: nil}, nil
}
// 文件相关
var pbFile *pb.File = nil
file, err := models.SharedFileDAO.FindEnabledFile(tx, int64(ipLibrary.FileId))
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
if file != nil {
pbFile = &pb.File{
Id: int64(file.Id),
Filename: file.Filename,
Size: int64(file.Size),
}
}
return &pb.FindEnabledIPLibraryResponse{
IpLibrary: &pb.IPLibrary{
Id: int64(ipLibrary.Id),
Type: ipLibrary.Type,
File: pbFile,
CreatedAt: int64(ipLibrary.CreatedAt),
},
}, nil
}
2021-07-11 18:05:57 +08:00
// FindLatestIPLibraryWithType 查找最新的IP库
2020-11-04 15:51:32 +08:00
func (this *IPLibraryService) FindLatestIPLibraryWithType(ctx context.Context, req *pb.FindLatestIPLibraryWithTypeRequest) (*pb.FindLatestIPLibraryWithTypeResponse, error) {
// 校验请求
2021-07-11 18:05:57 +08:00
_, _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
2022-07-22 15:05:30 +08:00
var tx = this.NullTx()
ipLibrary, err := models.SharedIPLibraryDAO.FindLatestIPLibraryWithType(tx, req.Type)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
if ipLibrary == nil {
return &pb.FindLatestIPLibraryWithTypeResponse{IpLibrary: nil}, nil
}
// 文件相关
var pbFile *pb.File = nil
file, err := models.SharedFileDAO.FindEnabledFile(tx, int64(ipLibrary.FileId))
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
if file != nil {
pbFile = &pb.File{
Id: int64(file.Id),
Filename: file.Filename,
Size: int64(file.Size),
}
}
return &pb.FindLatestIPLibraryWithTypeResponse{
IpLibrary: &pb.IPLibrary{
Id: int64(ipLibrary.Id),
Type: ipLibrary.Type,
File: pbFile,
CreatedAt: int64(ipLibrary.CreatedAt),
},
}, nil
}
2021-07-11 18:05:57 +08:00
// FindAllEnabledIPLibrariesWithType 列出某个类型的所有IP库
2020-11-04 15:51:32 +08:00
func (this *IPLibraryService) FindAllEnabledIPLibrariesWithType(ctx context.Context, req *pb.FindAllEnabledIPLibrariesWithTypeRequest) (*pb.FindAllEnabledIPLibrariesWithTypeResponse, error) {
// 校验请求
2021-07-11 18:05:57 +08:00
_, _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
2022-07-22 15:05:30 +08:00
var tx = this.NullTx()
ipLibraries, err := models.SharedIPLibraryDAO.FindAllEnabledIPLibrariesWithType(tx, req.Type)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
result := []*pb.IPLibrary{}
for _, library := range ipLibraries {
// 文件相关
var pbFile *pb.File = nil
file, err := models.SharedFileDAO.FindEnabledFile(tx, int64(library.FileId))
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
if file != nil {
pbFile = &pb.File{
Id: int64(file.Id),
Filename: file.Filename,
Size: int64(file.Size),
}
}
result = append(result, &pb.IPLibrary{
Id: int64(library.Id),
Type: library.Type,
File: pbFile,
CreatedAt: int64(library.CreatedAt),
})
}
return &pb.FindAllEnabledIPLibrariesWithTypeResponse{IpLibraries: result}, nil
}
2021-07-11 18:05:57 +08:00
// DeleteIPLibrary 删除IP库
func (this *IPLibraryService) DeleteIPLibrary(ctx context.Context, req *pb.DeleteIPLibraryRequest) (*pb.RPCSuccess, error) {
2020-11-04 15:51:32 +08:00
// 校验请求
2022-07-22 14:35:17 +08:00
_, err := this.ValidateAdmin(ctx)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
2022-07-22 15:05:30 +08:00
var tx = this.NullTx()
err = models.SharedIPLibraryDAO.DisableIPLibrary(tx, req.IpLibraryId)
2020-11-04 15:51:32 +08:00
if err != nil {
return nil, err
}
2020-11-24 15:02:44 +08:00
return this.Success()
2020-11-04 15:51:32 +08:00
}
2020-11-05 11:51:37 +08:00
2021-07-11 18:05:57 +08:00
// LookupIPRegion 查询某个IP信息
2020-11-05 11:51:37 +08:00
func (this *IPLibraryService) LookupIPRegion(ctx context.Context, req *pb.LookupIPRegionRequest) (*pb.LookupIPRegionResponse, error) {
// 校验请求
2022-09-17 16:07:37 +08:00
_, _, err := this.ValidateAdminAndUser(ctx, true)
2020-11-05 11:51:37 +08:00
if err != nil {
return nil, err
}
2022-08-21 20:38:34 +08:00
var result = iplibrary.LookupIP(req.Ip)
if result == nil || !result.IsOk() {
2021-01-13 17:00:29 +08:00
return &pb.LookupIPRegionResponse{IpRegion: nil}, nil
2020-11-05 11:51:37 +08:00
}
2021-01-13 17:00:29 +08:00
return &pb.LookupIPRegionResponse{IpRegion: &pb.IPRegion{
2022-08-21 20:38:34 +08:00
Country: result.CountryName(),
Region: "",
Province: result.ProvinceName(),
City: result.CityName(),
Isp: result.ProviderName(),
CountryId: result.CountryId(),
ProvinceId: result.ProvinceId(),
2023-04-19 20:36:48 +08:00
CityId: result.CityId(),
TownId: result.TownId(),
ProviderId: result.ProviderId(),
2021-01-13 17:00:29 +08:00
Summary: result.Summary(),
2020-11-05 11:51:37 +08:00
}}, nil
}
2021-01-13 17:00:29 +08:00
2021-07-11 18:05:57 +08:00
// LookupIPRegions 查询一组IP信息
2021-01-13 17:00:29 +08:00
func (this *IPLibraryService) LookupIPRegions(ctx context.Context, req *pb.LookupIPRegionsRequest) (*pb.LookupIPRegionsResponse, error) {
// 校验请求
2022-09-17 16:07:37 +08:00
_, _, err := this.ValidateAdminAndUser(ctx, true)
2021-01-13 17:00:29 +08:00
if err != nil {
return nil, err
}
2022-08-21 20:38:34 +08:00
var result = map[string]*pb.IPRegion{}
2021-01-13 17:00:29 +08:00
if len(req.IpList) > 0 {
for _, ip := range req.IpList {
2022-08-21 20:38:34 +08:00
var info = iplibrary.LookupIP(ip)
if info != nil && info.IsOk() {
2021-01-13 17:00:29 +08:00
result[ip] = &pb.IPRegion{
2023-04-19 20:36:48 +08:00
Country: info.CountryName(),
Region: "",
Province: info.ProvinceName(),
City: info.CityName(),
Isp: info.ProviderName(),
CountryId: info.CountryId(),
ProvinceId: info.ProvinceId(),
CityId: info.CityId(),
TownId: info.TownId(),
ProviderId: info.ProviderId(),
Summary: info.Summary(),
2021-01-13 17:00:29 +08:00
}
}
}
}
return &pb.LookupIPRegionsResponse{IpRegionMap: result}, nil
}