Skip to content

Commit

Permalink
build: refactor project layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamesits committed Apr 5, 2022
1 parent 5d44508 commit bfee28e
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 189 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ LICENSE
*.yml
build/
.github/
.idea/
59 changes: 59 additions & 0 deletions cmd/snd/snd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"flag"
"fmt"
"github.com/BurntSushi/toml"
"github.com/jamesits/libiferr/exception"
"github.com/jamesits/snd"
"github.com/jamesits/snd/pkg/config"
"github.com/jamesits/snd/pkg/dns_server"
"log"
"strings"
"sync"
)

var conf *config.Config
var configFilePath *string
var showVersionOnly *bool
var mainThreadWaitGroup = &sync.WaitGroup{}

func main() {
// parse flags
var err error
configFilePath = flag.String("Config", "/etc/snd/Config.toml", "Config directory")
showVersionOnly = flag.Bool("version", false, "show version and quit")
flag.Parse()

if *showVersionOnly {
fmt.Println(snd.GetVersionFullString())
return
} else {
log.Println(snd.GetVersionFullString())
}

// parse Config file
conf = &config.Config{}
metaData, err := toml.DecodeFile(*configFilePath, conf)
exception.HardFailWithReason("failed to read the config file", err)

// print unknown configs
for _, key := range metaData.Undecoded() {
log.Printf("Unknown key %q in the Config file, maybe a typo?", key.String())
}

// fix Config and fill in defaults
conf.FixConfig()

// listen on all the configured listeners
for _, elem := range conf.Listen {
r := strings.SplitN(*elem, ":", 2)
go func() {
mainThreadWaitGroup.Add(1)
defer mainThreadWaitGroup.Done()
go dns_server.ListenSync(conf, r[0], r[1])
}()
}

mainThreadWaitGroup.Wait()
}
7 changes: 5 additions & 2 deletions misc/perf.sh → contrib/perf/perf.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
cd "$( dirname "${BASH_SOURCE[0]}" )"/..
#!/usr/bin/env bash
set -Eeuo pipefail

cd "$( dirname "${BASH_SOURCE[0]}" )"/../..

DNSPERF_DATA_FILE="build/dnsperf.txt"

Expand All @@ -19,4 +22,4 @@ done

# download and compile https://github.com/cobblau/dnsperf
# need around 5mins for 5000000 requests
dnsperf -s 127.0.0.1 -d build/dnsperf.txt -c 1000 -Q 5000000
dnsperf -s 127.0.0.1 -d build/dnsperf.txt -c 1000 -Q 5000000
20 changes: 0 additions & 20 deletions exceptions.go

This file was deleted.

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module snd
module github.com/jamesits/snd

go 1.13

require (
github.com/BurntSushi/toml v1.0.0
github.com/jamesits/libiferr v0.0.0-20220318185358-918bd9502231
github.com/miekg/dns v1.1.48
golang.org/x/mod v0.6.0-dev.0.20220330205332-605edab4323b // indirect
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b // indirect
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jamesits/libiferr v0.0.0-20220318185358-918bd9502231 h1:B5PkrvZlQqr8iw9pjBtoUpMDkZTWjOVK3NjfbkQiEMo=
github.com/jamesits/libiferr v0.0.0-20220318185358-918bd9502231/go.mod h1:B1u0mUxDnlm/y//hSB1SRwPvodbjL1V/lBUlPrzHWkg=
github.com/miekg/dns v1.1.48 h1:Ucfr7IIVyMBz4lRE8qmGUuZ4Wt3/ZGu9hmcMT3Uu4tQ=
github.com/miekg/dns v1.1.48/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -24,6 +37,7 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cO
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -49,3 +63,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 3 additions & 3 deletions config.go → pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package config

import "net"

Expand All @@ -19,8 +19,8 @@ const (
FOUR_HEXS_NOTATION
)

// config file
type config struct {
// Config file
type Config struct {
Listen []*string `toml:"listen"`
PerNetConfigs []*perNetConfig `toml:"net"`
PerHostConfigs map[string]string `toml:"host"`
Expand Down
83 changes: 33 additions & 50 deletions config_parser.go → pkg/config/parser.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,40 @@
package main
package config

import (
"github.com/jamesits/libiferr/exception"
"log"
"net"
"strings"
)

func ensureDotAtRight(s *string) *string {
if !strings.HasSuffix(*s, ".") {
ret := *s + "."
return &ret
} else {
return s
}
}

func ensureNoDotAtLeft(s *string) *string {
if !strings.HasPrefix(*s, ".") {
ret := strings.TrimLeft(*s, ".")
return &ret
} else {
return s
}
}

func SOARecordFillDefault(r *SOARecord, useDefaultRecord bool) {
func (config *Config) SOARecordFillDefault(r *SOARecord, useDefaultRecord bool) {
if useDefaultRecord {
if len(*r.MName) == 0 {
r.MName = conf.DefaultSOARecord.MName
r.MName = config.DefaultSOARecord.MName
}

if len(*r.RName) == 0 {
r.RName = conf.DefaultSOARecord.RName
r.RName = config.DefaultSOARecord.RName
}

if r.Serial == 0 {
r.Serial = conf.DefaultSOARecord.Serial
r.Serial = config.DefaultSOARecord.Serial
}

if r.Refresh == 0 {
r.Refresh = conf.DefaultSOARecord.Refresh
r.Refresh = config.DefaultSOARecord.Refresh
}

if r.Retry == 0 {
r.Retry = conf.DefaultSOARecord.Retry
r.Retry = config.DefaultSOARecord.Retry
}

if r.Expire == 0 {
r.Expire = conf.DefaultSOARecord.Expire
r.Expire = config.DefaultSOARecord.Expire
}

if r.TTL == 0 {
r.TTL = conf.DefaultSOARecord.TTL
r.TTL = config.DefaultSOARecord.TTL
}
} else {
if r.Serial == 0 {
Expand Down Expand Up @@ -87,33 +70,33 @@ func SOARecordFillDefault(r *SOARecord, useDefaultRecord bool) {
r.MName = ensureDotAtRight(r.MName)
}

// fix config and fill in defaults
func fixConfig() {
// fix Config and fill in defaults
func (config *Config) FixConfig() {
var err error

if conf.DefaultTTL == 0 {
conf.DefaultTTL = 114514
if config.DefaultTTL == 0 {
config.DefaultTTL = 114514
}

for index, ns := range conf.DefaultNSes {
conf.DefaultNSes[index] = ensureDotAtRight(ns)
for index, ns := range config.DefaultNSes {
config.DefaultNSes[index] = ensureDotAtRight(ns)
}

if conf.DefaultSOARecord == nil {
conf.DefaultSOARecord = new(SOARecord)
if config.DefaultSOARecord == nil {
config.DefaultSOARecord = new(SOARecord)
}
SOARecordFillDefault(conf.DefaultSOARecord, false)
config.SOARecordFillDefault(config.DefaultSOARecord, false)

fixed_hosts := make([]*perNetConfig, 0)
for net, domain := range conf.PerHostConfigs {
for network, domain := range config.PerHostConfigs {
netCIDR := ""
for i := 0; i < len(net); i++ {
switch net[i] {
for i := 0; i < len(network); i++ {
switch network[i] {
case '.':
netCIDR = net + "/32"
netCIDR = network + "/32"
break
case ':':
netCIDR = net + "/128"
netCIDR = network + "/128"
break
}
}
Expand All @@ -123,21 +106,21 @@ func fixConfig() {
log.Printf("Loading host %s -> %s\n", netCIDR, domain)
mode := "fixed"
for _, d := range strings.Split(domain, ",") {
thisHost := &perNetConfig {
IPNetString: &netCIDR,
thisHost := &perNetConfig{
IPNetString: &netCIDR,
PtrGenerationModeString: &mode,
Domain: &d,
Domain: &d,
}
fixed_hosts = append(fixed_hosts, thisHost)
}
}
conf.PerNetConfigs = append(fixed_hosts, conf.PerNetConfigs...)
config.PerNetConfigs = append(fixed_hosts, config.PerNetConfigs...)

// note that range is byVal so we use index here
for _, currentConfig := range conf.PerNetConfigs {
for _, currentConfig := range config.PerNetConfigs {
// fill IPNet
_, currentConfig.IPNet, err = net.ParseCIDR(*currentConfig.IPNetString)
hardFailIf(err)
exception.HardFailWithReason("failed to parse CIDR", err)

log.Printf("Loading network %s\n", currentConfig.IPNet.String())

Expand Down Expand Up @@ -180,14 +163,14 @@ func fixConfig() {

// fill TTL
if currentConfig.TTL == 0 {
currentConfig.TTL = conf.DefaultTTL
currentConfig.TTL = config.DefaultTTL
}

// fill SOA
if currentConfig.SOARecord == nil {
currentConfig.SOARecord = conf.DefaultSOARecord
currentConfig.SOARecord = config.DefaultSOARecord
} else {
SOARecordFillDefault(currentConfig.SOARecord, true)
config.SOARecordFillDefault(currentConfig.SOARecord, true)
}
}
}
21 changes: 21 additions & 0 deletions pkg/config/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package config

import "strings"

func ensureDotAtRight(s *string) *string {
if !strings.HasSuffix(*s, ".") {
ret := *s + "."
return &ret
} else {
return s
}
}

func ensureNoDotAtLeft(s *string) *string {
if !strings.HasPrefix(*s, ".") {
ret := strings.TrimLeft(*s, ".")
return &ret
} else {
return s
}
}
Loading

0 comments on commit bfee28e

Please sign in to comment.