From 7b07a81a5b7a741a60c018d855a01c51e27d5576 Mon Sep 17 00:00:00 2001 From: core Date: Sun, 1 Oct 2023 16:43:10 -0400 Subject: [PATCH] initial commit --- .idea/.gitignore | 8 +++++++ .idea/modules.xml | 8 +++++++ .idea/pancheri.iml | 9 ++++++++ README.md | 3 +++ cmd/pancheri/main.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ config.example.yml | 3 +++ config.go | 44 +++++++++++++++++++++++++++++++++++++ go.mod | 13 +++++++++++ go.sum | 22 +++++++++++++++++++ main.go | 1 + resolver.go | 41 ++++++++++++++++++++++++++++++++++ 11 files changed, 204 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/pancheri.iml create mode 100644 README.md create mode 100644 cmd/pancheri/main.go create mode 100644 config.example.yml create mode 100644 config.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 resolver.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3a42f36 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/pancheri.iml b/.idea/pancheri.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/pancheri.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b5d8905 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Pancheri +A custom-built authoritative DNS server and proxy server built for the e3team internal network. +Capable of serving arbitrary custom domains and asking an upstream DNS server for all other queries. \ No newline at end of file diff --git a/cmd/pancheri/main.go b/cmd/pancheri/main.go new file mode 100644 index 0000000..feb982f --- /dev/null +++ b/cmd/pancheri/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "flag" + "fmt" + "git.e3t.cc/e3team/pancheri" + "github.com/miekg/dns" + "github.com/sirupsen/logrus" + "os" +) + +func main() { + configPath := flag.String("config", "/etc/pancheri/config.yml", "Path to enter a file or directory to load configuration from") + printUsage := flag.Bool("help", false, "Print command line usage") + + flag.Parse() + + if *printUsage { + flag.Usage() + os.Exit(0) + } + + c, err := pancheri.LoadConfig(*configPath) + + if err != nil { + fmt.Printf("failed to load config: %s", err) + } + + if c.Logging.Format == "json" { + logrus.SetFormatter(&logrus.JSONFormatter{}) + } + logrus.SetLevel(c.Logging.Level) + + logrus.WithFields(logrus.Fields{ + "host": c.Server.Host, + "port": c.Server.Port, + }).Info("starting DNS listener") + + var r *pancheri.Resolver + + if c.Resolver.Enable { + logrus.WithFields(logrus.Fields{ + "upstream": c.Resolver.Upstream, + }).Info("enabling upstream resolver") + + r = pancheri.NewResolver(c.Resolver.Upstream) + err = r.Resolve("example.com", dns.TypeA) + if err != nil { + logrus.Errorf("failed to resolve: %s", err) + } + } +} diff --git a/config.example.yml b/config.example.yml new file mode 100644 index 0000000..542e470 --- /dev/null +++ b/config.example.yml @@ -0,0 +1,3 @@ +server: + host: "0.0.0.0" + port: 53 \ No newline at end of file diff --git a/config.go b/config.go new file mode 100644 index 0000000..7f4df41 --- /dev/null +++ b/config.go @@ -0,0 +1,44 @@ +package pancheri + +import ( + "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" + "os" +) + +type Config struct { + Server struct { + Host string `yaml:"host"` + Port string `yaml:"port"` + } `yaml:"server"` + Resolver struct { + Enable bool `yaml:"enable"` + Upstream string `yaml:"upstream"` + } + Logging struct { + Format string `yaml:"format"` + Level logrus.Level `yaml:"level"` + } +} + +func LoadConfig(path string) (*Config, error) { + f, err := os.Open(path) + if err != nil { + return nil, err + } + + var cfg Config + decoder := yaml.NewDecoder(f) + + err = decoder.Decode(&cfg) + if err != nil { + return nil, err + } + + err = f.Close() + if err != nil { + return nil, err + } + + return &cfg, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a16670e --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module git.e3t.cc/e3team/pancheri + +go 1.21 + +require ( + github.com/miekg/dns v1.1.56 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/net v0.15.0 // indirect + golang.org/x/sys v0.12.0 // indirect + golang.org/x/tools v0.13.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d8ef838 --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/miekg/dns v1.1.56 h1:5imZaSeoRNvpM9SzWNhEcP9QliKiz20/dA2QabIGVnE= +github.com/miekg/dns v1.1.56/go.mod h1:cRm6Oo2C8TY9ZS/TqsSrseAcncm74lfK5G+ikN2SWWY= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +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.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..f8123f6 --- /dev/null +++ b/main.go @@ -0,0 +1 @@ +package pancheri diff --git a/resolver.go b/resolver.go new file mode 100644 index 0000000..fe21a34 --- /dev/null +++ b/resolver.go @@ -0,0 +1,41 @@ +package pancheri + +import ( + "github.com/miekg/dns" + "github.com/sirupsen/logrus" +) + +type Resolver struct { + client *dns.Client + upstream string +} + +func NewResolver(upstream string) *Resolver { + return &Resolver{ + client: new(dns.Client), + upstream: upstream, + } +} + +func (r *Resolver) Resolve(domain string, qtype uint16) error { + logrus.WithFields(logrus.Fields{ + "domain": domain, + "qtype": qtype, + }).Trace("performing upstream resolution") + + m := new(dns.Msg) + m.SetQuestion(dns.Fqdn(domain), qtype) + m.RecursionDesired = true + + in, _, err := r.client.Exchange(m, r.upstream) + + if err != nil { + return err + } + + for _, ans := range in.Answer { + logrus.Debug(ans) + } + + return nil +}