Skip to content

Commit

Permalink
dns works
Browse files Browse the repository at this point in the history
  • Loading branch information
monoxane committed Apr 12, 2023
1 parent 96a43df commit 4ebfbe2
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 9 deletions.
11 changes: 8 additions & 3 deletions cmd/vxconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/monoxane/vxconnect/internal/config"
"github.com/monoxane/vxconnect/internal/controller"
"github.com/monoxane/vxconnect/internal/dns"
"github.com/monoxane/vxconnect/internal/logging"
"github.com/monoxane/vxconnect/internal/persistance"
)
Expand All @@ -22,6 +23,8 @@ func main() {
os.Exit(ERR_CONFIG_LOAD_FAILED)
}

forever := make(chan bool)

logging.Configure()

log = logging.Log.With().Str("package", "cmd").Logger()
Expand All @@ -40,9 +43,11 @@ func main() {
log.Fatal().Err(migrationError).Msg("an error occured while migrating the persistance store")
}

dnsService := dns.New()
go dnsService.Run()

controllerSingleton := controller.New(8080, store)
go controllerSingleton.Run()

if err := controllerSingleton.Run(); err != nil {
log.Fatal().Err(err).Msg("an error occurred when initialising controller")
}
<-forever
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/miekg/dns v1.1.53 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -44,9 +45,11 @@ require (
github.com/ugorji/go/codec v1.2.9 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.3.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/miekg/dns v1.1.53 h1:ZBkuHr5dxHtB1caEOlZTLPo7D3L3TWckgUUs/RHfDxw=
github.com/miekg/dns v1.1.53/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -286,6 +288,8 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -437,6 +441,8 @@ golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.3.0 h1:SrNbZl6ECOS1qFzgTdQfWXZM9XBkiA6tkFrH9YSTPHM=
golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
21 changes: 15 additions & 6 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/monoxane/vxconnect/internal/auth"
"github.com/monoxane/vxconnect/internal/entity"
"github.com/monoxane/vxconnect/internal/logging"
"github.com/monoxane/vxconnect/internal/persistance"
)
Expand Down Expand Up @@ -78,11 +79,19 @@ func NotImplemented(c *gin.Context) {
c.String(http.StatusNotImplemented, "Not Implemented Yet")
}

func (c *Controller) Run() error {
c.log.Info().Msg("starting REST API interface")
if err := c.restEngine.Run(fmt.Sprintf("0.0.0.0:%d", c.restPort)); err != nil {
return fmt.Errorf("unable to start REST API interface: %s", err)
}
func (c *Controller) Run() {
go func() {
c.log.Info().Msg("starting REST API interface")
if err := c.restEngine.Run(fmt.Sprintf("0.0.0.0:%d", c.restPort)); err != nil {
c.log.Fatal().Err(err).Msg("unable to start Controller")
}

c.log.Fatal().Msg("the Controller exited")
}()
}

return fmt.Errorf("REST API interface failed")
func QueryRecord(name string) *entity.Record { return controller.QueryRecord(name) }
func (c *Controller) QueryRecord(name string) *entity.Record {
record, _ := c.persistance.GetRecordbyName(name)
return record
}
68 changes: 68 additions & 0 deletions internal/dns/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dns

import (
"net"
"strings"

"github.com/miekg/dns"
"github.com/monoxane/vxconnect/internal/controller"
"github.com/monoxane/vxconnect/internal/logging"
)

var (
server *Server
)

type Server struct {
service *dns.Server
log logging.Logger
}

func New() *Server {
server = &Server{
service: &dns.Server{Addr: ":53", Net: "udp"},
log: logging.Log.With().Str("package", "dns").Logger(),
}

dns.HandleFunc(".", handleRequest)

return server
}

func (server *Server) Run() {
go func() {
err := server.service.ListenAndServe()
if err != nil {
server.log.Fatal().Err(err).Msg("unable to start DNS service")
}
}()
}

func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
server.HandleRequest(w, r)
}

func (server *Server) HandleRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)

name := strings.TrimSuffix(r.Question[0].Name, ".")

record := controller.QueryRecord(name)
if record != nil {
hdr := dns.RR_Header{Name: r.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: uint32(record.TTL)}
switch record.Type {
case "CNAME":
m.Answer = []dns.RR{&dns.CNAME{Hdr: hdr, Target: record.Target}}
case "A":
ip := net.ParseIP(record.Target)
m.Answer = []dns.RR{&dns.A{Hdr: hdr, A: ip}}
}
server.log.Debug().Str("name", name).Interface("record", record).Msg("serving record")
}

err := w.WriteMsg(m)
if err != nil {
server.log.Error().Err(err).Msg("unable to write dns answer")
}
}
11 changes: 11 additions & 0 deletions internal/persistance/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ func (s *MariaDBStore) GetRecordByID(id string) (*entity.Record, error) {
return record, nil
}

func (s *MariaDBStore) GetRecordbyName(name string) (*entity.Record, error) {
record := &entity.Record{}
result := s.connection.First(&record, "name = ?", name)

if result.Error != nil {
return nil, fmt.Errorf("unable to query DB for zone records: %s", result.Error)
}

return record, nil
}

func (s *MariaDBStore) CreateRecord(record *entity.Record) error {
result := s.connection.Create(record)

Expand Down
1 change: 1 addition & 0 deletions internal/persistance/persistance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Store interface {

GetZoneRecords(zone string) ([]*entity.Record, error)
GetRecordByID(id string) (*entity.Record, error)
GetRecordbyName(name string) (*entity.Record, error)
CreateRecord(record *entity.Record) error
SaveRecord(record *entity.Record) error
DeleteRecord(id string) error
Expand Down

0 comments on commit 4ebfbe2

Please sign in to comment.