Skip to content

Commit

Permalink
Support of DoH/DoT as bootstrap DNS (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos authored Apr 22, 2022
1 parent b67df0b commit 62c7816
Show file tree
Hide file tree
Showing 65 changed files with 1,596 additions and 626 deletions.
5 changes: 3 additions & 2 deletions api/api_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ func (s *BlockingEndpoint) apiBlockingDisable(rw http.ResponseWriter, req *http.
func (s *BlockingEndpoint) apiBlockingStatus(rw http.ResponseWriter, _ *http.Request) {
status := s.control.BlockingStatus()

response, _ := json.Marshal(status)
_, err := rw.Write(response)
response, err := json.Marshal(status)
util.LogOnError("unable to marshal response ", err)

_, err = rw.Write(response)
util.LogOnError("unable to write response ", err)
}
4 changes: 2 additions & 2 deletions api/api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package api_test
import (
"testing"

. "github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/log"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestResolver(t *testing.T) {
ConfigureLogger(LevelFatal, FormatTypeText, true)
log.Silence()
RegisterFailHandler(Fail)
RunSpecs(t, "API Suite")
}
4 changes: 2 additions & 2 deletions cache/expirationcache/expiration_cache_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package expirationcache_test
import (
"testing"

. "github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/log"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestCache(t *testing.T) {
ConfigureLogger(LevelFatal, FormatTypeText, true)
log.Silence()
RegisterFailHandler(Fail)
RunSpecs(t, "Expiration cache suite")
}
4 changes: 2 additions & 2 deletions cache/stringcache/string_cache_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package stringcache_test
import (
"testing"

. "github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/log"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestCache(t *testing.T) {
ConfigureLogger(LevelFatal, FormatTypeText, true)
log.Silence()
RegisterFailHandler(Fail)
RunSpecs(t, "String cache suite")
}
12 changes: 8 additions & 4 deletions cmd/blocking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ var _ = Describe("Blocking command", func() {
When("status blocking is called via REST and blocking is enabled", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
response, _ := json.Marshal(api.BlockingStatus{
response, err := json.Marshal(api.BlockingStatus{
Enabled: true,
AutoEnableInSec: uint(5),
})
_, err := w.Write(response)
Expect(err).Should(Succeed())

_, err = w.Write(response)
Expect(err).Should(Succeed())
}
})
Expand All @@ -104,12 +106,14 @@ var _ = Describe("Blocking command", func() {
var autoEnable uint
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
response, _ := json.Marshal(api.BlockingStatus{
response, err := json.Marshal(api.BlockingStatus{
Enabled: false,
AutoEnableInSec: autoEnable,
DisabledGroups: []string{"abc"},
})
_, err := w.Write(response)
Expect(err).Should(Succeed())

_, err = w.Write(response)
Expect(err).Should(Succeed())
}
})
Expand Down
9 changes: 5 additions & 4 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"testing"

. "github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/log"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand All @@ -16,14 +16,15 @@ var (
)

func TestCmd(t *testing.T) {
log.Silence()
BeforeSuite(func() {
Log().ExitFunc = func(int) { fatal = true }
log.Log().ExitFunc = func(int) { fatal = true }

loggerHook = test.NewGlobal()
Log().AddHook(loggerHook)
log.Log().AddHook(loggerHook)
})
AfterSuite(func() {
Log().ExitFunc = nil
log.Log().ExitFunc = nil
loggerHook.Reset()
})
RegisterFailHandler(Fail)
Expand Down
3 changes: 2 additions & 1 deletion cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func query(cmd *cobra.Command, args []string) {
Query: args[0],
Type: typeFlag,
}
jsonValue, _ := json.Marshal(apiRequest)
jsonValue, err := json.Marshal(apiRequest)
util.FatalOnError("can't marshal request: ", err)

resp, err := http.Post(apiURL(api.PathQueryPath), "application/json", bytes.NewBuffer(jsonValue))

Expand Down
12 changes: 8 additions & 4 deletions cmd/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,30 @@ var _ = Describe("Blocking command", func() {
Describe("Call query command", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
response, _ := json.Marshal(api.QueryResult{
response, err := json.Marshal(api.QueryResult{
Reason: "Reason",
ResponseType: "Type",
Response: "Response",
ReturnCode: "NOERROR",
})
_, err := w.Write(response)
Expect(err).Should(Succeed())

_, err = w.Write(response)
Expect(err).Should(Succeed())
}
})
When("query command is called via REST", func() {
BeforeEach(func() {
mockFn = func(w http.ResponseWriter, _ *http.Request) {
response, _ := json.Marshal(api.QueryResult{
response, err := json.Marshal(api.QueryResult{
Reason: "Reason",
ResponseType: "Type",
Response: "Response",
ReturnCode: "NOERROR",
})
_, err := w.Write(response)
Expect(err).Should(Succeed())

_, err = w.Write(response)
Expect(err).Should(Succeed())
}
})
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func init() {
}

func initConfig() {
err := config.LoadConfig(configPath, false)
cfg, err := config.LoadConfig(configPath, false)
if err != nil {
util.FatalOnError("unable to load configuration: ", err)
}

log.ConfigureLogger(config.GetConfig().LogLevel, config.GetConfig().LogFormat, config.GetConfig().LogTimestamp)
log.ConfigureLogger(cfg.LogLevel, cfg.LogFormat, cfg.LogTimestamp)

if len(config.GetConfig().HTTPPorts) != 0 {
split := strings.Split(config.GetConfig().HTTPPorts[0], ":")
if len(cfg.HTTPPorts) != 0 {
split := strings.Split(cfg.HTTPPorts[0], ":")

lastIdx := len(split) - 1

Expand Down
3 changes: 3 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"io/ioutil"

"github.com/0xERR0R/blocky/log"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -12,6 +14,7 @@ var _ = Describe("Version command", func() {
log.Log().ExitFunc = nil
It("should execute without error", func() {
c := NewRootCommand()
c.SetOutput(ioutil.Discard)
c.SetArgs([]string{"help"})
err := c.Execute()
Expect(err).Should(Succeed())
Expand Down
26 changes: 7 additions & 19 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package cmd

import (
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/0xERR0R/blocky/config"
"github.com/0xERR0R/blocky/evt"
Expand All @@ -18,7 +16,8 @@ import (

//nolint:gochecknoglobals
var (
done chan bool
done chan bool
isConfigMandatory = true
)

func newServeCommand() *cobra.Command {
Expand All @@ -33,22 +32,18 @@ func newServeCommand() *cobra.Command {
func startServer(_ *cobra.Command, _ []string) {
printBanner()

err := config.LoadConfig(configPath, true)
if err != nil {
util.FatalOnError("unable to load configuration: ", err)
}

log.ConfigureLogger(config.GetConfig().LogLevel, config.GetConfig().LogFormat, config.GetConfig().LogTimestamp)
cfg, err := config.LoadConfig(configPath, isConfigMandatory)
util.FatalOnError("unable to load configuration: ", err)

configureHTTPClient(config.GetConfig())
log.ConfigureLogger(cfg.LogLevel, cfg.LogFormat, cfg.LogTimestamp)

signals := make(chan os.Signal, 1)
done = make(chan bool, 1)

signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)

srv, err := server.NewServer(config.GetConfig())
util.FatalOnError("cant start server: ", err)
srv, err := server.NewServer(cfg)
util.FatalOnError("can't start server: ", err)

srv.Start()

Expand All @@ -63,13 +58,6 @@ func startServer(_ *cobra.Command, _ []string) {
<-done
}

func configureHTTPClient(cfg *config.Config) {
http.DefaultTransport = &http.Transport{
Dial: (util.Dialer(cfg)).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
}

func printBanner() {
log.Log().Info("_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/")
log.Log().Info("_/ _/")
Expand Down
13 changes: 9 additions & 4 deletions cmd/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import (
var _ = Describe("Serve command", func() {
When("Serve command is called", func() {
It("should start DNS server", func() {
config.GetConfig().BootstrapDNS = config.Upstream{
Net: config.NetProtocolTcpTls,
Host: "1.1.1.1",
Port: 53,
config.GetConfig().BootstrapDNS = config.BootstrapConfig{
Upstream: config.Upstream{
Net: config.NetProtocolTcpTls,
Host: "1.1.1.1",
Port: 53,
},
}

isConfigMandatory = false

go startServer(newServeCommand(), []string{})

time.Sleep(100 * time.Millisecond)
Expand Down
Loading

0 comments on commit 62c7816

Please sign in to comment.