refactor DNS configuration and add alternate resolver support
This commit is contained in:
parent
68fc296c20
commit
11c213e7b0
7 changed files with 49 additions and 20 deletions
|
|
@ -1,5 +0,0 @@
|
|||
port: 53
|
||||
records:
|
||||
type_a:
|
||||
z1.com: 10.10.10.1
|
||||
z2.com: 10.10.10.2
|
||||
8
app/dns.yml
Normal file
8
app/dns.yml
Normal file
|
|
@ -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
|
||||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -73,3 +73,7 @@ func Start(app func()) {
|
|||
<-sigs
|
||||
|
||||
}
|
||||
|
||||
func ClearScreen() {
|
||||
print("\033[H\033[2J")
|
||||
}
|
||||
|
|
|
|||
11
app/main.go
11
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...")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
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
|
||||
BIN
app/tmp/main
BIN
app/tmp/main
Binary file not shown.
Loading…
Reference in a new issue