ddns: remove ipv6 nameservers, support custom nameservers (#439)

This commit is contained in:
UUBulb 2024-10-18 22:06:01 +08:00 committed by GitHub
parent c58c4f866a
commit f6531a52bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 45 additions and 3 deletions

View File

@ -1012,6 +1012,7 @@ type settingForm struct {
DashboardTheme string DashboardTheme string
CustomCode string CustomCode string
CustomCodeDashboard string CustomCodeDashboard string
CustomNameservers string
ViewPassword string ViewPassword string
IgnoredIPNotification string IgnoredIPNotification string
IPChangeNotificationTag string // IP变更提醒的通知组 IPChangeNotificationTag string // IP变更提醒的通知组
@ -1078,6 +1079,7 @@ func (ma *memberAPI) updateSetting(c *gin.Context) {
singleton.Conf.Site.DashboardTheme = sf.DashboardTheme singleton.Conf.Site.DashboardTheme = sf.DashboardTheme
singleton.Conf.Site.CustomCode = sf.CustomCode singleton.Conf.Site.CustomCode = sf.CustomCode
singleton.Conf.Site.CustomCodeDashboard = sf.CustomCodeDashboard singleton.Conf.Site.CustomCodeDashboard = sf.CustomCodeDashboard
singleton.Conf.DNSServers = sf.CustomNameservers
singleton.Conf.Site.ViewPassword = sf.ViewPassword singleton.Conf.Site.ViewPassword = sf.ViewPassword
singleton.Conf.Oauth2.Admin = sf.Admin singleton.Conf.Oauth2.Admin = sf.Admin
// 保证NotificationTag不为空 // 保证NotificationTag不为空
@ -1093,6 +1095,8 @@ func (ma *memberAPI) updateSetting(c *gin.Context) {
} }
// 更新系统语言 // 更新系统语言
singleton.InitLocalizer() singleton.InitLocalizer()
// 更新DNS服务器
singleton.OnNameserverUpdate()
c.JSON(http.StatusOK, model.Response{ c.JSON(http.StatusOK, model.Response{
Code: http.StatusOK, Code: http.StatusOK,
}) })

View File

@ -125,6 +125,8 @@ type Config struct {
IgnoredIPNotificationServerIDs map[uint64]bool // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内 IgnoredIPNotificationServerIDs map[uint64]bool // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内
MaxTCPPingValue int32 MaxTCPPingValue int32
AvgPingCount int AvgPingCount int
DNSServers string
} }
// Read 读取配置文件并应用 // Read 读取配置文件并应用

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"strings"
"time" "time"
"github.com/libdns/libdns" "github.com/libdns/libdns"
@ -13,7 +14,10 @@ import (
"github.com/naiba/nezha/pkg/utils" "github.com/naiba/nezha/pkg/utils"
) )
var dnsTimeOut = 10 * time.Second var (
dnsTimeOut = 10 * time.Second
customDNSServers []string
)
type IP struct { type IP struct {
Ipv4Addr string Ipv4Addr string
@ -33,6 +37,12 @@ type Provider struct {
Setter libdns.RecordSetter Setter libdns.RecordSetter
} }
func InitDNSServers(s string) {
if s != "" {
customDNSServers = strings.Split(s, ",")
}
}
func (provider *Provider) UpdateDomain(ctx context.Context) { func (provider *Provider) UpdateDomain(ctx context.Context) {
provider.ctx = ctx provider.ctx = ctx
for _, domain := range provider.DDNSProfile.Domains { for _, domain := range provider.DDNSProfile.Domains {
@ -95,12 +105,17 @@ func splitDomainSOA(domain string) (prefix string, zone string, err error) {
domain += "." domain += "."
indexes := dns.Split(domain) indexes := dns.Split(domain)
servers := utils.DNSServers
if len(customDNSServers) > 0 {
servers = customDNSServers
}
var r *dns.Msg var r *dns.Msg
for _, idx := range indexes { for _, idx := range indexes {
m := new(dns.Msg) m := new(dns.Msg)
m.SetQuestion(domain[idx:], dns.TypeSOA) m.SetQuestion(domain[idx:], dns.TypeSOA)
for _, server := range utils.DNSServers { for _, server := range servers {
r, _, err = c.Exchange(m, server) r, _, err = c.Exchange(m, server)
if err != nil { if err != nil {
return return

View File

@ -13,7 +13,7 @@ import (
var ( var (
Json = jsoniter.ConfigCompatibleWithStandardLibrary Json = jsoniter.ConfigCompatibleWithStandardLibrary
DNSServers = []string{"1.1.1.1:53", "223.5.5.5:53", "[2606:4700:4700::1111]:53", "[2400:3200::1]:53"} DNSServers = []string{"1.1.1.1:53", "223.5.5.5:53"}
) )
func IsWindows() bool { func IsWindows() bool {

View File

@ -747,3 +747,6 @@ other = "Expired"
[Days] [Days]
other = "d" other = "d"
[CustomNameservers]
other = "Custom Public DNS Nameservers for DDNS (separate with comma)"

View File

@ -747,3 +747,6 @@ other = "Expired"
[Days] [Days]
other = "d" other = "d"
[CustomNameservers]
other = "Servidores DNS públicos personalizados para DDNS (separar con coma)"

View File

@ -747,3 +747,6 @@ other = "已到期"
[Days] [Days]
other = "天" other = "天"
[CustomNameservers]
other = "自定义DDNS使用的公共DNS服务器逗号分隔"

View File

@ -747,3 +747,6 @@ other = "已到期"
[Days] [Days]
other = "天" other = "天"
[CustomNameservers]
other = "自訂DDNS使用的公共DNS伺服器逗號分隔"

View File

@ -53,6 +53,10 @@
<label>{{tr "PanelServerDomainAndIP"}}</label> <label>{{tr "PanelServerDomainAndIP"}}</label>
<input type="text" name="GRPCHost" placeholder="" value="{{.Conf.GRPCHost}}"> <input type="text" name="GRPCHost" placeholder="" value="{{.Conf.GRPCHost}}">
</div> </div>
<div class="field">
<label>{{tr "CustomNameservers"}}</label>
<input type="text" name="CustomNameservers" placeholder="" value="{{.Conf.DNSServers}}">
</div>
<div class="field"> <div class="field">
<label>{{tr "IPChangeAlert"}}</label> <label>{{tr "IPChangeAlert"}}</label>
</div> </div>

View File

@ -20,6 +20,7 @@ var (
func initDDNS() { func initDDNS() {
OnDDNSUpdate() OnDDNSUpdate()
OnNameserverUpdate()
} }
func OnDDNSUpdate() { func OnDDNSUpdate() {
@ -33,6 +34,10 @@ func OnDDNSUpdate() {
} }
} }
func OnNameserverUpdate() {
ddns2.InitDNSServers(Conf.DNSServers)
}
func GetDDNSProvidersFromProfiles(profileId []uint64, ip *ddns2.IP) ([]*ddns2.Provider, error) { func GetDDNSProvidersFromProfiles(profileId []uint64, ip *ddns2.IP) ([]*ddns2.Provider, error) {
profiles := make([]*model.DDNSProfile, 0, len(profileId)) profiles := make([]*model.DDNSProfile, 0, len(profileId))
ddnsCacheLock.RLock() ddnsCacheLock.RLock()