Browse Source

Look for country version of a label

Ask Bjørn Hansen 13 years ago
parent
commit
50c04bbdb5
2 changed files with 29 additions and 10 deletions
  1. 22 4
      geodns.go
  2. 7 6
      serve.go

+ 22 - 4
geodns.go

@@ -46,11 +46,29 @@ var (
 	flagrun = flag.Bool("run", false, "run server")
 	flagrun = flag.Bool("run", false, "run server")
 )
 )
 
 
-func (z *Zone) findLabels(s string) *Label {
-	if label, ok := z.Labels[s]; ok {
-		return label
+func (z *Zone) findLabels(s, cc string, qtype uint16) *Label {
+
+	selectors := []string{}
+
+	if len(cc) > 0 {
+		if len(s) > 0 {
+			cc = s + "." + cc
+		}
+		selectors = append(selectors, cc)
+	}
+	// TODO(ask) Add continent, see https://github.com/abh/geodns/issues/1
+	selectors = append(selectors, s)
+
+	for _, name := range selectors {
+		if label, ok := z.Labels[name]; ok {
+			// return the label if it has the right records
+			if label.Records[qtype] != nil {
+				fmt.Println("ALBEL", label)
+				return label
+			}
+		}
 	}
 	}
-	return nil
+	return z.Labels[s]
 }
 }
 
 
 func main() {
 func main() {

+ 7 - 6
serve.go

@@ -18,7 +18,11 @@ func getQuestionName(z *Zone, req *dns.Msg) string {
 var geoIP = setupGeoIP()
 var geoIP = setupGeoIP()
 
 
 func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
 func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
-	logPrintf("[zone %s] incoming %s %s %d from %s\n", z.Origin, req.Question[0].Name, dns.Rr_str[req.Question[0].Qtype], req.MsgHdr.Id, w.RemoteAddr())
+
+	qtype := req.Question[0].Qtype
+
+	logPrintf("[zone %s] incoming %s %s %d from %s\n", z.Origin, req.Question[0].Name,
+		dns.Rr_str[qtype], req.MsgHdr.Id, w.RemoteAddr())
 
 
 	fmt.Printf("ZONE DATA  %#v\n", z)
 	fmt.Printf("ZONE DATA  %#v\n", z)
 
 
@@ -39,7 +43,7 @@ func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
 	m.MsgHdr.Authoritative = true
 	m.MsgHdr.Authoritative = true
 
 
 	// TODO(ask): Function to find appropriate label with records based on the country/continent	
 	// TODO(ask): Function to find appropriate label with records based on the country/continent	
-	labels := z.findLabels(label)
+	labels := z.findLabels(label, *country, qtype)
 	if labels == nil {
 	if labels == nil {
 		// return NXDOMAIN
 		// return NXDOMAIN
 		m.SetRcode(req, dns.RcodeNameError)
 		m.SetRcode(req, dns.RcodeNameError)
@@ -50,7 +54,7 @@ func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
 
 
 	fmt.Println("Has the label, looking for records")
 	fmt.Println("Has the label, looking for records")
 
 
-	if region_rr := labels.Records[req.Question[0].Qtype]; region_rr != nil {
+	if region_rr := labels.Records[qtype]; region_rr != nil {
 		//fmt.Printf("REGION_RR %T %v\n", region_rr, region_rr)
 		//fmt.Printf("REGION_RR %T %v\n", region_rr, region_rr)
 		max := len(region_rr)
 		max := len(region_rr)
 		if max > 4 {
 		if max > 4 {
@@ -78,7 +82,6 @@ func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
 
 
 func setupServer(Zone Zone) func(dns.ResponseWriter, *dns.Msg) {
 func setupServer(Zone Zone) func(dns.ResponseWriter, *dns.Msg) {
 	return func(w dns.ResponseWriter, r *dns.Msg) {
 	return func(w dns.ResponseWriter, r *dns.Msg) {
-		fmt.Println("Going to call serve with", Zone.Origin)
 		serve(w, r, &Zone)
 		serve(w, r, &Zone)
 	}
 	}
 }
 }
@@ -86,8 +89,6 @@ func setupServer(Zone Zone) func(dns.ResponseWriter, *dns.Msg) {
 func runServe(Zones *Zones) {
 func runServe(Zones *Zones) {
 
 
 	for zoneName, Zone := range *Zones {
 	for zoneName, Zone := range *Zones {
-		// BUG(ask) For some reason the closure here gets setup so only the second zone gets used
-		fmt.Printf("Configuring zoneName %s %#v\n", zoneName, Zone)
 		dns.HandleFunc(zoneName, setupServer(*Zone))
 		dns.HandleFunc(zoneName, setupServer(*Zone))
 	}
 	}
 	// Only listen on UDP for now
 	// Only listen on UDP for now