-
-
Save alibo/b1201c51f6409ab2068a45ce290e8d37 to your computer and use it in GitHub Desktop.
Simple Golang DNS Server (simplified version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"strconv" | |
"github.com/miekg/dns" | |
) | |
func main() { | |
// attach request handler func | |
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) { | |
m := new(dns.Msg) | |
m.SetReply(r) | |
m.Compress = false | |
if r.Opcode == dns.OpcodeQuery { | |
for _, q := range m.Question { | |
if q.Qtype == dns.TypeA { | |
log.Printf("Query for %s\n", q.Name) | |
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, "1.2.3.4")) | |
if err == nil { | |
m.Answer = append(m.Answer, rr) | |
} | |
} | |
} | |
} | |
w.WriteMsg(m) | |
}) | |
port := 53 | |
server := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: "udp"} | |
log.Printf("Starting at %d\n", port) | |
err := server.ListenAndServe() | |
defer server.Shutdown() | |
if err != nil { | |
log.Fatalf("Failed to start server: %s\n ", err.Error()) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dig @localhost -p 53 google.com +short |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment