mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-24 22:16:36 +08:00
IP名单增加是否全局
This commit is contained in:
@@ -3,6 +3,7 @@ package models
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ipconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
@@ -16,7 +17,7 @@ const (
|
||||
IPListStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
var listTypeCacheMap = map[int64]string{} // listId => type
|
||||
var listTypeCacheMap = map[int64]*IPList{} // listId => *IPList
|
||||
|
||||
type IPListDAO dbs.DAO
|
||||
|
||||
@@ -77,38 +78,46 @@ func (this *IPListDAO) FindIPListName(tx *dbs.Tx, id int64) (string, error) {
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// FindIPListTypeCacheable 获取名单类型
|
||||
func (this *IPListDAO) FindIPListTypeCacheable(tx *dbs.Tx, listId int64) (string, error) {
|
||||
// FindIPListCacheable 获取名单
|
||||
func (this *IPListDAO) FindIPListCacheable(tx *dbs.Tx, listId int64) (*IPList, error) {
|
||||
// 全局黑名单
|
||||
if listId == firewallconfigs.GlobalListId {
|
||||
return &IPList{
|
||||
Id: uint32(listId),
|
||||
IsPublic: 1,
|
||||
IsGlobal: 1,
|
||||
Type: "black",
|
||||
State: IPListStateEnabled,
|
||||
IsOn: 1,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 检查缓存
|
||||
SharedCacheLocker.RLock()
|
||||
listType, ok := listTypeCacheMap[listId]
|
||||
list, ok := listTypeCacheMap[listId]
|
||||
SharedCacheLocker.RUnlock()
|
||||
if ok {
|
||||
return listType, nil
|
||||
return list, nil
|
||||
}
|
||||
|
||||
listType, err := this.Query(tx).
|
||||
one, err := this.Query(tx).
|
||||
Pk(listId).
|
||||
Result("type").
|
||||
FindStringCol("")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(listType) == 0 {
|
||||
return "", nil
|
||||
Result("isGlobal", "type", "state", "id", "isPublic", "isGlobal").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 保存缓存
|
||||
SharedCacheLocker.Lock()
|
||||
listTypeCacheMap[listId] = listType
|
||||
listTypeCacheMap[listId] = one.(*IPList)
|
||||
SharedCacheLocker.Unlock()
|
||||
|
||||
return listType, nil
|
||||
return one.(*IPList), nil
|
||||
}
|
||||
|
||||
// CreateIPList 创建名单
|
||||
func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs.IPListType, name string, code string, timeoutJSON []byte, description string, isPublic bool) (int64, error) {
|
||||
func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs.IPListType, name string, code string, timeoutJSON []byte, description string, isPublic bool, isGlobal bool) (int64, error) {
|
||||
op := NewIPListOperator()
|
||||
op.IsOn = true
|
||||
op.UserId = userId
|
||||
@@ -121,6 +130,7 @@ func (this *IPListDAO) CreateIPList(tx *dbs.Tx, userId int64, listType ipconfigs
|
||||
}
|
||||
op.Description = description
|
||||
op.IsPublic = isPublic
|
||||
op.IsGlobal = isGlobal
|
||||
err := this.Save(tx, op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
||||
Reference in New Issue
Block a user