Skip to content

Commit

Permalink
ddns: remove ipv6 nameservers, support custom nameservers (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
uubulb authored Oct 18, 2024
1 parent c58c4f8 commit f6531a5
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/dashboard/controller/member_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ type settingForm struct {
DashboardTheme string
CustomCode string
CustomCodeDashboard string
CustomNameservers string
ViewPassword string
IgnoredIPNotification string
IPChangeNotificationTag string // IP变更提醒的通知组
Expand Down Expand Up @@ -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不为空
Expand All @@ -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,
})
Expand Down
2 changes: 2 additions & 0 deletions model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ type Config struct {
IgnoredIPNotificationServerIDs map[uint64]bool // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内)
MaxTCPPingValue int32
AvgPingCount int

DNSServers string
}

// Read 读取配置文件并应用
Expand Down
19 changes: 17 additions & 2 deletions pkg/ddns/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/libdns/libdns"
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions resource/l10n/en-US.toml
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,6 @@ other = "Expired"

[Days]
other = "d"

[CustomNameservers]
other = "Custom Public DNS Nameservers for DDNS (separate with comma)"
3 changes: 3 additions & 0 deletions resource/l10n/es-ES.toml
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,6 @@ other = "Expired"

[Days]
other = "d"

[CustomNameservers]
other = "Servidores DNS públicos personalizados para DDNS (separar con coma)"
3 changes: 3 additions & 0 deletions resource/l10n/zh-CN.toml
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,6 @@ other = "已到期"

[Days]
other = ""

[CustomNameservers]
other = "自定义DDNS使用的公共DNS服务器(逗号分隔)"
3 changes: 3 additions & 0 deletions resource/l10n/zh-TW.toml
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,6 @@ other = "已到期"

[Days]
other = ""

[CustomNameservers]
other = "自訂DDNS使用的公共DNS伺服器(逗號分隔)"
4 changes: 4 additions & 0 deletions resource/template/dashboard-default/setting.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<label>{{tr "PanelServerDomainAndIP"}}</label>
<input type="text" name="GRPCHost" placeholder="" value="{{.Conf.GRPCHost}}">
</div>
<div class="field">
<label>{{tr "CustomNameservers"}}</label>
<input type="text" name="CustomNameservers" placeholder="" value="{{.Conf.DNSServers}}">
</div>
<div class="field">
<label>{{tr "IPChangeAlert"}}</label>
</div>
Expand Down
5 changes: 5 additions & 0 deletions service/singleton/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (

func initDDNS() {
OnDDNSUpdate()
OnNameserverUpdate()
}

func OnDDNSUpdate() {
Expand All @@ -33,6 +34,10 @@ func OnDDNSUpdate() {
}
}

func OnNameserverUpdate() {
ddns2.InitDNSServers(Conf.DNSServers)
}

func GetDDNSProvidersFromProfiles(profileId []uint64, ip *ddns2.IP) ([]*ddns2.Provider, error) {
profiles := make([]*model.DDNSProfile, 0, len(profileId))
ddnsCacheLock.RLock()
Expand Down

0 comments on commit f6531a5

Please sign in to comment.