Skip to content

Commit

Permalink
Merge pull request #1 from vinted/feature/initial_release
Browse files Browse the repository at this point in the history
dns-parking: inital release
  • Loading branch information
Seitanas authored Dec 18, 2023
2 parents fe97668 + 124311b commit 626fb36
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# dns-parking
Service for parking DNS records

Service sends predefined SOA or NS replies to any domain request.
See config.json file for configuration options.
23 changes: 23 additions & 0 deletions cmd/dns-parking/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"flag"
"github.com/vinted/dns-parking/pkg/config"
"github.com/vinted/dns-parking/pkg/dns"
"log"
)

func main() {
var (
configFile string
listenAddress string
)
flag.StringVar(&configFile, "configFile", "/etc/dns-parking/config.json", "Path to config file.")
flag.StringVar(&listenAddress, "listenAddress", "0.0.0.0:53", "Address to bind to.")
flag.Parse()
err := config.Init(configFile)
if err != nil {
log.Fatalf("Failed to read configuration. %v", err)
}
dns.Start(listenAddress)
}
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"SOARname": "domains.parking.com",
"SOASerial": "2023121801",
"SOARefresh": "86400",
"SOARetry": "7200",
"SOAExpire": "3600000",
"SOATTL": "3600",
"NS": [
"ns1.parking.com",
"ns2.parking.com"
]
}
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/vinted/dns-parking

go 1.21.0

require github.com/miekg/dns v1.1.57

require (
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM=
github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
44 changes: 44 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import (
"encoding/json"
"log"
"os"
)

type Cfg struct {
SOARname string
SOASerial string
SOARefresh string
SOARetry string
SOAExpire string
SOATTL string
NS []string
}

var (
Config *Cfg
ConfigFile string
)

func Init(configFile string) error {
ConfigFile = configFile
content := []byte(`{}`)
_, err := os.Stat(ConfigFile)
if !os.IsNotExist(err) {
content, err = os.ReadFile(ConfigFile)
if err != nil {
return err
}
}
if len(content) == 0 {
content = []byte(`{}`)
}
err = json.Unmarshal(content, &Config)
if err != nil {
return err
}

log.Printf("Finished reading config.")
return nil
}
57 changes: 57 additions & 0 deletions pkg/dns/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dns

import (
"fmt"
"github.com/miekg/dns"
"github.com/vinted/dns-parking/pkg/config"
"log"
)

func parseQuery(m *dns.Msg) {
cfg := *config.Config
for _, q := range m.Question {
switch q.Qtype {
case dns.TypeSOA:
log.Printf("SOA for %s\n", q.Name)
rr, err := dns.NewRR(fmt.Sprintf("%s %s IN SOA %s. %s. %s %s %s %s %s", q.Name, cfg.SOARefresh, cfg.NS[0], cfg.SOARname, cfg.SOASerial, cfg.SOARefresh, cfg.SOARetry, cfg.SOAExpire, cfg.SOATTL))
if err == nil {
m.Answer = append(m.Answer, rr)
} else {
log.Printf("Error sending SOA record: %s\n", err)
}
case dns.TypeNS:
log.Printf("NS for %s\n", q.Name)
for _, ns := range config.Config.NS {
rr, err := dns.NewRR(fmt.Sprintf("%s %s IN NS %s.", q.Name, config.Config.SOATTL, ns))
if err == nil {
m.Answer = append(m.Answer, rr)
} else {
log.Printf("Error sending NS record: %s\n", err)
}
}
}
}
}

func handleDnsRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false

switch r.Opcode {
case dns.OpcodeQuery:
parseQuery(m)
}
w.WriteMsg(m)
}

func Start(listenAddress string) {
dns.HandleFunc(".", handleDnsRequest)
server := &dns.Server{Addr: listenAddress, Net: "udp"}
log.Printf("Server started at %s\n", listenAddress)
err := server.ListenAndServe()
defer server.Shutdown()
if err != nil {
log.Fatalf("Failed to start server: %s\n ", err.Error())
}
}

0 comments on commit 626fb36

Please sign in to comment.