feat: remote dns for sing

This commit is contained in:
cubemaze
2023-09-21 12:34:03 +08:00
parent b85a78209a
commit cecfffd081
10 changed files with 136 additions and 16 deletions

View File

@@ -5,10 +5,12 @@ import (
"github.com/fsnotify/fsnotify"
"log"
"path"
"path/filepath"
"strings"
"time"
)
func (p *Conf) Watch(filePath, dnsPath string, reload func()) error {
func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func()) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("new watcher error: %s", err)
@@ -28,9 +30,10 @@ func (p *Conf) Watch(filePath, dnsPath string, reload func()) error {
pre = time.Now()
go func() {
time.Sleep(5 * time.Second)
if e.Name == dnsPath {
switch filepath.Base(strings.TrimSuffix(e.Name, "~")) {
case filepath.Base(xDnsPath), filepath.Base(sDnsPath):
log.Println("DNS file changed, reloading...")
} else {
default:
log.Println("config dir changed, reloading...")
}
*p = *New()
@@ -52,8 +55,14 @@ func (p *Conf) Watch(filePath, dnsPath string, reload func()) error {
if err != nil {
return fmt.Errorf("watch file error: %s", err)
}
if dnsPath != "" {
err = watcher.Add(path.Dir(dnsPath))
if xDnsPath != "" {
err = watcher.Add(path.Dir(xDnsPath))
if err != nil {
return fmt.Errorf("watch dns file error: %s", err)
}
}
if sDnsPath != "" {
err = watcher.Add(path.Dir(sDnsPath))
if err != nil {
return fmt.Errorf("watch dns file error: %s", err)
}