mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	增加根据IP名单代号查找IP名单ID的接口
This commit is contained in:
		@@ -13,6 +13,7 @@ import (
 | 
			
		||||
	"github.com/iwind/TeaGo/lists"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"regexp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@@ -31,6 +32,8 @@ var DefaultGlobalIPList = &IPList{
 | 
			
		||||
	IsOn:     true,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var ipListCodeRegexp = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
 | 
			
		||||
 | 
			
		||||
type IPListDAO dbs.DAO
 | 
			
		||||
 | 
			
		||||
func NewIPListDAO() *IPListDAO {
 | 
			
		||||
@@ -352,3 +355,20 @@ func (this *IPListDAO) FindServerIdWithListId(tx *dbs.Tx, listId int64) (serverI
 | 
			
		||||
		FindInt64Col(0)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindIPListIdWithCode 根据IP名单代号查找名单ID
 | 
			
		||||
func (this *IPListDAO) FindIPListIdWithCode(tx *dbs.Tx, listCode string) (int64, error) {
 | 
			
		||||
	if len(listCode) == 0 {
 | 
			
		||||
		return 0, nil
 | 
			
		||||
	}
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		ResultPk().
 | 
			
		||||
		State(IPListStateEnabled).
 | 
			
		||||
		Attr("code", listCode).
 | 
			
		||||
		FindInt64Col(0)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ValidateIPListCode 校验IP名单代号格式
 | 
			
		||||
func (this *IPListDAO) ValidateIPListCode(code string) bool {
 | 
			
		||||
	return ipListCodeRegexp.MatchString(code)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,12 @@ package services
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
 | 
			
		||||
	"github.com/iwind/TeaGo/lists"
 | 
			
		||||
	"github.com/iwind/TeaGo/rands"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// IPListService IP名单相关服务
 | 
			
		||||
@@ -24,9 +26,14 @@ func (this *IPListService) CreateIPList(ctx context.Context, req *pb.CreateIPLis
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
 | 
			
		||||
	// 修正默认的代号
 | 
			
		||||
	if req.Code == "white" || req.Code == "black" {
 | 
			
		||||
		req.Code = req.Code + "-" + rands.HexString(8)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 检查用户相关信息
 | 
			
		||||
	if userId > 0 {
 | 
			
		||||
		// 检查服务ID
 | 
			
		||||
		// 检查网站ID
 | 
			
		||||
		if req.ServerId > 0 {
 | 
			
		||||
			err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
@@ -35,6 +42,21 @@ func (this *IPListService) CreateIPList(ctx context.Context, req *pb.CreateIPLis
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 检查代号
 | 
			
		||||
	if len(req.Code) > 0 {
 | 
			
		||||
		if !models.SharedIPListDAO.ValidateIPListCode(req.Code) {
 | 
			
		||||
			return nil, errors.New("invalid 'code' format")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		oldListId, findErr := models.SharedIPListDAO.FindIPListIdWithCode(tx, req.Code)
 | 
			
		||||
		if findErr != nil {
 | 
			
		||||
			return nil, findErr
 | 
			
		||||
		}
 | 
			
		||||
		if oldListId > 0 {
 | 
			
		||||
			return nil, errors.New("the code '" + req.Code + "' has been used")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	listId, err := models.SharedIPListDAO.CreateIPList(tx, userId, req.ServerId, req.Type, req.Name, req.Code, req.TimeoutJSON, req.Description, req.IsPublic, req.IsGlobal)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -52,6 +74,21 @@ func (this *IPListService) UpdateIPList(ctx context.Context, req *pb.UpdateIPLis
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
 | 
			
		||||
	// 检查代号
 | 
			
		||||
	if len(req.Code) > 0 {
 | 
			
		||||
		if !models.SharedIPListDAO.ValidateIPListCode(req.Code) {
 | 
			
		||||
			return nil, errors.New("invalid 'code' format")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		oldListId, findErr := models.SharedIPListDAO.FindIPListIdWithCode(tx, req.Code)
 | 
			
		||||
		if findErr != nil {
 | 
			
		||||
			return nil, findErr
 | 
			
		||||
		}
 | 
			
		||||
		if oldListId > 0 && oldListId != req.IpListId {
 | 
			
		||||
			return nil, errors.New("the code '" + req.Code + "' has been used")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedIPListDAO.UpdateIPList(tx, req.IpListId, req.Name, req.Code, req.TimeoutJSON, req.Description)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -250,3 +287,34 @@ func (this *IPListService) FindServerIdWithIPListId(ctx context.Context, req *pb
 | 
			
		||||
		ServerId: serverId,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindIPListIdWithCode 根据IP名单代号获取IP名单ID
 | 
			
		||||
func (this *IPListService) FindIPListIdWithCode(ctx context.Context, req *pb.FindIPListIdWithCodeRequest) (*pb.FindIPListIdWithCodeResponse, error) {
 | 
			
		||||
	_, userId, err := this.ValidateAdminAndUser(ctx, true)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(req.Code) == 0 {
 | 
			
		||||
		return nil, errors.New("require 'code'")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	listId, err := models.SharedIPListDAO.FindIPListIdWithCode(tx, req.Code)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if listId > 0 {
 | 
			
		||||
		if userId > 0 {
 | 
			
		||||
			err = models.SharedIPListDAO.CheckUserIPList(tx, userId, listId)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.FindIPListIdWithCodeResponse{
 | 
			
		||||
		IpListId: listId,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -104082,7 +104082,7 @@
 | 
			
		||||
      "name": "edgeIPLists",
 | 
			
		||||
      "engine": "InnoDB",
 | 
			
		||||
      "charset": "utf8mb4_general_ci",
 | 
			
		||||
      "definition": "CREATE TABLE `edgeIPLists` (\n  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n  `isOn` tinyint(1) unsigned DEFAULT '1' COMMENT '是否启用',\n  `type` varchar(255) DEFAULT NULL COMMENT '类型',\n  `adminId` int(11) unsigned DEFAULT '0' COMMENT '用户ID',\n  `userId` int(11) unsigned DEFAULT '0' COMMENT '用户ID',\n  `serverId` bigint(11) unsigned DEFAULT '0' COMMENT '服务ID',\n  `name` varchar(255) DEFAULT NULL COMMENT '列表名',\n  `code` varchar(255) DEFAULT NULL COMMENT '代号',\n  `state` tinyint(1) unsigned DEFAULT '1' COMMENT '状态',\n  `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n  `timeout` json DEFAULT NULL COMMENT '默认超时时间',\n  `actions` json DEFAULT NULL COMMENT 'IP触发的动作',\n  `description` varchar(512) DEFAULT NULL COMMENT '描述',\n  `isPublic` tinyint(1) unsigned DEFAULT '0' COMMENT '是否公用',\n  `isGlobal` tinyint(1) unsigned DEFAULT '0' COMMENT '是否全局',\n  PRIMARY KEY (`id`),\n  KEY `userId` (`userId`),\n  KEY `type` (`type`),\n  KEY `serverId` (`serverId`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='IP名单'",
 | 
			
		||||
      "definition": "CREATE TABLE `edgeIPLists` (\n  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n  `isOn` tinyint(1) unsigned DEFAULT '1' COMMENT '是否启用',\n  `type` varchar(255) DEFAULT NULL COMMENT '类型',\n  `adminId` int(11) unsigned DEFAULT '0' COMMENT '用户ID',\n  `userId` int(11) unsigned DEFAULT '0' COMMENT '用户ID',\n  `serverId` bigint(11) unsigned DEFAULT '0' COMMENT '服务ID',\n  `name` varchar(255) DEFAULT NULL COMMENT '列表名',\n  `code` varchar(255) DEFAULT NULL COMMENT '代号',\n  `state` tinyint(1) unsigned DEFAULT '1' COMMENT '状态',\n  `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n  `timeout` json DEFAULT NULL COMMENT '默认超时时间',\n  `actions` json DEFAULT NULL COMMENT 'IP触发的动作',\n  `description` varchar(512) DEFAULT NULL COMMENT '描述',\n  `isPublic` tinyint(1) unsigned DEFAULT '0' COMMENT '是否公用',\n  `isGlobal` tinyint(1) unsigned DEFAULT '0' COMMENT '是否全局',\n  PRIMARY KEY (`id`),\n  KEY `userId` (`userId`),\n  KEY `type` (`type`),\n  KEY `serverId` (`serverId`),\n  KEY `code` (`code`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='IP名单'",
 | 
			
		||||
      "fields": [
 | 
			
		||||
        {
 | 
			
		||||
          "name": "id",
 | 
			
		||||
@@ -104161,6 +104161,10 @@
 | 
			
		||||
        {
 | 
			
		||||
          "name": "serverId",
 | 
			
		||||
          "definition": "KEY `serverId` (`serverId`) USING BTREE"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          "name": "code",
 | 
			
		||||
          "definition": "KEY `code` (`code`) USING BTREE"
 | 
			
		||||
        }
 | 
			
		||||
      ],
 | 
			
		||||
      "records": []
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user