支持任意域名通过CNAME访问服务(开启选项后)/可以重新生成服务CNAME

This commit is contained in:
GoEdgeLab
2021-10-16 12:03:21 +08:00
parent 1ada07f1f7
commit ec88c40b23
6 changed files with 100 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
package utils package utils
import ( import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@@ -16,13 +17,19 @@ func LookupCNAME(host string) (string, error) {
m.SetQuestion(host+".", dns.TypeCNAME) m.SetQuestion(host+".", dns.TypeCNAME)
m.RecursionDesired = true m.RecursionDesired = true
r, _, err := c.Exchange(m, config.Servers[0]+":"+config.Port)
if err != nil {
return "", err
}
if len(r.Answer) == 0 {
return "", nil
}
return r.Answer[0].(*dns.CNAME).Target, nil var lastErr error
for _, serverAddr := range config.Servers {
r, _, err := c.Exchange(m, configutils.QuoteIP(serverAddr)+":"+config.Port)
if err != nil {
lastErr = err
continue
}
if len(r.Answer) == 0 {
continue
}
return r.Answer[0].(*dns.CNAME).Target, nil
}
return "", lastErr
} }

View File

@@ -3,6 +3,7 @@ package dns
import ( import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
) )
type IndexAction struct { type IndexAction struct {
@@ -28,6 +29,28 @@ func (this *IndexAction) RunGet(params struct {
} else { } else {
this.Data["dnsDomain"] = "" this.Data["dnsDomain"] = ""
} }
this.Data["supportCNAME"] = dnsInfoResp.SupportCNAME
this.Show() this.Show()
} }
func (this *IndexAction) RunPost(params struct {
ServerId int64
SupportCNAME bool
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo("修改服务 %d 的DNS设置", params.ServerId)
_, err := this.RPC().ServerRPC().UpdateServerDNS(this.AdminContext(), &pb.UpdateServerDNSRequest{
ServerId: params.ServerId,
SupportCNAME: params.SupportCNAME,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -14,6 +14,7 @@ func init() {
Helper(serverutils.NewServerHelper()). Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/dns"). Prefix("/servers/server/settings/dns").
GetPost("", new(IndexAction)). GetPost("", new(IndexAction)).
Post("/regenerateCNAME", new(RegenerateCNAMEAction)).
EndAll() EndAll()
}) })
} }

View File

@@ -0,0 +1,26 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package dns
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
type RegenerateCNAMEAction struct {
actionutils.ParentAction
}
func (this *RegenerateCNAMEAction) RunPost(params struct {
ServerId int64
}) {
defer this.CreateLogInfo("重新生成服务 %d 的CNAME", params.ServerId)
_, err := this.RPC().ServerRPC().RegenerateServerCNAME(this.AdminContext(), &pb.RegenerateServerCNAMERequest{ServerId: params.ServerId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -3,13 +3,25 @@
{$template "/left_menu"} {$template "/left_menu"}
<div class="right-box"> <div class="right-box">
<table class="ui table selectable definition"> <form class="ui form" data-tea-action="$" data-tea-success="success">
<tr> <csrf-token></csrf-token>
<td class="title">当前服务CNAME</td> <input type="hidden" name="serverId" :value="serverId"/>
<td> <table class="ui table selectable definition">
<span id="cname-text">{{dnsName}}.<span v-if="dnsDomain.length > 0">{{dnsDomain}}.</span><span v-else>根域名</span></span> &nbsp; <copy-to-clipboard :v-target="'cname-text'"></copy-to-clipboard> <tr>
<p class="comment">可以在DNS设置一个CNAME记录名字为你自己的域名值为上面这个值。</p> <td class="title">当前服务CNAME</td>
</td> <td>
</tr> <span id="cname-text">{{dnsName}}.<span v-if="dnsDomain.length > 0">{{dnsDomain}}.</span><span v-else>根域名</span></span> &nbsp; <copy-to-clipboard :v-target="'cname-text'"></copy-to-clipboard> &nbsp;<a href="" @click.prevent="regenerateCNAME()">[重新生成]</a>
</table> <p class="comment">可以在DNS设置一个CNAME记录名字为你自己的域名值为上面这个值。</p>
</td>
</tr>
<tr>
<td>支持CNAME</td>
<td>
<checkbox name="supportCNAME" v-model="supportCNAME"></checkbox>
<p class="comment">选中后表示允许任意域名使用此服务的CNAME直接访问此服务。需要节点服务器可以正确解析DNS记录。在严格匹配域名时才会生效。</p>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>
</div> </div>

View File

@@ -0,0 +1,14 @@
Tea.context(function () {
this.success = NotifyReloadSuccess("保存成功")
this.regenerateCNAME = function () {
let serverId = this.serverId
teaweb.confirm("确定要重新生成此服务的CNAME吗", function () {
this.$post(".regenerateCNAME")
.params({
serverId: serverId
})
.refresh()
})
}
})