You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gdns/server.go

178 lines
3.2 KiB
Go

9 years ago
/*
gdns is a dns proxy server write by go.
gdns much like dnsmasq or chinadns, but it can run on windows.
Features:
support different domains use different upstream dns servers
support contact to the upstream dns server by tcp or udp
support blacklist list to block the fake ip
Usage:
generate a config file and edit it
$ gdns -dumpflags > dns.ini
run it
$ sudo gdns -config dns.ini
*/
9 years ago
package main
import (
"github.com/miekg/dns"
"log"
)
var client_udp *dns.Client = &dns.Client{}
var client_tcp *dns.Client = &dns.Client{Net: "tcp"}
var Servers []*UpstreamServer = nil
var logger *LogOut = nil
var Blacklist_ips Kv = nil
var debug bool = false
var hostfile string = ""
var record_hosts Hosts = nil
9 years ago
func in_blacklist(m *dns.Msg) bool {
if Blacklist_ips == nil {
return false
}
if m == nil {
return false
}
for _, rr := range m.Answer {
/* A */
9 years ago
if t, ok := rr.(*dns.A); ok {
ip := t.A.String()
if _, ok1 := Blacklist_ips[ip]; ok1 {
logger.Debug("%s is in blacklist\n", ip)
return true
}
}
/* AAAA */
if t, ok := rr.(*dns.AAAA); ok {
ip := t.AAAA.String()
if _, ok1 := Blacklist_ips[ip]; ok1 {
logger.Debug("%s is in blacklist\n", ip)
return true
}
}
9 years ago
}
9 years ago
return false
}
9 years ago
func handleRoot(w dns.ResponseWriter, r *dns.Msg) {
var err error
var res *dns.Msg
domain := r.Question[0].Name
var done int
/*
reply from hosts
*/
if record_hosts != nil {
rr := record_hosts.Get(domain, r.Question[0].Qtype)
if rr != nil {
msg := new(dns.Msg)
msg.SetReply(r)
msg.Answer = append(msg.Answer, rr)
w.WriteMsg(msg)
logger.Debug("%s query %s %s %s, reply from hosts\n",
w.RemoteAddr(),
domain,
dns.ClassToString[r.Question[0].Qclass],
dns.TypeToString[r.Question[0].Qtype],
)
return
}
}
9 years ago
for i := 0; i < 2; i++ {
done = 0
for _, sv := range Servers {
if sv.match(domain) {
logger.Debug("%s query %s %s %s, forward to %s:%s\n",
w.RemoteAddr(),
9 years ago
domain,
dns.ClassToString[r.Question[0].Qclass],
dns.TypeToString[r.Question[0].Qtype],
sv.Proto, sv.Addr)
res, err = sv.query(r)
if err == nil {
if !in_blacklist(res) && res.Rcode != dns.RcodeServerFailure {
w.WriteMsg(res)
done = 1
break
}
} else {
logger.Error("%s", err)
}
}
}
if done != 1 {
res, err = DefaultServer.query(r)
logger.Debug("%s query %s %s %s, use default server %s:%s\n",
w.RemoteAddr(),
9 years ago
domain,
dns.ClassToString[r.Question[0].Qclass],
dns.TypeToString[r.Question[0].Qtype],
DefaultServer.Proto, DefaultServer.Addr)
if err == nil {
if !in_blacklist(res) && res.Rcode != dns.RcodeServerFailure {
w.WriteMsg(res)
done = 1
}
} else {
logger.Error("%s", err)
}
}
if done == 1 {
break
}
}
if done != 1 {
dns.HandleFailed(w, r)
}
}
func main() {
parse_flags()
dns.HandleFunc(".", handleRoot)
logger = NewLogger(logfile, debug)
logger.Info("Listen on %s\n", bind_addr)
go func() {
/* listen tcp */
err := dns.ListenAndServe(bind_addr, "tcp", nil)
if err != nil {
log.Fatal(err)
}
}()
/* listen udp */
9 years ago
err := dns.ListenAndServe(bind_addr, "udp", nil)
if err != nil {
log.Fatal(err)
}
}