mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-03 13:06:35 +08:00
实现IP名单管理
This commit is contained in:
147
internal/db/models/ip_item_dao.go
Normal file
147
internal/db/models/ip_item_dao.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
const (
|
||||
IPItemStateEnabled = 1 // 已启用
|
||||
IPItemStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type IPItemDAO dbs.DAO
|
||||
|
||||
func NewIPItemDAO() *IPItemDAO {
|
||||
return dbs.NewDAO(&IPItemDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeIPItems",
|
||||
Model: new(IPItem),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*IPItemDAO)
|
||||
}
|
||||
|
||||
var SharedIPItemDAO *IPItemDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedIPItemDAO = NewIPItemDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *IPItemDAO) EnableIPItem(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", IPItemStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *IPItemDAO) DisableIPItem(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", IPItemStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *IPItemDAO) FindEnabledIPItem(id int64) (*IPItem, error) {
|
||||
result, err := this.Query().
|
||||
Pk(id).
|
||||
Attr("state", IPItemStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*IPItem), err
|
||||
}
|
||||
|
||||
// 创建IP
|
||||
func (this *IPItemDAO) CreateIPItem(listId int64, ipFrom string, ipTo string, expiredAt int64, reason string) (int64, error) {
|
||||
version, err := SharedIPListDAO.IncreaseVersion(listId)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
op := NewIPItemOperator()
|
||||
op.ListId = listId
|
||||
op.IpFrom = ipFrom
|
||||
op.IpTo = ipTo
|
||||
op.Reason = reason
|
||||
op.Version = version
|
||||
if expiredAt < 0 {
|
||||
expiredAt = 0
|
||||
}
|
||||
op.ExpiredAt = expiredAt
|
||||
op.State = IPItemStateEnabled
|
||||
_, err = this.Save(op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 修改IP
|
||||
func (this *IPItemDAO) UpdateIPItem(itemId int64, ipFrom string, ipTo string, expiredAt int64, reason string) error {
|
||||
if itemId <= 0 {
|
||||
return errors.New("invalid itemId")
|
||||
}
|
||||
|
||||
listId, err := this.Query().
|
||||
Pk(itemId).
|
||||
Result("listId").
|
||||
FindInt64Col(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if listId == 0 {
|
||||
return errors.New("not found")
|
||||
}
|
||||
|
||||
version, err := SharedIPListDAO.IncreaseVersion(listId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
op := NewIPItemOperator()
|
||||
op.Id = itemId
|
||||
op.IpFrom = ipFrom
|
||||
op.IpTo = ipTo
|
||||
op.Reason = reason
|
||||
if expiredAt < 0 {
|
||||
expiredAt = 0
|
||||
}
|
||||
op.ExpiredAt = expiredAt
|
||||
op.Version = version
|
||||
_, err = this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 计算IP数量
|
||||
func (this *IPItemDAO) CountIPItemsWithListId(listId int64) (int64, error) {
|
||||
return this.Query().
|
||||
State(IPItemStateEnabled).
|
||||
Attr("listId", listId).
|
||||
Count()
|
||||
}
|
||||
|
||||
// 查找IP列表
|
||||
func (this *IPItemDAO) ListIPItemsWithListId(listId int64, offset int64, size int64) (result []*IPItem, err error) {
|
||||
_, err = this.Query().
|
||||
State(IPItemStateEnabled).
|
||||
Attr("listId", listId).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
5
internal/db/models/ip_item_dao_test.go
Normal file
5
internal/db/models/ip_item_dao_test.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
32
internal/db/models/ip_item_model.go
Normal file
32
internal/db/models/ip_item_model.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package models
|
||||
|
||||
// IP
|
||||
type IPItem struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
ListId uint32 `field:"listId"` // 所属名单ID
|
||||
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"` // 加入说明
|
||||
State uint8 `field:"state"` // 状态
|
||||
ExpiredAt uint64 `field:"expiredAt"` // 过期时间
|
||||
}
|
||||
|
||||
type IPItemOperator struct {
|
||||
Id interface{} // ID
|
||||
ListId interface{} // 所属名单ID
|
||||
IpFrom interface{} // 开始IP
|
||||
IpTo interface{} // 结束IP
|
||||
Version interface{} // 版本
|
||||
CreatedAt interface{} // 创建时间
|
||||
UpdatedAt interface{} // 修改时间
|
||||
Reason interface{} // 加入说明
|
||||
State interface{} // 状态
|
||||
ExpiredAt interface{} // 过期时间
|
||||
}
|
||||
|
||||
func NewIPItemOperator() *IPItemOperator {
|
||||
return &IPItemOperator{}
|
||||
}
|
||||
1
internal/db/models/ip_item_model_ext.go
Normal file
1
internal/db/models/ip_item_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
129
internal/db/models/ip_list_dao.go
Normal file
129
internal/db/models/ip_list_dao.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ipconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
const (
|
||||
IPListStateEnabled = 1 // 已启用
|
||||
IPListStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type IPListDAO dbs.DAO
|
||||
|
||||
func NewIPListDAO() *IPListDAO {
|
||||
return dbs.NewDAO(&IPListDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeIPLists",
|
||||
Model: new(IPList),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*IPListDAO)
|
||||
}
|
||||
|
||||
var SharedIPListDAO *IPListDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedIPListDAO = NewIPListDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *IPListDAO) EnableIPList(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", IPListStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *IPListDAO) DisableIPList(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", IPListStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *IPListDAO) FindEnabledIPList(id int64) (*IPList, error) {
|
||||
result, err := this.Query().
|
||||
Pk(id).
|
||||
Attr("state", IPListStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*IPList), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
func (this *IPListDAO) FindIPListName(id int64) (string, error) {
|
||||
return this.Query().
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 创建名单
|
||||
func (this *IPListDAO) CreateIPList(listType ipconfigs.IPListType, name string, code string, timeoutJSON []byte) (int64, error) {
|
||||
op := NewIPListOperator()
|
||||
op.IsOn = true
|
||||
op.State = IPListStateEnabled
|
||||
op.Type = listType
|
||||
op.Name = name
|
||||
op.Code = code
|
||||
if len(timeoutJSON) > 0 {
|
||||
op.Timeout = timeoutJSON
|
||||
}
|
||||
_, err := this.Save(op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 修改名单
|
||||
func (this *IPListDAO) UpdateIPList(listId int64, name string, code string, timeoutJSON []byte) error {
|
||||
if listId <= 0 {
|
||||
return errors.New("invalid listId")
|
||||
}
|
||||
op := NewIPListOperator()
|
||||
op.Id = listId
|
||||
op.Name = name
|
||||
op.Code = code
|
||||
if len(timeoutJSON) > 0 {
|
||||
op.Timeout = timeoutJSON
|
||||
} else {
|
||||
op.Timeout = "null"
|
||||
}
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 增加版本
|
||||
func (this *IPListDAO) IncreaseVersion(listId int64) (int64, error) {
|
||||
if listId <= 0 {
|
||||
return 0, errors.New("invalid listId")
|
||||
}
|
||||
op := NewIPListOperator()
|
||||
op.Id = listId
|
||||
op.Version = dbs.SQL("version+1")
|
||||
_, err := this.Save(op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return this.Query().
|
||||
Pk(listId).
|
||||
Result("version").
|
||||
FindInt64Col(0)
|
||||
}
|
||||
25
internal/db/models/ip_list_dao_test.go
Normal file
25
internal/db/models/ip_list_dao_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIPListDAO_IncreaseVersion(t *testing.T) {
|
||||
dao := NewIPListDAO()
|
||||
version, err := dao.IncreaseVersion(1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("version:", version)
|
||||
}
|
||||
|
||||
func BenchmarkIPListDAO_IncreaseVersion(b *testing.B) {
|
||||
runtime.GOMAXPROCS(1)
|
||||
|
||||
dao := NewIPListDAO()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _ = dao.IncreaseVersion(1)
|
||||
}
|
||||
}
|
||||
34
internal/db/models/ip_list_model.go
Normal file
34
internal/db/models/ip_list_model.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package models
|
||||
|
||||
// IP名单
|
||||
type IPList struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
Type string `field:"type"` // 类型
|
||||
AdminId uint32 `field:"adminId"` // 用户ID
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
Name string `field:"name"` // 列表名
|
||||
Code string `field:"code"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Timeout string `field:"timeout"` // 默认超时时间
|
||||
Version uint64 `field:"version"` // 版本
|
||||
}
|
||||
|
||||
type IPListOperator struct {
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
Type interface{} // 类型
|
||||
AdminId interface{} // 用户ID
|
||||
UserId interface{} // 用户ID
|
||||
Name interface{} // 列表名
|
||||
Code interface{} // 代号
|
||||
State interface{} // 状态
|
||||
CreatedAt interface{} // 创建时间
|
||||
Timeout interface{} // 默认超时时间
|
||||
Version interface{} // 版本
|
||||
}
|
||||
|
||||
func NewIPListOperator() *IPListOperator {
|
||||
return &IPListOperator{}
|
||||
}
|
||||
1
internal/db/models/ip_list_model_ext.go
Normal file
1
internal/db/models/ip_list_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
Reference in New Issue
Block a user