mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-25 14:36:35 +08:00
实现基础的DDoS防护
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"github.com/andybalholm/brotli"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
@@ -767,6 +768,7 @@ func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.Find
|
||||
NodeJSON: data,
|
||||
DataSize: int64(len(data)),
|
||||
IsCompressed: isCompressed,
|
||||
Timestamp: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -1789,3 +1791,161 @@ func (this *NodeService) UpdateNodeDNSResolver(ctx context.Context, req *pb.Upda
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindNodeDDoSProtection 获取集群的DDoS设置
|
||||
func (this *NodeService) FindNodeDDoSProtection(ctx context.Context, req *pb.FindNodeDDoSProtectionRequest) (*pb.FindNodeDDoSProtectionResponse, error) {
|
||||
var nodeId = req.NodeId
|
||||
var isFromNode = false
|
||||
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
// 检查是否来自节点
|
||||
currentNodeId, err2 := this.ValidateNode(ctx)
|
||||
if err2 != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if nodeId > 0 && currentNodeId != nodeId {
|
||||
return nil, errors.New("invalid 'nodeId'")
|
||||
}
|
||||
|
||||
nodeId = currentNodeId
|
||||
isFromNode = true
|
||||
}
|
||||
|
||||
var tx *dbs.Tx
|
||||
ddosProtection, err := models.SharedNodeDAO.FindNodeDDoSProtection(tx, nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ddosProtection == nil {
|
||||
ddosProtection = ddosconfigs.DefaultProtectionConfig()
|
||||
}
|
||||
|
||||
// 组合父级节点配置
|
||||
// 只有从节点读取配置时才需要组合
|
||||
if isFromNode {
|
||||
clusterId, err := models.SharedNodeDAO.FindNodeClusterId(tx, nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if clusterId > 0 {
|
||||
clusterDDoSProtection, err := models.SharedNodeClusterDAO.FindClusterDDoSProtection(tx, clusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if clusterDDoSProtection == nil {
|
||||
clusterDDoSProtection = ddosconfigs.DefaultProtectionConfig()
|
||||
}
|
||||
|
||||
clusterDDoSProtection.Merge(ddosProtection)
|
||||
ddosProtection = clusterDDoSProtection
|
||||
}
|
||||
}
|
||||
|
||||
ddosProtectionJSON, err := json.Marshal(ddosProtection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result = &pb.FindNodeDDoSProtectionResponse{
|
||||
DdosProtectionJSON: ddosProtectionJSON,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// UpdateNodeDDoSProtection 修改集群的DDOS设置
|
||||
func (this *NodeService) UpdateNodeDDoSProtection(ctx context.Context, req *pb.UpdateNodeDDoSProtectionRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ddosProtection = &ddosconfigs.ProtectionConfig{}
|
||||
err = json.Unmarshal(req.DdosProtectionJSON, ddosProtection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx *dbs.Tx
|
||||
err = models.SharedNodeDAO.UpdateNodeDDoSProtection(tx, req.NodeId, ddosProtection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindEnabledNodeConfigInfo 取得节点的配置概要信息
|
||||
func (this *NodeService) FindEnabledNodeConfigInfo(ctx context.Context, req *pb.FindEnabledNodeConfigInfoRequest) (*pb.FindEnabledNodeConfigInfoResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
var result = &pb.FindEnabledNodeConfigInfoResponse{}
|
||||
node, err := models.SharedNodeDAO.FindEnabledNode(tx, req.NodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if node == nil {
|
||||
// 总是返回非空
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// dns
|
||||
if len(node.DNSRouteCodes()) > 0 {
|
||||
result.HasDNSInfo = true
|
||||
}
|
||||
|
||||
// cache
|
||||
if len(node.CacheDiskDir) > 0 {
|
||||
result.HasCacheInfo = true
|
||||
} else {
|
||||
var diskCapacity = node.DecodeMaxCacheDiskCapacity()
|
||||
var memoryCapacity = node.DecodeMaxCacheMemoryCapacity()
|
||||
if (diskCapacity != nil && diskCapacity.IsNotEmpty()) || (memoryCapacity != nil && memoryCapacity.IsNotEmpty()) {
|
||||
result.HasCacheInfo = true
|
||||
}
|
||||
}
|
||||
|
||||
// thresholds
|
||||
countThresholds, err := models.SharedNodeThresholdDAO.CountAllEnabledThresholds(tx, nodeconfigs.NodeRoleNode, 0, req.NodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.HasThresholds = countThresholds > 0
|
||||
|
||||
// ssh
|
||||
nodeLogin, err := models.SharedNodeLoginDAO.FindEnabledNodeLoginWithNodeId(tx, nodeconfigs.NodeRoleNode, req.NodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if nodeLogin != nil {
|
||||
sshParams, err := nodeLogin.DecodeSSHParams()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if sshParams != nil {
|
||||
result.HasSSH = len(sshParams.Host) > 0 || sshParams.Port > 0
|
||||
}
|
||||
}
|
||||
|
||||
// systemSettings
|
||||
if node.MaxCPU > 0 {
|
||||
result.HasSystemSettings = true
|
||||
} else {
|
||||
// dns resolver
|
||||
var dnsResolverConfig = node.DecodeDNSResolver()
|
||||
if dnsResolverConfig != nil {
|
||||
result.HasSystemSettings = dnsResolverConfig.Type != nodeconfigs.DNSResolverTypeDefault
|
||||
}
|
||||
}
|
||||
|
||||
// ddos protection
|
||||
result.HasDDoSProtection = node.HasDDoSProtection()
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
@@ -83,7 +84,7 @@ func (this *NodeClusterService) UpdateNodeCluster(ctx context.Context, req *pb.U
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = models.SharedNodeClusterDAO.UpdateCluster(tx, req.NodeClusterId, req.Name, req.NodeGrantId, req.InstallDir, req.TimeZone, req.NodeMaxThreads, req.NodeTCPMaxConnections, req.AutoOpenPorts)
|
||||
err = models.SharedNodeClusterDAO.UpdateCluster(tx, req.NodeClusterId, req.Name, req.NodeGrantId, req.InstallDir, req.TimeZone, req.NodeMaxThreads, req.AutoOpenPorts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -148,22 +149,21 @@ func (this *NodeClusterService) FindEnabledNodeCluster(ctx context.Context, req
|
||||
}
|
||||
|
||||
return &pb.FindEnabledNodeClusterResponse{NodeCluster: &pb.NodeCluster{
|
||||
Id: int64(cluster.Id),
|
||||
Name: cluster.Name,
|
||||
CreatedAt: int64(cluster.CreatedAt),
|
||||
InstallDir: cluster.InstallDir,
|
||||
NodeGrantId: int64(cluster.GrantId),
|
||||
UniqueId: cluster.UniqueId,
|
||||
Secret: cluster.Secret,
|
||||
HttpCachePolicyId: int64(cluster.CachePolicyId),
|
||||
HttpFirewallPolicyId: int64(cluster.HttpFirewallPolicyId),
|
||||
DnsName: cluster.DnsName,
|
||||
DnsDomainId: int64(cluster.DnsDomainId),
|
||||
IsOn: cluster.IsOn,
|
||||
TimeZone: cluster.TimeZone,
|
||||
NodeMaxThreads: int32(cluster.NodeMaxThreads),
|
||||
NodeTCPMaxConnections: int32(cluster.NodeTCPMaxConnections),
|
||||
AutoOpenPorts: cluster.AutoOpenPorts == 1,
|
||||
Id: int64(cluster.Id),
|
||||
Name: cluster.Name,
|
||||
CreatedAt: int64(cluster.CreatedAt),
|
||||
InstallDir: cluster.InstallDir,
|
||||
NodeGrantId: int64(cluster.GrantId),
|
||||
UniqueId: cluster.UniqueId,
|
||||
Secret: cluster.Secret,
|
||||
HttpCachePolicyId: int64(cluster.CachePolicyId),
|
||||
HttpFirewallPolicyId: int64(cluster.HttpFirewallPolicyId),
|
||||
DnsName: cluster.DnsName,
|
||||
DnsDomainId: int64(cluster.DnsDomainId),
|
||||
IsOn: cluster.IsOn,
|
||||
TimeZone: cluster.TimeZone,
|
||||
NodeMaxThreads: int32(cluster.NodeMaxThreads),
|
||||
AutoOpenPorts: cluster.AutoOpenPorts == 1,
|
||||
}}, nil
|
||||
}
|
||||
|
||||
@@ -1054,6 +1054,9 @@ func (this *NodeClusterService) FindEnabledNodeClusterConfigInfo(ctx context.Con
|
||||
}
|
||||
}
|
||||
|
||||
// ddos
|
||||
result.HasDDoSProtection = cluster.HasDDoSProtection()
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -1114,3 +1117,51 @@ func (this *NodeClusterService) UpdateNodeClusterWebPPolicy(ctx context.Context,
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindNodeClusterDDoSProtection 获取集群的DDOS设置
|
||||
func (this *NodeClusterService) FindNodeClusterDDoSProtection(ctx context.Context, req *pb.FindNodeClusterDDoSProtectionRequest) (*pb.FindNodeClusterDDoSProtectionResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx *dbs.Tx
|
||||
ddosProtection, err := models.SharedNodeClusterDAO.FindClusterDDoSProtection(tx, req.NodeClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ddosProtection == nil {
|
||||
ddosProtection = ddosconfigs.DefaultProtectionConfig()
|
||||
}
|
||||
ddosProtectionJSON, err := json.Marshal(ddosProtection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result = &pb.FindNodeClusterDDoSProtectionResponse{
|
||||
DdosProtectionJSON: ddosProtectionJSON,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// UpdateNodeClusterDDoSProtection 修改集群的DDOS设置
|
||||
func (this *NodeClusterService) UpdateNodeClusterDDoSProtection(ctx context.Context, req *pb.UpdateNodeClusterDDoSProtectionRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ddosProtection = &ddosconfigs.ProtectionConfig{}
|
||||
err = json.Unmarshal(req.DdosProtectionJSON, ddosProtection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx *dbs.Tx
|
||||
err = models.SharedNodeClusterDAO.UpdateClusterDDoSProtection(tx, req.NodeClusterId, ddosProtection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user