Skip to content

Commit

Permalink
Updated code to add changes in config directly
Browse files Browse the repository at this point in the history
  • Loading branch information
vanshaj committed Dec 21, 2021
1 parent ede714c commit 015ec15
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions blackhatgo/chapter5/dnsServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import (
"log"
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"

"github.com/miekg/dns"
)

var records map[string]string
var recordLock sync.RWMutex

func parse(filename string) (map[string]string, error) {
records := make(map[string]string)
Expand Down Expand Up @@ -46,7 +50,9 @@ func dnsRequestHandler(w dns.ResponseWriter, req *dns.Msg) {
if len(parts) > 1 {
name = strings.Join(parts[len(parts)-2:], ".")
}
recordLock.RLock()
match, ok := records[name]
recordLock.RUnlock()
name = fmt.Sprintf("%v.", name)
if !ok {
dns.HandleFailed(w, req)
Expand Down Expand Up @@ -76,5 +82,18 @@ func dnsRequestHandler(w dns.ResponseWriter, req *dns.Msg) {
func main() {
records, _ = parse("proxy.config")
dns.HandleFunc(".", dnsRequestHandler)
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.Signal(0xa))
for sig := range sigs {
switch sig {
case syscall.Signal(0xa):
log.Println("SIGUSR1: reloading records")
recordLock.Lock()
parse("proxy.config")
recordLock.Unlock()
}
}
}()
log.Fatal(dns.ListenAndServe(":53", "udp", nil))
}

0 comments on commit 015ec15

Please sign in to comment.