Skip to content

Commit

Permalink
fix: fsnotify
Browse files Browse the repository at this point in the history
perf: mem of dns
  • Loading branch information
BaiMeow committed May 10, 2024
1 parent 66f58cb commit 65042e1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func Init(file string) {
func update() {
DDNS.Interval = time.Duration(viper.GetInt("ddns.interval")) * time.Second
DDNS.HandleShakeMax = time.Duration(viper.GetInt("ddns.handshake_max")) * time.Second
DDNS.IfaceOnly = viper.GetStringSlice("ddns.iface")
DDNS.IfaceSkip = viper.GetStringSlice("ddns.skip")
DDNS.IfaceOnly = viper.GetStringSlice("ddns.only_ifaces")
DDNS.IfaceSkip = viper.GetStringSlice("ddns.skip_ifaces")

StartOnBoot.Enabled = viper.GetBool("start_on_boot.enabled")
StartOnBoot.IfaceOnly = viper.GetStringSlice("start_on_boot.only_ifaces")
Expand Down
4 changes: 4 additions & 0 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func newDaemon() *daemon {
func (d *daemon) Run() {
// prepare config
for _, iface := range utils.FindIface(conf.DDNS.IfaceOnly, conf.DDNS.IfaceSkip) {
logrus.WithField("iface", iface).Infoln("find iface, init ddns config")
ddns, err := newDDNS(iface)
if err != nil {
logrus.WithField("iface", iface).WithError(err).Error("failed to init ddns config")
Expand Down Expand Up @@ -112,6 +113,7 @@ func (d *daemon) registerWatch() {
if conf.DDNS.IfaceSkip != nil && slices.Index(conf.DDNS.IfaceSkip, name) != -1 {
return
}
logrus.WithField("iface", name).Infoln("iface update, add to pending list")
d.lock.Lock()
defer d.lock.Unlock()
if slices.Index(d.pendingIfaces, name) == -1 {
Expand All @@ -125,6 +127,7 @@ func (d *daemon) registerWatch() {
if conf.DDNS.IfaceSkip != nil && slices.Index(conf.DDNS.IfaceSkip, name) != -1 {
return
}
logrus.WithField("iface", name).Infoln("iface remove, remove from run list")
d.lock.Lock()
defer d.lock.Unlock()
delete(d.runIfaces, name)
Expand All @@ -145,6 +148,7 @@ func (d *daemon) goUpdate() {
logrus.WithField("iface", iface).WithError(err).Error("failed to init ddns config")
continue
}
logrus.WithField("iface", iface).Infoln("init success, move to run list")
d.runIfaces[iface] = ddns
deleteList = append(deleteList, iface)
}
Expand Down
6 changes: 4 additions & 2 deletions daemon/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
import (
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"path/filepath"
"strings"
)

Expand All @@ -23,10 +24,11 @@ func (w *WireguardWatcher) Watch() {
if !ok {
return
}
name := strings.TrimSuffix(event.Name, ".conf")
if len(name) == len(event.Name) {
_, filename := filepath.Split(event.Name)
if !strings.HasSuffix(filename, ".conf") {
continue
}
name := strings.TrimSuffix(filename, ".conf")
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
logrus.Info("update file:", event.Name)
if w.UpdateCallback != nil {
Expand Down
11 changes: 5 additions & 6 deletions lib/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

var RoaFinder string = "223.5.5.5:53"
var DefaultClient = new(dns.Client)

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

func directDNS(addr string) (net.IP, error) {
addr = dns.Fqdn(addr)
c := new(dns.Client)

msg := new(dns.Msg)
msg.SetQuestion(addr, dns.TypeCNAME)
rec, _, err := c.Exchange(msg, RoaFinder)
rec, _, err := DefaultClient.Exchange(msg, RoaFinder)
if err != nil {
return nil, fmt.Errorf("write msg failed: %w", err)
}
Expand All @@ -92,7 +91,7 @@ func directDNS(addr string) (net.IP, error) {
var NsServer string
for fa := addr; dns.Split(fa) != nil; {
msg.SetQuestion(fa, dns.TypeNS)
rec, _, err := c.Exchange(msg, RoaFinder)
rec, _, err := DefaultClient.Exchange(msg, RoaFinder)
if err != nil {
return nil, fmt.Errorf("write msg failed: %w", err)
}
Expand All @@ -115,7 +114,7 @@ func directDNS(addr string) (net.IP, error) {

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

msg.SetQuestion(dns.Fqdn(NsServer), dns.TypeAAAA)
rec, _, err = c.Exchange(msg, nsAddr)
rec, _, err = DefaultClient.Exchange(msg, nsAddr)
if err == nil {
for _, ans := range rec.Answer {
if a, ok := ans.(*dns.AAAA); ok {
Expand Down
3 changes: 3 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func FindIface(only []string, skip []string) []string {
return nil
}
for _, v := range entry {
if !strings.HasSuffix(v.Name(), ".conf") {
continue
}
name := strings.TrimSuffix(v.Name(), ".conf")
if slices.Index(skip, name) != -1 {
continue
Expand Down

0 comments on commit 65042e1

Please sign in to comment.