IP名单新增IPv6和所有IP两种类型

This commit is contained in:
GoEdgeLab
2021-02-02 15:25:40 +08:00
parent f742f7728f
commit 04215db205
15 changed files with 227 additions and 47 deletions

View File

@@ -0,0 +1,71 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
FirewallActionStateEnabled = 1 // 已启用
FirewallActionStateDisabled = 0 // 已禁用
)
type FirewallActionDAO dbs.DAO
func NewFirewallActionDAO() *FirewallActionDAO {
return dbs.NewDAO(&FirewallActionDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeFirewallActions",
Model: new(FirewallAction),
PkName: "id",
},
}).(*FirewallActionDAO)
}
var SharedFirewallActionDAO *FirewallActionDAO
func init() {
dbs.OnReady(func() {
SharedFirewallActionDAO = NewFirewallActionDAO()
})
}
// 启用条目
func (this *FirewallActionDAO) EnableFirewallAction(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", FirewallActionStateEnabled).
Update()
return err
}
// 禁用条目
func (this *FirewallActionDAO) DisableFirewallAction(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", FirewallActionStateDisabled).
Update()
return err
}
// 查找启用中的条目
func (this *FirewallActionDAO) FindEnabledFirewallAction(tx *dbs.Tx, id uint32) (*FirewallAction, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", FirewallActionStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*FirewallAction), err
}
// 根据主键查找名称
func (this *FirewallActionDAO) FindFirewallActionName(tx *dbs.Tx, id uint32) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}

View File

@@ -0,0 +1,6 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,26 @@
package models
// 防火墙动作
type FirewallAction struct {
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
Name string `field:"name"` // 名称
Code string `field:"code"` // 快速查询代号
Type string `field:"type"` // 动作类型
Params string `field:"params"` // 参数
State uint8 `field:"state"` // 状态
}
type FirewallActionOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
Name interface{} // 名称
Code interface{} // 快速查询代号
Type interface{} // 动作类型
Params interface{} // 参数
State interface{} // 状态
}
func NewFirewallActionOperator() *FirewallActionOperator {
return &FirewallActionOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -15,6 +15,14 @@ const (
IPItemStateDisabled = 0 // 已禁用
)
type IPItemType = string
const (
IPItemTypeIPv4 IPItemType = "ipv4" // IPv4
IPItemTypeIPv6 IPItemType = "ipv6" // IPv6
IPItemTypeAll IPItemType = "all" // 所有IP
)
type IPItemDAO dbs.DAO
func NewIPItemDAO() *IPItemDAO {
@@ -57,7 +65,11 @@ func (this *IPItemDAO) DisableIPItem(tx *dbs.Tx, id int64) error {
Set("state", IPItemStateDisabled).
Set("version", version).
Update()
return err
if err != nil {
return err
}
return this.NotifyUpdate(tx, id)
}
// 查找启用中的条目
@@ -73,7 +85,7 @@ func (this *IPItemDAO) FindEnabledIPItem(tx *dbs.Tx, id int64) (*IPItem, error)
}
// 创建IP
func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string, expiredAt int64, reason string) (int64, error) {
func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipTo string, expiredAt int64, reason string, itemType IPItemType) (int64, error) {
version, err := SharedIPListDAO.IncreaseVersion(tx)
if err != nil {
return 0, err
@@ -84,6 +96,7 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipT
op.IpFrom = ipFrom
op.IpTo = ipTo
op.Reason = reason
op.Type = itemType
op.Version = version
if expiredAt < 0 {
expiredAt = 0
@@ -94,11 +107,17 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx, listId int64, ipFrom string, ipT
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
itemId := types.Int64(op.Id)
err = this.NotifyUpdate(tx, itemId)
if err != nil {
return 0, err
}
return itemId, nil
}
// 修改IP
func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipTo string, expiredAt int64, reason string) error {
func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipTo string, expiredAt int64, reason string, itemType IPItemType) error {
if itemId <= 0 {
return errors.New("invalid itemId")
}
@@ -124,13 +143,18 @@ func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipT
op.IpFrom = ipFrom
op.IpTo = ipTo
op.Reason = reason
op.Type = itemType
if expiredAt < 0 {
expiredAt = 0
}
op.ExpiredAt = expiredAt
op.Version = version
err = this.Save(tx, op)
return err
if err != nil {
return err
}
return this.NotifyUpdate(tx, itemId)
}
// 计算IP数量
@@ -177,7 +201,7 @@ func (this *IPItemDAO) FindItemListId(tx *dbs.Tx, itemId int64) (int64, error) {
}
// 通知更新
func (this *IPItemDAO) NotifyClustersUpdate(tx *dbs.Tx, itemId int64, taskType NodeTaskType) error {
func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
// 获取ListId
listId, err := this.FindItemListId(tx, itemId)
if err != nil {
@@ -231,7 +255,7 @@ func (this *IPItemDAO) NotifyClustersUpdate(tx *dbs.Tx, itemId int64, taskType N
if len(resultClusterIds) > 0 {
for _, clusterId := range resultClusterIds {
err = SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, taskType)
err = SharedNodeTaskDAO.CreateClusterTask(tx, clusterId, NodeTaskTypeIPItemChanged)
if err != nil {
return err
}

View File

@@ -4,12 +4,14 @@ package models
type IPItem struct {
Id uint64 `field:"id"` // ID
ListId uint32 `field:"listId"` // 所属名单ID
Type string `field:"type"` // 类型
IpFrom string `field:"ipFrom"` // 开始IP
IpTo string `field:"ipTo"` // 结束IP
Version uint64 `field:"version"` // 版本
CreatedAt uint64 `field:"createdAt"` // 创建时间
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
Reason string `field:"reason"` // 加入说明
Action string `field:"action"` // 动作代号
State uint8 `field:"state"` // 状态
ExpiredAt uint64 `field:"expiredAt"` // 过期时间
}
@@ -17,12 +19,14 @@ type IPItem struct {
type IPItemOperator struct {
Id interface{} // ID
ListId interface{} // 所属名单ID
Type interface{} // 类型
IpFrom interface{} // 开始IP
IpTo interface{} // 结束IP
Version interface{} // 版本
CreatedAt interface{} // 创建时间
UpdatedAt interface{} // 修改时间
Reason interface{} // 加入说明
Action interface{} // 动作代号
State interface{} // 状态
ExpiredAt interface{} // 过期时间
}

View File

@@ -2,9 +2,7 @@ package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ipconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -115,21 +113,7 @@ func (this *IPListDAO) UpdateIPList(tx *dbs.Tx, listId int64, name string, code
// 增加版本
func (this *IPListDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
valueJSON, err := SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeIPListVersion)
if err != nil {
return 0, err
}
if len(valueJSON) == 0 {
err = SharedSysSettingDAO.UpdateSetting(tx, systemconfigs.SettingCodeIPListVersion, []byte("1"))
if err != nil {
return 0, err
}
return 1, nil
}
value := types.Int64(string(valueJSON)) + 1
err = SharedSysSettingDAO.UpdateSetting(tx, systemconfigs.SettingCodeIPListVersion, []byte(numberutils.FormatInt64(value)))
return value, nil
return SharedSysLockerDAO.Increase(tx, "IP_LIST_VERSION", 1000000)
}
// 检查用户权限

View File

@@ -61,7 +61,7 @@ func TestNewServerDAO_md5(t *testing.T) {
func TestServerDAO_genDNSName(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
dnsName, err := SharedServerDAO.genDNSName(tx)
dnsName, err := SharedServerDAO.GenDNSName(tx)
if err != nil {
t.Fatal(err)
}

View File

@@ -4,6 +4,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"time"
)
@@ -110,3 +111,33 @@ func (this *SysLockerDAO) Unlock(tx *dbs.Tx, key string) error {
Update()
return err
}
// 增加版本号
func (this *SysLockerDAO) Increase(tx *dbs.Tx, key string, defaultValue int64) (int64, error) {
if tx == nil {
var result int64
var err error
err = this.Instance.RunTx(func(tx *dbs.Tx) error {
result, err = this.Increase(tx, key, defaultValue)
if err != nil {
return err
}
return nil
})
return result, err
}
err := this.Query(tx).
InsertOrUpdateQuickly(maps.Map{
"key": key,
"version": defaultValue,
}, maps.Map{
"version": dbs.SQL("version+1"),
})
if err != nil {
return 0, err
}
return this.Query(tx).
Attr("key", key).
Result("version").
FindInt64Col(0)
}

View File

@@ -3,6 +3,7 @@ package models
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
"sync"
"testing"
)
@@ -22,3 +23,23 @@ func TestSysLockerDAO_Lock(t *testing.T) {
}
}
}
func TestSysLocker_Increase(t *testing.T) {
count := 100
wg := sync.WaitGroup{}
wg.Add(count)
for i := 0; i < count; i++ {
go func() {
defer wg.Done()
v, err := NewSysLockerDAO().Increase(nil, "hello", 0)
if err != nil {
t.Fatal(err)
}
t.Log("v:", v)
}()
}
wg.Wait()
t.Log("ok")
}