mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-07 18:50:26 +08:00
[网站服务]可以审核域名、查看审核结果,用户端可以启用、停用域名
This commit is contained in:
5
internal/db/models/errors.go
Normal file
5
internal/db/models/errors.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
|
||||||
|
var ErrNotFound = errors.New("not found")
|
||||||
@@ -25,16 +25,18 @@ const (
|
|||||||
type MessageType = string
|
type MessageType = string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed"
|
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed"
|
||||||
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp"
|
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp"
|
||||||
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown"
|
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown"
|
||||||
MessageTypeNodeInactive MessageType = "NodeInactive"
|
MessageTypeNodeInactive MessageType = "NodeInactive"
|
||||||
MessageTypeNodeActive MessageType = "NodeActive"
|
MessageTypeNodeActive MessageType = "NodeActive"
|
||||||
MessageTypeClusterDNSSyncFailed MessageType = "ClusterDNSSyncFailed"
|
MessageTypeClusterDNSSyncFailed MessageType = "ClusterDNSSyncFailed"
|
||||||
MessageTypeSSLCertExpiring MessageType = "SSLCertExpiring" // SSL证书即将过期
|
MessageTypeSSLCertExpiring MessageType = "SSLCertExpiring" // SSL证书即将过期
|
||||||
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
||||||
MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
|
MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
|
||||||
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
|
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
|
||||||
|
MessageTypeServerNamesAuditingSuccess MessageType = "ServerNamesAuditingSuccess" // 服务域名审核成功
|
||||||
|
MessageTypeServerNamesAuditingFailed MessageType = "ServerNamesAuditingFailed" // 服务域名审核失败
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageDAO dbs.DAO
|
type MessageDAO dbs.DAO
|
||||||
|
|||||||
@@ -5,15 +5,19 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/rpc"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -87,7 +91,26 @@ func (this *ServerDAO) FindEnabledServerType(serverId int64) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 创建服务
|
// 创建服务
|
||||||
func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serverconfigs.ServerType, name string, description string, serverNamesJSON string, httpJSON string, httpsJSON string, tcpJSON string, tlsJSON string, unixJSON string, udpJSON string, webId int64, reverseProxyJSON []byte, clusterId int64, includeNodesJSON string, excludeNodesJSON string, groupIds []int64) (serverId int64, err error) {
|
func (this *ServerDAO) CreateServer(adminId int64,
|
||||||
|
userId int64,
|
||||||
|
serverType serverconfigs.ServerType,
|
||||||
|
name string,
|
||||||
|
description string,
|
||||||
|
serverNamesJSON []byte,
|
||||||
|
isAuditing bool,
|
||||||
|
auditingServerNamesJSON []byte,
|
||||||
|
httpJSON string,
|
||||||
|
httpsJSON string,
|
||||||
|
tcpJSON string,
|
||||||
|
tlsJSON string,
|
||||||
|
unixJSON string,
|
||||||
|
udpJSON string,
|
||||||
|
webId int64,
|
||||||
|
reverseProxyJSON []byte,
|
||||||
|
clusterId int64,
|
||||||
|
includeNodesJSON string,
|
||||||
|
excludeNodesJSON string,
|
||||||
|
groupIds []int64) (serverId int64, err error) {
|
||||||
op := NewServerOperator()
|
op := NewServerOperator()
|
||||||
op.UserId = userId
|
op.UserId = userId
|
||||||
op.AdminId = adminId
|
op.AdminId = adminId
|
||||||
@@ -95,9 +118,13 @@ func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serv
|
|||||||
op.Type = serverType
|
op.Type = serverType
|
||||||
op.Description = description
|
op.Description = description
|
||||||
|
|
||||||
if IsNotNull(serverNamesJSON) {
|
if len(serverNamesJSON) > 0 {
|
||||||
op.ServerNames = serverNamesJSON
|
op.ServerNames = serverNamesJSON
|
||||||
}
|
}
|
||||||
|
op.IsAuditing = isAuditing
|
||||||
|
if len(auditingServerNamesJSON) > 0 {
|
||||||
|
op.AuditingServerNames = auditingServerNamesJSON
|
||||||
|
}
|
||||||
if IsNotNull(httpJSON) {
|
if IsNotNull(httpJSON) {
|
||||||
op.Http = httpJSON
|
op.Http = httpJSON
|
||||||
}
|
}
|
||||||
@@ -204,6 +231,15 @@ func (this *ServerDAO) UpdateServerBasic(serverId int64, name string, descriptio
|
|||||||
return this.createEvent()
|
return this.createEvent()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 修复服务是否启用
|
||||||
|
func (this *ServerDAO) UpdateServerIsOn(serverId int64, isOn bool) error {
|
||||||
|
_, err := this.Query().
|
||||||
|
Pk(serverId).
|
||||||
|
Set("isOn", isOn).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// 修改服务配置
|
// 修改服务配置
|
||||||
func (this *ServerDAO) UpdateServerConfig(serverId int64, configJSON []byte, updateMd5 bool) (isChanged bool, err error) {
|
func (this *ServerDAO) UpdateServerConfig(serverId int64, configJSON []byte, updateMd5 bool) (isChanged bool, err error) {
|
||||||
if serverId <= 0 {
|
if serverId <= 0 {
|
||||||
@@ -419,37 +455,81 @@ func (this *ServerDAO) InitServerWeb(serverId int64) (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 查找ServerNames配置
|
// 查找ServerNames配置
|
||||||
func (this *ServerDAO) FindServerNames(serverId int64) (serverNamesJSON []byte, err error) {
|
func (this *ServerDAO) FindServerNames(serverId int64) (serverNamesJSON []byte, isAuditing bool, auditingServerNamesJSON []byte, auditingResultJSON []byte, err error) {
|
||||||
col, err := this.Query().
|
if serverId <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
one, err := this.Query().
|
||||||
Pk(serverId).
|
Pk(serverId).
|
||||||
Result("serverNames").
|
Result("serverNames", "isAuditing", "auditingServerNames", "auditingResult").
|
||||||
FindStringCol("")
|
Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, false, nil, nil, err
|
||||||
}
|
}
|
||||||
if len(col) == 0 || col == "null" {
|
if one == nil {
|
||||||
return []byte("[]"), nil
|
return
|
||||||
}
|
}
|
||||||
return []byte(col), nil
|
server := one.(*Server)
|
||||||
|
return []byte(server.ServerNames), server.IsAuditing == 1, []byte(server.AuditingServerNames), []byte(server.AuditingResult), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改ServerNames配置
|
// 修改ServerNames配置
|
||||||
func (this *ServerDAO) UpdateServerNames(serverId int64, config []byte) error {
|
func (this *ServerDAO) UpdateServerNames(serverId int64, serverNames []byte) error {
|
||||||
if serverId <= 0 {
|
if serverId <= 0 {
|
||||||
return errors.New("serverId should not be smaller than 0")
|
return errors.New("serverId should not be smaller than 0")
|
||||||
}
|
}
|
||||||
if len(config) == 0 {
|
|
||||||
config = []byte("null")
|
op := NewServerOperator()
|
||||||
|
op.Id = serverId
|
||||||
|
|
||||||
|
if len(serverNames) == 0 {
|
||||||
|
serverNames = []byte("[]")
|
||||||
}
|
}
|
||||||
_, err := this.Query().
|
op.ServerNames = serverNames
|
||||||
Pk(serverId).
|
return this.createEvent()
|
||||||
Set("serverNames", string(config)).
|
}
|
||||||
Update()
|
|
||||||
|
// 修改域名审核
|
||||||
|
func (this *ServerDAO) UpdateAuditingServerNames(serverId int64, isAuditing bool, auditingServerNamesJSON []byte) error {
|
||||||
|
if serverId <= 0 {
|
||||||
|
return errors.New("serverId should not be smaller than 0")
|
||||||
|
}
|
||||||
|
|
||||||
|
op := NewServerOperator()
|
||||||
|
op.Id = serverId
|
||||||
|
op.IsAuditing = isAuditing
|
||||||
|
if len(auditingServerNamesJSON) == 0 {
|
||||||
|
op.AuditingServerNames = "[]"
|
||||||
|
} else {
|
||||||
|
op.AuditingServerNames = auditingServerNamesJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.createEvent()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改域名审核结果
|
||||||
|
func (this *ServerDAO) UpdateServerAuditing(serverId int64, result *pb.ServerNameAuditingResult) error {
|
||||||
|
if serverId <= 0 {
|
||||||
|
return errors.New("invalid serverId")
|
||||||
|
}
|
||||||
|
|
||||||
|
resultJSON, err := json.Marshal(maps.Map{
|
||||||
|
"isOk": result.IsOk,
|
||||||
|
"reason": result.Reason,
|
||||||
|
"createdAt": time.Now().Unix(),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.createEvent()
|
op := NewServerOperator()
|
||||||
|
op.Id = serverId
|
||||||
|
op.IsAuditing = false
|
||||||
|
op.AuditingResult = resultJSON
|
||||||
|
if result.IsOk {
|
||||||
|
op.ServerNames = dbs.SQL("auditingServerNames")
|
||||||
|
}
|
||||||
|
return this.Save(op)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改反向代理配置
|
// 修改反向代理配置
|
||||||
@@ -469,7 +549,7 @@ func (this *ServerDAO) UpdateServerReverseProxy(serverId int64, config []byte) e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 计算所有可用服务数量
|
// 计算所有可用服务数量
|
||||||
func (this *ServerDAO) CountAllEnabledServersMatch(groupId int64, keyword string, userId int64) (int64, error) {
|
func (this *ServerDAO) CountAllEnabledServersMatch(groupId int64, keyword string, userId int64, clusterId int64, auditingFlag rpc.BoolFlag) (int64, error) {
|
||||||
query := this.Query().
|
query := this.Query().
|
||||||
State(ServerStateEnabled)
|
State(ServerStateEnabled)
|
||||||
if groupId > 0 {
|
if groupId > 0 {
|
||||||
@@ -483,11 +563,17 @@ func (this *ServerDAO) CountAllEnabledServersMatch(groupId int64, keyword string
|
|||||||
if userId > 0 {
|
if userId > 0 {
|
||||||
query.Attr("userId", userId)
|
query.Attr("userId", userId)
|
||||||
}
|
}
|
||||||
|
if clusterId > 0 {
|
||||||
|
query.Attr("clusterId", clusterId)
|
||||||
|
}
|
||||||
|
if auditingFlag == rpc.BoolFlagTrue {
|
||||||
|
query.Attr("isAuditing", true)
|
||||||
|
}
|
||||||
return query.Count()
|
return query.Count()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 列出单页的服务
|
// 列出单页的服务
|
||||||
func (this *ServerDAO) ListEnabledServersMatch(offset int64, size int64, groupId int64, keyword string, userId int64) (result []*Server, err error) {
|
func (this *ServerDAO) ListEnabledServersMatch(offset int64, size int64, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag int32) (result []*Server, err error) {
|
||||||
query := this.Query().
|
query := this.Query().
|
||||||
State(ServerStateEnabled).
|
State(ServerStateEnabled).
|
||||||
Offset(offset).
|
Offset(offset).
|
||||||
@@ -506,6 +592,12 @@ func (this *ServerDAO) ListEnabledServersMatch(offset int64, size int64, groupId
|
|||||||
if userId > 0 {
|
if userId > 0 {
|
||||||
query.Attr("userId", userId)
|
query.Attr("userId", userId)
|
||||||
}
|
}
|
||||||
|
if clusterId > 0 {
|
||||||
|
query.Attr("clusterId", clusterId)
|
||||||
|
}
|
||||||
|
if auditingFlag == 1 {
|
||||||
|
query.Attr("isAuditing", true)
|
||||||
|
}
|
||||||
|
|
||||||
_, err = query.FindAll()
|
_, err = query.FindAll()
|
||||||
return
|
return
|
||||||
@@ -937,6 +1029,24 @@ func (this *ServerDAO) FindServerAdminIdAndUserId(serverId int64) (adminId int64
|
|||||||
return int64(one.(*Server).AdminId), int64(one.(*Server).UserId), nil
|
return int64(one.(*Server).AdminId), int64(one.(*Server).UserId), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查用户服务
|
||||||
|
func (this *ServerDAO) CheckUserServer(serverId int64, userId int64) error {
|
||||||
|
if serverId <= 0 || userId <= 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
ok, err := this.Query().
|
||||||
|
Pk(serverId).
|
||||||
|
Attr("userId", userId).
|
||||||
|
Exist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// 生成DNS Name
|
// 生成DNS Name
|
||||||
func (this *ServerDAO) genDNSName() (string, error) {
|
func (this *ServerDAO) genDNSName() (string, error) {
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -2,61 +2,67 @@ package models
|
|||||||
|
|
||||||
// 服务
|
// 服务
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Id uint32 `field:"id"` // ID
|
Id uint32 `field:"id"` // ID
|
||||||
IsOn uint8 `field:"isOn"` // 是否启用
|
IsOn uint8 `field:"isOn"` // 是否启用
|
||||||
UserId uint32 `field:"userId"` // 用户ID
|
UserId uint32 `field:"userId"` // 用户ID
|
||||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||||
Type string `field:"type"` // 服务类型
|
Type string `field:"type"` // 服务类型
|
||||||
Name string `field:"name"` // 名称
|
Name string `field:"name"` // 名称
|
||||||
Description string `field:"description"` // 描述
|
Description string `field:"description"` // 描述
|
||||||
ServerNames string `field:"serverNames"` // 域名列表
|
ServerNames string `field:"serverNames"` // 域名列表
|
||||||
Http string `field:"http"` // HTTP配置
|
AuditingServerNames string `field:"auditingServerNames"` // 审核中的域名
|
||||||
Https string `field:"https"` // HTTPS配置
|
IsAuditing uint8 `field:"isAuditing"` // 是否正在审核
|
||||||
Tcp string `field:"tcp"` // TCP配置
|
AuditingResult string `field:"auditingResult"` // 审核结果
|
||||||
Tls string `field:"tls"` // TLS配置
|
Http string `field:"http"` // HTTP配置
|
||||||
Unix string `field:"unix"` // Unix配置
|
Https string `field:"https"` // HTTPS配置
|
||||||
Udp string `field:"udp"` // UDP配置
|
Tcp string `field:"tcp"` // TCP配置
|
||||||
WebId uint32 `field:"webId"` // WEB配置
|
Tls string `field:"tls"` // TLS配置
|
||||||
ReverseProxy string `field:"reverseProxy"` // 反向代理配置
|
Unix string `field:"unix"` // Unix配置
|
||||||
GroupIds string `field:"groupIds"` // 分组ID列表
|
Udp string `field:"udp"` // UDP配置
|
||||||
Config string `field:"config"` // 服务配置,自动生成
|
WebId uint32 `field:"webId"` // WEB配置
|
||||||
ConfigMd5 string `field:"configMd5"` // Md5
|
ReverseProxy string `field:"reverseProxy"` // 反向代理配置
|
||||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
GroupIds string `field:"groupIds"` // 分组ID列表
|
||||||
IncludeNodes string `field:"includeNodes"` // 部署条件
|
Config string `field:"config"` // 服务配置,自动生成
|
||||||
ExcludeNodes string `field:"excludeNodes"` // 节点排除条件
|
ConfigMd5 string `field:"configMd5"` // Md5
|
||||||
Version uint32 `field:"version"` // 版本号
|
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
IncludeNodes string `field:"includeNodes"` // 部署条件
|
||||||
State uint8 `field:"state"` // 状态
|
ExcludeNodes string `field:"excludeNodes"` // 节点排除条件
|
||||||
DnsName string `field:"dnsName"` // DNS名称
|
Version uint32 `field:"version"` // 版本号
|
||||||
|
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||||
|
State uint8 `field:"state"` // 状态
|
||||||
|
DnsName string `field:"dnsName"` // DNS名称
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerOperator struct {
|
type ServerOperator struct {
|
||||||
Id interface{} // ID
|
Id interface{} // ID
|
||||||
IsOn interface{} // 是否启用
|
IsOn interface{} // 是否启用
|
||||||
UserId interface{} // 用户ID
|
UserId interface{} // 用户ID
|
||||||
AdminId interface{} // 管理员ID
|
AdminId interface{} // 管理员ID
|
||||||
Type interface{} // 服务类型
|
Type interface{} // 服务类型
|
||||||
Name interface{} // 名称
|
Name interface{} // 名称
|
||||||
Description interface{} // 描述
|
Description interface{} // 描述
|
||||||
ServerNames interface{} // 域名列表
|
ServerNames interface{} // 域名列表
|
||||||
Http interface{} // HTTP配置
|
AuditingServerNames interface{} // 审核中的域名
|
||||||
Https interface{} // HTTPS配置
|
IsAuditing interface{} // 是否正在审核
|
||||||
Tcp interface{} // TCP配置
|
AuditingResult interface{} // 审核结果
|
||||||
Tls interface{} // TLS配置
|
Http interface{} // HTTP配置
|
||||||
Unix interface{} // Unix配置
|
Https interface{} // HTTPS配置
|
||||||
Udp interface{} // UDP配置
|
Tcp interface{} // TCP配置
|
||||||
WebId interface{} // WEB配置
|
Tls interface{} // TLS配置
|
||||||
ReverseProxy interface{} // 反向代理配置
|
Unix interface{} // Unix配置
|
||||||
GroupIds interface{} // 分组ID列表
|
Udp interface{} // UDP配置
|
||||||
Config interface{} // 服务配置,自动生成
|
WebId interface{} // WEB配置
|
||||||
ConfigMd5 interface{} // Md5
|
ReverseProxy interface{} // 反向代理配置
|
||||||
ClusterId interface{} // 集群ID
|
GroupIds interface{} // 分组ID列表
|
||||||
IncludeNodes interface{} // 部署条件
|
Config interface{} // 服务配置,自动生成
|
||||||
ExcludeNodes interface{} // 节点排除条件
|
ConfigMd5 interface{} // Md5
|
||||||
Version interface{} // 版本号
|
ClusterId interface{} // 集群ID
|
||||||
CreatedAt interface{} // 创建时间
|
IncludeNodes interface{} // 部署条件
|
||||||
State interface{} // 状态
|
ExcludeNodes interface{} // 节点排除条件
|
||||||
DnsName interface{} // DNS名称
|
Version interface{} // 版本号
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
|
State interface{} // 状态
|
||||||
|
DnsName interface{} // DNS名称
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerOperator() *ServerOperator {
|
func NewServerOperator() *ServerOperator {
|
||||||
|
|||||||
9
internal/rpc/bool_flag.go
Normal file
9
internal/rpc/bool_flag.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
type BoolFlag = int32
|
||||||
|
|
||||||
|
const (
|
||||||
|
BoolFlagNone BoolFlag = 0
|
||||||
|
BoolFlagTrue BoolFlag = 1
|
||||||
|
BoolFlagFalse BoolFlag = 2
|
||||||
|
)
|
||||||
@@ -360,7 +360,7 @@ func (this *NodeClusterService) FindAllEnabledNodeClustersWithGrantId(ctx contex
|
|||||||
// 查找集群的DNS配置
|
// 查找集群的DNS配置
|
||||||
func (this *NodeClusterService) FindEnabledNodeClusterDNS(ctx context.Context, req *pb.FindEnabledNodeClusterDNSRequest) (*pb.FindEnabledNodeClusterDNSResponse, error) {
|
func (this *NodeClusterService) FindEnabledNodeClusterDNS(ctx context.Context, req *pb.FindEnabledNodeClusterDNSRequest) (*pb.FindEnabledNodeClusterDNSResponse, error) {
|
||||||
// 校验请求
|
// 校验请求
|
||||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
_, _, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerService struct {
|
type ServerService struct {
|
||||||
@@ -41,7 +42,23 @@ func (this *ServerService) CreateServer(ctx context.Context, req *pb.CreateServe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serverId, err := models.SharedServerDAO.CreateServer(req.AdminId, req.UserId, req.Type, req.Name, req.Description, string(req.ServerNamesJON), string(req.HttpJSON), string(req.HttpsJSON), string(req.TcpJSON), string(req.TlsJSON), string(req.UnixJSON), string(req.UdpJSON), req.WebId, req.ReverseProxyJSON, req.NodeClusterId, string(req.IncludeNodesJSON), string(req.ExcludeNodesJSON), req.GroupIds)
|
// 是否需要审核
|
||||||
|
isAuditing := false
|
||||||
|
serverNamesJSON := req.ServerNamesJON
|
||||||
|
auditingServerNamesJSON := []byte("[]")
|
||||||
|
if userId > 0 {
|
||||||
|
globalConfig, err := models.SharedSysSettingDAO.ReadGlobalConfig()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if globalConfig != nil && globalConfig.HTTPAll.DomainAuditingIsOn {
|
||||||
|
isAuditing = true
|
||||||
|
serverNamesJSON = []byte("[]")
|
||||||
|
auditingServerNamesJSON = req.ServerNamesJON
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serverId, err := models.SharedServerDAO.CreateServer(req.AdminId, req.UserId, req.Type, req.Name, req.Description, serverNamesJSON, isAuditing, auditingServerNamesJSON, string(req.HttpJSON), string(req.HttpsJSON), string(req.TcpJSON), string(req.TlsJSON), string(req.UnixJSON), string(req.UdpJSON), req.WebId, req.ReverseProxyJSON, req.NodeClusterId, string(req.IncludeNodesJSON), string(req.ExcludeNodesJSON), req.GroupIds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -55,7 +72,7 @@ func (this *ServerService) CreateServer(ctx context.Context, req *pb.CreateServe
|
|||||||
return &pb.CreateServerResponse{ServerId: serverId}, nil
|
return &pb.CreateServerResponse{ServerId: serverId}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改服务
|
// 修改服务基本信息
|
||||||
func (this *ServerService) UpdateServerBasic(ctx context.Context, req *pb.UpdateServerBasicRequest) (*pb.RPCSuccess, error) {
|
func (this *ServerService) UpdateServerBasic(ctx context.Context, req *pb.UpdateServerBasicRequest) (*pb.RPCSuccess, error) {
|
||||||
// 校验请求
|
// 校验请求
|
||||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||||
@@ -109,6 +126,25 @@ func (this *ServerService) UpdateServerBasic(ctx context.Context, req *pb.Update
|
|||||||
return this.Success()
|
return this.Success()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 修改服务是否启用
|
||||||
|
func (this *ServerService) UpdateServerIsOn(ctx context.Context, req *pb.UpdateServerIsOnRequest) (*pb.RPCSuccess, error) {
|
||||||
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if userId > 0 {
|
||||||
|
err = models.SharedServerDAO.CheckUserServer(req.ServerId, userId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = models.SharedServerDAO.UpdateServerIsOn(req.ServerId, req.IsOn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return this.Success()
|
||||||
|
}
|
||||||
|
|
||||||
// 修改HTTP服务
|
// 修改HTTP服务
|
||||||
func (this *ServerService) UpdateServerHTTP(ctx context.Context, req *pb.UpdateServerHTTPRequest) (*pb.RPCSuccess, error) {
|
func (this *ServerService) UpdateServerHTTP(ctx context.Context, req *pb.UpdateServerHTTPRequest) (*pb.RPCSuccess, error) {
|
||||||
// 校验请求
|
// 校验请求
|
||||||
@@ -355,22 +391,46 @@ func (this *ServerService) UpdateServerReverseProxy(ctx context.Context, req *pb
|
|||||||
|
|
||||||
// 查找服务的域名设置
|
// 查找服务的域名设置
|
||||||
func (this *ServerService) FindServerNames(ctx context.Context, req *pb.FindServerNamesRequest) (*pb.FindServerNamesResponse, error) {
|
func (this *ServerService) FindServerNames(ctx context.Context, req *pb.FindServerNamesRequest) (*pb.FindServerNamesResponse, error) {
|
||||||
_, err := this.ValidateAdmin(ctx, 0)
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
serverNamesJSON, err := models.SharedServerDAO.FindServerNames(req.ServerId)
|
if userId > 0 {
|
||||||
|
err = models.SharedServerDAO.CheckUserServer(req.ServerId, userId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serverNamesJSON, isAuditing, auditingServerNamesJSON, auditingResultJSON, err := models.SharedServerDAO.FindServerNames(req.ServerId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &pb.FindServerNamesResponse{ServerNamesJSON: serverNamesJSON}, nil
|
|
||||||
|
// 审核结果
|
||||||
|
auditingResult := &pb.ServerNameAuditingResult{}
|
||||||
|
if len(auditingResultJSON) > 0 {
|
||||||
|
err = json.Unmarshal(auditingResultJSON, auditingResult)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auditingResult.IsOk = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return &pb.FindServerNamesResponse{
|
||||||
|
ServerNamesJSON: serverNamesJSON,
|
||||||
|
IsAuditing: isAuditing,
|
||||||
|
AuditingServerNamesJSON: auditingServerNamesJSON,
|
||||||
|
AuditingResult: auditingResult,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改域名服务
|
// 修改域名服务
|
||||||
func (this *ServerService) UpdateServerNames(ctx context.Context, req *pb.UpdateServerNamesRequest) (*pb.RPCSuccess, error) {
|
func (this *ServerService) UpdateServerNames(ctx context.Context, req *pb.UpdateServerNamesRequest) (*pb.RPCSuccess, error) {
|
||||||
// 校验请求
|
// 校验请求
|
||||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -379,17 +439,23 @@ func (this *ServerService) UpdateServerNames(ctx context.Context, req *pb.Update
|
|||||||
return nil, errors.New("invalid serverId")
|
return nil, errors.New("invalid serverId")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询老的节点信息
|
// 是否需要审核
|
||||||
server, err := models.SharedServerDAO.FindEnabledServer(req.ServerId)
|
if userId > 0 {
|
||||||
if err != nil {
|
globalConfig, err := models.SharedSysSettingDAO.ReadGlobalConfig()
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
if server == nil {
|
}
|
||||||
return nil, errors.New("can not find server")
|
if globalConfig != nil && globalConfig.HTTPAll.DomainAuditingIsOn {
|
||||||
|
err = models.SharedServerDAO.UpdateAuditingServerNames(req.ServerId, true, req.ServerNamesJSON)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return this.Success()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改配置
|
// 修改配置
|
||||||
err = models.SharedServerDAO.UpdateServerNames(req.ServerId, req.Config)
|
err = models.SharedServerDAO.UpdateServerNames(req.ServerId, req.ServerNamesJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -397,6 +463,46 @@ func (this *ServerService) UpdateServerNames(ctx context.Context, req *pb.Update
|
|||||||
return this.Success()
|
return this.Success()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 审核服务的域名设置
|
||||||
|
func (this *ServerService) UpdateServerNamesAuditing(ctx context.Context, req *pb.UpdateServerNamesAuditingRequest) (*pb.RPCSuccess, error) {
|
||||||
|
// 校验请求
|
||||||
|
_, err := this.ValidateAdmin(ctx, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.AuditingResult == nil {
|
||||||
|
return nil, errors.New("'result' should not be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = models.SharedServerDAO.UpdateServerAuditing(req.ServerId, req.AuditingResult)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送消息提醒
|
||||||
|
_, userId, err := models.SharedServerDAO.FindServerAdminIdAndUserId(req.ServerId)
|
||||||
|
if userId > 0 {
|
||||||
|
if req.AuditingResult.IsOk {
|
||||||
|
err = models.SharedMessageDAO.CreateMessage(0, userId, models.MessageTypeServerNamesAuditingSuccess, models.LevelInfo, "服务域名审核通过", maps.Map{
|
||||||
|
"serverId": req.ServerId,
|
||||||
|
}.AsJSON())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = models.SharedMessageDAO.CreateMessage(0, userId, models.MessageTypeServerNamesAuditingFailed, models.LevelError, "服务域名审核失败,原因:"+req.AuditingResult.Reason, maps.Map{
|
||||||
|
"serverId": req.ServerId,
|
||||||
|
}.AsJSON())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.Success()
|
||||||
|
}
|
||||||
|
|
||||||
// 计算服务数量
|
// 计算服务数量
|
||||||
func (this *ServerService) CountAllEnabledServersMatch(ctx context.Context, req *pb.CountAllEnabledServersMatchRequest) (*pb.RPCCountResponse, error) {
|
func (this *ServerService) CountAllEnabledServersMatch(ctx context.Context, req *pb.CountAllEnabledServersMatchRequest) (*pb.RPCCountResponse, error) {
|
||||||
// 校验请求
|
// 校验请求
|
||||||
@@ -404,7 +510,7 @@ func (this *ServerService) CountAllEnabledServersMatch(ctx context.Context, req
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
count, err := models.SharedServerDAO.CountAllEnabledServersMatch(req.GroupId, req.Keyword, req.UserId)
|
count, err := models.SharedServerDAO.CountAllEnabledServersMatch(req.GroupId, req.Keyword, req.UserId, req.ClusterId, req.AuditingFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -419,7 +525,7 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
servers, err := models.SharedServerDAO.ListEnabledServersMatch(req.Offset, req.Size, req.GroupId, req.Keyword, req.UserId)
|
servers, err := models.SharedServerDAO.ListEnabledServersMatch(req.Offset, req.Size, req.GroupId, req.Keyword, req.UserId, req.ClusterId, req.AuditingFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -466,24 +572,38 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 审核结果
|
||||||
|
auditingResult := &pb.ServerNameAuditingResult{}
|
||||||
|
if len(server.AuditingResult) > 0 {
|
||||||
|
err = json.Unmarshal([]byte(server.AuditingResult), auditingResult)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auditingResult.IsOk = true
|
||||||
|
}
|
||||||
|
|
||||||
result = append(result, &pb.Server{
|
result = append(result, &pb.Server{
|
||||||
Id: int64(server.Id),
|
Id: int64(server.Id),
|
||||||
IsOn: server.IsOn == 1,
|
IsOn: server.IsOn == 1,
|
||||||
Type: server.Type,
|
Type: server.Type,
|
||||||
Config: []byte(server.Config),
|
Config: []byte(server.Config),
|
||||||
Name: server.Name,
|
Name: server.Name,
|
||||||
Description: server.Description,
|
Description: server.Description,
|
||||||
HttpJSON: []byte(server.Http),
|
HttpJSON: []byte(server.Http),
|
||||||
HttpsJSON: []byte(server.Https),
|
HttpsJSON: []byte(server.Https),
|
||||||
TcpJSON: []byte(server.Tcp),
|
TcpJSON: []byte(server.Tcp),
|
||||||
TlsJSON: []byte(server.Tls),
|
TlsJSON: []byte(server.Tls),
|
||||||
UnixJSON: []byte(server.Unix),
|
UnixJSON: []byte(server.Unix),
|
||||||
UdpJSON: []byte(server.Udp),
|
UdpJSON: []byte(server.Udp),
|
||||||
IncludeNodes: []byte(server.IncludeNodes),
|
IncludeNodes: []byte(server.IncludeNodes),
|
||||||
ExcludeNodes: []byte(server.ExcludeNodes),
|
ExcludeNodes: []byte(server.ExcludeNodes),
|
||||||
ServerNamesJSON: []byte(server.ServerNames),
|
ServerNamesJSON: []byte(server.ServerNames),
|
||||||
CreatedAt: int64(server.CreatedAt),
|
IsAuditing: server.IsAuditing == 1,
|
||||||
DnsName: server.DnsName,
|
AuditingServerNamesJSON: []byte(server.AuditingServerNames),
|
||||||
|
AuditingResult: auditingResult,
|
||||||
|
CreatedAt: int64(server.CreatedAt),
|
||||||
|
DnsName: server.DnsName,
|
||||||
NodeCluster: &pb.NodeCluster{
|
NodeCluster: &pb.NodeCluster{
|
||||||
Id: int64(server.ClusterId),
|
Id: int64(server.ClusterId),
|
||||||
Name: clusterName,
|
Name: clusterName,
|
||||||
@@ -497,13 +617,20 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 禁用某服务
|
// 禁用某服务
|
||||||
func (this *ServerService) DisableServer(ctx context.Context, req *pb.DisableServerRequest) (*pb.DisableServerResponse, error) {
|
func (this *ServerService) DeleteServer(ctx context.Context, req *pb.DeleteServerRequest) (*pb.RPCSuccess, error) {
|
||||||
// 校验请求
|
// 校验请求
|
||||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if userId > 0 {
|
||||||
|
err = models.SharedServerDAO.CheckUserServer(req.ServerId, userId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 查找服务
|
// 查找服务
|
||||||
server, err := models.SharedServerDAO.FindEnabledServer(req.ServerId)
|
server, err := models.SharedServerDAO.FindEnabledServer(req.ServerId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -525,7 +652,7 @@ func (this *ServerService) DisableServer(ctx context.Context, req *pb.DisableSer
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &pb.DisableServerResponse{}, nil
|
return this.Success()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找单个服务
|
// 查找单个服务
|
||||||
@@ -848,7 +975,7 @@ func (this *ServerService) FindAllEnabledServersDNSWithClusterId(ctx context.Con
|
|||||||
// 查找单个服务的DNS信息
|
// 查找单个服务的DNS信息
|
||||||
func (this *ServerService) FindEnabledServerDNS(ctx context.Context, req *pb.FindEnabledServerDNSRequest) (*pb.FindEnabledServerDNSResponse, error) {
|
func (this *ServerService) FindEnabledServerDNS(ctx context.Context, req *pb.FindEnabledServerDNSRequest) (*pb.FindEnabledServerDNSResponse, error) {
|
||||||
// 校验请求
|
// 校验请求
|
||||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
_, _, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -932,3 +1059,16 @@ func (this *ServerService) notifyServerDNSChanged(serverId int64) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查服务是否属于某个用户
|
||||||
|
func (this *ServerService) CheckUserServer(ctx context.Context, req *pb.CheckUserServerRequest) (*pb.RPCSuccess, error) {
|
||||||
|
userId, err := this.ValidateUser(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = models.SharedServerDAO.CheckUserServer(req.ServerId, userId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return this.Success()
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package services
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/rpc"
|
||||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
@@ -259,7 +260,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 网站数量
|
// 网站数量
|
||||||
countServers, err := models.SharedServerDAO.CountAllEnabledServersMatch(0, "", req.UserId)
|
countServers, err := models.SharedServerDAO.CountAllEnabledServersMatch(0, "", req.UserId, 0, rpc.BoolFlagNone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user