mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	IP名单新增IPv6和所有IP两种类型
This commit is contained in:
		
							
								
								
									
										4
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.mod
									
									
									
									
									
								
							@@ -4,8 +4,6 @@ go 1.15
 | 
			
		||||
 | 
			
		||||
replace github.com/TeaOSLab/EdgeCommon => ../EdgeCommon
 | 
			
		||||
 | 
			
		||||
replace github.com/iwind/TeaGo => /Users/WorkSpace/TeaGo
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
 | 
			
		||||
	github.com/TeaOSLab/EdgeCommon v0.0.0-00010101000000-000000000000
 | 
			
		||||
@@ -15,7 +13,7 @@ require (
 | 
			
		||||
	github.com/go-sql-driver/mysql v1.5.0
 | 
			
		||||
	github.com/go-yaml/yaml v2.1.0+incompatible
 | 
			
		||||
	github.com/golang/protobuf v1.4.2
 | 
			
		||||
	github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e
 | 
			
		||||
	github.com/iwind/TeaGo v0.0.0-20210125103732-4d79fc3c3d0b
 | 
			
		||||
	github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
 | 
			
		||||
	github.com/mozillazg/go-pinyin v0.18.0
 | 
			
		||||
	github.com/pkg/sftp v1.12.0
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										3
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								go.sum
									
									
									
									
									
								
							@@ -173,6 +173,9 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
 | 
			
		||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
 | 
			
		||||
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
 | 
			
		||||
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
 | 
			
		||||
github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
 | 
			
		||||
github.com/iwind/TeaGo v0.0.0-20210125103732-4d79fc3c3d0b h1:niQL2t//041GUaqDPM5D+ldyr0Ng2WKwZJHPRLQhQtk=
 | 
			
		||||
github.com/iwind/TeaGo v0.0.0-20210125103732-4d79fc3c3d0b/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
 | 
			
		||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
 | 
			
		||||
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
 | 
			
		||||
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										71
									
								
								internal/db/models/firewall_action_dao.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								internal/db/models/firewall_action_dao.go
									
									
									
									
									
										Normal 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("")
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										6
									
								
								internal/db/models/firewall_action_dao_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								internal/db/models/firewall_action_dao_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	_ "github.com/iwind/TeaGo/bootstrap"
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										26
									
								
								internal/db/models/firewall_action_model.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								internal/db/models/firewall_action_model.go
									
									
									
									
									
										Normal 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{}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								internal/db/models/firewall_action_model_ext.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								internal/db/models/firewall_action_model_ext.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
package models
 | 
			
		||||
@@ -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
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -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{} // 过期时间
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 检查用户权限
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,8 +3,9 @@ package iplibrary
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	"github.com/iwind/TeaGo/logs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
 | 
			
		||||
	"github.com/lionsoul2014/ip2region/binding/golang/ip2region"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type IP2RegionLibrary struct {
 | 
			
		||||
@@ -26,11 +27,16 @@ func (this *IP2RegionLibrary) Lookup(ip string) (*Result, error) {
 | 
			
		||||
		return nil, errors.New("library has not been loaded")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 暂不支持IPv6
 | 
			
		||||
	if strings.Contains(ip, ":") {
 | 
			
		||||
		return nil, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer func() {
 | 
			
		||||
		// 防止panic发生
 | 
			
		||||
		err := recover()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			logs.Println("[IP2RegionLibrary]panic: " + fmt.Sprintf("%#v", err))
 | 
			
		||||
			remotelogs.Error("IP2RegionLibrary", "panic: "+fmt.Sprintf("%#v", err))
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -47,13 +47,11 @@ func (this *IPItemService) CreateIPItem(ctx context.Context, req *pb.CreateIPIte
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	itemId, err := models.SharedIPItemDAO.CreateIPItem(tx, req.IpListId, req.IpFrom, req.IpTo, req.ExpiredAt, req.Reason)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	if len(req.Type) == 0 {
 | 
			
		||||
		req.Type = models.IPItemTypeIPv4
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 通知更新
 | 
			
		||||
	err = models.SharedIPListDAO.NotifyUpdate(tx, req.IpListId, models.NodeTaskTypeIPItemChanged)
 | 
			
		||||
	itemId, err := models.SharedIPItemDAO.CreateIPItem(tx, req.IpListId, req.IpFrom, req.IpTo, req.ExpiredAt, req.Reason, req.Type)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -83,13 +81,11 @@ func (this *IPItemService) UpdateIPItem(ctx context.Context, req *pb.UpdateIPIte
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedIPItemDAO.UpdateIPItem(tx, req.IpItemId, req.IpFrom, req.IpTo, req.ExpiredAt, req.Reason)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	if len(req.Type) == 0 {
 | 
			
		||||
		req.Type = models.IPItemTypeIPv4
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 通知更新
 | 
			
		||||
	err = models.SharedIPItemDAO.NotifyClustersUpdate(tx, req.IpItemId, models.NodeTaskTypeIPItemChanged)
 | 
			
		||||
	err = models.SharedIPItemDAO.UpdateIPItem(tx, req.IpItemId, req.IpFrom, req.IpTo, req.ExpiredAt, req.Reason, req.Type)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -124,12 +120,6 @@ func (this *IPItemService) DeleteIPItem(ctx context.Context, req *pb.DeleteIPIte
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 通知更新
 | 
			
		||||
	err = models.SharedIPItemDAO.NotifyClustersUpdate(tx, req.IpItemId, models.NodeTaskTypeIPItemChanged)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -180,6 +170,10 @@ func (this *IPItemService) ListIPItemsWithListId(ctx context.Context, req *pb.Li
 | 
			
		||||
	}
 | 
			
		||||
	result := []*pb.IPItem{}
 | 
			
		||||
	for _, item := range items {
 | 
			
		||||
		if len(item.Type) == 0 {
 | 
			
		||||
			item.Type = models.IPItemTypeIPv4
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		result = append(result, &pb.IPItem{
 | 
			
		||||
			Id:        int64(item.Id),
 | 
			
		||||
			IpFrom:    item.IpFrom,
 | 
			
		||||
@@ -187,6 +181,7 @@ func (this *IPItemService) ListIPItemsWithListId(ctx context.Context, req *pb.Li
 | 
			
		||||
			Version:   int64(item.Version),
 | 
			
		||||
			ExpiredAt: int64(item.ExpiredAt),
 | 
			
		||||
			Reason:    item.Reason,
 | 
			
		||||
			Type:      item.Type,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -218,6 +213,10 @@ func (this *IPItemService) FindEnabledIPItem(ctx context.Context, req *pb.FindEn
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(item.Type) == 0 {
 | 
			
		||||
		item.Type = models.IPItemTypeIPv4
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.FindEnabledIPItemResponse{IpItem: &pb.IPItem{
 | 
			
		||||
		Id:        int64(item.Id),
 | 
			
		||||
		IpFrom:    item.IpFrom,
 | 
			
		||||
@@ -225,6 +224,7 @@ func (this *IPItemService) FindEnabledIPItem(ctx context.Context, req *pb.FindEn
 | 
			
		||||
		Version:   int64(item.Version),
 | 
			
		||||
		ExpiredAt: int64(item.ExpiredAt),
 | 
			
		||||
		Reason:    item.Reason,
 | 
			
		||||
		Type:      item.Type,
 | 
			
		||||
	}}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -244,6 +244,10 @@ func (this *IPItemService) ListIPItemsAfterVersion(ctx context.Context, req *pb.
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	for _, item := range items {
 | 
			
		||||
		if len(item.Type) == 0 {
 | 
			
		||||
			item.Type = models.IPItemTypeIPv4
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		result = append(result, &pb.IPItem{
 | 
			
		||||
			Id:        int64(item.Id),
 | 
			
		||||
			IpFrom:    item.IpFrom,
 | 
			
		||||
@@ -253,6 +257,7 @@ func (this *IPItemService) ListIPItemsAfterVersion(ctx context.Context, req *pb.
 | 
			
		||||
			Reason:    "", // 这里我们不需要这个数据
 | 
			
		||||
			ListId:    int64(item.ListId),
 | 
			
		||||
			IsDeleted: item.State == 0,
 | 
			
		||||
			Type:      item.Type,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Reference in New Issue
	
	Block a user