* move "dnsServer" to "config"

This commit is contained in:
Simon Zolin 2019-07-09 19:00:11 +03:00
parent 001b4b981f
commit af21a5f17b
4 changed files with 16 additions and 17 deletions

View File

@ -70,6 +70,7 @@ type configuration struct {
versionCheckJSON []byte
versionCheckLastTime time.Time
dnsServer *dnsforward.Server
dhcpServer dhcpd.Server
httpServer *http.Server
httpsServer HTTPSServer

View File

@ -172,7 +172,7 @@ func handleQueryLogDisable(w http.ResponseWriter, r *http.Request) {
func handleQueryLog(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL)
data := dnsServer.GetQueryLog()
data := config.dnsServer.GetQueryLog()
jsonVal, err := json.Marshal(data)
if err != nil {
@ -189,7 +189,7 @@ func handleQueryLog(w http.ResponseWriter, r *http.Request) {
func handleStatsTop(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL)
s := dnsServer.GetStatsTop()
s := config.dnsServer.GetStatsTop()
// use manual json marshalling because we want maps to be sorted by value
statsJSON := bytes.Buffer{}
@ -236,7 +236,7 @@ func handleStatsTop(w http.ResponseWriter, r *http.Request) {
// handleStatsReset resets the stats caches
func handleStatsReset(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL)
dnsServer.PurgeStats()
config.dnsServer.PurgeStats()
_, err := fmt.Fprintf(w, "OK\n")
if err != nil {
httpError(w, http.StatusInternalServerError, "Couldn't write body: %s", err)
@ -246,7 +246,7 @@ func handleStatsReset(w http.ResponseWriter, r *http.Request) {
// handleStats returns aggregated stats data for the 24 hours
func handleStats(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL)
summed := dnsServer.GetAggregatedStats()
summed := config.dnsServer.GetAggregatedStats()
statsJSON, err := json.Marshal(summed)
if err != nil {
@ -293,7 +293,7 @@ func handleStatsHistory(w http.ResponseWriter, r *http.Request) {
return
}
data, err := dnsServer.GetStatsHistory(timeUnit, startTime, endTime)
data, err := config.dnsServer.GetStatsHistory(timeUnit, startTime, endTime)
if err != nil {
httpError(w, http.StatusBadRequest, "Cannot get stats history: %s", err)
return
@ -709,7 +709,7 @@ func handleFilteringRemoveURL(w http.ResponseWriter, r *http.Request) {
// Stop DNS server:
// we close urlfilter object which in turn closes file descriptors to filter files.
// Otherwise, Windows won't allow us to remove the file which is being currently used.
_ = dnsServer.Stop()
_ = config.dnsServer.Stop()
// go through each element and delete if url matches
config.Lock()
@ -968,7 +968,7 @@ func handleDOH(w http.ResponseWriter, r *http.Request) {
return
}
dnsServer.ServeHTTP(w, r)
config.dnsServer.ServeHTTP(w, r)
}
// ------------------------

View File

@ -17,8 +17,6 @@ import (
"github.com/miekg/dns"
)
var dnsServer *dnsforward.Server
const (
rdnsTimeout = 3 * time.Second // max time to wait for rDNS response
)
@ -43,7 +41,7 @@ func initDNSServer(baseDir string) {
log.Fatalf("Cannot create DNS data dir at %s: %s", baseDir, err)
}
dnsServer = dnsforward.NewServer(baseDir)
config.dnsServer = dnsforward.NewServer(baseDir)
bindhost := config.DNS.BindHost
if config.DNS.BindHost == "0.0.0.0" {
@ -64,7 +62,7 @@ func initDNSServer(baseDir string) {
}
func isRunning() bool {
return dnsServer != nil && dnsServer.IsRunning()
return config.dnsServer != nil && config.dnsServer.IsRunning()
}
func beginAsyncRDNS(ip string) {
@ -242,12 +240,12 @@ func startDNSServer() error {
if err != nil {
return errorx.Decorate(err, "Couldn't start forwarding DNS server")
}
err = dnsServer.Start(&newconfig)
err = config.dnsServer.Start(&newconfig)
if err != nil {
return errorx.Decorate(err, "Couldn't start forwarding DNS server")
}
top := dnsServer.GetStatsTop()
top := config.dnsServer.GetStatsTop()
for k := range top.Clients {
beginAsyncRDNS(k)
}
@ -256,11 +254,11 @@ func startDNSServer() error {
}
func reconfigureDNSServer() error {
config, err := generateServerConfig()
newconfig, err := generateServerConfig()
if err != nil {
return errorx.Decorate(err, "Couldn't start forwarding DNS server")
}
err = dnsServer.Reconfigure(&config)
err = config.dnsServer.Reconfigure(&newconfig)
if err != nil {
return errorx.Decorate(err, "Couldn't start forwarding DNS server")
}
@ -273,7 +271,7 @@ func stopDNSServer() error {
return nil
}
err := dnsServer.Stop()
err := config.dnsServer.Stop()
if err != nil {
return errorx.Decorate(err, "Couldn't stop forwarding DNS server")
}

View File

@ -222,7 +222,7 @@ func refreshFiltersIfNecessary(force bool) int {
stopped := false
if updateCount != 0 {
_ = dnsServer.Stop()
_ = config.dnsServer.Stop()
stopped = true
}