mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-07 18:50:26 +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
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
)
|
||||
|
||||
// NSClusterService 域名服务集群相关服务
|
||||
@@ -115,6 +116,9 @@ func (this *NSClusterService) FindEnabledNSCluster(ctx context.Context, req *pb.
|
||||
IsOn: cluster.IsOn,
|
||||
Name: cluster.Name,
|
||||
InstallDir: cluster.InstallDir,
|
||||
TcpJSON: cluster.Tcp,
|
||||
TlsJSON: cluster.Tls,
|
||||
UdpJSON: cluster.Udp,
|
||||
}}, nil
|
||||
}
|
||||
|
||||
@@ -216,3 +220,123 @@ func (this *NSClusterService) FindNSClusterRecursionConfig(ctx context.Context,
|
||||
RecursionJSON: recursion,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FindNSClusterTCPConfig 查找集群的TCP设置
|
||||
func (this *NSClusterService) FindNSClusterTCPConfig(ctx context.Context, req *pb.FindNSClusterTCPConfigRequest) (*pb.FindNSClusterTCPConfigResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
tcpJSON, err := models.SharedNSClusterDAO.FindClusterTCP(tx, req.NsClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.FindNSClusterTCPConfigResponse{
|
||||
TcpJSON: tcpJSON,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateNSClusterTCP 修改集群的TCP设置
|
||||
func (this *NSClusterService) UpdateNSClusterTCP(ctx context.Context, req *pb.UpdateNSClusterTCPRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var config = &serverconfigs.TCPProtocolConfig{}
|
||||
err = json.Unmarshal(req.TcpJSON, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = models.SharedNSClusterDAO.UpdateClusterTCP(tx, req.NsClusterId, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindNSClusterTLSConfig 查找集群的TLS设置
|
||||
func (this *NSClusterService) FindNSClusterTLSConfig(ctx context.Context, req *pb.FindNSClusterTLSConfigRequest) (*pb.FindNSClusterTLSConfigResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
tlsJSON, err := models.SharedNSClusterDAO.FindClusterTLS(tx, req.NsClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.FindNSClusterTLSConfigResponse{
|
||||
TlsJSON: tlsJSON,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateNSClusterTLS 修改集群的TLS设置
|
||||
func (this *NSClusterService) UpdateNSClusterTLS(ctx context.Context, req *pb.UpdateNSClusterTLSRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var config = &serverconfigs.TLSProtocolConfig{}
|
||||
err = json.Unmarshal(req.TlsJSON, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = models.SharedNSClusterDAO.UpdateClusterTLS(tx, req.NsClusterId, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// FindNSClusterUDPConfig 查找集群的UDP设置
|
||||
func (this *NSClusterService) FindNSClusterUDPConfig(ctx context.Context, req *pb.FindNSClusterUDPConfigRequest) (*pb.FindNSClusterUDPConfigResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
udpJSON, err := models.SharedNSClusterDAO.FindClusterUDP(tx, req.NsClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.FindNSClusterUDPConfigResponse{
|
||||
UdpJSON: udpJSON,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateNSClusterUDP 修改集群的UDP设置
|
||||
func (this *NSClusterService) UpdateNSClusterUDP(ctx context.Context, req *pb.UpdateNSClusterUDPRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var config = &serverconfigs.UDPProtocolConfig{}
|
||||
err = json.Unmarshal(req.UdpJSON, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = models.SharedNSClusterDAO.UpdateClusterUDP(tx, req.NsClusterId, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -81,6 +81,9 @@ var upgradeFuncs = []*upgradeVersion{
|
||||
{
|
||||
"0.4.9", upgradeV0_4_9,
|
||||
},
|
||||
{
|
||||
"0.4.11", upgradeV0_4_11,
|
||||
},
|
||||
}
|
||||
|
||||
// UpgradeSQLData 升级SQL数据
|
||||
@@ -792,3 +795,51 @@ func upgradeV0_4_9(db *dbs.DB) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// v0.4.11
|
||||
func upgradeV0_4_11(db *dbs.DB) error {
|
||||
// 升级ns端口
|
||||
{
|
||||
// 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 err
|
||||
}
|
||||
_, err = db.Exec("UPDATE edgeNSClusters SET tcp=? WHERE tcp IS NULL", configJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 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 err
|
||||
}
|
||||
_, err = db.Exec("UPDATE edgeNSClusters SET udp=? WHERE udp IS NULL", configJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -195,7 +195,6 @@ func TestUpgradeSQLData_v0_4_8(t *testing.T) {
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
|
||||
func TestUpgradeSQLData_v0_4_9(t *testing.T) {
|
||||
db, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{
|
||||
Driver: "mysql",
|
||||
@@ -214,3 +213,22 @@ func TestUpgradeSQLData_v0_4_9(t *testing.T) {
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestUpgradeSQLData_v0_4_11(t *testing.T) {
|
||||
db, err := dbs.NewInstanceFromConfig(&dbs.DBConfig{
|
||||
Driver: "mysql",
|
||||
Dsn: "root:123456@tcp(127.0.0.1:3306)/db_edge?charset=utf8mb4&timeout=30s",
|
||||
Prefix: "edge",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
_ = db.Close()
|
||||
}()
|
||||
err = upgradeV0_4_11(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user