From dbd4b579e5e653c3acb039543de83d860ed3a886 Mon Sep 17 00:00:00 2001 From: daru Date: Sun, 17 Oct 2021 13:24:27 +0200 Subject: [PATCH 1/4] Read TOML configuration files --- .gitignore | 2 ++ conf.toml | 3 +++ config/config.go | 13 ++++++++----- config/toml.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++++- go.sum | 16 ++++++++++++++++ main.go | 16 ++++++++++------ 7 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 .gitignore create mode 100644 conf.toml create mode 100644 config/toml.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cacb21e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +gertdns +gertdns.exe diff --git a/conf.toml b/conf.toml new file mode 100644 index 0000000..23ed074 --- /dev/null +++ b/conf.toml @@ -0,0 +1,3 @@ +Port = 5353 +Host = '0.0.0.0' +Domains = [] diff --git a/config/config.go b/config/config.go index 469a8bc..02822f1 100644 --- a/config/config.go +++ b/config/config.go @@ -1,18 +1,21 @@ package config -var Config *Configuration = nil - type Configuration struct { Port uint16 Host string Domains []string } -func Load() error { - Config = &Configuration{ +var ( + Config Configuration + + defaultConfig = Configuration{ Port: 5353, Host: "0.0.0.0", Domains: []string{}, } - return nil +) + +func Load(configFilePath string) { + Config = loadConfFile(configFilePath) } diff --git a/config/toml.go b/config/toml.go new file mode 100644 index 0000000..6fcb28b --- /dev/null +++ b/config/toml.go @@ -0,0 +1,44 @@ +package config + +import ( + "io/ioutil" + "os" + + "github.com/gookit/color" + "github.com/pelletier/go-toml/v2" +) + +func loadConfFile(configFilePath string) Configuration { + bytes, err := ioutil.ReadFile(configFilePath) + + if err != nil { + color.Errorln(err.Error()) + color.Warnln("Creating new configuration file") + return createNewConfig(configFilePath) + } + + var cfg Configuration + err = toml.Unmarshal(bytes, &cfg) + if err != nil { + color.Errorln(err.Error()) + color.Errorln("Configuration file " + configFilePath + " could not be read as TOML file") + panic(err) + } + color.Infoln("Loaded configuration file " + configFilePath) + return cfg +} + +func createNewConfig(configFilePath string) Configuration { + confBytes, err := toml.Marshal(defaultConfig) + if err != nil { + color.Errorln(err.Error()) + color.Errorln("Default config struct is not TOML conform") + panic(err) + } + err = os.WriteFile(configFilePath, confBytes, 0644) + if err != nil { + color.Errorln(err.Error()) + color.Warnln("Using default config without file") + } + return defaultConfig +} diff --git a/go.mod b/go.mod index 91dd049..19240ad 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,9 @@ go 1.17 require github.com/miekg/dns v1.1.43 require ( + github.com/gookit/color v1.4.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.0-beta.3 // indirect + github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect - golang.org/x/sys v0.0.0-20210303074136-134d130e1a04 // indirect + golang.org/x/sys v0.0.0-20211015200801-69063c4bb744 // indirect ) diff --git a/go.sum b/go.sum index e4731d1..2198bbe 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,16 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gookit/color v1.4.2 h1:tXy44JFSFkKnELV6WaMo/lLfu/meqITX3iAV52do7lk= +github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ= github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg= github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4= +github.com/pelletier/go-toml/v2 v2.0.0-beta.3 h1:PNCTU4naEJ8mKal97P3A2qDU74QRQGlv4FXiL1XDqi4= +github.com/pelletier/go-toml/v2 v2.0.0-beta.3/go.mod h1:aNseLYu/uKskg0zpr/kbr2z8yGuWtotWf/0BpGIAL2Y= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= @@ -7,6 +18,11 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210303074136-134d130e1a04 h1:cEhElsAv9LUt9ZUUocxzWe05oFLVd+AA2nstydTeI8g= golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20211015200801-69063c4bb744 h1:KzbpndAYEM+4oHRp9JmB2ewj0NHHxO3Z0g7Gus2O1kk= +golang.org/x/sys v0.0.0-20211015200801-69063c4bb744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 8422580..d0507d6 100644 --- a/main.go +++ b/main.go @@ -1,19 +1,23 @@ package main import ( + "flag" "log" "github.com/MarekWojt/gertdns/config" "github.com/MarekWojt/gertdns/dns" ) -func main() { - err := config.Load() - if err != nil { - log.Fatalf("Failed to load config: %s\n ", err.Error()) - } +var ( + configFile = flag.String("configFile", "conf.toml", "Path to configuration file") +) - err = dns.Run() +func main() { + flag.Parse() + + config.Load(*configFile) + + err := dns.Run() if err != nil { log.Fatalf("Failed to start DNS server: %s\n ", err.Error()) } From 1d7dcbe3ba4c9e611915aca5eb1b46f83b96b2f7 Mon Sep 17 00:00:00 2001 From: daru Date: Sun, 17 Oct 2021 13:29:26 +0200 Subject: [PATCH 2/4] Exclude conf from git --- .gitignore | 1 + conf.toml | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 conf.toml diff --git a/.gitignore b/.gitignore index cacb21e..fd32655 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ gertdns gertdns.exe +conf.toml diff --git a/conf.toml b/conf.toml deleted file mode 100644 index 23ed074..0000000 --- a/conf.toml +++ /dev/null @@ -1,3 +0,0 @@ -Port = 5353 -Host = '0.0.0.0' -Domains = [] From 290a9af7f1b55b0d122742c67d2e18205e45db1e Mon Sep 17 00:00:00 2001 From: daru Date: Sun, 17 Oct 2021 13:49:56 +0200 Subject: [PATCH 3/4] Don't panic! Propagate error --- config/config.go | 5 +++-- config/toml.go | 12 ++++++------ main.go | 7 +++++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 02822f1..bbfd6d9 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ var ( } ) -func Load(configFilePath string) { - Config = loadConfFile(configFilePath) +func Load(configFilePath string) (*Configuration, error) { + Config, err := loadConfFile(configFilePath) + return &Config, err } diff --git a/config/toml.go b/config/toml.go index 6fcb28b..ce8914a 100644 --- a/config/toml.go +++ b/config/toml.go @@ -8,7 +8,7 @@ import ( "github.com/pelletier/go-toml/v2" ) -func loadConfFile(configFilePath string) Configuration { +func loadConfFile(configFilePath string) (Configuration, error) { bytes, err := ioutil.ReadFile(configFilePath) if err != nil { @@ -22,23 +22,23 @@ func loadConfFile(configFilePath string) Configuration { if err != nil { color.Errorln(err.Error()) color.Errorln("Configuration file " + configFilePath + " could not be read as TOML file") - panic(err) + return defaultConfig, err } color.Infoln("Loaded configuration file " + configFilePath) - return cfg + return cfg, err } -func createNewConfig(configFilePath string) Configuration { +func createNewConfig(configFilePath string) (Configuration, error) { confBytes, err := toml.Marshal(defaultConfig) if err != nil { color.Errorln(err.Error()) color.Errorln("Default config struct is not TOML conform") - panic(err) + return defaultConfig, err } err = os.WriteFile(configFilePath, confBytes, 0644) if err != nil { color.Errorln(err.Error()) color.Warnln("Using default config without file") } - return defaultConfig + return defaultConfig, err } diff --git a/main.go b/main.go index d0507d6..2671bd8 100644 --- a/main.go +++ b/main.go @@ -15,9 +15,12 @@ var ( func main() { flag.Parse() - config.Load(*configFile) + _, err := config.Load(*configFile) + if err != nil { + log.Fatalf("Failed to load configuration: %s\n ", err.Error()) + } - err := dns.Run() + err = dns.Run() if err != nil { log.Fatalf("Failed to start DNS server: %s\n ", err.Error()) } From 5ef2f14cc7004f9649169c7c893ab3a73d61faa0 Mon Sep 17 00:00:00 2001 From: marek Date: Sun, 17 Oct 2021 14:19:29 +0200 Subject: [PATCH 4/4] config.Load doesn't return Configuration; global config.Config is actually set --- config/config.go | 7 ++++--- main.go | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index bbfd6d9..f56288d 100644 --- a/config/config.go +++ b/config/config.go @@ -16,7 +16,8 @@ var ( } ) -func Load(configFilePath string) (*Configuration, error) { - Config, err := loadConfFile(configFilePath) - return &Config, err +func Load(configFilePath string) error { + config, err := loadConfFile(configFilePath) + Config = config + return err } diff --git a/main.go b/main.go index 2671bd8..7070785 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ var ( func main() { flag.Parse() - _, err := config.Load(*configFile) + err := config.Load(*configFile) if err != nil { log.Fatalf("Failed to load configuration: %s\n ", err.Error()) }