From f6531a52bd888e8eecc464b4ca0a99687e6423d1 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Fri, 18 Oct 2024 22:06:01 +0800 Subject: [PATCH] ddns: remove ipv6 nameservers, support custom nameservers (#439) --- cmd/dashboard/controller/member_api.go | 4 ++++ model/config.go | 2 ++ pkg/ddns/ddns.go | 19 +++++++++++++++++-- pkg/utils/utils.go | 2 +- resource/l10n/en-US.toml | 3 +++ resource/l10n/es-ES.toml | 3 +++ resource/l10n/zh-CN.toml | 3 +++ resource/l10n/zh-TW.toml | 3 +++ .../template/dashboard-default/setting.html | 4 ++++ service/singleton/ddns.go | 5 +++++ 10 files changed, 45 insertions(+), 3 deletions(-) diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index e038f57..4d76a2d 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -1012,6 +1012,7 @@ type settingForm struct { DashboardTheme string CustomCode string CustomCodeDashboard string + CustomNameservers string ViewPassword string IgnoredIPNotification string IPChangeNotificationTag string // IP变更提醒的通知组 @@ -1078,6 +1079,7 @@ func (ma *memberAPI) updateSetting(c *gin.Context) { singleton.Conf.Site.DashboardTheme = sf.DashboardTheme singleton.Conf.Site.CustomCode = sf.CustomCode singleton.Conf.Site.CustomCodeDashboard = sf.CustomCodeDashboard + singleton.Conf.DNSServers = sf.CustomNameservers singleton.Conf.Site.ViewPassword = sf.ViewPassword singleton.Conf.Oauth2.Admin = sf.Admin // 保证NotificationTag不为空 @@ -1093,6 +1095,8 @@ func (ma *memberAPI) updateSetting(c *gin.Context) { } // 更新系统语言 singleton.InitLocalizer() + // 更新DNS服务器 + singleton.OnNameserverUpdate() c.JSON(http.StatusOK, model.Response{ Code: http.StatusOK, }) diff --git a/model/config.go b/model/config.go index 8560922..5dcc842 100644 --- a/model/config.go +++ b/model/config.go @@ -125,6 +125,8 @@ type Config struct { IgnoredIPNotificationServerIDs map[uint64]bool // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内) MaxTCPPingValue int32 AvgPingCount int + + DNSServers string } // Read 读取配置文件并应用 diff --git a/pkg/ddns/ddns.go b/pkg/ddns/ddns.go index 59f0d3d..286f6a5 100644 --- a/pkg/ddns/ddns.go +++ b/pkg/ddns/ddns.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "strings" "time" "github.com/libdns/libdns" @@ -13,7 +14,10 @@ import ( "github.com/naiba/nezha/pkg/utils" ) -var dnsTimeOut = 10 * time.Second +var ( + dnsTimeOut = 10 * time.Second + customDNSServers []string +) type IP struct { Ipv4Addr string @@ -33,6 +37,12 @@ type Provider struct { Setter libdns.RecordSetter } +func InitDNSServers(s string) { + if s != "" { + customDNSServers = strings.Split(s, ",") + } +} + func (provider *Provider) UpdateDomain(ctx context.Context) { provider.ctx = ctx for _, domain := range provider.DDNSProfile.Domains { @@ -95,12 +105,17 @@ func splitDomainSOA(domain string) (prefix string, zone string, err error) { domain += "." indexes := dns.Split(domain) + servers := utils.DNSServers + if len(customDNSServers) > 0 { + servers = customDNSServers + } + var r *dns.Msg for _, idx := range indexes { m := new(dns.Msg) m.SetQuestion(domain[idx:], dns.TypeSOA) - for _, server := range utils.DNSServers { + for _, server := range servers { r, _, err = c.Exchange(m, server) if err != nil { return diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 4c8e2ea..5c45c28 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -13,7 +13,7 @@ import ( var ( 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 { diff --git a/resource/l10n/en-US.toml b/resource/l10n/en-US.toml index cfdf7ed..38cdfa5 100644 --- a/resource/l10n/en-US.toml +++ b/resource/l10n/en-US.toml @@ -747,3 +747,6 @@ other = "Expired" [Days] other = "d" + +[CustomNameservers] +other = "Custom Public DNS Nameservers for DDNS (separate with comma)" diff --git a/resource/l10n/es-ES.toml b/resource/l10n/es-ES.toml index c008fc4..c6b598c 100644 --- a/resource/l10n/es-ES.toml +++ b/resource/l10n/es-ES.toml @@ -747,3 +747,6 @@ other = "Expired" [Days] other = "d" + +[CustomNameservers] +other = "Servidores DNS públicos personalizados para DDNS (separar con coma)" diff --git a/resource/l10n/zh-CN.toml b/resource/l10n/zh-CN.toml index d990355..204b830 100644 --- a/resource/l10n/zh-CN.toml +++ b/resource/l10n/zh-CN.toml @@ -747,3 +747,6 @@ other = "已到期" [Days] other = "天" + +[CustomNameservers] +other = "自定义DDNS使用的公共DNS服务器(逗号分隔)" diff --git a/resource/l10n/zh-TW.toml b/resource/l10n/zh-TW.toml index 227eba5..4e41db7 100644 --- a/resource/l10n/zh-TW.toml +++ b/resource/l10n/zh-TW.toml @@ -747,3 +747,6 @@ other = "已到期" [Days] other = "天" + +[CustomNameservers] +other = "自訂DDNS使用的公共DNS伺服器(逗號分隔)" diff --git a/resource/template/dashboard-default/setting.html b/resource/template/dashboard-default/setting.html index fb731dd..9bcd9c8 100644 --- a/resource/template/dashboard-default/setting.html +++ b/resource/template/dashboard-default/setting.html @@ -53,6 +53,10 @@ +