mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-06 07:25:48 +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
|
||||
|
||||
const (
|
||||
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed"
|
||||
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp"
|
||||
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown"
|
||||
MessageTypeNodeInactive MessageType = "NodeInactive"
|
||||
MessageTypeNodeActive MessageType = "NodeActive"
|
||||
MessageTypeClusterDNSSyncFailed MessageType = "ClusterDNSSyncFailed"
|
||||
MessageTypeSSLCertExpiring MessageType = "SSLCertExpiring" // SSL证书即将过期
|
||||
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
||||
MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
|
||||
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
|
||||
MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed"
|
||||
MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp"
|
||||
MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown"
|
||||
MessageTypeNodeInactive MessageType = "NodeInactive"
|
||||
MessageTypeNodeActive MessageType = "NodeActive"
|
||||
MessageTypeClusterDNSSyncFailed MessageType = "ClusterDNSSyncFailed"
|
||||
MessageTypeSSLCertExpiring MessageType = "SSLCertExpiring" // SSL证书即将过期
|
||||
MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
|
||||
MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
|
||||
MessageTypeLogCapacityOverflow MessageType = "LogCapacityOverflow" // 日志超出最大限制
|
||||
MessageTypeServerNamesAuditingSuccess MessageType = "ServerNamesAuditingSuccess" // 服务域名审核成功
|
||||
MessageTypeServerNamesAuditingFailed MessageType = "ServerNamesAuditingFailed" // 服务域名审核失败
|
||||
)
|
||||
|
||||
type MessageDAO dbs.DAO
|
||||
|
||||
@@ -5,15 +5,19 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
_ "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/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
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.UserId = userId
|
||||
op.AdminId = adminId
|
||||
@@ -95,9 +118,13 @@ func (this *ServerDAO) CreateServer(adminId int64, userId int64, serverType serv
|
||||
op.Type = serverType
|
||||
op.Description = description
|
||||
|
||||
if IsNotNull(serverNamesJSON) {
|
||||
if len(serverNamesJSON) > 0 {
|
||||
op.ServerNames = serverNamesJSON
|
||||
}
|
||||
op.IsAuditing = isAuditing
|
||||
if len(auditingServerNamesJSON) > 0 {
|
||||
op.AuditingServerNames = auditingServerNamesJSON
|
||||
}
|
||||
if IsNotNull(httpJSON) {
|
||||
op.Http = httpJSON
|
||||
}
|
||||
@@ -204,6 +231,15 @@ func (this *ServerDAO) UpdateServerBasic(serverId int64, name string, descriptio
|
||||
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) {
|
||||
if serverId <= 0 {
|
||||
@@ -419,37 +455,81 @@ func (this *ServerDAO) InitServerWeb(serverId int64) (int64, error) {
|
||||
}
|
||||
|
||||
// 查找ServerNames配置
|
||||
func (this *ServerDAO) FindServerNames(serverId int64) (serverNamesJSON []byte, err error) {
|
||||
col, err := this.Query().
|
||||
func (this *ServerDAO) FindServerNames(serverId int64) (serverNamesJSON []byte, isAuditing bool, auditingServerNamesJSON []byte, auditingResultJSON []byte, err error) {
|
||||
if serverId <= 0 {
|
||||
return
|
||||
}
|
||||
one, err := this.Query().
|
||||
Pk(serverId).
|
||||
Result("serverNames").
|
||||
FindStringCol("")
|
||||
Result("serverNames", "isAuditing", "auditingServerNames", "auditingResult").
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, false, nil, nil, err
|
||||
}
|
||||
if len(col) == 0 || col == "null" {
|
||||
return []byte("[]"), nil
|
||||
if one == nil {
|
||||
return
|
||||
}
|
||||
return []byte(col), nil
|
||||
server := one.(*Server)
|
||||
return []byte(server.ServerNames), server.IsAuditing == 1, []byte(server.AuditingServerNames), []byte(server.AuditingResult), nil
|
||||
}
|
||||
|
||||
// 修改ServerNames配置
|
||||
func (this *ServerDAO) UpdateServerNames(serverId int64, config []byte) error {
|
||||
func (this *ServerDAO) UpdateServerNames(serverId int64, serverNames []byte) error {
|
||||
if serverId <= 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().
|
||||
Pk(serverId).
|
||||
Set("serverNames", string(config)).
|
||||
Update()
|
||||
op.ServerNames = serverNames
|
||||
return this.createEvent()
|
||||
}
|
||||
|
||||
// 修改域名审核
|
||||
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 {
|
||||
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().
|
||||
State(ServerStateEnabled)
|
||||
if groupId > 0 {
|
||||
@@ -483,11 +563,17 @@ func (this *ServerDAO) CountAllEnabledServersMatch(groupId int64, keyword string
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
if clusterId > 0 {
|
||||
query.Attr("clusterId", clusterId)
|
||||
}
|
||||
if auditingFlag == rpc.BoolFlagTrue {
|
||||
query.Attr("isAuditing", true)
|
||||
}
|
||||
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().
|
||||
State(ServerStateEnabled).
|
||||
Offset(offset).
|
||||
@@ -506,6 +592,12 @@ func (this *ServerDAO) ListEnabledServersMatch(offset int64, size int64, groupId
|
||||
if userId > 0 {
|
||||
query.Attr("userId", userId)
|
||||
}
|
||||
if clusterId > 0 {
|
||||
query.Attr("clusterId", clusterId)
|
||||
}
|
||||
if auditingFlag == 1 {
|
||||
query.Attr("isAuditing", true)
|
||||
}
|
||||
|
||||
_, err = query.FindAll()
|
||||
return
|
||||
@@ -937,6 +1029,24 @@ func (this *ServerDAO) FindServerAdminIdAndUserId(serverId int64) (adminId int64
|
||||
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
|
||||
func (this *ServerDAO) genDNSName() (string, error) {
|
||||
for {
|
||||
|
||||
@@ -2,61 +2,67 @@ package models
|
||||
|
||||
// 服务
|
||||
type Server struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
Type string `field:"type"` // 服务类型
|
||||
Name string `field:"name"` // 名称
|
||||
Description string `field:"description"` // 描述
|
||||
ServerNames string `field:"serverNames"` // 域名列表
|
||||
Http string `field:"http"` // HTTP配置
|
||||
Https string `field:"https"` // HTTPS配置
|
||||
Tcp string `field:"tcp"` // TCP配置
|
||||
Tls string `field:"tls"` // TLS配置
|
||||
Unix string `field:"unix"` // Unix配置
|
||||
Udp string `field:"udp"` // UDP配置
|
||||
WebId uint32 `field:"webId"` // WEB配置
|
||||
ReverseProxy string `field:"reverseProxy"` // 反向代理配置
|
||||
GroupIds string `field:"groupIds"` // 分组ID列表
|
||||
Config string `field:"config"` // 服务配置,自动生成
|
||||
ConfigMd5 string `field:"configMd5"` // Md5
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
IncludeNodes string `field:"includeNodes"` // 部署条件
|
||||
ExcludeNodes string `field:"excludeNodes"` // 节点排除条件
|
||||
Version uint32 `field:"version"` // 版本号
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
State uint8 `field:"state"` // 状态
|
||||
DnsName string `field:"dnsName"` // DNS名称
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
Type string `field:"type"` // 服务类型
|
||||
Name string `field:"name"` // 名称
|
||||
Description string `field:"description"` // 描述
|
||||
ServerNames string `field:"serverNames"` // 域名列表
|
||||
AuditingServerNames string `field:"auditingServerNames"` // 审核中的域名
|
||||
IsAuditing uint8 `field:"isAuditing"` // 是否正在审核
|
||||
AuditingResult string `field:"auditingResult"` // 审核结果
|
||||
Http string `field:"http"` // HTTP配置
|
||||
Https string `field:"https"` // HTTPS配置
|
||||
Tcp string `field:"tcp"` // TCP配置
|
||||
Tls string `field:"tls"` // TLS配置
|
||||
Unix string `field:"unix"` // Unix配置
|
||||
Udp string `field:"udp"` // UDP配置
|
||||
WebId uint32 `field:"webId"` // WEB配置
|
||||
ReverseProxy string `field:"reverseProxy"` // 反向代理配置
|
||||
GroupIds string `field:"groupIds"` // 分组ID列表
|
||||
Config string `field:"config"` // 服务配置,自动生成
|
||||
ConfigMd5 string `field:"configMd5"` // Md5
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
IncludeNodes string `field:"includeNodes"` // 部署条件
|
||||
ExcludeNodes string `field:"excludeNodes"` // 节点排除条件
|
||||
Version uint32 `field:"version"` // 版本号
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
State uint8 `field:"state"` // 状态
|
||||
DnsName string `field:"dnsName"` // DNS名称
|
||||
}
|
||||
|
||||
type ServerOperator struct {
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
UserId interface{} // 用户ID
|
||||
AdminId interface{} // 管理员ID
|
||||
Type interface{} // 服务类型
|
||||
Name interface{} // 名称
|
||||
Description interface{} // 描述
|
||||
ServerNames interface{} // 域名列表
|
||||
Http interface{} // HTTP配置
|
||||
Https interface{} // HTTPS配置
|
||||
Tcp interface{} // TCP配置
|
||||
Tls interface{} // TLS配置
|
||||
Unix interface{} // Unix配置
|
||||
Udp interface{} // UDP配置
|
||||
WebId interface{} // WEB配置
|
||||
ReverseProxy interface{} // 反向代理配置
|
||||
GroupIds interface{} // 分组ID列表
|
||||
Config interface{} // 服务配置,自动生成
|
||||
ConfigMd5 interface{} // Md5
|
||||
ClusterId interface{} // 集群ID
|
||||
IncludeNodes interface{} // 部署条件
|
||||
ExcludeNodes interface{} // 节点排除条件
|
||||
Version interface{} // 版本号
|
||||
CreatedAt interface{} // 创建时间
|
||||
State interface{} // 状态
|
||||
DnsName interface{} // DNS名称
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
UserId interface{} // 用户ID
|
||||
AdminId interface{} // 管理员ID
|
||||
Type interface{} // 服务类型
|
||||
Name interface{} // 名称
|
||||
Description interface{} // 描述
|
||||
ServerNames interface{} // 域名列表
|
||||
AuditingServerNames interface{} // 审核中的域名
|
||||
IsAuditing interface{} // 是否正在审核
|
||||
AuditingResult interface{} // 审核结果
|
||||
Http interface{} // HTTP配置
|
||||
Https interface{} // HTTPS配置
|
||||
Tcp interface{} // TCP配置
|
||||
Tls interface{} // TLS配置
|
||||
Unix interface{} // Unix配置
|
||||
Udp interface{} // UDP配置
|
||||
WebId interface{} // WEB配置
|
||||
ReverseProxy interface{} // 反向代理配置
|
||||
GroupIds interface{} // 分组ID列表
|
||||
Config interface{} // 服务配置,自动生成
|
||||
ConfigMd5 interface{} // Md5
|
||||
ClusterId interface{} // 集群ID
|
||||
IncludeNodes interface{} // 部署条件
|
||||
ExcludeNodes interface{} // 节点排除条件
|
||||
Version interface{} // 版本号
|
||||
CreatedAt interface{} // 创建时间
|
||||
State interface{} // 状态
|
||||
DnsName interface{} // DNS名称
|
||||
}
|
||||
|
||||
func NewServerOperator() *ServerOperator {
|
||||
|
||||
Reference in New Issue
Block a user