Skip to content

Commit c511e97

Browse files
committed
[ndns] auto reload record file
1 parent aebf931 commit c511e97

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

ndns/ndns.go

+61-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sync"
1010
"time"
1111

12+
"github.com/fsnotify/fsnotify"
1213
"github.com/miekg/dns"
1314
)
1415

@@ -21,23 +22,33 @@ type record struct {
2122
}
2223

2324
var (
24-
file string
25-
tcp bool
26-
udp bool
27-
addr string
28-
29-
recordMap map[string]*record
25+
file string
26+
tcp bool
27+
udp bool
28+
addr string
29+
autoload bool
30+
31+
recordLock sync.Mutex
32+
recordMap map[string]*record
3033
)
3134

3235
func init() {
3336
flag.StringVar(&file, "f", "record.json", "record file")
3437
flag.BoolVar(&tcp, "t", false, "listen tcp")
3538
flag.BoolVar(&udp, "u", true, "listen udp")
3639
flag.StringVar(&addr, "l", ":53", "listen address")
40+
flag.BoolVar(&autoload, "a", true, "auto reload record file")
3741

3842
recordMap = make(map[string]*record)
3943
}
4044

45+
func lookUpRecord(k string) (r *record, ok bool) {
46+
recordLock.Lock()
47+
r, ok = recordMap[k]
48+
recordLock.Unlock()
49+
return
50+
}
51+
4152
type handler struct {
4253
isTcpServer bool
4354
}
@@ -51,7 +62,7 @@ func (this *handler) packAnswers(msg *dns.Msg, qtype uint16, domain string) {
5162
return
5263
}
5364

54-
r, ok := recordMap[domain]
65+
r, ok := lookUpRecord(domain)
5566
if !ok {
5667
log.Println("domain", domain, "not found")
5768
return
@@ -124,18 +135,55 @@ func runUdpServer() {
124135
}
125136
}
126137

127-
func main() {
128-
flag.Parse()
129-
138+
func loadFile() error {
130139
records, err := readRecords(file)
131140
if err != nil {
132-
log.Fatal(err)
141+
return err
133142
}
134143

144+
recordLock.Lock()
145+
defer recordLock.Unlock()
146+
147+
recordMap = make(map[string]*record)
148+
135149
for _, v := range records {
136150
recordMap[v.Host+"."] = v
137151
}
138152

153+
return nil
154+
}
155+
156+
func updater() {
157+
watcher, err := fsnotify.NewWatcher()
158+
if err != nil {
159+
log.Fatal(err)
160+
}
161+
defer watcher.Close()
162+
163+
err = watcher.Add(file)
164+
if err != nil {
165+
log.Fatal(err)
166+
}
167+
defer watcher.Remove(file)
168+
169+
for event := range watcher.Events {
170+
log.Println("updater go event", event)
171+
err = loadFile()
172+
if err != nil {
173+
log.Println("try to load file, but failed", err)
174+
}
175+
watcher.Remove(file)
176+
watcher.Add(file)
177+
}
178+
}
179+
180+
func main() {
181+
flag.Parse()
182+
183+
if err := loadFile(); err != nil {
184+
log.Fatal(err)
185+
}
186+
139187
var wg sync.WaitGroup
140188

141189
if tcp {
@@ -154,5 +202,7 @@ func main() {
154202
}()
155203
}
156204

205+
go updater()
206+
157207
wg.Wait()
158208
}

ndns/record.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"host": "www.baidu.com",
4-
"tc": true,
4+
"tc": false,
55
"ips": [
66
"127.0.0.2",
77
"127.0.0.3",

util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ time_format(char *buf, size_t cap, time_t now)
664664
return 0;
665665
}
666666

667-
int
667+
static int
668668
read_command_output(const char *command, char *buf, size_t n)
669669
{
670670
if (buf == NULL || n == 0) {

0 commit comments

Comments
 (0)