aboutsummaryrefslogtreecommitdiff
path: root/rescached.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-11-14 14:01:10 +0700
committerShulhan <ms@kilabit.info>2021-11-14 14:01:10 +0700
commit7e545957536142a995ee8176c2b2a44f085e5dfb (patch)
treef04f16bf8f71bbfcceb996462f66ea3768d3e0ee /rescached.go
parentbe4d9303ec1a8e99047b7c2719b48497481eac40 (diff)
downloadrescached-7e545957536142a995ee8176c2b2a44f085e5dfb.tar.xz
all: update share module
The update bring new features and enhancements for DNS server. * add support to save and load caches to/from storage rescached now able to save and load caches to local storage upon restart. On POSIX, the caches is stored in /var/cache/rescached/rescached.gob, encoded using gob. Update #9 * remove the fallback name servers (NS) from server options The original idea of fallback NS is to send the query to the one define in resolv.conf, instead of using the one defined by user in ServerOptions NameServers, when an error occured. But, most of error usually caused by network (disconnected, time out), so re-sending query to fallback NS does not have any effect if the network it self is not working. This changes remove the unnecessary and complex fallback NS from server. * do not cache truncated answer Previously only answer with non-zero response code is ignored. This changes ignore also answer where response header is truncated. * lib/dns: make the TCP forwarders as complementary of UDP The TCP forwarders only active when client send the DNS request as TCP. When the server receive that request it should also forward the request as TCP not as UDP to prevent the truncated response. Another use case for TCP is when the response is truncated, the client will send the query back through TCP connection. The server should forward this request using TCP instead of UDP.
Diffstat (limited to 'rescached.go')
-rw-r--r--rescached.go54
1 files changed, 53 insertions, 1 deletions
diff --git a/rescached.go b/rescached.go
index 6d6464f..c990b77 100644
--- a/rescached.go
+++ b/rescached.go
@@ -10,6 +10,7 @@ import (
"fmt"
"log"
"os"
+ "path/filepath"
"sync"
"github.com/shuLhan/share/lib/debug"
@@ -18,6 +19,11 @@ import (
libio "github.com/shuLhan/share/lib/io"
)
+const (
+ cachesDir = "/var/cache/rescached/"
+ cachesFile = "rescached.gob"
+)
+
// Server implement caching DNS server.
type Server struct {
fileConfig string
@@ -57,11 +63,31 @@ func New(fileConfig string) (srv *Server, err error) {
// it.
//
func (srv *Server) Start() (err error) {
+ logp := "Start"
+
srv.dns, err = dns.NewServer(&srv.env.ServerOptions)
if err != nil {
return err
}
+ cachesPath := filepath.Join(cachesDir, cachesFile)
+
+ fcaches, err := os.Open(cachesPath)
+ if err == nil {
+ // Load stored caches from file.
+ answers, err := srv.dns.CachesLoad(fcaches)
+ if err != nil {
+ log.Printf("%s: %s", logp, err)
+ } else {
+ fmt.Printf("%s: %d caches loaded from %s\n", logp, len(answers), cachesPath)
+ }
+
+ err = fcaches.Close()
+ if err != nil {
+ log.Printf("%s: %s", logp, err)
+ }
+ }
+
systemHostsFile, err := dns.ParseHostsFile(dns.GetSystemHosts())
if err != nil {
return err
@@ -134,10 +160,36 @@ func (srv *Server) run() {
// Stop the server.
//
func (srv *Server) Stop() {
+ logp := "Stop"
+
if srv.rcWatcher != nil {
srv.rcWatcher.Stop()
}
srv.dns.Stop()
+
+ cachesPath := filepath.Join(cachesDir, cachesFile)
+
+ // Stores caches to file for next start.
+ err := os.MkdirAll(cachesDir, 0700)
+ if err != nil {
+ log.Printf("%s: %s", logp, err)
+ return
+ }
+ fcaches, err := os.Create(cachesPath)
+ if err != nil {
+ log.Printf("%s: %s", logp, err)
+ return
+ }
+ n, err := srv.dns.CachesSave(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, cachesPath)
}
func (srv *Server) watchResolvConf(ns *libio.NodeState) {
@@ -155,6 +207,6 @@ func (srv *Server) watchResolvConf(ns *libio.NodeState) {
break
}
- srv.dns.RestartForwarders(srv.env.NameServers, srv.env.FallbackNS)
+ srv.dns.RestartForwarders(srv.env.NameServers)
}
}