Created
September 14, 2012 15:49
-
-
Save mrluanma/3722792 to your computer and use it in GitHub Desktop.
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 ( | |
"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