mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-04 14:27:05 +08:00
智能DNS支持自定义端口
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
@@ -84,6 +86,42 @@ func (this *NSClusterDAO) CreateCluster(tx *dbs.Tx, name string, accessLogRefJSO
|
||||
|
||||
op.IsOn = true
|
||||
op.State = NSClusterStateEnabled
|
||||
|
||||
// 默认端口
|
||||
// TCP
|
||||
{
|
||||
var config = &serverconfigs.TCPProtocolConfig{}
|
||||
config.IsOn = true
|
||||
config.Listen = []*serverconfigs.NetworkAddressConfig{
|
||||
{
|
||||
Protocol: serverconfigs.ProtocolTCP,
|
||||
PortRange: "53",
|
||||
},
|
||||
}
|
||||
configJSON, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Tcp = configJSON
|
||||
}
|
||||
|
||||
// UDP
|
||||
{
|
||||
var config = &serverconfigs.UDPProtocolConfig{}
|
||||
config.IsOn = true
|
||||
config.Listen = []*serverconfigs.NetworkAddressConfig{
|
||||
{
|
||||
Protocol: serverconfigs.ProtocolUDP,
|
||||
PortRange: "53",
|
||||
},
|
||||
}
|
||||
configJSON, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Udp = configJSON
|
||||
}
|
||||
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
@@ -193,6 +231,78 @@ func (this *NSClusterDAO) FindClusterRecursion(tx *dbs.Tx, clusterId int64) ([]b
|
||||
return []byte(recursion), nil
|
||||
}
|
||||
|
||||
// FindClusterTCP 查找集群的TCP设置
|
||||
func (this *NSClusterDAO) FindClusterTCP(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||
return this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("tcp").
|
||||
FindBytesCol()
|
||||
}
|
||||
|
||||
// UpdateClusterTCP 修改集群的TCP设置
|
||||
func (this *NSClusterDAO) UpdateClusterTCP(tx *dbs.Tx, clusterId int64, tcpConfig *serverconfigs.TCPProtocolConfig) error {
|
||||
tcpJSON, err := json.Marshal(tcpConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Set("tcp", tcpJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, clusterId)
|
||||
}
|
||||
|
||||
// FindClusterTLS 查找集群的TLS设置
|
||||
func (this *NSClusterDAO) FindClusterTLS(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||
return this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("tls").
|
||||
FindBytesCol()
|
||||
}
|
||||
|
||||
// UpdateClusterTLS 修改集群的TLS设置
|
||||
func (this *NSClusterDAO) UpdateClusterTLS(tx *dbs.Tx, clusterId int64, tlsConfig *serverconfigs.TLSProtocolConfig) error {
|
||||
tlsJSON, err := json.Marshal(tlsConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Set("tls", tlsJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, clusterId)
|
||||
}
|
||||
|
||||
// FindClusterUDP 查找集群的TCP设置
|
||||
func (this *NSClusterDAO) FindClusterUDP(tx *dbs.Tx, clusterId int64) ([]byte, error) {
|
||||
return this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Result("udp").
|
||||
FindBytesCol()
|
||||
}
|
||||
|
||||
// UpdateClusterUDP 修改集群的UDP设置
|
||||
func (this *NSClusterDAO) UpdateClusterUDP(tx *dbs.Tx, clusterId int64, udpConfig *serverconfigs.UDPProtocolConfig) error {
|
||||
udpJSON, err := json.Marshal(udpConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = this.Query(tx).
|
||||
Pk(clusterId).
|
||||
Set("udp", udpJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, clusterId)
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更改
|
||||
func (this *NSClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
|
||||
return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleDNS, clusterId, 0, NSNodeTaskTypeConfigChanged)
|
||||
|
||||
@@ -12,6 +12,9 @@ type NSCluster struct {
|
||||
AccessLog dbs.JSON `field:"accessLog"` // 访问日志配置
|
||||
GrantId uint32 `field:"grantId"` // 授权ID
|
||||
Recursion dbs.JSON `field:"recursion"` // 递归DNS设置
|
||||
Tcp dbs.JSON `field:"tcp"` // TCP设置
|
||||
Tls dbs.JSON `field:"tls"` // TLS设置
|
||||
Udp dbs.JSON `field:"udp"` // UDP设置
|
||||
}
|
||||
|
||||
type NSClusterOperator struct {
|
||||
@@ -23,6 +26,9 @@ type NSClusterOperator struct {
|
||||
AccessLog interface{} // 访问日志配置
|
||||
GrantId interface{} // 授权ID
|
||||
Recursion interface{} // 递归DNS设置
|
||||
Tcp interface{} // TCP设置
|
||||
Tls interface{} // TLS设置
|
||||
Udp interface{} // UDP设置
|
||||
}
|
||||
|
||||
func NewNSClusterOperator() *NSClusterOperator {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
@@ -395,7 +396,7 @@ func (this *NSNodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*dnsconfigs.
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
config := &dnsconfigs.NSNodeConfig{
|
||||
var config = &dnsconfigs.NSNodeConfig{
|
||||
Id: int64(node.Id),
|
||||
NodeId: node.UniqueId,
|
||||
Secret: node.Secret,
|
||||
@@ -432,19 +433,57 @@ func (this *NSNodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*dnsconfigs.
|
||||
}
|
||||
|
||||
// 递归DNS配置
|
||||
recursionJSON, err := SharedNSClusterDAO.FindClusterRecursion(tx, int64(node.ClusterId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(recursionJSON) > 0 {
|
||||
if IsNotNull(cluster.Recursion) {
|
||||
var recursionConfig = &dnsconfigs.RecursionConfig{}
|
||||
err = json.Unmarshal(recursionJSON, recursionConfig)
|
||||
err = json.Unmarshal(cluster.Recursion, recursionConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.RecursionConfig = recursionConfig
|
||||
}
|
||||
|
||||
// TCP
|
||||
if IsNotNull(cluster.Tcp) {
|
||||
var tcpConfig = &serverconfigs.TCPProtocolConfig{}
|
||||
err = json.Unmarshal(cluster.Tcp, tcpConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.TCP = tcpConfig
|
||||
}
|
||||
|
||||
// TLS
|
||||
if IsNotNull(cluster.Tls) {
|
||||
var tlsConfig = &serverconfigs.TLSProtocolConfig{}
|
||||
err = json.Unmarshal(cluster.Tls, tlsConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// SSL
|
||||
if tlsConfig.SSLPolicyRef != nil {
|
||||
sslPolicyConfig, err := SharedSSLPolicyDAO.ComposePolicyConfig(tx, tlsConfig.SSLPolicyRef.SSLPolicyId, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if sslPolicyConfig != nil {
|
||||
tlsConfig.SSLPolicy = sslPolicyConfig
|
||||
}
|
||||
}
|
||||
|
||||
config.TLS = tlsConfig
|
||||
}
|
||||
|
||||
// UDP
|
||||
if IsNotNull(cluster.Udp) {
|
||||
var udpConfig = &serverconfigs.UDPProtocolConfig{}
|
||||
err = json.Unmarshal(cluster.Udp, udpConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.UDP = udpConfig
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user