2020-07-22 22:17:53 +08:00
package models
import (
2020-09-15 14:44:11 +08:00
"encoding/json"
2020-07-29 19:02:28 +08:00
"errors"
2021-01-25 16:40:03 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
2020-10-29 21:45:45 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
2020-12-24 19:15:17 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
2021-08-08 15:47:48 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2020-12-19 19:09:14 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
2020-09-13 20:37:28 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
2020-07-22 22:17:53 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
2021-01-17 16:48:00 +08:00
"github.com/iwind/TeaGo/lists"
2020-12-19 19:09:14 +08:00
"github.com/iwind/TeaGo/maps"
2020-11-11 21:32:25 +08:00
"github.com/iwind/TeaGo/rands"
2020-07-29 19:02:28 +08:00
"github.com/iwind/TeaGo/types"
2021-06-25 11:05:02 +08:00
"regexp"
2020-09-30 17:46:43 +08:00
"strconv"
"strings"
2020-12-19 19:09:14 +08:00
"time"
2020-07-22 22:17:53 +08:00
)
const (
ServerStateEnabled = 1 // 已启用
ServerStateDisabled = 0 // 已禁用
)
type ServerDAO dbs . DAO
func NewServerDAO ( ) * ServerDAO {
return dbs . NewDAO ( & ServerDAO {
DAOObject : dbs . DAOObject {
DB : Tea . Env ,
Table : "edgeServers" ,
Model : new ( Server ) ,
PkName : "id" ,
} ,
} ) . ( * ServerDAO )
}
2020-10-13 20:05:13 +08:00
var SharedServerDAO * ServerDAO
func init ( ) {
dbs . OnReady ( func ( ) {
SharedServerDAO = NewServerDAO ( )
} )
}
2020-07-22 22:17:53 +08:00
2021-05-03 15:16:02 +08:00
// Init 初始化
2020-09-26 08:06:40 +08:00
func ( this * ServerDAO ) Init ( ) {
this . DAOObject . Init ( )
// 这里不处理增删改事件, 是为了避免Server修改本身的时候, 也要触发别的Server变更
}
2021-05-03 15:16:02 +08:00
// EnableServer 启用条目
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) EnableServer ( tx * dbs . Tx , id uint32 ) ( rowsAffected int64 , err error ) {
return this . Query ( tx ) .
2020-07-22 22:17:53 +08:00
Pk ( id ) .
Set ( "state" , ServerStateEnabled ) .
Update ( )
}
2021-05-03 15:16:02 +08:00
// DisableServer 禁用条目
2021-01-27 23:00:02 +08:00
func ( this * ServerDAO ) DisableServer ( tx * dbs . Tx , serverId int64 ) ( err error ) {
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2021-01-27 23:00:02 +08:00
Pk ( serverId ) .
2020-07-22 22:17:53 +08:00
Set ( "state" , ServerStateDisabled ) .
Update ( )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
2021-01-27 23:00:02 +08:00
err = this . NotifyUpdate ( tx , serverId )
if err != nil {
return err
}
err = this . NotifyDNSUpdate ( tx , serverId )
if err != nil {
return err
}
return nil
2020-07-22 22:17:53 +08:00
}
2021-05-03 15:16:02 +08:00
// FindEnabledServer 查找启用中的服务
2021-01-14 16:34:52 +08:00
func ( this * ServerDAO ) FindEnabledServer ( tx * dbs . Tx , serverId int64 ) ( * Server , error ) {
2021-01-01 23:31:30 +08:00
result , err := this . Query ( tx ) .
2021-01-14 16:34:52 +08:00
Pk ( serverId ) .
2020-07-22 22:17:53 +08:00
Attr ( "state" , ServerStateEnabled ) .
Find ( )
if result == nil {
return nil , err
}
return result . ( * Server ) , err
}
2020-07-29 19:02:28 +08:00
2021-05-03 15:16:02 +08:00
// FindEnabledServerName 查找服务名称
2021-01-27 23:00:02 +08:00
func ( this * ServerDAO ) FindEnabledServerName ( tx * dbs . Tx , serverId int64 ) ( string , error ) {
return this . Query ( tx ) .
Pk ( serverId ) .
State ( ServerStateEnabled ) .
Result ( "name" ) .
FindStringCol ( "" )
}
2021-05-03 15:16:02 +08:00
// FindEnabledServerBasic 查找服务基本信息
2021-01-14 16:34:52 +08:00
func ( this * ServerDAO ) FindEnabledServerBasic ( tx * dbs . Tx , serverId int64 ) ( * Server , error ) {
result , err := this . Query ( tx ) .
Pk ( serverId ) .
State ( ServerStateEnabled ) .
Result ( "id" , "name" , "description" , "isOn" , "type" , "clusterId" ) .
Find ( )
if result == nil {
return nil , err
}
return result . ( * Server ) , err
}
2021-05-03 15:16:02 +08:00
// FindEnabledServerType 查找服务类型
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindEnabledServerType ( tx * dbs . Tx , serverId int64 ) ( string , error ) {
return this . Query ( tx ) .
2020-09-15 14:44:11 +08:00
Pk ( serverId ) .
Result ( "type" ) .
FindStringCol ( "" )
}
2021-05-03 15:16:02 +08:00
// CreateServer 创建服务
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) CreateServer ( tx * dbs . Tx ,
adminId int64 ,
2020-12-19 19:09:14 +08:00
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 ) {
2020-07-29 19:02:28 +08:00
op := NewServerOperator ( )
op . UserId = userId
op . AdminId = adminId
2020-09-13 20:37:28 +08:00
op . Name = name
op . Type = serverType
op . Description = description
2020-09-15 14:44:11 +08:00
2020-12-19 19:09:14 +08:00
if len ( serverNamesJSON ) > 0 {
2020-09-15 14:44:11 +08:00
op . ServerNames = serverNamesJSON
}
2020-12-19 19:09:14 +08:00
op . IsAuditing = isAuditing
if len ( auditingServerNamesJSON ) > 0 {
op . AuditingServerNames = auditingServerNamesJSON
}
2020-09-21 11:37:17 +08:00
if IsNotNull ( httpJSON ) {
2020-09-15 14:44:11 +08:00
op . Http = httpJSON
}
2020-09-21 11:37:17 +08:00
if IsNotNull ( httpsJSON ) {
2020-09-15 14:44:11 +08:00
op . Https = httpsJSON
}
2020-09-21 11:37:17 +08:00
if IsNotNull ( tcpJSON ) {
2020-09-15 14:44:11 +08:00
op . Tcp = tcpJSON
}
2020-09-21 11:37:17 +08:00
if IsNotNull ( tlsJSON ) {
2020-09-15 14:44:11 +08:00
op . Tls = tlsJSON
}
2020-09-21 11:37:17 +08:00
if IsNotNull ( unixJSON ) {
2020-09-15 14:44:11 +08:00
op . Unix = unixJSON
}
2020-09-21 11:37:17 +08:00
if IsNotNull ( udpJSON ) {
2020-09-15 14:44:11 +08:00
op . Udp = udpJSON
2020-07-29 19:02:28 +08:00
}
2020-09-15 14:44:11 +08:00
op . WebId = webId
2020-09-21 11:37:17 +08:00
if len ( reverseProxyJSON ) > 0 {
op . ReverseProxy = reverseProxyJSON
}
2020-09-15 14:44:11 +08:00
op . ClusterId = clusterId
2020-07-29 19:02:28 +08:00
if len ( includeNodesJSON ) > 0 {
op . IncludeNodes = includeNodesJSON
}
if len ( excludeNodesJSON ) > 0 {
op . ExcludeNodes = excludeNodesJSON
}
2020-09-27 18:41:15 +08:00
2020-10-29 21:37:56 +08:00
if len ( groupIds ) == 0 {
op . GroupIds = "[]"
} else {
groupIdsJSON , err := json . Marshal ( groupIds )
if err != nil {
return 0 , err
}
op . GroupIds = groupIdsJSON
}
2021-01-27 23:00:02 +08:00
dnsName , err := this . GenDNSName ( tx )
2020-11-11 21:32:25 +08:00
if err != nil {
return 0 , err
}
op . DnsName = dnsName
2020-07-29 19:02:28 +08:00
op . Version = 1
op . IsOn = 1
op . State = ServerStateEnabled
2021-01-01 23:31:30 +08:00
err = this . Save ( tx , op )
2020-09-15 14:44:11 +08:00
if err != nil {
return 0 , err
}
serverId = types . Int64 ( op . Id )
2020-09-26 11:21:43 +08:00
2021-01-27 23:00:02 +08:00
// 通知配置更改
2021-01-17 16:48:00 +08:00
err = this . NotifyUpdate ( tx , serverId )
2020-10-01 16:01:17 +08:00
if err != nil {
2021-01-17 16:48:00 +08:00
return 0 , err
2020-09-26 11:21:43 +08:00
}
2021-01-27 23:00:02 +08:00
// 通知DNS更改
err = this . NotifyDNSUpdate ( tx , serverId )
if err != nil {
return 0 , err
}
2020-09-26 08:06:40 +08:00
return serverId , nil
2020-07-29 19:02:28 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerBasic 修改服务基本信息
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerBasic ( tx * dbs . Tx , serverId int64 , name string , description string , clusterId int64 , isOn bool , groupIds [ ] int64 ) error {
2020-07-29 19:02:28 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
op := NewServerOperator ( )
op . Id = serverId
2020-09-13 20:37:28 +08:00
op . Name = name
op . Description = description
2020-07-29 19:02:28 +08:00
op . ClusterId = clusterId
2020-09-28 16:25:39 +08:00
op . IsOn = isOn
2020-10-29 21:37:56 +08:00
if len ( groupIds ) == 0 {
op . GroupIds = "[]"
} else {
groupIdsJSON , err := json . Marshal ( groupIds )
if err != nil {
return err
}
op . GroupIds = groupIdsJSON
}
2021-01-01 23:31:30 +08:00
err := this . Save ( tx , op )
2020-09-26 08:06:40 +08:00
if err != nil {
return err
}
2021-01-17 16:48:00 +08:00
// 通知更新
err = this . NotifyUpdate ( tx , serverId )
2020-09-28 16:25:39 +08:00
if err != nil {
return err
}
2021-01-27 23:00:02 +08:00
// 因为可能有isOn的原因, 所以需要修改
return this . NotifyDNSUpdate ( tx , serverId )
2020-07-29 19:02:28 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateUserServerBasic 设置用户相关的基本信息
2021-01-14 16:34:52 +08:00
func ( this * ServerDAO ) UpdateUserServerBasic ( tx * dbs . Tx , serverId int64 , name string ) error {
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
op := NewServerOperator ( )
op . Id = serverId
op . Name = name
err := this . Save ( tx , op )
if err != nil {
return err
}
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2021-01-14 16:34:52 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerIsOn 修复服务是否启用
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerIsOn ( tx * dbs . Tx , serverId int64 , isOn bool ) error {
_ , err := this . Query ( tx ) .
2020-12-19 19:09:14 +08:00
Pk ( serverId ) .
Set ( "isOn" , isOn ) .
Update ( )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
err = this . NotifyUpdate ( tx , serverId )
if err != nil {
return err
}
return nil
2020-12-19 19:09:14 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerHTTP 修改HTTP配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerHTTP ( tx * dbs . Tx , serverId int64 , config [ ] byte ) error {
2020-09-15 14:44:11 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
if len ( config ) == 0 {
config = [ ] byte ( "null" )
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-09-15 14:44:11 +08:00
Pk ( serverId ) .
Set ( "http" , string ( config ) ) .
Update ( )
if err != nil {
return err
}
2020-09-26 08:06:40 +08:00
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-09-15 14:44:11 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerHTTPS 修改HTTPS配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerHTTPS ( tx * dbs . Tx , serverId int64 , httpsJSON [ ] byte ) error {
2020-09-15 14:44:11 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
2020-10-01 16:01:17 +08:00
if len ( httpsJSON ) == 0 {
httpsJSON = [ ] byte ( "null" )
2020-09-15 14:44:11 +08:00
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-09-15 14:44:11 +08:00
Pk ( serverId ) .
2020-10-01 16:01:17 +08:00
Set ( "https" , string ( httpsJSON ) ) .
2020-09-15 14:44:11 +08:00
Update ( )
if err != nil {
return err
}
2020-09-26 08:06:40 +08:00
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-09-15 14:44:11 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerTCP 修改TCP配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerTCP ( tx * dbs . Tx , serverId int64 , config [ ] byte ) error {
2020-09-15 14:44:11 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
if len ( config ) == 0 {
config = [ ] byte ( "null" )
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-09-15 14:44:11 +08:00
Pk ( serverId ) .
Set ( "tcp" , string ( config ) ) .
Update ( )
if err != nil {
return err
}
2020-09-26 08:06:40 +08:00
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-09-15 14:44:11 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerTLS 修改TLS配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerTLS ( tx * dbs . Tx , serverId int64 , config [ ] byte ) error {
2020-09-15 14:44:11 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
if len ( config ) == 0 {
config = [ ] byte ( "null" )
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-09-15 14:44:11 +08:00
Pk ( serverId ) .
Set ( "tls" , string ( config ) ) .
Update ( )
if err != nil {
return err
}
2020-09-26 08:06:40 +08:00
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-09-15 14:44:11 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerUnix 修改Unix配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerUnix ( tx * dbs . Tx , serverId int64 , config [ ] byte ) error {
2020-09-15 14:44:11 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
if len ( config ) == 0 {
config = [ ] byte ( "null" )
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-09-15 14:44:11 +08:00
Pk ( serverId ) .
Set ( "unix" , string ( config ) ) .
Update ( )
if err != nil {
return err
}
2020-09-26 08:06:40 +08:00
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-09-15 14:44:11 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerUDP 修改UDP配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerUDP ( tx * dbs . Tx , serverId int64 , config [ ] byte ) error {
2020-09-15 14:44:11 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
if len ( config ) == 0 {
config = [ ] byte ( "null" )
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-09-15 14:44:11 +08:00
Pk ( serverId ) .
Set ( "udp" , string ( config ) ) .
Update ( )
if err != nil {
return err
}
2020-09-26 08:06:40 +08:00
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-09-15 14:44:11 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerWeb 修改Web配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerWeb ( tx * dbs . Tx , serverId int64 , webId int64 ) error {
2020-09-15 14:44:11 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
2021-01-01 23:31:30 +08:00
_ , err := this . Query ( tx ) .
2020-09-15 14:44:11 +08:00
Pk ( serverId ) .
Set ( "webId" , webId ) .
Update ( )
if err != nil {
return err
}
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-09-15 14:44:11 +08:00
}
2021-05-03 15:16:02 +08:00
// InitServerWeb 初始化Web配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) InitServerWeb ( tx * dbs . Tx , serverId int64 ) ( int64 , error ) {
2020-09-16 09:09:21 +08:00
if serverId <= 0 {
return 0 , errors . New ( "serverId should not be smaller than 0" )
}
2021-01-01 23:31:30 +08:00
adminId , userId , err := this . FindServerAdminIdAndUserId ( tx , serverId )
2020-12-18 21:18:53 +08:00
if err != nil {
return 0 , err
}
2021-01-01 23:31:30 +08:00
webId , err := SharedHTTPWebDAO . CreateWeb ( tx , adminId , userId , nil )
2020-09-16 09:09:21 +08:00
if err != nil {
return 0 , err
}
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2020-09-16 09:09:21 +08:00
Pk ( serverId ) .
Set ( "webId" , webId ) .
Update ( )
if err != nil {
return 0 , err
}
2021-01-17 16:48:00 +08:00
err = this . NotifyUpdate ( tx , serverId )
2020-09-26 08:06:40 +08:00
if err != nil {
return webId , err
}
2020-09-16 09:09:21 +08:00
return webId , nil
}
2021-05-03 15:16:02 +08:00
// FindServerServerNames 查找ServerNames配置
2021-01-27 23:00:02 +08:00
func ( this * ServerDAO ) FindServerServerNames ( tx * dbs . Tx , serverId int64 ) ( serverNamesJSON [ ] byte , isAuditing bool , auditingServerNamesJSON [ ] byte , auditingResultJSON [ ] byte , err error ) {
2020-12-19 19:09:14 +08:00
if serverId <= 0 {
return
}
2021-01-01 23:31:30 +08:00
one , err := this . Query ( tx ) .
2020-12-03 21:07:12 +08:00
Pk ( serverId ) .
2020-12-19 19:09:14 +08:00
Result ( "serverNames" , "isAuditing" , "auditingServerNames" , "auditingResult" ) .
Find ( )
2020-12-03 21:07:12 +08:00
if err != nil {
2020-12-19 19:09:14 +08:00
return nil , false , nil , nil , err
2020-12-03 21:07:12 +08:00
}
2020-12-19 19:09:14 +08:00
if one == nil {
return
2020-12-03 21:07:12 +08:00
}
2020-12-19 19:09:14 +08:00
server := one . ( * Server )
return [ ] byte ( server . ServerNames ) , server . IsAuditing == 1 , [ ] byte ( server . AuditingServerNames ) , [ ] byte ( server . AuditingResult ) , nil
2020-12-03 21:07:12 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerNames 修改ServerNames配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerNames ( tx * dbs . Tx , serverId int64 , serverNames [ ] byte ) error {
2020-09-15 14:44:11 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
2020-12-19 19:09:14 +08:00
op := NewServerOperator ( )
op . Id = serverId
if len ( serverNames ) == 0 {
serverNames = [ ] byte ( "[]" )
2020-09-15 14:44:11 +08:00
}
2020-12-19 19:09:14 +08:00
op . ServerNames = serverNames
2021-01-01 23:31:30 +08:00
err := this . Save ( tx , op )
2020-12-23 09:52:50 +08:00
if err != nil {
return err
}
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-12-19 19:09:14 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateAuditingServerNames 修改域名审核
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateAuditingServerNames ( tx * dbs . Tx , serverId int64 , isAuditing bool , auditingServerNamesJSON [ ] byte ) error {
2020-12-19 19:09:14 +08:00
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
}
2020-12-23 09:52:50 +08:00
op . AuditingResult = ` { "isOk":true} `
2021-01-01 23:31:30 +08:00
err := this . Save ( tx , op )
2020-12-23 09:52:50 +08:00
if err != nil {
return err
}
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-12-19 19:09:14 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerAuditing 修改域名审核结果
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerAuditing ( tx * dbs . Tx , serverId int64 , result * pb . ServerNameAuditingResult ) error {
2020-12-19 19:09:14 +08:00
if serverId <= 0 {
return errors . New ( "invalid serverId" )
}
resultJSON , err := json . Marshal ( maps . Map {
"isOk" : result . IsOk ,
"reason" : result . Reason ,
"createdAt" : time . Now ( ) . Unix ( ) ,
} )
2020-09-15 14:44:11 +08:00
if err != nil {
return err
}
2020-09-26 08:06:40 +08:00
2020-12-19 19:09:14 +08:00
op := NewServerOperator ( )
op . Id = serverId
op . IsAuditing = false
op . AuditingResult = resultJSON
if result . IsOk {
op . ServerNames = dbs . SQL ( "auditingServerNames" )
}
2021-01-17 16:48:00 +08:00
err = this . Save ( tx , op )
if err != nil {
return err
}
2021-01-27 23:00:02 +08:00
err = this . NotifyUpdate ( tx , serverId )
if err != nil {
return err
}
return this . NotifyDNSUpdate ( tx , serverId )
2020-09-15 14:44:11 +08:00
}
2021-05-03 15:16:02 +08:00
// UpdateServerReverseProxy 修改反向代理配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) UpdateServerReverseProxy ( tx * dbs . Tx , serverId int64 , config [ ] byte ) error {
2020-09-15 14:44:11 +08:00
if serverId <= 0 {
return errors . New ( "serverId should not be smaller than 0" )
}
2020-09-21 11:37:17 +08:00
op := NewServerOperator ( )
op . Id = serverId
op . ReverseProxy = JSONBytes ( config )
2021-01-01 23:31:30 +08:00
err := this . Save ( tx , op )
2020-09-15 14:44:11 +08:00
if err != nil {
return err
}
2020-09-26 08:06:40 +08:00
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , serverId )
2020-09-15 14:44:11 +08:00
}
2021-05-03 15:16:02 +08:00
// CountAllEnabledServers 计算所有可用服务数量
2021-01-21 18:55:34 +08:00
func ( this * ServerDAO ) CountAllEnabledServers ( tx * dbs . Tx ) ( int64 , error ) {
return this . Query ( tx ) .
State ( ServerStateEnabled ) .
Count ( )
}
2021-05-03 15:16:02 +08:00
// CountAllEnabledServersMatch 计算所有可用服务数量
2021-01-14 16:34:52 +08:00
func ( this * ServerDAO ) CountAllEnabledServersMatch ( tx * dbs . Tx , groupId int64 , keyword string , userId int64 , clusterId int64 , auditingFlag configutils . BoolState , protocolFamily string ) ( int64 , error ) {
2021-01-01 23:31:30 +08:00
query := this . Query ( tx ) .
2020-10-31 15:21:29 +08:00
State ( ServerStateEnabled )
if groupId > 0 {
query . Where ( "JSON_CONTAINS(groupIds, :groupId)" ) .
Param ( "groupId" , numberutils . FormatInt64 ( groupId ) )
}
if len ( keyword ) > 0 {
2021-06-25 11:05:02 +08:00
if regexp . MustCompile ( ` ^\d+$ ` ) . MatchString ( keyword ) {
query . Where ( "(name LIKE :keyword OR serverNames LIKE :keyword OR JSON_CONTAINS(http, :portRange, '$.listen') OR JSON_CONTAINS(https, :portRange, '$.listen') OR JSON_CONTAINS(tcp, :portRange, '$.listen') OR JSON_CONTAINS(tls, :portRange, '$.listen'))" ) .
Param ( "portRange" , maps . Map { "portRange" : keyword } . AsJSON ( ) ) .
Param ( "keyword" , "%" + keyword + "%" )
} else {
query . Where ( "(name LIKE :keyword OR serverNames LIKE :keyword)" ) .
Param ( "keyword" , "%" + keyword + "%" )
}
2020-10-31 15:21:29 +08:00
}
2020-12-18 21:18:53 +08:00
if userId > 0 {
2020-12-15 16:53:31 +08:00
query . Attr ( "userId" , userId )
}
2020-12-19 19:09:14 +08:00
if clusterId > 0 {
query . Attr ( "clusterId" , clusterId )
}
2020-12-24 19:15:17 +08:00
if auditingFlag == configutils . BoolStateYes {
2020-12-19 19:09:14 +08:00
query . Attr ( "isAuditing" , true )
}
2021-01-14 16:34:52 +08:00
if protocolFamily == "http" {
query . Where ( "(http IS NOT NULL OR https IS NOT NULL)" )
} else if protocolFamily == "tcp" {
query . Where ( "(tcp IS NOT NULL OR tls IS NOT NULL)" )
}
2020-10-31 15:21:29 +08:00
return query . Count ( )
2020-07-29 19:02:28 +08:00
}
2021-05-03 15:16:02 +08:00
// ListEnabledServersMatch 列出单页的服务
2021-01-14 16:34:52 +08:00
func ( this * ServerDAO ) ListEnabledServersMatch ( tx * dbs . Tx , offset int64 , size int64 , groupId int64 , keyword string , userId int64 , clusterId int64 , auditingFlag int32 , protocolFamily string ) ( result [ ] * Server , err error ) {
2021-01-01 23:31:30 +08:00
query := this . Query ( tx ) .
2020-07-29 19:02:28 +08:00
State ( ServerStateEnabled ) .
Offset ( offset ) .
Limit ( size ) .
DescPk ( ) .
2020-10-31 15:21:29 +08:00
Slice ( & result )
if groupId > 0 {
query . Where ( "JSON_CONTAINS(groupIds, :groupId)" ) .
Param ( "groupId" , numberutils . FormatInt64 ( groupId ) )
}
if len ( keyword ) > 0 {
2021-06-25 11:05:02 +08:00
if regexp . MustCompile ( ` ^\d+$ ` ) . MatchString ( keyword ) {
query . Where ( "(name LIKE :keyword OR serverNames LIKE :keyword OR JSON_CONTAINS(http, :portRange, '$.listen') OR JSON_CONTAINS(https, :portRange, '$.listen') OR JSON_CONTAINS(tcp, :portRange, '$.listen') OR JSON_CONTAINS(tls, :portRange, '$.listen'))" ) .
Param ( "portRange" , string ( maps . Map { "portRange" : keyword } . AsJSON ( ) ) )
} else {
query . Where ( "(name LIKE :keyword OR serverNames LIKE :keyword)" ) .
Param ( "keyword" , "%" + keyword + "%" )
}
2020-10-31 15:21:29 +08:00
}
2020-12-18 21:18:53 +08:00
if userId > 0 {
query . Attr ( "userId" , userId )
}
2020-12-19 19:09:14 +08:00
if clusterId > 0 {
query . Attr ( "clusterId" , clusterId )
}
if auditingFlag == 1 {
query . Attr ( "isAuditing" , true )
}
2021-01-14 16:34:52 +08:00
if protocolFamily == "http" {
query . Where ( "(http IS NOT NULL OR https IS NOT NULL)" )
} else if protocolFamily == "tcp" {
query . Where ( "(tcp IS NOT NULL OR tls IS NOT NULL)" )
}
2020-10-31 15:21:29 +08:00
_ , err = query . FindAll ( )
2020-07-29 19:02:28 +08:00
return
}
2020-08-21 12:32:33 +08:00
2021-05-03 15:16:02 +08:00
// FindAllEnabledServersWithNode 获取节点中的所有服务
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindAllEnabledServersWithNode ( tx * dbs . Tx , nodeId int64 ) ( result [ ] * Server , err error ) {
2021-07-31 22:23:11 +08:00
// 节点所在主集群
clusterIds , err := SharedNodeDAO . FindEnabledAndOnNodeClusterIds ( tx , nodeId )
2020-08-21 12:32:33 +08:00
if err != nil {
return nil , err
}
2021-07-31 22:23:11 +08:00
for _ , clusterId := range clusterIds {
ones , err := this . Query ( tx ) .
Attr ( "clusterId" , clusterId ) .
State ( ServerStateEnabled ) .
AscPk ( ) .
FindAll ( )
if err != nil {
return nil , err
}
for _ , one := range ones {
result = append ( result , one . ( * Server ) )
}
2020-08-21 12:32:33 +08:00
}
return
}
2021-05-03 15:16:02 +08:00
// FindAllEnabledServerIds 获取所有的服务ID
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindAllEnabledServerIds ( tx * dbs . Tx ) ( serverIds [ ] int64 , err error ) {
ones , err := this . Query ( tx ) .
2020-09-26 08:06:40 +08:00
State ( ServerStateEnabled ) .
AscPk ( ) .
ResultPk ( ) .
FindAll ( )
for _ , one := range ones {
serverIds = append ( serverIds , int64 ( one . ( * Server ) . Id ) )
}
return
}
2021-05-03 15:16:02 +08:00
// FindAllEnabledServerIdsWithUserId 获取某个用户的所有的服务ID
2021-01-17 16:48:00 +08:00
func ( this * ServerDAO ) FindAllEnabledServerIdsWithUserId ( tx * dbs . Tx , userId int64 ) ( serverIds [ ] int64 , err error ) {
ones , err := this . Query ( tx ) .
State ( ServerStateEnabled ) .
Attr ( "userId" , userId ) .
AscPk ( ) .
ResultPk ( ) .
FindAll ( )
for _ , one := range ones {
serverIds = append ( serverIds , int64 ( one . ( * Server ) . Id ) )
}
return
}
2021-05-03 15:16:02 +08:00
// FindServerNodeFilters 查找服务的搜索条件
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindServerNodeFilters ( tx * dbs . Tx , serverId int64 ) ( isOk bool , clusterId int64 , err error ) {
one , err := this . Query ( tx ) .
2020-09-26 08:06:40 +08:00
Pk ( serverId ) .
Result ( "clusterId" ) .
Find ( )
if err != nil {
return false , 0 , err
}
if one == nil {
isOk = false
return
}
server := one . ( * Server )
return true , int64 ( server . ClusterId ) , nil
}
2021-08-01 14:56:08 +08:00
// ComposeServerConfigWithServerId 构造服务的Config
func ( this * ServerDAO ) ComposeServerConfigWithServerId ( tx * dbs . Tx , serverId int64 ) ( * serverconfigs . ServerConfig , error ) {
2021-01-01 23:31:30 +08:00
server , err := this . FindEnabledServer ( tx , serverId )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
if server == nil {
2021-01-17 16:48:00 +08:00
return nil , ErrNotFound
2020-09-15 14:44:11 +08:00
}
2021-08-01 14:56:08 +08:00
return this . ComposeServerConfig ( tx , server )
}
// ComposeServerConfig 构造服务的Config
func ( this * ServerDAO ) ComposeServerConfig ( tx * dbs . Tx , server * Server ) ( * serverconfigs . ServerConfig , error ) {
if server == nil {
return nil , ErrNotFound
}
2020-09-15 14:44:11 +08:00
config := & serverconfigs . ServerConfig { }
2021-08-01 14:56:08 +08:00
config . Id = int64 ( server . Id )
2021-08-01 11:13:46 +08:00
config . ClusterId = int64 ( server . ClusterId )
2020-09-15 14:44:11 +08:00
config . Type = server . Type
config . IsOn = server . IsOn == 1
config . Name = server . Name
config . Description = server . Description
// ServerNames
if len ( server . ServerNames ) > 0 && server . ServerNames != "null" {
serverNames := [ ] * serverconfigs . ServerNameConfig { }
2021-08-01 14:56:08 +08:00
err := json . Unmarshal ( [ ] byte ( server . ServerNames ) , & serverNames )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
config . ServerNames = serverNames
}
2020-11-17 16:53:58 +08:00
// CNAME
if server . ClusterId > 0 && len ( server . DnsName ) > 0 {
2021-01-01 23:31:30 +08:00
clusterDNS , err := SharedNodeClusterDAO . FindClusterDNSInfo ( tx , int64 ( server . ClusterId ) )
2020-11-17 16:53:58 +08:00
if err != nil {
return nil , err
}
if clusterDNS != nil && clusterDNS . DnsDomainId > 0 {
2021-01-25 16:40:03 +08:00
domain , err := dns . SharedDNSDomainDAO . FindEnabledDNSDomain ( tx , int64 ( clusterDNS . DnsDomainId ) )
2020-11-17 16:53:58 +08:00
if err != nil {
return nil , err
}
if domain != nil {
cname := server . DnsName + "." + domain . Name
config . AliasServerNames = append ( config . AliasServerNames , cname )
}
}
}
2020-09-15 14:44:11 +08:00
// HTTP
if len ( server . Http ) > 0 && server . Http != "null" {
httpConfig := & serverconfigs . HTTPProtocolConfig { }
2021-08-01 14:56:08 +08:00
err := json . Unmarshal ( [ ] byte ( server . Http ) , httpConfig )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
config . HTTP = httpConfig
}
// HTTPS
if len ( server . Https ) > 0 && server . Https != "null" {
httpsConfig := & serverconfigs . HTTPSProtocolConfig { }
2021-08-01 14:56:08 +08:00
err := json . Unmarshal ( [ ] byte ( server . Https ) , httpsConfig )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
2020-09-30 17:46:43 +08:00
// SSL
2020-10-01 16:01:17 +08:00
if httpsConfig . SSLPolicyRef != nil && httpsConfig . SSLPolicyRef . SSLPolicyId > 0 {
2021-01-01 23:31:30 +08:00
sslPolicyConfig , err := SharedSSLPolicyDAO . ComposePolicyConfig ( tx , httpsConfig . SSLPolicyRef . SSLPolicyId )
2020-09-30 17:46:43 +08:00
if err != nil {
return nil , err
}
if sslPolicyConfig != nil {
httpsConfig . SSLPolicy = sslPolicyConfig
}
}
2020-09-15 14:44:11 +08:00
config . HTTPS = httpsConfig
}
// TCP
if len ( server . Tcp ) > 0 && server . Tcp != "null" {
tcpConfig := & serverconfigs . TCPProtocolConfig { }
2021-08-01 14:56:08 +08:00
err := json . Unmarshal ( [ ] byte ( server . Tcp ) , tcpConfig )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
config . TCP = tcpConfig
}
// TLS
if len ( server . Tls ) > 0 && server . Tls != "null" {
tlsConfig := & serverconfigs . TLSProtocolConfig { }
2021-08-01 14:56:08 +08:00
err := json . Unmarshal ( [ ] byte ( server . Tls ) , tlsConfig )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
2020-09-30 17:46:43 +08:00
// SSL
if tlsConfig . SSLPolicyRef != nil {
2021-01-01 23:31:30 +08:00
sslPolicyConfig , err := SharedSSLPolicyDAO . ComposePolicyConfig ( tx , tlsConfig . SSLPolicyRef . SSLPolicyId )
2020-09-30 17:46:43 +08:00
if err != nil {
return nil , err
}
if sslPolicyConfig != nil {
tlsConfig . SSLPolicy = sslPolicyConfig
}
}
2020-09-15 14:44:11 +08:00
config . TLS = tlsConfig
}
// Unix
if len ( server . Unix ) > 0 && server . Unix != "null" {
unixConfig := & serverconfigs . UnixProtocolConfig { }
2021-08-01 14:56:08 +08:00
err := json . Unmarshal ( [ ] byte ( server . Unix ) , unixConfig )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
config . Unix = unixConfig
}
// UDP
if len ( server . Udp ) > 0 && server . Udp != "null" {
udpConfig := & serverconfigs . UDPProtocolConfig { }
2021-08-01 14:56:08 +08:00
err := json . Unmarshal ( [ ] byte ( server . Udp ) , udpConfig )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
config . UDP = udpConfig
}
// Web
if server . WebId > 0 {
2021-01-01 23:31:30 +08:00
webConfig , err := SharedHTTPWebDAO . ComposeWebConfig ( tx , int64 ( server . WebId ) )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
if webConfig != nil {
config . Web = webConfig
}
}
// ReverseProxy
2020-09-21 11:37:17 +08:00
if IsNotNull ( server . ReverseProxy ) {
reverseProxyRef := & serverconfigs . ReverseProxyRef { }
2021-08-01 14:56:08 +08:00
err := json . Unmarshal ( [ ] byte ( server . ReverseProxy ) , reverseProxyRef )
2020-09-21 11:37:17 +08:00
if err != nil {
return nil , err
}
config . ReverseProxyRef = reverseProxyRef
2021-01-01 23:31:30 +08:00
reverseProxyConfig , err := SharedReverseProxyDAO . ComposeReverseProxyConfig ( tx , reverseProxyRef . ReverseProxyId )
2020-09-15 14:44:11 +08:00
if err != nil {
return nil , err
}
if reverseProxyConfig != nil {
config . ReverseProxy = reverseProxyConfig
}
}
2021-08-01 14:56:08 +08:00
// WAF策略
clusterId := int64 ( server . ClusterId )
httpFirewallPolicyId , err := SharedNodeClusterDAO . FindClusterHTTPFirewallPolicyId ( tx , clusterId )
if err != nil {
return nil , err
}
if httpFirewallPolicyId > 0 {
config . HTTPFirewallPolicyId = httpFirewallPolicyId
}
// 缓存策略
httpCachePolicyId , err := SharedNodeClusterDAO . FindClusterHTTPCachePolicyId ( tx , clusterId )
if err != nil {
return nil , err
}
if httpCachePolicyId > 0 {
config . HTTPCachePolicyId = httpCachePolicyId
}
2020-09-15 14:44:11 +08:00
return config , nil
}
2021-05-03 15:16:02 +08:00
// FindReverseProxyRef 根据条件获取反向代理配置
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindReverseProxyRef ( tx * dbs . Tx , serverId int64 ) ( * serverconfigs . ReverseProxyRef , error ) {
reverseProxy , err := this . Query ( tx ) .
2020-09-15 14:44:11 +08:00
Pk ( serverId ) .
Result ( "reverseProxy" ) .
FindStringCol ( "" )
if err != nil {
return nil , err
}
if len ( reverseProxy ) == 0 || reverseProxy == "null" {
return nil , nil
}
2020-09-21 11:37:17 +08:00
config := & serverconfigs . ReverseProxyRef { }
2020-09-15 14:44:11 +08:00
err = json . Unmarshal ( [ ] byte ( reverseProxy ) , config )
return config , err
}
2020-09-13 20:37:28 +08:00
2021-05-03 15:16:02 +08:00
// FindServerWebId 查找Server对应的WebId
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindServerWebId ( tx * dbs . Tx , serverId int64 ) ( int64 , error ) {
webId , err := this . Query ( tx ) .
2020-09-16 20:29:18 +08:00
Pk ( serverId ) .
Result ( "webId" ) .
FindIntCol ( 0 )
if err != nil {
return 0 , err
}
return int64 ( webId ) , nil
}
2021-05-03 15:16:02 +08:00
// CountAllEnabledServersWithSSLPolicyIds 计算使用SSL策略的所有服务数量
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) CountAllEnabledServersWithSSLPolicyIds ( tx * dbs . Tx , sslPolicyIds [ ] int64 ) ( count int64 , err error ) {
2020-09-30 17:46:43 +08:00
if len ( sslPolicyIds ) == 0 {
return
}
policyStringIds := [ ] string { }
for _ , policyId := range sslPolicyIds {
policyStringIds = append ( policyStringIds , strconv . FormatInt ( policyId , 10 ) )
}
2021-01-01 23:31:30 +08:00
return this . Query ( tx ) .
2020-09-30 17:46:43 +08:00
State ( ServerStateEnabled ) .
Where ( "(FIND_IN_SET(JSON_EXTRACT(https, '$.sslPolicyRef.sslPolicyId'), :policyIds) OR FIND_IN_SET(JSON_EXTRACT(tls, '$.sslPolicyRef.sslPolicyId'), :policyIds))" ) .
Param ( "policyIds" , strings . Join ( policyStringIds , "," ) ) .
Count ( )
}
2021-05-03 15:16:02 +08:00
// FindAllEnabledServersWithSSLPolicyIds 查找使用某个SSL策略的所有服务
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindAllEnabledServersWithSSLPolicyIds ( tx * dbs . Tx , sslPolicyIds [ ] int64 ) ( result [ ] * Server , err error ) {
2020-09-30 17:46:43 +08:00
if len ( sslPolicyIds ) == 0 {
return
}
policyStringIds := [ ] string { }
for _ , policyId := range sslPolicyIds {
policyStringIds = append ( policyStringIds , strconv . FormatInt ( policyId , 10 ) )
}
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2020-09-30 17:46:43 +08:00
State ( ServerStateEnabled ) .
Result ( "id" , "name" , "https" , "tls" , "isOn" , "type" ) .
Where ( "(FIND_IN_SET(JSON_EXTRACT(https, '$.sslPolicyRef.sslPolicyId'), :policyIds) OR FIND_IN_SET(JSON_EXTRACT(tls, '$.sslPolicyRef.sslPolicyId'), :policyIds))" ) .
Param ( "policyIds" , strings . Join ( policyStringIds , "," ) ) .
Slice ( & result ) .
AscPk ( ) .
FindAll ( )
return
}
2021-05-03 15:16:02 +08:00
// FindAllEnabledServerIdsWithSSLPolicyIds 查找使用某个SSL策略的所有服务Id
2021-01-17 16:48:00 +08:00
func ( this * ServerDAO ) FindAllEnabledServerIdsWithSSLPolicyIds ( tx * dbs . Tx , sslPolicyIds [ ] int64 ) ( result [ ] int64 , err error ) {
if len ( sslPolicyIds ) == 0 {
return
}
for _ , policyId := range sslPolicyIds {
ones , err := this . Query ( tx ) .
State ( ServerStateEnabled ) .
ResultPk ( ) .
Where ( "(JSON_CONTAINS(https, :jsonQuery) OR JSON_CONTAINS(tls, :jsonQuery))" ) .
Param ( "jsonQuery" , maps . Map { "sslPolicyRef" : maps . Map { "sslPolicyId" : policyId } } . AsJSON ( ) ) .
FindAll ( )
if err != nil {
return nil , err
}
for _ , one := range ones {
serverId := int64 ( one . ( * Server ) . Id )
if ! lists . ContainsInt64 ( result , serverId ) {
result = append ( result , serverId )
}
}
}
return
}
2021-05-03 15:16:02 +08:00
// CountEnabledServersWithWebIds 计算使用某个缓存策略的所有服务数量
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) CountEnabledServersWithWebIds ( tx * dbs . Tx , webIds [ ] int64 ) ( count int64 , err error ) {
2020-10-02 17:22:32 +08:00
if len ( webIds ) == 0 {
return
}
2021-01-01 23:31:30 +08:00
return this . Query ( tx ) .
2020-10-02 17:22:32 +08:00
State ( ServerStateEnabled ) .
Attr ( "webId" , webIds ) .
2021-03-08 19:05:24 +08:00
Reuse ( false ) .
2020-10-02 17:22:32 +08:00
Count ( )
}
2021-05-03 15:16:02 +08:00
// FindAllEnabledServersWithWebIds 查找使用某个缓存策略的所有服务
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindAllEnabledServersWithWebIds ( tx * dbs . Tx , webIds [ ] int64 ) ( result [ ] * Server , err error ) {
2020-10-02 17:22:32 +08:00
if len ( webIds ) == 0 {
return
}
2021-01-01 23:31:30 +08:00
_ , err = this . Query ( tx ) .
2020-10-02 17:22:32 +08:00
State ( ServerStateEnabled ) .
Attr ( "webId" , webIds ) .
2021-03-08 19:05:24 +08:00
Reuse ( false ) .
2020-10-02 17:22:32 +08:00
AscPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-05-03 15:16:02 +08:00
// CountAllEnabledServersWithNodeClusterId 计算使用某个集群的所有服务数量
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) CountAllEnabledServersWithNodeClusterId ( tx * dbs . Tx , clusterId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-10-25 16:22:44 +08:00
State ( ServerStateEnabled ) .
Attr ( "clusterId" , clusterId ) .
Count ( )
}
2021-05-03 15:16:02 +08:00
// CountAllEnabledServersWithGroupId 计算使用某个分组的服务数量
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) CountAllEnabledServersWithGroupId ( tx * dbs . Tx , groupId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-10-29 20:53:40 +08:00
State ( ServerStateEnabled ) .
Where ( "JSON_CONTAINS(groupIds, :groupId)" ) .
2020-10-29 21:45:45 +08:00
Param ( "groupId" , numberutils . FormatInt64 ( groupId ) ) .
2020-10-29 20:53:40 +08:00
Count ( )
}
2021-05-03 15:16:02 +08:00
// FindAllServerDNSNamesWithDNSDomainId 查询使用某个DNS域名的所有服务域名
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindAllServerDNSNamesWithDNSDomainId ( tx * dbs . Tx , dnsDomainId int64 ) ( [ ] string , error ) {
clusterIds , err := SharedNodeClusterDAO . FindAllEnabledClusterIdsWithDNSDomainId ( tx , dnsDomainId )
2020-11-13 18:22:22 +08:00
if err != nil {
return nil , err
}
if len ( clusterIds ) == 0 {
return nil , nil
}
2021-01-01 23:31:30 +08:00
ones , err := this . Query ( tx ) .
2020-11-13 18:22:22 +08:00
State ( ServerStateEnabled ) .
Attr ( "isOn" , true ) .
Attr ( "clusterId" , clusterIds ) .
Result ( "dnsName" ) .
Reuse ( false ) . // 避免因为IN语句造成内存占用过多
FindAll ( )
if err != nil {
return nil , err
}
result := [ ] string { }
for _ , one := range ones {
dnsName := one . ( * Server ) . DnsName
if len ( dnsName ) == 0 {
continue
}
result = append ( result , dnsName )
}
return result , nil
}
2021-05-03 15:16:02 +08:00
// FindAllServersDNSWithClusterId 获取某个集群下的服务DNS信息
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindAllServersDNSWithClusterId ( tx * dbs . Tx , clusterId int64 ) ( result [ ] * Server , err error ) {
_ , err = this . Query ( tx ) .
2020-11-14 11:05:43 +08:00
State ( ServerStateEnabled ) .
Attr ( "isOn" , true ) .
2020-12-24 17:16:51 +08:00
Attr ( "isAuditing" , false ) . // 不在审核中
2020-11-14 11:05:43 +08:00
Attr ( "clusterId" , clusterId ) .
Result ( "id" , "name" , "dnsName" ) .
DescPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-05-03 15:16:02 +08:00
// GenerateServerDNSName 重新生成子域名
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) GenerateServerDNSName ( tx * dbs . Tx , serverId int64 ) ( string , error ) {
2020-11-14 11:05:43 +08:00
if serverId <= 0 {
return "" , errors . New ( "invalid serverId" )
}
2021-01-27 23:00:02 +08:00
dnsName , err := this . GenDNSName ( tx )
2020-11-14 11:05:43 +08:00
if err != nil {
return "" , err
}
op := NewServerOperator ( )
op . Id = serverId
op . DnsName = dnsName
2021-01-01 23:31:30 +08:00
err = this . Save ( tx , op )
2021-01-17 16:48:00 +08:00
if err != nil {
return "" , err
}
2020-11-14 11:05:43 +08:00
2021-01-17 16:48:00 +08:00
err = this . NotifyUpdate ( tx , serverId )
if err != nil {
return "" , err
}
2021-01-27 23:00:02 +08:00
err = this . NotifyDNSUpdate ( tx , serverId )
if err != nil {
return "" , err
}
2021-01-17 16:48:00 +08:00
return dnsName , nil
2020-08-21 12:32:33 +08:00
}
2020-11-11 21:32:25 +08:00
2021-05-03 15:16:02 +08:00
// FindServerClusterId 查询当前服务的集群ID
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindServerClusterId ( tx * dbs . Tx , serverId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-11-16 15:26:51 +08:00
Pk ( serverId ) .
Result ( "clusterId" ) .
FindInt64Col ( 0 )
}
2021-05-03 15:16:02 +08:00
// FindServerDNSName 查询服务的DNS名称
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindServerDNSName ( tx * dbs . Tx , serverId int64 ) ( string , error ) {
return this . Query ( tx ) .
2020-11-16 23:30:47 +08:00
Pk ( serverId ) .
Result ( "dnsName" ) .
FindStringCol ( "" )
}
2021-05-03 15:16:02 +08:00
// FindStatelessServerDNS 查询服务的DNS相关信息, 并且不关注状态
2021-01-27 23:00:02 +08:00
func ( this * ServerDAO ) FindStatelessServerDNS ( tx * dbs . Tx , serverId int64 ) ( * Server , error ) {
one , err := this . Query ( tx ) .
Pk ( serverId ) .
Result ( "id" , "dnsName" , "isOn" , "state" , "clusterId" ) .
Find ( )
if err != nil || one == nil {
return nil , err
}
return one . ( * Server ) , nil
}
2021-05-03 15:16:02 +08:00
// FindServerAdminIdAndUserId 获取当前服务的管理员ID和用户ID
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindServerAdminIdAndUserId ( tx * dbs . Tx , serverId int64 ) ( adminId int64 , userId int64 , err error ) {
one , err := this . Query ( tx ) .
2020-12-18 21:18:53 +08:00
Pk ( serverId ) .
Result ( "adminId" , "userId" ) .
Find ( )
if err != nil {
return 0 , 0 , err
}
if one == nil {
return 0 , 0 , nil
}
return int64 ( one . ( * Server ) . AdminId ) , int64 ( one . ( * Server ) . UserId ) , nil
}
2021-07-11 18:05:57 +08:00
// FindServerUserId 查找服务的用户ID
func ( this * ServerDAO ) FindServerUserId ( tx * dbs . Tx , serverId int64 ) ( userId int64 , err error ) {
one , _ , err := this . Query ( tx ) .
Pk ( serverId ) .
Result ( "userId" ) .
FindOne ( )
if err != nil || one == nil {
return 0 , err
}
return one . GetInt64 ( "userId" ) , nil
}
2021-05-03 15:16:02 +08:00
// CheckUserServer 检查用户服务
2021-01-17 16:48:00 +08:00
func ( this * ServerDAO ) CheckUserServer ( tx * dbs . Tx , userId int64 , serverId int64 ) error {
2020-12-19 19:09:14 +08:00
if serverId <= 0 || userId <= 0 {
return ErrNotFound
}
2021-01-01 23:31:30 +08:00
ok , err := this . Query ( tx ) .
2020-12-19 19:09:14 +08:00
Pk ( serverId ) .
Attr ( "userId" , userId ) .
Exist ( )
if err != nil {
return err
}
if ! ok {
return ErrNotFound
}
return nil
}
2021-05-03 15:16:02 +08:00
// UpdateUserServersClusterId 设置一个用户下的所有服务的所属集群
2021-06-19 22:19:12 +08:00
func ( this * ServerDAO ) UpdateUserServersClusterId ( tx * dbs . Tx , userId int64 , oldClusterId , newClusterId int64 ) error {
_ , err := this . Query ( tx ) .
2020-12-23 19:44:50 +08:00
Attr ( "userId" , userId ) .
2021-06-19 22:19:12 +08:00
Set ( "clusterId" , newClusterId ) .
2020-12-23 19:44:50 +08:00
Update ( )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
if oldClusterId > 0 {
2021-08-08 15:47:48 +08:00
err = SharedNodeTaskDAO . CreateClusterTask ( tx , nodeconfigs . NodeRoleNode , oldClusterId , NodeTaskTypeConfigChanged )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
2021-08-08 15:47:48 +08:00
err = SharedNodeTaskDAO . CreateClusterTask ( tx , nodeconfigs . NodeRoleNode , oldClusterId , NodeTaskTypeIPItemChanged )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
2021-06-19 22:19:12 +08:00
err = dns . SharedDNSTaskDAO . CreateClusterTask ( tx , oldClusterId , dns . DNSTaskTypeClusterChange )
if err != nil {
return err
}
2021-01-17 16:48:00 +08:00
}
2021-06-19 22:19:12 +08:00
if newClusterId > 0 {
2021-08-08 15:47:48 +08:00
err = SharedNodeTaskDAO . CreateClusterTask ( tx , nodeconfigs . NodeRoleNode , newClusterId , NodeTaskTypeConfigChanged )
2021-06-19 22:19:12 +08:00
if err != nil {
return err
}
2021-08-08 15:47:48 +08:00
err = SharedNodeTaskDAO . CreateClusterTask ( tx , nodeconfigs . NodeRoleNode , newClusterId , NodeTaskTypeIPItemChanged )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
2021-06-19 22:19:12 +08:00
err = dns . SharedDNSTaskDAO . CreateClusterTask ( tx , newClusterId , dns . DNSTaskTypeClusterChange )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
}
2020-12-23 19:44:50 +08:00
return err
}
2021-05-03 15:16:02 +08:00
// FindAllEnabledServersWithUserId 查找用户的所有的服务
2021-01-01 23:31:30 +08:00
func ( this * ServerDAO ) FindAllEnabledServersWithUserId ( tx * dbs . Tx , userId int64 ) ( result [ ] * Server , err error ) {
_ , err = this . Query ( tx ) .
2020-12-23 21:24:48 +08:00
State ( ServerStateEnabled ) .
Attr ( "userId" , userId ) .
DescPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-05-03 15:16:02 +08:00
// FindEnabledServerIdWithWebId 根据WebId查找ServerId
2021-01-10 17:34:35 +08:00
func ( this * ServerDAO ) FindEnabledServerIdWithWebId ( tx * dbs . Tx , webId int64 ) ( serverId int64 , err error ) {
if webId <= 0 {
return 0 , nil
}
return this . Query ( tx ) .
State ( ServerStateEnabled ) .
Attr ( "webId" , webId ) .
ResultPk ( ) .
FindInt64Col ( 0 )
}
2021-05-03 15:16:02 +08:00
// FindEnabledServerIdWithReverseProxyId 查找包含某个反向代理的Server
2021-01-17 16:48:00 +08:00
func ( this * ServerDAO ) FindEnabledServerIdWithReverseProxyId ( tx * dbs . Tx , reverseProxyId int64 ) ( serverId int64 , err error ) {
return this . Query ( tx ) .
State ( ServerStateEnabled ) .
Where ( "JSON_CONTAINS(reverseProxy, :jsonQuery)" ) .
Param ( "jsonQuery" , maps . Map { "reverseProxyId" : reverseProxyId } . AsJSON ( ) ) .
ResultPk ( ) .
FindInt64Col ( 0 )
}
2021-05-03 15:16:02 +08:00
// CheckPortIsUsing 检查端口是否被使用
2021-01-27 11:53:30 +08:00
func ( this * ServerDAO ) CheckPortIsUsing ( tx * dbs . Tx , clusterId int64 , port int , excludeServerId int64 , excludeProtocol string ) ( bool , error ) {
2021-01-14 16:34:52 +08:00
listen := maps . Map {
"portRange" : strconv . Itoa ( port ) ,
}
2021-01-27 11:53:30 +08:00
query := this . Query ( tx ) .
2021-01-14 16:34:52 +08:00
Attr ( "clusterId" , clusterId ) .
2021-01-27 11:53:30 +08:00
State ( ServerStateEnabled )
protocols := [ ] string { "http" , "https" , "tcp" , "tls" , "udp" }
where := ""
if excludeServerId <= 0 {
conds := [ ] string { }
for _ , p := range protocols {
conds = append ( conds , "JSON_CONTAINS(" + p + ", :listen, '$.listen')" )
}
where = strings . Join ( conds , " OR " )
} else {
conds := [ ] string { }
for _ , p := range protocols {
conds = append ( conds , "JSON_CONTAINS(" + p + ", :listen, '$.listen')" )
}
where1 := "(id!=:serverId AND (" + strings . Join ( conds , " OR " ) + "))"
conds = [ ] string { }
for _ , p := range protocols {
if p == excludeProtocol {
continue
}
conds = append ( conds , "JSON_CONTAINS(" + p + ", :listen, '$.listen')" )
}
where2 := "(id=:serverId AND (" + strings . Join ( conds , " OR " ) + "))"
where = where1 + " OR " + where2
query . Param ( "serverId" , excludeServerId )
}
return query .
Where ( "(" + where + ")" ) .
Param ( "listen" , string ( listen . AsJSON ( ) ) ) .
2021-01-14 16:34:52 +08:00
Exist ( )
}
2021-05-03 15:16:02 +08:00
// ExistServerNameInCluster 检查ServerName是否已存在
2021-02-06 21:19:19 +08:00
func ( this * ServerDAO ) ExistServerNameInCluster ( tx * dbs . Tx , clusterId int64 , serverName string , excludeServerId int64 ) ( bool , error ) {
query := this . Query ( tx ) .
Attr ( "clusterId" , clusterId ) .
Where ( "(JSON_CONTAINS(serverNames, :jsonQuery1) OR JSON_CONTAINS(serverNames, :jsonQuery2))" ) .
Param ( "jsonQuery1" , maps . Map { "name" : serverName } . AsJSON ( ) ) .
Param ( "jsonQuery2" , maps . Map { "subNames" : serverName } . AsJSON ( ) )
if excludeServerId > 0 {
query . Neq ( "id" , excludeServerId )
}
query . State ( ServerStateEnabled )
return query . Exist ( )
}
2021-05-03 15:16:02 +08:00
// GenDNSName 生成DNS Name
2021-01-27 23:00:02 +08:00
func ( this * ServerDAO ) GenDNSName ( tx * dbs . Tx ) ( string , error ) {
for {
dnsName := rands . HexString ( 8 )
exist , err := this . Query ( tx ) .
Attr ( "dnsName" , dnsName ) .
Exist ( )
if err != nil {
return "" , err
}
if ! exist {
return dnsName , nil
}
}
}
2021-05-03 15:16:02 +08:00
// FindLatestServers 查询最近访问的服务
func ( this * ServerDAO ) FindLatestServers ( tx * dbs . Tx , size int64 ) ( result [ ] * Server , err error ) {
itemTable := SharedLatestItemDAO . Table
itemType := LatestItemTypeServer
_ , err = this . Query ( tx ) .
Result ( this . Table + ".id" , this . Table + ".name" ) .
Join ( SharedLatestItemDAO , dbs . QueryJoinRight , this . Table + ".id=" + itemTable + ".itemId AND " + itemTable + ".itemType='" + itemType + "'" ) .
Asc ( "CEIL((UNIX_TIMESTAMP() - " + itemTable + ".updatedAt) / (7 * 86400))" ) . // 优先一个星期以内的
Desc ( itemTable + ".count" ) .
State ( NodeClusterStateEnabled ) .
Limit ( size ) .
Slice ( & result ) .
FindAll ( )
return
}
// NotifyUpdate 同步集群
2021-01-17 16:48:00 +08:00
func ( this * ServerDAO ) NotifyUpdate ( tx * dbs . Tx , serverId int64 ) error {
// 创建任务
clusterId , err := this . FindServerClusterId ( tx , serverId )
if err != nil {
return err
}
if clusterId == 0 {
return nil
}
2021-08-08 15:47:48 +08:00
return SharedNodeTaskDAO . CreateClusterTask ( tx , nodeconfigs . NodeRoleNode , clusterId , NodeTaskTypeConfigChanged )
2021-01-17 16:48:00 +08:00
}
2021-05-03 15:16:02 +08:00
// NotifyDNSUpdate 通知DNS更新
2021-01-27 23:00:02 +08:00
func ( this * ServerDAO ) NotifyDNSUpdate ( tx * dbs . Tx , serverId int64 ) error {
clusterId , err := this . Query ( tx ) .
Pk ( serverId ) .
Result ( "clusterId" ) .
FindInt64Col ( 0 ) // 这里不需要加服务状态条件, 因为我们即使删除也要删除对应的服务的DNS解析
if err != nil {
return err
}
if clusterId <= 0 {
return nil
}
dnsInfo , err := SharedNodeClusterDAO . FindClusterDNSInfo ( tx , clusterId )
if err != nil {
return err
}
if dnsInfo == nil {
return nil
}
if len ( dnsInfo . DnsName ) == 0 || dnsInfo . DnsDomainId <= 0 {
return nil
2020-11-11 21:32:25 +08:00
}
2021-01-27 23:00:02 +08:00
return dns . SharedDNSTaskDAO . CreateServerTask ( tx , serverId , dns . DNSTaskTypeServerChange )
2020-11-11 21:32:25 +08:00
}