Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -ip flag to specify resolve ip address #3

Merged
merged 2 commits into from Dec 29, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions main.go
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/miekg/dns"
)

var resolveIP net.IP

func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
q := r.Question[0]

Expand All @@ -19,7 +21,7 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
m.SetReply(r)
a := new(dns.A)
a.Hdr = dns.RR_Header{Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 600}
a.A = net.IPv4(127, 0, 0, 1)
a.A = resolveIP
m.Answer = []dns.RR{a}
w.WriteMsg(m)
log.Printf("%s (RESOLVED)\n", info)
Expand All @@ -34,10 +36,23 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg) {

func main() {
var addr = flag.String("addr", "127.0.0.1:5300", "listen address")
var ip = flag.String("ip", "127.0.0.1", "resolve ipv4 address")

flag.Parse()

resolveIP = net.ParseIP(*ip)

if resolveIP == nil {
log.Fatalf("Invalid ip address: %s\n", *ip)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add

if resolveIp.To4() == nil {
  log.Fatalf("Invalid ipv4 address: %s\n", *ip)
}


if resolveIP.To4() == nil {
log.Fatalf("Invalid ipv4 address: %s\n", *ip)
}

server := &dns.Server{Addr: *addr, Net: "udp"}
server.Handler = dns.HandlerFunc(handleRequest)
log.Printf("Listening on %s\n", *addr)

log.Printf("Listening on %s, resolving to %s\n", *addr, *ip)
log.Fatal(server.ListenAndServe())
}