mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	实现基本的区域监控终端管理功能
This commit is contained in:
		
							
								
								
									
										28
									
								
								internal/db/models/connectivity_result_dao.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								internal/db/models/connectivity_result_dao.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ConnectivityResultDAO dbs.DAO
 | 
			
		||||
 | 
			
		||||
func NewConnectivityResultDAO() *ConnectivityResultDAO {
 | 
			
		||||
	return dbs.NewDAO(&ConnectivityResultDAO{
 | 
			
		||||
		DAOObject: dbs.DAOObject{
 | 
			
		||||
			DB:     Tea.Env,
 | 
			
		||||
			Table:  "edgeConnectivityResults",
 | 
			
		||||
			Model:  new(ConnectivityResult),
 | 
			
		||||
			PkName: "id",
 | 
			
		||||
		},
 | 
			
		||||
	}).(*ConnectivityResultDAO)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var SharedConnectivityResultDAO *ConnectivityResultDAO
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	dbs.OnReady(func() {
 | 
			
		||||
		SharedConnectivityResultDAO = NewConnectivityResultDAO()
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										6
									
								
								internal/db/models/connectivity_result_dao_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								internal/db/models/connectivity_result_dao_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	_ "github.com/iwind/TeaGo/bootstrap"
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										30
									
								
								internal/db/models/connectivity_result_model.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								internal/db/models/connectivity_result_model.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
// ConnectivityResult 连通性监控结果
 | 
			
		||||
type ConnectivityResult struct {
 | 
			
		||||
	Id           uint64  `field:"id"`           // ID
 | 
			
		||||
	Type         string  `field:"type"`         // 对象类型
 | 
			
		||||
	TargetId     uint32  `field:"targetId"`     // 对象ID
 | 
			
		||||
	UpdatedAt    uint64  `field:"updatedAt"`    // 更新时间
 | 
			
		||||
	ReportNodeId uint32  `field:"reportNodeId"` // 监控节点ID
 | 
			
		||||
	IsOk         uint8   `field:"isOk"`         // 是否可连接
 | 
			
		||||
	CostMs       float64 `field:"costMs"`       // 单次连接花费的时间
 | 
			
		||||
	Port         uint32  `field:"port"`         // 连接的端口
 | 
			
		||||
	Error        string  `field:"error"`        // 产生的错误信息
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ConnectivityResultOperator struct {
 | 
			
		||||
	Id           interface{} // ID
 | 
			
		||||
	Type         interface{} // 对象类型
 | 
			
		||||
	TargetId     interface{} // 对象ID
 | 
			
		||||
	UpdatedAt    interface{} // 更新时间
 | 
			
		||||
	ReportNodeId interface{} // 监控节点ID
 | 
			
		||||
	IsOk         interface{} // 是否可连接
 | 
			
		||||
	CostMs       interface{} // 单次连接花费的时间
 | 
			
		||||
	Port         interface{} // 连接的端口
 | 
			
		||||
	Error        interface{} // 产生的错误信息
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewConnectivityResultOperator() *ConnectivityResultOperator {
 | 
			
		||||
	return &ConnectivityResultOperator{}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								internal/db/models/connectivity_result_model_ext.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								internal/db/models/connectivity_result_model_ext.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
package models
 | 
			
		||||
@@ -43,8 +43,11 @@ const (
 | 
			
		||||
	MessageTypeIPAddrUp                   MessageType = "IPAddrUp"                   // IP地址上线
 | 
			
		||||
	MessageTypeIPAddrDown                 MessageType = "IPAddrDown"                 // IP地址下线
 | 
			
		||||
 | 
			
		||||
	MessageTypeNSNodeInactive MessageType = "NSNodeInactive" // 边缘节点不活跃
 | 
			
		||||
	MessageTypeNSNodeActive   MessageType = "NSNodeActive"   // 边缘节点活跃
 | 
			
		||||
	MessageTypeNSNodeInactive MessageType = "NSNodeInactive" // NS节点不活跃
 | 
			
		||||
	MessageTypeNSNodeActive   MessageType = "NSNodeActive"   // NS节点活跃
 | 
			
		||||
 | 
			
		||||
	MessageTypeReportNodeInactive MessageType = "ReportNodeInactive" // 区域监控节点节点不活跃
 | 
			
		||||
	MessageTypeReportNodeActive   MessageType = "ReportNodeActive"   // 区域监控节点活跃
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type MessageDAO dbs.DAO
 | 
			
		||||
 
 | 
			
		||||
@@ -8,10 +8,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"
 | 
			
		||||
	"math"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@@ -278,143 +275,6 @@ func (this *NodeIPAddressDAO) FindNodeAccessAndUpIPAddresses(tx *dbs.Tx, nodeId
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FireThresholds 触发阈值
 | 
			
		||||
func (this *NodeIPAddressDAO) FireThresholds(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) error {
 | 
			
		||||
 | 
			
		||||
	ones, err := this.Query(tx).
 | 
			
		||||
		Attr("state", NodeIPAddressStateEnabled).
 | 
			
		||||
		Attr("role", role).
 | 
			
		||||
		Attr("nodeId", nodeId).
 | 
			
		||||
		Attr("canAccess", true).
 | 
			
		||||
		Attr("isOn", true).
 | 
			
		||||
		FindAll()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	for _, one := range ones {
 | 
			
		||||
		addr := one.(*NodeIPAddress)
 | 
			
		||||
		var thresholds = addr.DecodeThresholds()
 | 
			
		||||
		if len(thresholds) == 0 {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		var isOk = true
 | 
			
		||||
		var positiveSummarys = []string{}
 | 
			
		||||
		var negativeSummarys = []string{}
 | 
			
		||||
		for _, threshold := range thresholds {
 | 
			
		||||
			if threshold.Value <= 0 || threshold.Duration <= 0 {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			var value = float64(0)
 | 
			
		||||
			var summary = ""
 | 
			
		||||
			var op = nodeconfigs.FindNodeValueOperatorName(threshold.Operator)
 | 
			
		||||
			switch threshold.Item {
 | 
			
		||||
			case "avgRequests":
 | 
			
		||||
				value, err = SharedNodeValueDAO.SumValues(tx, role, nodeId, nodeconfigs.NodeValueItemRequests, "total", nodeconfigs.NodeValueSumMethodAvg, types.Int32(threshold.Duration), threshold.DurationUnit)
 | 
			
		||||
				value = math.Round(value / 60)
 | 
			
		||||
				summary = "平均请求数:" + types.String(value) + "/s,阈值:" + op + " " + types.String(threshold.Value) + "/s"
 | 
			
		||||
			case "avgTrafficOut":
 | 
			
		||||
				value, err = SharedNodeValueDAO.SumValues(tx, role, nodeId, nodeconfigs.NodeValueItemTrafficOut, "total", nodeconfigs.NodeValueSumMethodAvg, types.Int32(threshold.Duration), threshold.DurationUnit)
 | 
			
		||||
				value = math.Round(value*100/1024/1024/60) / 100 // 100 = 两位小数
 | 
			
		||||
				summary = "平均下行流量:" + types.String(value) + "MB/s,阈值:" + op + " " + types.String(threshold.Value) + "MB/s"
 | 
			
		||||
			case "avgTrafficIn":
 | 
			
		||||
				value, err = SharedNodeValueDAO.SumValues(tx, role, nodeId, nodeconfigs.NodeValueItemTrafficIn, "total", nodeconfigs.NodeValueSumMethodAvg, types.Int32(threshold.Duration), threshold.DurationUnit)
 | 
			
		||||
				value = math.Round(value*100/1024/1024/60) / 100 // 100 = 两位小数
 | 
			
		||||
				summary = "平均上行流量:" + types.String(value) + "MB/s,阈值:" + op + " " + types.String(threshold.Value) + "MB/s"
 | 
			
		||||
			default:
 | 
			
		||||
				// TODO 支持更多
 | 
			
		||||
				err = errors.New("threshold item '" + threshold.Item + "' not supported")
 | 
			
		||||
			}
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			if !nodeconfigs.CompareNodeValue(threshold.Operator, value, float64(threshold.Value)) {
 | 
			
		||||
				isOk = false
 | 
			
		||||
				negativeSummarys = append(negativeSummarys, summary)
 | 
			
		||||
			} else {
 | 
			
		||||
				positiveSummarys = append(positiveSummarys, summary)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if isOk && addr.IsUp == 0 { // 新上线
 | 
			
		||||
			_, err := this.Query(tx).
 | 
			
		||||
				Pk(addr.Id).
 | 
			
		||||
				Set("isUp", true).
 | 
			
		||||
				Update()
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// 增加日志
 | 
			
		||||
			var description = ""
 | 
			
		||||
			if len(negativeSummarys) > 0 {
 | 
			
		||||
				description = "触发阈值:" + strings.Join(negativeSummarys, ",")
 | 
			
		||||
			} else {
 | 
			
		||||
				description = "触发阈值:" + strings.Join(positiveSummarys, ",")
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			err = SharedNodeIPAddressLogDAO.CreateLog(tx, 0, int64(addr.Id), description)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			clusterId, err := SharedNodeDAO.FindNodeClusterId(tx, nodeId)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			err = SharedMessageDAO.CreateNodeMessage(tx, role, clusterId, nodeId, MessageTypeIPAddrUp, MessageLevelSuccess, "节点IP'"+addr.Ip+"'因为达到阈值而上线", "节点IP'"+addr.Ip+"'因为达到阈值而上线。"+description+"。", maps.Map{
 | 
			
		||||
				"addrId": addr.Id,
 | 
			
		||||
			}.AsJSON())
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			err = this.NotifyUpdate(tx, int64(addr.Id))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
		} else if !isOk && addr.IsUp == 1 { // 新离线
 | 
			
		||||
			_, err := this.Query(tx).
 | 
			
		||||
				Pk(addr.Id).
 | 
			
		||||
				Set("isUp", false).
 | 
			
		||||
				Update()
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// 增加日志
 | 
			
		||||
			var description = ""
 | 
			
		||||
			if len(negativeSummarys) > 0 {
 | 
			
		||||
				description = "触发阈值:" + strings.Join(negativeSummarys, ";")
 | 
			
		||||
			} else {
 | 
			
		||||
				description = "触发阈值:" + strings.Join(positiveSummarys, ";")
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			err = SharedNodeIPAddressLogDAO.CreateLog(tx, 0, int64(addr.Id), description)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			clusterId, err := SharedNodeDAO.FindNodeClusterId(tx, nodeId)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			err = SharedMessageDAO.CreateNodeMessage(tx, role, clusterId, nodeId, MessageTypeIPAddrDown, MessageLevelWarning, "节点IP'"+addr.Ip+"'因为达到阈值而下线", "节点IP'"+addr.Ip+"'因为达到阈值而下线。"+description+"。", maps.Map{
 | 
			
		||||
				"addrId": addr.Id,
 | 
			
		||||
			}.AsJSON())
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			err = this.NotifyUpdate(tx, int64(addr.Id))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountAllEnabledIPAddresses 计算IP地址数量
 | 
			
		||||
// TODO 目前支持边缘节点,将来支持NS节点
 | 
			
		||||
func (this *NodeIPAddressDAO) CountAllEnabledIPAddresses(tx *dbs.Tx, role string, nodeClusterId int64, upState configutils.BoolState, keyword string) (int64, error) {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								internal/db/models/node_ip_address_dao_community.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								internal/db/models/node_ip_address_dao_community.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
 | 
			
		||||
//go:build community
 | 
			
		||||
// +build community
 | 
			
		||||
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// FireThresholds 触发阈值
 | 
			
		||||
func (this *NodeIPAddressDAO) FireThresholds(tx *dbs.Tx, role nodeconfigs.NodeRole, nodeId int64) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										269
									
								
								internal/db/models/report_node_dao.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										269
									
								
								internal/db/models/report_node_dao.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,269 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/reporterconfigs"
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
	"github.com/iwind/TeaGo/rands"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	ReportNodeStateEnabled  = 1 // 已启用
 | 
			
		||||
	ReportNodeStateDisabled = 0 // 已禁用
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ReportNodeDAO dbs.DAO
 | 
			
		||||
 | 
			
		||||
func NewReportNodeDAO() *ReportNodeDAO {
 | 
			
		||||
	return dbs.NewDAO(&ReportNodeDAO{
 | 
			
		||||
		DAOObject: dbs.DAOObject{
 | 
			
		||||
			DB:     Tea.Env,
 | 
			
		||||
			Table:  "edgeReportNodes",
 | 
			
		||||
			Model:  new(ReportNode),
 | 
			
		||||
			PkName: "id",
 | 
			
		||||
		},
 | 
			
		||||
	}).(*ReportNodeDAO)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var SharedReportNodeDAO *ReportNodeDAO
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	dbs.OnReady(func() {
 | 
			
		||||
		SharedReportNodeDAO = NewReportNodeDAO()
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// EnableReportNode 启用条目
 | 
			
		||||
func (this *ReportNodeDAO) EnableReportNode(tx *dbs.Tx, id int64) error {
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
		Set("state", ReportNodeStateEnabled).
 | 
			
		||||
		Update()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DisableReportNode 禁用条目
 | 
			
		||||
func (this *ReportNodeDAO) DisableReportNode(tx *dbs.Tx, id int64) error {
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
		Set("state", ReportNodeStateDisabled).
 | 
			
		||||
		Update()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindEnabledReportNode 查找启用中的条目
 | 
			
		||||
func (this *ReportNodeDAO) FindEnabledReportNode(tx *dbs.Tx, id int64) (*ReportNode, error) {
 | 
			
		||||
	result, err := this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
		Attr("state", ReportNodeStateEnabled).
 | 
			
		||||
		Find()
 | 
			
		||||
	if result == nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return result.(*ReportNode), err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindReportNodeName 根据主键查找名称
 | 
			
		||||
func (this *ReportNodeDAO) FindReportNodeName(tx *dbs.Tx, id int64) (string, error) {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		Pk(id).
 | 
			
		||||
		Result("name").
 | 
			
		||||
		FindStringCol("")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateReportNode 创建终端
 | 
			
		||||
func (this *ReportNodeDAO) CreateReportNode(tx *dbs.Tx, name string, location string, isp string, allowIPs []string) (int64, error) {
 | 
			
		||||
	uniqueId, err := this.GenUniqueId(tx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	secret := rands.String(32)
 | 
			
		||||
 | 
			
		||||
	// 保存API Token
 | 
			
		||||
	err = SharedApiTokenDAO.CreateAPIToken(tx, uniqueId, secret, nodeconfigs.NodeRoleReport)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op := NewReportNodeOperator()
 | 
			
		||||
	op.UniqueId = uniqueId
 | 
			
		||||
	op.Secret = secret
 | 
			
		||||
	op.Name = name
 | 
			
		||||
	op.Location = location
 | 
			
		||||
	op.Isp = isp
 | 
			
		||||
 | 
			
		||||
	if len(allowIPs) > 0 {
 | 
			
		||||
		allowIPSJSON, err := json.Marshal(allowIPs)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return 0, err
 | 
			
		||||
		}
 | 
			
		||||
		op.AllowIPs = allowIPSJSON
 | 
			
		||||
	} else {
 | 
			
		||||
		op.AllowIPs = "[]"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op.IsOn = true
 | 
			
		||||
	op.State = ReportNodeStateEnabled
 | 
			
		||||
	return this.SaveInt64(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateReportNode 修改终端
 | 
			
		||||
func (this *ReportNodeDAO) UpdateReportNode(tx *dbs.Tx, nodeId int64, name string, location string, isp string, allowIPs []string, isOn bool) error {
 | 
			
		||||
	if nodeId <= 0 {
 | 
			
		||||
		return errors.New("invalid nodeId")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op := NewReportNodeOperator()
 | 
			
		||||
	op.Id = nodeId
 | 
			
		||||
	op.Name = name
 | 
			
		||||
	op.Location = location
 | 
			
		||||
	op.Isp = isp
 | 
			
		||||
 | 
			
		||||
	if len(allowIPs) > 0 {
 | 
			
		||||
		allowIPSJSON, err := json.Marshal(allowIPs)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		op.AllowIPs = allowIPSJSON
 | 
			
		||||
	} else {
 | 
			
		||||
		op.AllowIPs = "[]"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	op.IsOn = isOn
 | 
			
		||||
	return this.Save(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountAllEnabledReportNodes 计算终端数量
 | 
			
		||||
func (this *ReportNodeDAO) CountAllEnabledReportNodes(tx *dbs.Tx, keyword string) (int64, error) {
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		State(ReportNodeStateEnabled)
 | 
			
		||||
	if len(keyword) > 0 {
 | 
			
		||||
		query.Where("(name LIKE :keyword OR location LIKE :keyword OR isp LIKE :keyword OR allowIPs LIKE :keyword OR (status IS NOT NULL AND JSON_EXTRACT(status, 'ip') LIKE :keyword))")
 | 
			
		||||
		query.Param("keyword", "%"+keyword+"%")
 | 
			
		||||
	}
 | 
			
		||||
	return query.Count()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListEnabledReportNodes 列出单页终端
 | 
			
		||||
func (this *ReportNodeDAO) ListEnabledReportNodes(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*ReportNode, err error) {
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		State(ReportNodeStateEnabled)
 | 
			
		||||
	if len(keyword) > 0 {
 | 
			
		||||
		query.Where(`(
 | 
			
		||||
	name LIKE :keyword 
 | 
			
		||||
	OR location LIKE :keyword 
 | 
			
		||||
	OR isp LIKE :keyword 
 | 
			
		||||
	OR allowIPs LIKE :keyword 
 | 
			
		||||
	OR (status IS NOT NULL 
 | 
			
		||||
		AND (
 | 
			
		||||
			JSON_EXTRACT(status, '$.ip') LIKE :keyword) 
 | 
			
		||||
			OR (LENGTH(location)=0 AND JSON_EXTRACT(status, '$.location') LIKE :keyword) 
 | 
			
		||||
			OR (LENGTH(isp)=0 AND JSON_EXTRACT(status, '$.isp') LIKE :keyword)
 | 
			
		||||
       ))`)
 | 
			
		||||
		query.Param("keyword", "%"+keyword+"%")
 | 
			
		||||
	}
 | 
			
		||||
	query.Slice(&result)
 | 
			
		||||
	_, err = query.Asc("isActive").
 | 
			
		||||
		Offset(offset).
 | 
			
		||||
		Limit(size).
 | 
			
		||||
		DescPk().
 | 
			
		||||
		FindAll()
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GenUniqueId 生成唯一ID
 | 
			
		||||
func (this *ReportNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
 | 
			
		||||
	for {
 | 
			
		||||
		uniqueId := rands.HexString(32)
 | 
			
		||||
		ok, err := this.Query(tx).
 | 
			
		||||
			Attr("uniqueId", uniqueId).
 | 
			
		||||
			Exist()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
		if ok {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		return uniqueId, nil
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateNodeActive 修改节点活跃状态
 | 
			
		||||
func (this *ReportNodeDAO) UpdateNodeActive(tx *dbs.Tx, nodeId int64, isActive bool) error {
 | 
			
		||||
	if nodeId <= 0 {
 | 
			
		||||
		return errors.New("invalid nodeId")
 | 
			
		||||
	}
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Pk(nodeId).
 | 
			
		||||
		Set("isActive", isActive).
 | 
			
		||||
		Update()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindNodeActive 检查节点活跃状态
 | 
			
		||||
func (this *ReportNodeDAO) FindNodeActive(tx *dbs.Tx, nodeId int64) (bool, error) {
 | 
			
		||||
	isActive, err := this.Query(tx).
 | 
			
		||||
		Pk(nodeId).
 | 
			
		||||
		Result("isActive").
 | 
			
		||||
		FindIntCol(0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
	return isActive == 1, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindEnabledNodeIdWithUniqueId 根据唯一ID获取节点ID
 | 
			
		||||
func (this *ReportNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		Attr("uniqueId", uniqueId).
 | 
			
		||||
		Attr("state", ReportNodeStateEnabled).
 | 
			
		||||
		ResultPk().
 | 
			
		||||
		FindInt64Col(0)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateNodeStatus 更改节点状态
 | 
			
		||||
func (this ReportNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
 | 
			
		||||
	if statusJSON == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	_, err := this.Query(tx).
 | 
			
		||||
		Pk(nodeId).
 | 
			
		||||
		Set("status", string(statusJSON)).
 | 
			
		||||
		Update()
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ComposeConfig 组合配置
 | 
			
		||||
func (this *ReportNodeDAO) ComposeConfig(tx *dbs.Tx, nodeId int64) (*reporterconfigs.NodeConfig, error) {
 | 
			
		||||
	node, err := this.FindEnabledReportNode(tx, nodeId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if node == nil {
 | 
			
		||||
		return nil, nil
 | 
			
		||||
	}
 | 
			
		||||
	var config = &reporterconfigs.NodeConfig{
 | 
			
		||||
		Id: int64(node.Id),
 | 
			
		||||
	}
 | 
			
		||||
	return config, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindNodeAllowIPs 查询节点允许的IP
 | 
			
		||||
func (this *ReportNodeDAO) FindNodeAllowIPs(tx *dbs.Tx, nodeId int64) ([]string, error) {
 | 
			
		||||
	node, err := this.Query(tx).
 | 
			
		||||
		Pk(nodeId).
 | 
			
		||||
		Result("allowIPs").
 | 
			
		||||
		Find()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if node == nil {
 | 
			
		||||
		return nil, nil
 | 
			
		||||
	}
 | 
			
		||||
	return node.(*ReportNode).DecodeAllowIPs(), nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										6
									
								
								internal/db/models/report_node_dao_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								internal/db/models/report_node_dao_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	_ "github.com/iwind/TeaGo/bootstrap"
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										36
									
								
								internal/db/models/report_node_model.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								internal/db/models/report_node_model.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
// ReportNode 连通性报告终端
 | 
			
		||||
type ReportNode struct {
 | 
			
		||||
	Id        uint32 `field:"id"`        // ID
 | 
			
		||||
	UniqueId  string `field:"uniqueId"`  // 唯一ID
 | 
			
		||||
	Secret    string `field:"secret"`    // 密钥
 | 
			
		||||
	IsOn      uint8  `field:"isOn"`      // 是否启用
 | 
			
		||||
	Name      string `field:"name"`      // 名称
 | 
			
		||||
	Location  string `field:"location"`  // 所在区域
 | 
			
		||||
	Isp       string `field:"isp"`       // 网络服务商
 | 
			
		||||
	AllowIPs  string `field:"allowIPs"`  // 允许的IP
 | 
			
		||||
	IsActive  uint8  `field:"isActive"`  // 是否活跃
 | 
			
		||||
	Status    string `field:"status"`    // 状态
 | 
			
		||||
	State     uint8  `field:"state"`     // 状态
 | 
			
		||||
	CreatedAt uint64 `field:"createdAt"` // 创建时间
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ReportNodeOperator struct {
 | 
			
		||||
	Id        interface{} // ID
 | 
			
		||||
	UniqueId  interface{} // 唯一ID
 | 
			
		||||
	Secret    interface{} // 密钥
 | 
			
		||||
	IsOn      interface{} // 是否启用
 | 
			
		||||
	Name      interface{} // 名称
 | 
			
		||||
	Location  interface{} // 所在区域
 | 
			
		||||
	Isp       interface{} // 网络服务商
 | 
			
		||||
	AllowIPs  interface{} // 允许的IP
 | 
			
		||||
	IsActive  interface{} // 是否活跃
 | 
			
		||||
	Status    interface{} // 状态
 | 
			
		||||
	State     interface{} // 状态
 | 
			
		||||
	CreatedAt interface{} // 创建时间
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewReportNodeOperator() *ReportNodeOperator {
 | 
			
		||||
	return &ReportNodeOperator{}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								internal/db/models/report_node_model_ext.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								internal/db/models/report_node_model_ext.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import "encoding/json"
 | 
			
		||||
 | 
			
		||||
func (this *ReportNode) DecodeAllowIPs() []string {
 | 
			
		||||
	var result = []string{}
 | 
			
		||||
	if len(this.AllowIPs) > 0 {
 | 
			
		||||
		// 忽略错误
 | 
			
		||||
		_ = json.Unmarshal([]byte(this.AllowIPs), &result)
 | 
			
		||||
	}
 | 
			
		||||
	return result
 | 
			
		||||
}
 | 
			
		||||
@@ -162,7 +162,7 @@ func (this *APINodeService) ListEnabledAPINodes(ctx context.Context, req *pb.Lis
 | 
			
		||||
	return &pb.ListEnabledAPINodesResponse{Nodes: result}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 根据ID查找节点
 | 
			
		||||
// FindEnabledAPINode 根据ID查找节点
 | 
			
		||||
func (this *APINodeService) FindEnabledAPINode(ctx context.Context, req *pb.FindEnabledAPINodeRequest) (*pb.FindEnabledAPINodeResponse, error) {
 | 
			
		||||
	_, _, err := this.ValidateAdminAndUser(ctx, 0, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -194,6 +194,8 @@ func (this *BaseService) ValidateNodeId(ctx context.Context, roles ...rpcutils.U
 | 
			
		||||
		nodeIntId, err = models.SharedMonitorNodeDAO.FindEnabledMonitorNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
	case rpcutils.UserTypeDNS:
 | 
			
		||||
		nodeIntId, err = models.SharedNSNodeDAO.FindEnabledNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
	case rpcutils.UserTypeReport:
 | 
			
		||||
		nodeIntId, err = models.SharedReportNodeDAO.FindEnabledNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
	case rpcutils.UserTypeAuthority:
 | 
			
		||||
		nodeIntId, err = authority.SharedAuthorityNodeDAO.FindEnabledAuthorityNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
	default:
 | 
			
		||||
 
 | 
			
		||||
@@ -31,6 +31,7 @@ const (
 | 
			
		||||
	UserTypeLog       = "log"
 | 
			
		||||
	UserTypeAPI       = "api"
 | 
			
		||||
	UserTypeAuthority = "authority"
 | 
			
		||||
	UserTypeReport    = "report"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ValidateRequest 校验请求
 | 
			
		||||
@@ -160,38 +161,48 @@ func ValidateRequest(ctx context.Context, userTypes ...UserType) (userType UserT
 | 
			
		||||
	case UserTypeUser:
 | 
			
		||||
		nodeIntId, err := models.SharedUserNodeDAO.FindEnabledUserNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return UserTypeNode, 0, 0, errors.New("context: " + err.Error())
 | 
			
		||||
			return UserTypeUser, 0, 0, errors.New("context: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		if nodeIntId <= 0 {
 | 
			
		||||
			return UserTypeNode, 0, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
			return UserTypeUser, 0, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
		}
 | 
			
		||||
		resultNodeId = nodeIntId
 | 
			
		||||
	case UserTypeMonitor:
 | 
			
		||||
		nodeIntId, err := models.SharedMonitorNodeDAO.FindEnabledMonitorNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return UserTypeNode, 0, 0, errors.New("context: " + err.Error())
 | 
			
		||||
			return UserTypeMonitor, 0, 0, errors.New("context: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		if nodeIntId <= 0 {
 | 
			
		||||
			return UserTypeNode, 0, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
			return UserTypeMonitor, 0, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
		}
 | 
			
		||||
		resultNodeId = nodeIntId
 | 
			
		||||
	case UserTypeAuthority:
 | 
			
		||||
		nodeIntId, err := authority.SharedAuthorityNodeDAO.FindEnabledAuthorityNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return UserTypeNode, 0, 0, errors.New("context: " + err.Error())
 | 
			
		||||
			return UserTypeAuthority, 0, 0, errors.New("context: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		if nodeIntId <= 0 {
 | 
			
		||||
			return UserTypeNode, 0, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
			return UserTypeAuthority, 0, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
		}
 | 
			
		||||
		nodeUserId = nodeIntId
 | 
			
		||||
		resultNodeId = nodeIntId
 | 
			
		||||
	case UserTypeDNS:
 | 
			
		||||
		nodeIntId, err := models.SharedNSNodeDAO.FindEnabledNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return UserTypeNode, nodeIntId, 0, errors.New("context: " + err.Error())
 | 
			
		||||
			return UserTypeDNS, nodeIntId, 0, errors.New("context: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		if nodeIntId <= 0 {
 | 
			
		||||
			return UserTypeNode, nodeIntId, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
			return UserTypeDNS, nodeIntId, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
		}
 | 
			
		||||
		nodeUserId = nodeIntId
 | 
			
		||||
		resultNodeId = nodeIntId
 | 
			
		||||
	case UserTypeReport:
 | 
			
		||||
		nodeIntId, err := models.SharedReportNodeDAO.FindEnabledNodeIdWithUniqueId(nil, nodeId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return UserTypeReport, nodeIntId, 0, errors.New("context: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		if nodeIntId <= 0 {
 | 
			
		||||
			return UserTypeReport, nodeIntId, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
		}
 | 
			
		||||
		nodeUserId = nodeIntId
 | 
			
		||||
		resultNodeId = nodeIntId
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user