summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-06-02 01:31:30 +0700
committerShulhan <m.shulhan@gmail.com>2020-07-26 03:48:51 +0700
commite643e59ae5aaac660682b04b2b09e3bf3c399fac (patch)
treed5dfa732ba8ac1628de7869e625f4f9740d4ac3b
parentb1b190cfb830305e4fddd2b8bf8264984b41f3e3 (diff)
downloadrescached-e643e59ae5aaac660682b04b2b09e3bf3c399fac.tar.xz
all: store the loaded hosts and master files
To be able to edit the hosts or master file through web UI, we need to store it some where. This changes is premilinary commit before we add the actual UI to edit hosts file.
-rw-r--r--cmd/rescached/rescached.cfg2
-rw-r--r--cmd/resolverbench/main.go18
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--rescached.go28
5 files changed, 38 insertions, 16 deletions
diff --git a/cmd/rescached/rescached.cfg b/cmd/rescached/rescached.cfg
index 93ffb22..0684a69 100644
--- a/cmd/rescached/rescached.cfg
+++ b/cmd/rescached/rescached.cfg
@@ -16,6 +16,7 @@ hosts_block = http://someonewhocares.org/hosts/hosts
[dns "server"]
#parent=udp://18.136.35.199
#parent=tcp://18.136.35.199
+parent = https://kilabit.info/dns-query
listen = 127.0.0.1:53
## Uncomment line below if you want to serve DNS to other computers.
@@ -38,4 +39,3 @@ doh.behind_proxy = false
cache.prune_delay = 1h0m0s
cache.prune_threshold = -1h0m0s
-parent = https://kilabit.info/dns-query
diff --git a/cmd/resolverbench/main.go b/cmd/resolverbench/main.go
index 0b517a2..a12679d 100644
--- a/cmd/resolverbench/main.go
+++ b/cmd/resolverbench/main.go
@@ -7,7 +7,7 @@ import (
"os"
"time"
- libdns "github.com/shuLhan/share/lib/dns"
+ "github.com/shuLhan/share/lib/dns"
)
func usage() {
@@ -22,30 +22,30 @@ func main() {
log.SetFlags(0)
- cl, err := libdns.NewUDPClient(os.Args[1])
+ cl, err := dns.NewUDPClient(os.Args[1])
if err != nil {
log.Fatal(err)
}
- msgs, err := libdns.HostsLoad(os.Args[2])
+ hostsFile, err := dns.ParseHostsFile(os.Args[2])
if err != nil {
log.Fatal(err)
}
var nfail int
- fmt.Printf("= Benchmarking with %d messages\n", len(msgs))
+ fmt.Printf("= Benchmarking with %d messages\n", len(hostsFile.Messages))
timeStart := time.Now()
- for x := 0; x < len(msgs); x++ {
- res, err := cl.Query(msgs[x])
+ for x := 0; x < len(hostsFile.Messages); x++ {
+ res, err := cl.Query(hostsFile.Messages[x])
if err != nil {
nfail++
log.Println("! Send error: ", err)
continue
}
- exp := msgs[x].Answer[0].RData().([]byte)
+ exp := hostsFile.Messages[x].Answer[0].RData().([]byte)
got := res.Answer[0].RData().([]byte)
if !bytes.Equal(exp, got) {
@@ -53,12 +53,12 @@ func main() {
log.Printf(`! Answer not matched %s:
expecting: %s
got: %s
-`, msgs[x].Question.String(), exp, got)
+`, hostsFile.Messages[x].Question.String(), exp, got)
}
}
timeEnd := time.Now()
- fmt.Printf("= Total: %d\n", len(msgs))
+ fmt.Printf("= Total: %d\n", len(hostsFile.Messages))
fmt.Printf("= Failed: %d\n", nfail)
fmt.Printf("= Elapsed time: %v\n", timeEnd.Sub(timeStart))
}
diff --git a/go.mod b/go.mod
index 3945da1..321d444 100644
--- a/go.mod
+++ b/go.mod
@@ -2,6 +2,6 @@ module github.com/shuLhan/rescached-go/v3
go 1.13
-require github.com/shuLhan/share v0.15.1-0.20200516135503-9e0bd9a6fefc
+require github.com/shuLhan/share v0.15.1-0.20200601173406-840f49c04de3
//replace github.com/shuLhan/share => ../share
diff --git a/go.sum b/go.sum
index 5e3ab57..f8e89a7 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/shuLhan/share v0.15.1-0.20200516135503-9e0bd9a6fefc h1:DqO6rIUITvdYjT/T/357cnZohfdWMyhjLfLBD3FeVPY=
-github.com/shuLhan/share v0.15.1-0.20200516135503-9e0bd9a6fefc/go.mod h1:mpa0ub5qmuko/muUlOROOqLCSHKU76GzuAR/sUaSwRo=
+github.com/shuLhan/share v0.15.1-0.20200601173406-840f49c04de3 h1:z9Wz8SpktZf01HuNbiCzh9EGCAiAmH6QEuUtYq8XhWI=
+github.com/shuLhan/share v0.15.1-0.20200601173406-840f49c04de3/go.mod h1:mpa0ub5qmuko/muUlOROOqLCSHKU76GzuAR/sUaSwRo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
diff --git a/rescached.go b/rescached.go
index 0161883..3aab17a 100644
--- a/rescached.go
+++ b/rescached.go
@@ -25,6 +25,9 @@ type Server struct {
httpd *http.Server
httpdRunner sync.Once
+
+ hostsFiles map[string]*dns.HostsFile
+ masterFiles map[string]*dns.MasterFile
}
//
@@ -60,9 +63,28 @@ func (srv *Server) Start() (err error) {
return err
}
- srv.dns.LoadHostsDir(dirHosts)
- srv.dns.LoadMasterDir(dirMaster)
- srv.dns.LoadHostsFile("")
+ hostsFile, err := dns.ParseHostsFile(dns.GetSystemHosts())
+ if err != nil {
+ return err
+ }
+ srv.dns.PopulateCaches(hostsFile.Messages)
+
+ srv.hostsFiles, err = dns.LoadHostsDir(dirHosts)
+ if err != nil {
+ return err
+ }
+
+ for _, hostsFile := range srv.hostsFiles {
+ srv.dns.PopulateCaches(hostsFile.Messages)
+ }
+
+ srv.masterFiles, err = dns.LoadMasterDir(dirMaster)
+ if err != nil {
+ return err
+ }
+ for _, masterFile := range srv.masterFiles {
+ srv.dns.PopulateCaches(masterFile.Messages)
+ }
if len(srv.env.FileResolvConf) > 0 {
srv.rcWatcher, err = libio.NewWatcher(