-
Notifications
You must be signed in to change notification settings - Fork 1
/
rescached.go
240 lines (200 loc) · 4.52 KB
/
rescached.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// SPDX-FileCopyrightText: 2018 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
// Package rescached implement DNS forwarder with cache.
package rescached
import (
"errors"
"fmt"
"log"
"os"
"sync"
"git.sr.ht/~shulhan/pakakeh.go/lib/debug"
"git.sr.ht/~shulhan/pakakeh.go/lib/dns"
"git.sr.ht/~shulhan/pakakeh.go/lib/http"
"git.sr.ht/~shulhan/pakakeh.go/lib/memfs"
)
// Version of program, overwritten by build.
var Version = `4.4.3`
// Server implement caching DNS server.
type Server struct {
dns *dns.Server
env *Environment
rcWatcher *memfs.Watcher
httpd *http.Server
httpdRunner sync.Once
}
// New create and initialize new rescached server.
func New(env *Environment) (srv *Server, err error) {
if debug.Value >= 1 {
fmt.Printf("--- rescached: config: %+v\n", env)
}
err = env.init()
if err != nil {
return nil, fmt.Errorf("rescached: New: %w", err)
}
env.initHostsBlock()
srv = &Server{
env: env,
}
err = srv.httpdInit()
if err != nil {
return nil, err
}
return srv, nil
}
// Start the server, waiting for DNS query from clients, read it and response
// it.
func (srv *Server) Start() (err error) {
var (
logp = "Start"
fcaches *os.File
hb *Blockd
hfile *dns.HostsFile
zone *dns.Zone
answers []*dns.Answer
)
srv.dns, err = dns.NewServer(&srv.env.ServerOptions)
if err != nil {
return err
}
fcaches, err = os.Open(srv.env.pathFileCaches)
if err == nil {
// Load stored caches from file.
answers, err = srv.dns.Caches.ExternalLoad(fcaches)
if err != nil {
log.Printf("%s: %s", logp, err)
} else {
fmt.Printf("%s: %d caches loaded from %s\n", logp, len(answers), srv.env.pathFileCaches)
}
err = fcaches.Close()
if err != nil {
log.Printf("%s: %s", logp, err)
}
}
for _, hb = range srv.env.HostBlockd {
if !hb.IsEnabled {
continue
}
hfile, err = dns.ParseHostsFile(hb.file)
if err != nil {
return fmt.Errorf("%s: %w", logp, err)
}
err = srv.dns.Caches.InternalPopulateRecords(hfile.Records, hfile.Path)
if err != nil {
return fmt.Errorf("%s: %w", logp, err)
}
srv.env.hostBlockdFile[hfile.Name] = hfile
}
srv.env.hostsd, err = dns.LoadHostsDir(srv.env.pathDirHosts)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
err = os.MkdirAll(srv.env.pathDirHosts, 0700)
if err != nil {
return err
}
}
for _, hfile = range srv.env.hostsd {
err = srv.dns.Caches.InternalPopulateRecords(hfile.Records, hfile.Path)
if err != nil {
return err
}
}
srv.env.zoned, err = dns.LoadZoneDir(srv.env.pathDirZone)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
err = os.MkdirAll(srv.env.pathDirZone, 0700)
if err != nil {
return err
}
}
for _, zone = range srv.env.zoned {
srv.dns.Caches.InternalPopulateZone(zone)
}
if len(srv.env.FileResolvConf) > 0 {
go srv.watchResolvConf()
}
go func() {
srv.httpdRunner.Do(srv.httpdRun)
}()
go srv.run()
return nil
}
func (srv *Server) run() {
defer func() {
err := recover()
if err != nil {
log.Println("panic: ", err)
}
}()
err := srv.dns.ListenAndServe()
if err != nil {
log.Println(err)
}
}
// Stop the server.
func (srv *Server) Stop() {
var (
logp = "Stop"
fcaches *os.File
err error
n int
)
if srv.rcWatcher != nil {
srv.rcWatcher.Stop()
}
srv.dns.Stop()
// Stores caches to file for next start.
err = os.MkdirAll(srv.env.pathDirCaches, 0700)
if err != nil {
log.Printf("%s: %s", logp, err)
return
}
fcaches, err = os.Create(srv.env.pathFileCaches)
if err != nil {
log.Printf("%s: %s", logp, err)
return
}
n, err = srv.dns.Caches.ExternalSave(fcaches)
if err != nil {
log.Printf("%s: %s", logp, err)
// fall-through for Close.
}
err = fcaches.Close()
if err != nil {
log.Printf("%s: %s", logp, err)
}
fmt.Printf("%s: %d caches stored to %s\n", logp, n, srv.env.pathFileCaches)
}
// watchResolvConf watch an update to file resolv.conf.
func (srv *Server) watchResolvConf() {
var (
logp = "watchResolvConf"
ns memfs.NodeState
err error
)
srv.rcWatcher, err = memfs.NewWatcher(srv.env.FileResolvConf, 0)
if err != nil {
log.Fatalf("%s: %s", logp, err)
}
for ns = range srv.rcWatcher.C {
switch ns.State {
case memfs.FileStateDeleted:
log.Printf("= %s: file %q deleted\n", logp, srv.env.FileResolvConf)
return
default:
ok, err := srv.env.loadResolvConf()
if err != nil {
log.Printf("%s: %s", logp, err)
break
}
if !ok {
break
}
srv.dns.RestartForwarders(srv.env.NameServers)
}
}
}