增加国家/地区封禁管理

This commit is contained in:
刘祥超
2020-11-06 11:02:53 +08:00
parent 62ae07484d
commit c5b0f5f3a2
15 changed files with 3843 additions and 11 deletions

View File

@@ -136,6 +136,22 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInboundAndOutbound(policy
return err
}
// 修改策略的Inbound
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInbound(policyId int64, inboundJSON []byte) error {
if policyId <= 0 {
return errors.New("invalid policyId")
}
op := NewHTTPFirewallPolicyOperator()
op.Id = policyId
if len(inboundJSON) > 0 {
op.Inbound = inboundJSON
} else {
op.Inbound = "null"
}
_, err := this.Save(op)
return err
}
// 修改策略
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(policyId int64, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte) error {
if policyId <= 0 {

View File

@@ -1,9 +1,11 @@
package models
import (
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
@@ -69,3 +71,32 @@ func (this *RegionCityDAO) FindRegionCityName(id uint32) (string, error) {
Result("name").
FindStringCol("")
}
// 根据数据ID查找城市
func (this *RegionCityDAO) FindCityWithDataId(dataId string) (int64, error) {
return this.Query().
Attr("dataId", dataId).
ResultPk().
FindInt64Col(0)
}
// 创建城市
func (this *RegionCityDAO) CreateCity(provinceId int64, name string, dataId string) (int64, error) {
op := NewRegionCityOperator()
op.ProvinceId = provinceId
op.Name = name
op.DataId = dataId
op.State = RegionCityStateEnabled
codes := []string{name}
codesJSON, err := json.Marshal(codes)
if err != nil {
return 0, err
}
op.Codes = codesJSON
_, err = this.Save(op)
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}

View File

@@ -7,6 +7,7 @@ type RegionCity struct {
Name string `field:"name"` // 名称
Codes string `field:"codes"` // 代号
State uint8 `field:"state"` // 状态
DataId string `field:"dataId"` // 原始数据ID
}
type RegionCityOperator struct {
@@ -15,6 +16,7 @@ type RegionCityOperator struct {
Name interface{} // 名称
Codes interface{} // 代号
State interface{} // 状态
DataId interface{} // 原始数据ID
}
func NewRegionCityOperator() *RegionCityOperator {

View File

@@ -1,9 +1,13 @@
package models
import (
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
"github.com/mozillazg/go-pinyin"
"strings"
)
const (
@@ -42,7 +46,7 @@ func (this *RegionCountryDAO) EnableRegionCountry(id uint32) error {
}
// 禁用条目
func (this *RegionCountryDAO) DisableRegionCountry(id uint32) error {
func (this *RegionCountryDAO) DisableRegionCountry(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", RegionCountryStateDisabled).
@@ -51,7 +55,7 @@ func (this *RegionCountryDAO) DisableRegionCountry(id uint32) error {
}
// 查找启用中的条目
func (this *RegionCountryDAO) FindEnabledRegionCountry(id uint32) (*RegionCountry, error) {
func (this *RegionCountryDAO) FindEnabledRegionCountry(id int64) (*RegionCountry, error) {
result, err := this.Query().
Pk(id).
Attr("state", RegionCountryStateEnabled).
@@ -63,9 +67,56 @@ func (this *RegionCountryDAO) FindEnabledRegionCountry(id uint32) (*RegionCountr
}
// 根据主键查找名称
func (this *RegionCountryDAO) FindRegionCountryName(id uint32) (string, error) {
func (this *RegionCountryDAO) FindRegionCountryName(id int64) (string, error) {
return this.Query().
Pk(id).
Result("name").
FindStringCol("")
}
// 根据数据ID查找国家
func (this *RegionCountryDAO) FindCountryIdWithDataId(dataId string) (int64, error) {
return this.Query().
Attr("dataId", dataId).
ResultPk().
FindInt64Col(0)
}
// 根据数据ID创建国家
func (this *RegionCountryDAO) CreateCountry(name string, dataId string) (int64, error) {
op := NewRegionCountryOperator()
op.Name = name
pinyinPieces := pinyin.Pinyin(name, pinyin.NewArgs())
pinyinResult := []string{}
for _, piece := range pinyinPieces {
pinyinResult = append(pinyinResult, strings.Join(piece, " "))
}
pinyinJSON, err := json.Marshal([]string{strings.Join(pinyinResult, " ")})
op.Pinyin = pinyinJSON
codes := []string{name}
codesJSON, err := json.Marshal(codes)
if err != nil {
return 0, err
}
op.Codes = codesJSON
op.DataId = dataId
op.State = RegionCountryStateEnabled
_, err = this.Save(op)
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}
// 查找所有可用的国家
func (this *RegionCountryDAO) FindAllEnabledCountriesOrderByPinyin() (result []*RegionCountry, err error) {
_, err = this.Query().
State(RegionCountryStateEnabled).
Slice(&result).
Asc("pinyin").
FindAll()
return
}

View File

@@ -2,17 +2,21 @@ package models
//
type RegionCountry struct {
Id uint32 `field:"id"` // ID
Name string `field:"name"` // 名称
Codes string `field:"codes"` // 代号
State uint8 `field:"state"` // 状态
Id uint32 `field:"id"` // ID
Name string `field:"name"` // 名称
Codes string `field:"codes"` // 代号
State uint8 `field:"state"` // 状态
DataId string `field:"dataId"` // 原始数据ID
Pinyin string `field:"pinyin"` // 拼音
}
type RegionCountryOperator struct {
Id interface{} // ID
Name interface{} // 名称
Codes interface{} // 代号
State interface{} // 状态
Id interface{} // ID
Name interface{} // 名称
Codes interface{} // 代号
State interface{} // 状态
DataId interface{} // 原始数据ID
Pinyin interface{} // 拼音
}
func NewRegionCountryOperator() *RegionCountryOperator {

View File

@@ -1,9 +1,11 @@
package models
import (
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
@@ -69,3 +71,43 @@ func (this *RegionProvinceDAO) FindRegionProvinceName(id uint32) (string, error)
Result("name").
FindStringCol("")
}
// 根据数据ID查找省份
func (this *RegionProvinceDAO) FindProvinceIdWithDataId(dataId string) (int64, error) {
return this.Query().
Attr("dataId", dataId).
ResultPk().
FindInt64Col(0)
}
// 创建省份
func (this *RegionProvinceDAO) CreateProvince(countryId int64, name string, dataId string) (int64, error) {
op := NewRegionProvinceOperator()
op.CountryId = countryId
op.Name = name
op.DataId = dataId
op.State = RegionProvinceStateEnabled
codes := []string{name}
codesJSON, err := json.Marshal(codes)
if err != nil {
return 0, err
}
op.Codes = codesJSON
_, err = this.Save(op)
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}
// 查找所有省份
func (this *RegionProvinceDAO) FindAllEnabledProvincesWithCountryId(countryId int64) (result []*RegionProvince, err error) {
_, err = this.Query().
State(RegionProvinceStateEnabled).
Attr("countryId", countryId).
Asc("name").
Slice(&result).
FindAll()
return
}

View File

@@ -7,6 +7,7 @@ type RegionProvince struct {
Name string `field:"name"` // 名称
Codes string `field:"codes"` // 代号
State uint8 `field:"state"` // 状态
DataId string `field:"dataId"` // 原始数据ID
}
type RegionProvinceOperator struct {
@@ -15,6 +16,7 @@ type RegionProvinceOperator struct {
Name interface{} // 名称
Codes interface{} // 代号
State interface{} // 状态
DataId interface{} // 原始数据ID
}
func NewRegionProvinceOperator() *RegionProvinceOperator {

View File

@@ -176,6 +176,8 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err
pb.RegisterIPLibraryServiceServer(rpcServer, &services.IPLibraryService{})
pb.RegisterFileChunkServiceServer(rpcServer, &services.FileChunkService{})
pb.RegisterFileServiceServer(rpcServer, &services.FileService{})
pb.RegisterRegionCountryServiceServer(rpcServer, &services.RegionCountryService{})
pb.RegisterRegionProvinceServiceServer(rpcServer, &services.RegionProvinceService{})
err := rpcServer.Serve(listener)
if err != nil {
return errors.New("[API]start rpc failed: " + err.Error())

View File

@@ -251,6 +251,22 @@ func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallPolicyGroups(ctx contex
return rpcutils.RPCUpdateSuccess()
}
// 修改inbound信息
func (this *HTTPFirewallPolicyService) UpdateHTTPFirewallInboundConfig(ctx context.Context, req *pb.UpdateHTTPFirewallInboundConfigRequest) (*pb.RPCUpdateSuccess, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
err = models.SharedHTTPFirewallPolicyDAO.UpdateFirewallPolicyInbound(req.FirewallPolicyId, req.InboundJSON)
if err != nil {
return nil, err
}
return rpcutils.RPCUpdateSuccess()
}
// 计算可用的防火墙策略数量
func (this *HTTPFirewallPolicyService) CountAllEnabledFirewallPolicies(ctx context.Context, req *pb.CountAllEnabledFirewallPoliciesRequest) (*pb.CountAllEnabledFirewallPoliciesResponse, error) {
// 校验请求

View File

@@ -0,0 +1,48 @@
package services
import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// 国家相关服务
type RegionCountryService struct {
}
// 查找所有的国家列表
func (this *RegionCountryService) FindAllEnabledRegionCountries(ctx context.Context, req *pb.FindAllEnabledRegionCountriesRequest) (*pb.FindAllEnabledRegionCountriesResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
countries, err := models.SharedRegionCountryDAO.FindAllEnabledCountriesOrderByPinyin()
if err != nil {
return nil, err
}
result := []*pb.RegionCountry{}
for _, country := range countries {
pinyinStrings := []string{}
err = json.Unmarshal([]byte(country.Pinyin), &pinyinStrings)
if err != nil {
return nil, err
}
if len(pinyinStrings) == 0 {
continue
}
result = append(result, &pb.RegionCountry{
Id: int64(country.Id),
Name: country.Name,
Pinyin: pinyinStrings,
})
}
return &pb.FindAllEnabledRegionCountriesResponse{
Countries: result,
}, nil
}

View File

@@ -0,0 +1,37 @@
package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// 省份相关服务
type RegionProvinceService struct {
}
// 查找所有省份
func (this *RegionProvinceService) FindAllEnabledRegionProvincesWithCountryId(ctx context.Context, req *pb.FindAllEnabledRegionProvincesWithCountryIdRequest) (*pb.FindAllEnabledRegionProvincesWithCountryIdResponse, error) {
// 校验请求
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
if err != nil {
return nil, err
}
provinces, err := models.SharedRegionProvinceDAO.FindAllEnabledProvincesWithCountryId(req.CountryId)
if err != nil {
return nil, err
}
result := []*pb.RegionProvince{}
for _, province := range provinces {
result = append(result, &pb.RegionProvince{
Id: int64(province.Id),
Name: province.Name,
})
}
return &pb.FindAllEnabledRegionProvincesWithCountryIdResponse{
Provinces: result,
}, nil
}