Fix as212 server

This commit is contained in:
Miek Gieben 2013-06-22 21:40:19 +01:00
parent 93c0500dce
commit baa7ca4a82
3 changed files with 38 additions and 43 deletions

View File

@ -12,6 +12,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"runtime"
"syscall" "syscall"
) )
@ -45,6 +46,17 @@ func NewRR(s string) dns.RR {
} }
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()*2 + 1)
for z, rr := range zones {
rrx := rr.(*dns.SOA) // Some foo needed to created actual RR, on the not a reference
dns.HandleFunc(z, func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.Ns = []dns.RR{rrx}
w.WriteMsg(m)
})
}
go func() { go func() {
err := dns.ListenAndServe(":8053", "tcp", nil) err := dns.ListenAndServe(":8053", "tcp", nil)
if err != nil { if err != nil {
@ -57,18 +69,8 @@ func main() {
log.Fatal("Failed to set tcp listener %s\n", err.Error()) log.Fatal("Failed to set tcp listener %s\n", err.Error())
} }
}() }()
for z, rr := range zones {
dns.HandleFunc(z, func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.Ns = []dns.RR{rr}
w.WriteMsg(m)
})
}
sig := make(chan os.Signal) sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for { for {
select { select {
case s := <-sig: case s := <-sig:

View File

@ -3,12 +3,12 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Reflect is a small name server which sends back the IP address of its client, the // Reflect is a small name server which sends back the IP address of its client, the
// recursive resolver. // recursive resolver.
// When queried for type A (resp. AAAA), it sends back the IPv4 (resp. v6) address. // When queried for type A (resp. AAAA), it sends back the IPv4 (resp. v6) address.
// In the additional section the port number and transport are shown. // In the additional section the port number and transport are shown.
// //
// Basic use pattern: // Basic use pattern:
// //
// dig @localhost -p 8053 whoami.miek.nl A // dig @localhost -p 8053 whoami.miek.nl A
// //
// ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2157 // ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2157
@ -25,9 +25,9 @@
// Similar services: whoami.ultradns.net, whoami.akamai.net. Also (but it // Similar services: whoami.ultradns.net, whoami.akamai.net. Also (but it
// is not their normal goal): rs.dns-oarc.net, porttest.dns-oarc.net, // is not their normal goal): rs.dns-oarc.net, porttest.dns-oarc.net,
// amiopen.openresolvers.org. // amiopen.openresolvers.org.
// //
// Original version is from: Stephane Bortzmeyer <stephane+grong@bortzmeyer.org>. // Original version is from: Stephane Bortzmeyer <stephane+grong@bortzmeyer.org>.
// //
// Adapted to Go (i.e. completely rewritten) by Miek Gieben <miek@miek.nl>. // Adapted to Go (i.e. completely rewritten) by Miek Gieben <miek@miek.nl>.
package main package main
@ -39,6 +39,7 @@ import (
"net" "net"
"os" "os"
"os/signal" "os/signal"
"runtime"
"runtime/pprof" "runtime/pprof"
"strconv" "strconv"
"strings" "strings"
@ -77,14 +78,14 @@ func handleReflect(w dns.ResponseWriter, r *dns.Msg) {
} }
/* /*
if o := r.IsEdns0(); o != nil { if o := r.IsEdns0(); o != nil {
for _, s := range o.Option { for _, s := range o.Option {
switch e := s.(type) { switch e := s.(type) {
case *dns.EDNS0_SUBNET: case *dns.EDNS0_SUBNET:
log.Printf("Edns0 subnet %s", e.Address) log.Printf("Edns0 subnet %s", e.Address)
}
} }
} }
}
*/ */
if v4 { if v4 {
@ -160,6 +161,7 @@ func serve(net, name, secret string) {
} }
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()*2 + 1)
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file") cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
printf = flag.Bool("print", false, "print replies") printf = flag.Bool("print", false, "print replies")
compress = flag.Bool("compress", false, "compress replies") compress = flag.Bool("compress", false, "compress replies")

View File

@ -167,31 +167,22 @@ func ListenAndServe(addr string, network string, handler Handler) error {
func (mux *ServeMux) match(q string, t uint16) Handler { func (mux *ServeMux) match(q string, t uint16) Handler {
mux.m.RLock() mux.m.RLock()
defer mux.m.RUnlock() defer mux.m.RUnlock()
var ( var handler Handler
handler Handler off := 0
lastdot int = -1 end := false
lastbyte byte for {
seendot bool = true if h, ok := mux.z[q[off:]]; ok {
) if t != TypeDS {
for i := 0; i < len(q); i++ { return h
if seendot { } else {
if h, ok := mux.z[q[lastdot+1:]]; ok { // Continue for DS to see if we have a parent too, if so delegeate to the parent
if t != TypeDS { handler = h
return h
} else {
// Continue for DS to see if we have a parent too, if so delegeate to the parent
handler = h
}
} }
} }
off, end = NextLabel(q, off)
if q[i] == '.' && lastbyte != '\\' { if end {
lastdot = i break
seendot = true
} else {
seendot = false
} }
lastbyte = q[i]
} }
// Wildcard match, if we have found nothing try the root zone as a last resort. // Wildcard match, if we have found nothing try the root zone as a last resort.
if h, ok := mux.z["."]; ok { if h, ok := mux.z["."]; ok {