Skip to content

Commit 65042e1

Browse files
committed
fix: fsnotify
perf: mem of dns
1 parent 66f58cb commit 65042e1

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

conf/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func Init(file string) {
6767
func update() {
6868
DDNS.Interval = time.Duration(viper.GetInt("ddns.interval")) * time.Second
6969
DDNS.HandleShakeMax = time.Duration(viper.GetInt("ddns.handshake_max")) * time.Second
70-
DDNS.IfaceOnly = viper.GetStringSlice("ddns.iface")
71-
DDNS.IfaceSkip = viper.GetStringSlice("ddns.skip")
70+
DDNS.IfaceOnly = viper.GetStringSlice("ddns.only_ifaces")
71+
DDNS.IfaceSkip = viper.GetStringSlice("ddns.skip_ifaces")
7272

7373
StartOnBoot.Enabled = viper.GetBool("start_on_boot.enabled")
7474
StartOnBoot.IfaceOnly = viper.GetStringSlice("start_on_boot.only_ifaces")

daemon/daemon.go

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func newDaemon() *daemon {
2727
func (d *daemon) Run() {
2828
// prepare config
2929
for _, iface := range utils.FindIface(conf.DDNS.IfaceOnly, conf.DDNS.IfaceSkip) {
30+
logrus.WithField("iface", iface).Infoln("find iface, init ddns config")
3031
ddns, err := newDDNS(iface)
3132
if err != nil {
3233
logrus.WithField("iface", iface).WithError(err).Error("failed to init ddns config")
@@ -112,6 +113,7 @@ func (d *daemon) registerWatch() {
112113
if conf.DDNS.IfaceSkip != nil && slices.Index(conf.DDNS.IfaceSkip, name) != -1 {
113114
return
114115
}
116+
logrus.WithField("iface", name).Infoln("iface update, add to pending list")
115117
d.lock.Lock()
116118
defer d.lock.Unlock()
117119
if slices.Index(d.pendingIfaces, name) == -1 {
@@ -125,6 +127,7 @@ func (d *daemon) registerWatch() {
125127
if conf.DDNS.IfaceSkip != nil && slices.Index(conf.DDNS.IfaceSkip, name) != -1 {
126128
return
127129
}
130+
logrus.WithField("iface", name).Infoln("iface remove, remove from run list")
128131
d.lock.Lock()
129132
defer d.lock.Unlock()
130133
delete(d.runIfaces, name)
@@ -145,6 +148,7 @@ func (d *daemon) goUpdate() {
145148
logrus.WithField("iface", iface).WithError(err).Error("failed to init ddns config")
146149
continue
147150
}
151+
logrus.WithField("iface", iface).Infoln("init success, move to run list")
148152
d.runIfaces[iface] = ddns
149153
deleteList = append(deleteList, iface)
150154
}

daemon/watcher.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package daemon
33
import (
44
"github.com/fsnotify/fsnotify"
55
"github.com/sirupsen/logrus"
6+
"path/filepath"
67
"strings"
78
)
89

@@ -23,10 +24,11 @@ func (w *WireguardWatcher) Watch() {
2324
if !ok {
2425
return
2526
}
26-
name := strings.TrimSuffix(event.Name, ".conf")
27-
if len(name) == len(event.Name) {
27+
_, filename := filepath.Split(event.Name)
28+
if !strings.HasSuffix(filename, ".conf") {
2829
continue
2930
}
31+
name := strings.TrimSuffix(filename, ".conf")
3032
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
3133
logrus.Info("update file:", event.Name)
3234
if w.UpdateCallback != nil {

lib/dns/dns.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
var RoaFinder string = "223.5.5.5:53"
15+
var DefaultClient = new(dns.Client)
1516

1617
func Init() {
1718
if !conf.EnhancedDNS.DirectResolver.Enabled {
@@ -75,11 +76,9 @@ func resolveIPAddr(addr string) (net.IP, error) {
7576

7677
func directDNS(addr string) (net.IP, error) {
7778
addr = dns.Fqdn(addr)
78-
c := new(dns.Client)
79-
8079
msg := new(dns.Msg)
8180
msg.SetQuestion(addr, dns.TypeCNAME)
82-
rec, _, err := c.Exchange(msg, RoaFinder)
81+
rec, _, err := DefaultClient.Exchange(msg, RoaFinder)
8382
if err != nil {
8483
return nil, fmt.Errorf("write msg failed: %w", err)
8584
}
@@ -92,7 +91,7 @@ func directDNS(addr string) (net.IP, error) {
9291
var NsServer string
9392
for fa := addr; dns.Split(fa) != nil; {
9493
msg.SetQuestion(fa, dns.TypeNS)
95-
rec, _, err := c.Exchange(msg, RoaFinder)
94+
rec, _, err := DefaultClient.Exchange(msg, RoaFinder)
9695
if err != nil {
9796
return nil, fmt.Errorf("write msg failed: %w", err)
9897
}
@@ -115,7 +114,7 @@ func directDNS(addr string) (net.IP, error) {
115114

116115
nsAddr := net.JoinHostPort(NsServer, "53")
117116
msg.SetQuestion(dns.Fqdn(addr), dns.TypeA)
118-
rec, _, err = c.Exchange(msg, nsAddr)
117+
rec, _, err = DefaultClient.Exchange(msg, nsAddr)
119118
if err == nil {
120119
for _, ans := range rec.Answer {
121120
if a, ok := ans.(*dns.A); ok {
@@ -125,7 +124,7 @@ func directDNS(addr string) (net.IP, error) {
125124
}
126125

127126
msg.SetQuestion(dns.Fqdn(NsServer), dns.TypeAAAA)
128-
rec, _, err = c.Exchange(msg, nsAddr)
127+
rec, _, err = DefaultClient.Exchange(msg, nsAddr)
129128
if err == nil {
130129
for _, ans := range rec.Answer {
131130
if a, ok := ans.(*dns.AAAA); ok {

utils/utils.go

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func FindIface(only []string, skip []string) []string {
4242
return nil
4343
}
4444
for _, v := range entry {
45+
if !strings.HasSuffix(v.Name(), ".conf") {
46+
continue
47+
}
4548
name := strings.TrimSuffix(v.Name(), ".conf")
4649
if slices.Index(skip, name) != -1 {
4750
continue

0 commit comments

Comments
 (0)