Skip to content

Commit

Permalink
refactoring: generate enums
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Sep 11, 2021
1 parent 6f8b8c1 commit ee8f041
Show file tree
Hide file tree
Showing 49 changed files with 916 additions and 312 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 1,5 @@
#!/usr/bin/env bash

.PHONY: all clean build swagger test lint run help
.DEFAULT_GOAL := help

Expand All @@ -7,6 9,8 @@ DOCKER_IMAGE_NAME="spx01/blocky"
BINARY_NAME=blocky
BIN_OUT_DIR=bin

export PATH=$(shell go env GOPATH)/bin:$(shell echo $$PATH)

all: test lint build ## Build binary (with tests)

clean: ## cleans output directory
Expand All @@ -23,6 27,8 @@ serve_docs: ## serves online docs
mkdocs serve

build: ## Build binary
go install github.com/abice/go-enum
go generate ./...
go build -v -ldflags="-w -s -X github.com/0xERR0R/blocky/util.Version=${VERSION} -X github.com/0xERR0R/blocky/util.BuildTime=${BUILD_TIME}" -o $(BIN_OUT_DIR)/$(BINARY_NAME)$(BINARY_SUFFIX)

test: ## run tests
Expand Down
2 changes: 1 addition & 1 deletion api/api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 9,7 @@ import (
)

func TestResolver(t *testing.T) {
ConfigureLogger("Warn", "text", true)
ConfigureLogger(LevelFatal, FormatTypeText, true)
RegisterFailHandler(Fail)
RunSpecs(t, "API Suite")
}
2 changes: 1 addition & 1 deletion cmd/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 12,7 @@ var _ = Describe("Serve command", func() {
When("Serve command is called", func() {
It("should start DNS server", func() {
config.GetConfig().BootstrapDNS = config.Upstream{
Net: "tcp udp",
Net: config.NetProtocolTcpTls,
Host: "1.1.1.1",
Port: 53,
}
Expand Down
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,2 1,3 @@
ignore:
- "resolver/mocks.go"
- "**/*_enum.go"
103 changes: 45 additions & 58 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 1,4 @@
//go:generate go-enum -f=$GOFILE --marshal --names
package config

import (
Expand All @@ -15,38 16,37 @@ import (
"gopkg.in/yaml.v2"
)

// NetProtocol resolver protocol ENUM(
// udp // Deprecated: use tcp udp instead
// tcp // Deprecated: use tcp udp instead
// tcp udp // TCP and UDP protocols
// tcp-tls // TCP-TLS protocol
// https // HTTPS protocol
// )
type NetProtocol uint16

// QueryLogType type of the query log ENUM(
// none // use logger as fallback
// mysql // MySQL or MariaDB database
// csv // CSV file per day
// csv-client // CSV file per day and client
// )
type QueryLogType int16

const (
validUpstream = `(?P<Host>(?:\[[^\]] \])|[^\s/:] ):?(?P<Port>[^\s/:]*)?(?P<Path>/[^\s]*)?`
// NetUDP UDP protocol (deprecated)
NetUDP = "udp"

// NetTCP TCP protocol (deprecated)
NetTCP = "tcp"

// NetTCPUDP TCP and UDP protocols
NetTCPUDP = "tcp udp"

// NetTCPTLS TCP-TLS protocol
NetTCPTLS = "tcp-tls"

// NetHTTPS HTTPS protocol
NetHTTPS = "https"

QueryLogTypeMySQL = "mysql"
QueryLogTypeCSV = "csv"
QueryLogTypeCSVPerClient = "csv-client"
)

// nolint:gochecknoglobals
var netDefaultPort = map[string]uint16{
NetTCPUDP: 53,
NetTCPTLS: 853,
NetHTTPS: 443,
var netDefaultPort = map[NetProtocol]uint16{
NetProtocolTcpUdp: 53,
NetProtocolTcpTls: 853,
NetProtocolHttps: 443,
}

// Upstream is the definition of external DNS server
type Upstream struct {
Net string
Net NetProtocol
Host string
Port uint16
Path string
Expand Down Expand Up @@ -133,7 133,7 @@ func ParseUpstream(upstream string) (result Upstream, err error) {
return Upstream{}, nil
}

var n string
var n NetProtocol

n, upstream = extractNet(upstream)

Expand Down Expand Up @@ -168,31 168,31 @@ func ParseUpstream(upstream string) (result Upstream, err error) {
return Upstream{Net: n, Host: host, Port: port, Path: path}, nil
}

func extractNet(upstream string) (string, string) {
if strings.HasPrefix(upstream, NetTCP ":") {
func extractNet(upstream string) (NetProtocol, string) {
if strings.HasPrefix(upstream, NetProtocolTcp.String() ":") {
log.Log().Warnf("net prefix tcp is deprecated, using tcp udp as default fallback")

return NetTCPUDP, strings.Replace(upstream, NetTCP ":", "", 1)
return NetProtocolTcpUdp, strings.Replace(upstream, NetProtocolTcp.String() ":", "", 1)
}

if strings.HasPrefix(upstream, NetUDP ":") {
if strings.HasPrefix(upstream, NetProtocolUdp.String() ":") {
log.Log().Warnf("net prefix udp is deprecated, using tcp udp as default fallback")
return NetTCPUDP, strings.Replace(upstream, NetUDP ":", "", 1)
return NetProtocolTcpUdp, strings.Replace(upstream, NetProtocolUdp.String() ":", "", 1)
}

if strings.HasPrefix(upstream, NetTCPUDP ":") {
return NetTCPUDP, strings.Replace(upstream, NetTCPUDP ":", "", 1)
if strings.HasPrefix(upstream, NetProtocolTcpUdp.String() ":") {
return NetProtocolTcpUdp, strings.Replace(upstream, NetProtocolTcpUdp.String() ":", "", 1)
}

if strings.HasPrefix(upstream, NetTCPTLS ":") {
return NetTCPTLS, strings.Replace(upstream, NetTCPTLS ":", "", 1)
if strings.HasPrefix(upstream, NetProtocolTcpTls.String() ":") {
return NetProtocolTcpTls, strings.Replace(upstream, NetProtocolTcpTls.String() ":", "", 1)
}

if strings.HasPrefix(upstream, NetHTTPS ":") {
return NetHTTPS, strings.Replace(upstream, NetHTTPS ":", "", 1)
if strings.HasPrefix(upstream, NetProtocolHttps.String() ":") {
return NetProtocolHttps, strings.Replace(upstream, NetProtocolHttps.String() ":", "", 1)
}

return NetTCPUDP, upstream
return NetProtocolTcpUdp, upstream
}

const (
Expand All @@ -211,8 211,8 @@ type Config struct {
Caching CachingConfig `yaml:"caching"`
QueryLog QueryLogConfig `yaml:"queryLog"`
Prometheus PrometheusConfig `yaml:"prometheus"`
LogLevel string `yaml:"logLevel"`
LogFormat string `yaml:"logFormat"`
LogLevel log.Level `yaml:"logLevel"`
LogFormat log.FormatType `yaml:"logFormat"`
LogPrivacy bool `yaml:"logPrivacy"`
LogTimestamp bool `yaml:"logTimestamp"`
Port string `yaml:"port"`
Expand Down Expand Up @@ -289,10 289,10 @@ type QueryLogConfig struct {
// Deprecated
Dir string `yaml:"dir"`
// Deprecated
PerClient bool `yaml:"perClient"`
Target string `yaml:"target"`
Type string `yaml:"type"`
LogRetentionDays uint64 `yaml:"logRetentionDays"`
PerClient bool `yaml:"perClient"`
Target string `yaml:"target"`
Type QueryLogType `yaml:"type"`
LogRetentionDays uint64 `yaml:"logRetentionDays"`
}

// nolint:gochecknoglobals
Expand Down Expand Up @@ -327,29 327,18 @@ func LoadConfig(path string, mandatory bool) {
}

func validateConfig(cfg *Config) {
if cfg.LogFormat != log.CfgLogFormatText && cfg.LogFormat != log.CfgLogFormatJSON {
log.Log().Fatal("LogFormat should be 'text' or 'json'")
}

queryLogType := strings.ToLower(cfg.QueryLog.Type)
if queryLogType != "" && queryLogType != QueryLogTypeMySQL &&
queryLogType != QueryLogTypeCSV && queryLogType != QueryLogTypeCSVPerClient {
log.Log().Fatalf("queryLog.type should be one of: %s",
strings.Join([]string{QueryLogTypeMySQL, QueryLogTypeCSV, QueryLogTypeCSVPerClient}, ", "))
}

if cfg.QueryLog.Dir != "" {
log.Log().Warnf("queryLog.Dir is deprecated, use 'queryLog.target' instead")

if cfg.QueryLog.Target == "" {
cfg.QueryLog.Target = cfg.QueryLog.Dir
}

if cfg.QueryLog.Type == "" {
if cfg.QueryLog.Type == QueryLogTypeNone {
if cfg.QueryLog.PerClient {
cfg.QueryLog.Type = QueryLogTypeCSVPerClient
cfg.QueryLog.Type = QueryLogTypeCsvClient
} else {
cfg.QueryLog.Type = QueryLogTypeCSV
cfg.QueryLog.Type = QueryLogTypeCsv
}
}
}
Expand All @@ -362,8 351,6 @@ func GetConfig() *Config {

func setDefaultValues(cfg *Config) {
cfg.Port = cfgDefaultPort
cfg.LogLevel = "info"
cfg.LogFormat = log.CfgLogFormatText
cfg.LogTimestamp = true
cfg.Prometheus.Path = cfgDefaultPrometheusPath
}
Loading

0 comments on commit ee8f041

Please sign in to comment.