Skip to content

Instantly share code, notes, and snippets.

@mrluanma
Created September 14, 2012 15:49
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save mrluanma/3722792 to your computer and use it in GitHub Desktop.
Save mrluanma/3722792 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"github.com/miekg/dns"
"os"
)
var (
remote = flag.String("remote", "8.8.4.4:53", "remote dns address")
local = flag.String("local", ":53", "local listen address")
quiet = flag.Bool("quiet", false, "suppress output")
)
func main() {
flag.Parse()
dns.HandleFunc(".", proxyServe)
failure := make(chan error, 1)
go func(failure chan error) {
failure <- dns.ListenAndServe(*local, "tcp", nil)
}(failure)
go func(failure chan error) {
failure <- dns.ListenAndServe(*local, "udp", nil)
}(failure)
fmt.Println(<-failure)
os.Exit(1)
}
func proxyServe(w dns.ResponseWriter, req *dns.Msg) {
if req.MsgHdr.Response == true { // supposed responses sent to us are bogus
return
}
c := new(dns.Client)
c.Net = "tcp"
m, _, err := c.Exchange(req, *remote)
if !*quiet {
fmt.Println(req)
fmt.Println(m)
}
if err != nil {
fmt.Println(err)
} else {
w.WriteMsg(m)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment