简化IP名单中创建IP操作/支持IP以CIDR方式显示

This commit is contained in:
GoEdgeLab
2024-04-13 16:48:24 +08:00
parent 3b3763d35b
commit 06ddd03dd8
8 changed files with 321 additions and 35 deletions

View File

@@ -14,6 +14,7 @@ import (
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/types"
"net"
"strings"
"time"
)
@@ -155,6 +156,59 @@ func (this *IPItemDAO) DisableIPItemsWithIP(tx *dbs.Tx, ipFrom string, ipTo stri
return nil
}
// DisableIPItemsWithIPValue 禁用某个IP相关条目
func (this *IPItemDAO) DisableIPItemsWithIPValue(tx *dbs.Tx, value string, sourceUserId int64, listId int64) error {
if len(value) == 0 {
return errors.New("invalid 'value'")
}
var query = this.Query(tx).
Result("id", "listId").
Attr("value", value).
State(IPItemStateEnabled)
if listId > 0 {
query.Attr("listId", listId)
}
if sourceUserId > 0 {
query.Attr("sourceUserId", sourceUserId)
}
ones, err := query.FindAll()
if err != nil {
return err
}
var itemIds = []int64{}
for _, one := range ones {
var item = one.(*IPItem)
var itemId = int64(item.Id)
itemIds = append(itemIds, itemId)
}
for _, itemId := range itemIds {
version, err := SharedIPListDAO.IncreaseVersion(tx)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(itemId).
Set("state", IPItemStateDisabled).
Set("version", version).
Update()
if err != nil {
return err
}
}
if len(itemIds) > 0 {
return this.NotifyUpdate(tx, itemIds[len(itemIds)-1])
}
return nil
}
// DisableIPItemsWithListId 禁用某个IP名单内的所有IP
func (this *IPItemDAO) DisableIPItemsWithListId(tx *dbs.Tx, listId int64) error {
for {
@@ -236,9 +290,46 @@ func (this *IPItemDAO) DeleteOldItem(tx *dbs.Tx, listId int64, ipFrom string, ip
return nil
}
// DeleteOldItemWithValue 根据IP删除以前的旧记录
func (this *IPItemDAO) DeleteOldItemWithValue(tx *dbs.Tx, listId int64, value string) error {
if len(value) == 0 {
return nil
}
ones, err := this.Query(tx).
ResultPk().
UseIndex("ipFrom").
Attr("listId", listId).
Attr("value", value).
Attr("state", IPItemStateEnabled).
FindAll()
if err != nil {
return err
}
for _, one := range ones {
var itemId = int64(one.(*IPItem).Id)
version, err := SharedIPListDAO.IncreaseVersion(tx)
if err != nil {
return err
}
err = this.Query(tx).
Pk(itemId).
Set("version", version).
Set("state", IPItemStateDisabled).
UpdateQuickly()
if err != nil {
return err
}
}
return nil
}
// CreateIPItem 创建IP
func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx,
listId int64,
value string,
ipFrom string,
ipTo string,
expiredAt int64,
@@ -253,6 +344,15 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx,
sourceHTTPFirewallRuleGroupId int64,
sourceHTTPFirewallRuleSetId int64,
shouldNotify bool) (int64, error) {
// generate 'itemType'
if itemType != IPItemTypeAll && len(ipFrom) > 0 {
if iputils.IsIPv4(ipFrom) {
itemType = IPItemTypeIPv4
} else if iputils.IsIPv6(ipFrom) {
itemType = IPItemTypeIPv6
}
}
version, err := SharedIPListDAO.IncreaseVersion(tx)
if err != nil {
return 0, err
@@ -260,6 +360,7 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx,
var op = NewIPItemOperator()
op.ListId = listId
op.Value = value
op.IpFrom = ipFrom
op.IpTo = ipTo
@@ -318,11 +419,20 @@ func (this *IPItemDAO) CreateIPItem(tx *dbs.Tx,
}
// UpdateIPItem 修改IP
func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipTo string, expiredAt int64, reason string, itemType IPItemType, eventLevel string) error {
func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, value string, ipFrom string, ipTo string, expiredAt int64, reason string, itemType IPItemType, eventLevel string) error {
if itemId <= 0 {
return errors.New("invalid itemId")
}
// generate 'itemType'
if itemType != IPItemTypeAll && len(ipFrom) > 0 {
if iputils.IsIPv4(ipFrom) {
itemType = IPItemTypeIPv4
} else if iputils.IsIPv6(ipFrom) {
itemType = IPItemTypeIPv6
}
}
listId, err := this.Query(tx).
Pk(itemId).
Result("listId").
@@ -341,6 +451,7 @@ func (this *IPItemDAO) UpdateIPItem(tx *dbs.Tx, itemId int64, ipFrom string, ipT
var op = NewIPItemOperator()
op.Id = itemId
op.Value = value
op.IpFrom = ipFrom
op.IpTo = ipTo
@@ -711,6 +822,60 @@ func (this *IPItemDAO) CleanExpiredIPItems(tx *dbs.Tx) error {
return nil
}
// ParseIPValue 解析IP值
func (this *IPItemDAO) ParseIPValue(value string) (newValue string, ipFrom string, ipTo string, ok bool) {
if len(value) == 0 {
return
}
newValue = value
// ip1-ip2
if strings.Contains(value, "-") {
var pieces = strings.Split(value, "-")
if len(pieces) != 2 {
return
}
ipFrom = strings.TrimSpace(pieces[0])
ipTo = strings.TrimSpace(pieces[1])
if !iputils.IsValid(ipFrom) || !iputils.IsValid(ipTo) {
return
}
if !iputils.IsSameVersion(ipFrom, ipTo) {
return
}
if iputils.CompareIP(ipFrom, ipTo) > 0 {
ipFrom, ipTo = ipTo, ipFrom
newValue = ipFrom + "-" + ipTo
}
ok = true
return
}
// ip/mask
if strings.Contains(value, "/") {
cidr, err := iputils.ParseCIDR(value)
if err != nil {
return
}
return newValue, cidr.From().String(), cidr.To().String(), true
}
// single value
if iputils.IsValid(value) {
ipFrom = value
ok = true
return
}
return
}
// NotifyUpdate 通知更新
func (this *IPItemDAO) NotifyUpdate(tx *dbs.Tx, itemId int64) error {
// 获取ListId

View File

@@ -51,7 +51,8 @@ func TestIPItemDAO_CreateManyIPs(t *testing.T) {
var dao = models.NewIPItemDAO()
var n = 10
for i := 0; i < n; i++ {
itemId, err := dao.CreateIPItem(tx, firewallconfigs.GlobalListId, "192."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255)), "", time.Now().Unix()+86400, "test", models.IPItemTypeIPv4, "warning", 0, 0, 0, 0, 0, 0, 0, false)
var ip = "192." + types.String(rands.Int(0, 255)) + "." + types.String(rands.Int(0, 255)) + "." + types.String(rands.Int(0, 255))
itemId, err := dao.CreateIPItem(tx, firewallconfigs.GlobalListId, ip, ip, "", time.Now().Unix()+86400, "test", models.IPItemTypeIPv4, "warning", 0, 0, 0, 0, 0, 0, 0, false)
if err != nil {
t.Fatal(err)
}
@@ -74,3 +75,16 @@ func TestIPItemDAO_DisableIPItemsWithIP(t *testing.T) {
}
t.Log("ok")
}
func TestIPItemDAO_ParseIPValue(t *testing.T) {
var dao = models.NewIPItemDAO()
t.Log(dao.ParseIPValue("192.168.1.100"))
t.Log(dao.ParseIPValue("192.168.1.100-192.168.1.200"))
t.Log(dao.ParseIPValue("192.168.1.200-192.168.1.100"))
t.Log(dao.ParseIPValue("192.168.1.100/24"))
t.Log(dao.ParseIPValue("::1"))
t.Log(dao.ParseIPValue("192.168.1.100-::2"))
t.Log(dao.ParseIPValue("192"))
t.Log(dao.ParseIPValue("192.168.1.200/256"))
t.Log(dao.ParseIPValue("192.168.1.200-"))
}

View File

@@ -5,6 +5,7 @@ import "github.com/iwind/TeaGo/dbs"
const (
IPItemField_Id dbs.FieldName = "id" // ID
IPItemField_ListId dbs.FieldName = "listId" // 所属名单ID
IPItemField_Value dbs.FieldName = "value" // 原始值
IPItemField_Type dbs.FieldName = "type" // 类型
IPItemField_IpFrom dbs.FieldName = "ipFrom" // 开始IP
IPItemField_IpTo dbs.FieldName = "ipTo" // 结束IP
@@ -32,6 +33,7 @@ const (
type IPItem struct {
Id uint64 `field:"id"` // ID
ListId uint32 `field:"listId"` // 所属名单ID
Value string `field:"value"` // 原始值
Type string `field:"type"` // 类型
IpFrom string `field:"ipFrom"` // 开始IP
IpTo string `field:"ipTo"` // 结束IP
@@ -58,6 +60,7 @@ type IPItem struct {
type IPItemOperator struct {
Id any // ID
ListId any // 所属名单ID
Value any // 原始值
Type any // 类型
IpFrom any // 开始IP
IpTo any // 结束IP

View File

@@ -1 +1,15 @@
package models
// ComposeValue 组合原始值
func (this *IPItem) ComposeValue() string {
if len(this.Value) > 0 {
return this.Value
}
// 兼容以往版本
if len(this.IpTo) > 0 {
return this.IpFrom + "-" + this.IpTo
}
return this.IpFrom
}

View File

@@ -16,7 +16,7 @@ import (
func TestServerBandwidthStatDAO_UpdateServerBandwidth(t *testing.T) {
var dao = models.NewServerBandwidthStatDAO()
var tx *dbs.Tx
err := dao.UpdateServerBandwidth(tx, 1, 1, 0, 0, timeutil.Format("Ymd"), timeutil.FormatTime("Hi", time.Now().Unix()/300*300), 1024, 300, 0, 0, 0, 0, 0)
err := dao.UpdateServerBandwidth(tx, 1, 1, 0, 0, timeutil.Format("Ymd"), timeutil.FormatTime("Hi", time.Now().Unix()/300*300), 1024, 300, 0, 0, 0, 0, 0, 0)
if err != nil {
t.Fatal(err)
}
@@ -33,7 +33,7 @@ func TestSeverBandwidthStatDAO_InsertManyStats(t *testing.T) {
}
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -rands.Int(0, 200)))
var minute = fmt.Sprintf("%02d%02d", rands.Int(0, 23), rands.Int(0, 59))
err := dao.UpdateServerBandwidth(tx, 1, int64(rands.Int(1, 10000)), 0, 0, day, minute, 1024, 300, 0, 0, 0, 0, 0)
err := dao.UpdateServerBandwidth(tx, 1, int64(rands.Int(1, 10000)), 0, 0, day, minute, 1024, 300, 0, 0, 0, 0, 0, 0)
if err != nil {
t.Fatal(err)
}