diff --git a/app/dns.yaml b/app/dns.yaml deleted file mode 100644 index 6b083d4..0000000 --- a/app/dns.yaml +++ /dev/null @@ -1,5 +0,0 @@ -port: 53 -records: - type_a: - z1.com: 10.10.10.1 - z2.com: 10.10.10.2 \ No newline at end of file diff --git a/app/dns.yml b/app/dns.yml new file mode 100644 index 0000000..9569bc4 --- /dev/null +++ b/app/dns.yml @@ -0,0 +1,8 @@ +port: 53 +network: udp +alternate_resolver: + ip: 8.8.8.8 + port: 53 +records: + type_a: + test.com: 10.10.10.1 \ No newline at end of file diff --git a/app/dns/dns.go b/app/dns/dns.go index 83137d2..02b2d7c 100644 --- a/app/dns/dns.go +++ b/app/dns/dns.go @@ -51,16 +51,20 @@ func New(filePath string) *DNS { app := &DNS{} app.ConfigInit(filePath) app.Records.TypeA = app.Config.GetStringMapString("records.type_a") + alternate_resolver_ip := app.Config.GetString("alternate_resolver.ip") + alternate_resolver_port := strconv.Itoa(app.Config.GetInt("alternate_resolver.port")) app.ServerInit() app.MuxInit() - app.Resolver = NewResolver("1.1.1.1:53") + app.Resolver = NewResolver(alternate_resolver_ip + ":" + alternate_resolver_port) app.Server.Handler = app.Mux return app } func (a *DNS) Run() { - helper.P("DNS SERVER STARTED") - a.Server.ListenAndServe() + err := a.Server.ListenAndServe() + if err != nil { + fmt.Println(err.Error()) + } } func (a *DNS) ConfigInit(filePath string) { @@ -83,9 +87,14 @@ func (a *DNS) ServerInit() { Net: a.Config.GetString("network"), Handler: nil, } + helper.P("DNS server started at port ", port) + } + func (a *DNS) MuxInit() { a.Mux.ServeMux = miekgDNS.NewServeMux() + a.Mux.HandleFunc(".", a.HandleTypeA) + } func NewResolver(DNSserverAddr string) Resolver { @@ -100,13 +109,13 @@ func NewResolver(DNSserverAddr string) Resolver { } } -// func (a *DNS) Handler(f func(a *DNS, w miekgDNS.ResponseWriter, r *miekgDNS.Msg)) miekgDNS.HandlerFunc { -// return func(w miekgDNS.ResponseWriter, r *miekgDNS.Msg) { -// f(a, w, r) -// } -// } +func (a *DNS) Handler(f func(a *DNS, w miekgDNS.ResponseWriter, r *miekgDNS.Msg)) miekgDNS.HandlerFunc { + return func(w miekgDNS.ResponseWriter, r *miekgDNS.Msg) { + f(a, w, r) + } +} -func (r *Resolver) Lookup(lookupAddr string, DNSserverAddr string) string { +func (r *Resolver) Lookup(lookupAddr string) string { var resp []string var err error @@ -121,6 +130,7 @@ func (r *Resolver) Lookup(lookupAddr string, DNSserverAddr string) string { } func (a *DNS) HandleTypeA(w miekgDNS.ResponseWriter, r *miekgDNS.Msg) { + useAlternateResolver := false t := time.Now() msg := &miekgDNS.Msg{} msg.SetReply(r) @@ -129,23 +139,30 @@ func (a *DNS) HandleTypeA(w miekgDNS.ResponseWriter, r *miekgDNS.Msg) { ip := "" if ipValue, ok := a.Records.TypeA[domainName]; ok { + helper.P("FOUND => ", domainName) ip = ipValue } else { dSlices := strings.Split(domainName, ".") //check if wild card if len(dSlices) > 2 { + name := dSlices[len(dSlices)-2] tld := dSlices[len(dSlices)-1] cname := name + "." + tld wildCard := "*." + cname if ipValue, ok := a.Records.TypeA[wildCard]; ok { + ip = ipValue } else { - //lookup - ip = a.Resolver.Lookup(domainName, "1.1.1.1:53") + useAlternateResolver = true } + } else { + useAlternateResolver = true } + if useAlternateResolver { + ip = a.Resolver.Lookup(domainName) + } } RR_Header := miekgDNS.RR_Header{ Name: miekgDNS.Fqdn(domainName), diff --git a/app/helper/helper.go b/app/helper/helper.go index 77fb704..e719c5c 100644 --- a/app/helper/helper.go +++ b/app/helper/helper.go @@ -73,3 +73,7 @@ func Start(app func()) { <-sigs } + +func ClearScreen() { + print("\033[H\033[2J") +} diff --git a/app/main.go b/app/main.go index 6b4f236..67e5e65 100644 --- a/app/main.go +++ b/app/main.go @@ -10,16 +10,21 @@ import ( ) func main() { - helper.P("STARTING SERVERS") + // Clear the terminal screen + + helper.ClearScreen() + d := dns.New("./dns.yml") // Run the HTTP and DNS servers concurrently go runHTTP() + go func() { - dns := dns.New("./dns.yaml") - dns.Run() + d.Run() }() + // go runDNS() // Wait for SIGINT (Ctrl+C) to gracefully shut down the server stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt) <-stop + log.Println("Shutting down the server...") } diff --git a/app/tmp/build-errors.log b/app/tmp/build-errors.log index a764b5f..bfd43cc 100644 --- a/app/tmp/build-errors.log +++ b/app/tmp/build-errors.log @@ -1 +1 @@ -exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1 \ No newline at end of file +exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1 \ No newline at end of file diff --git a/app/tmp/main b/app/tmp/main index 1c4f807..5858432 100755 Binary files a/app/tmp/main and b/app/tmp/main differ