Replaced ad-hoc printf with log levels
This commit is contained in:
1 parent
d3d07deec6
commit
337919d741
6 files changed
+38
-35
No files matched your search
+10
-11
@@ -3,7 +3,6 @@ package weavedns
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"github.com/miekg/dns"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -28,60 +27,60 @@ func ListenHttp(domain string, db Zone, port int) {
|
||||
prefix := r.FormValue("routing_prefix")
|
||||
local_ip := r.FormValue("local_ip")
|
||||
if identifier == "" || weave_ipstr == "" || name == "" || prefix == "" || local_ip == "" {
|
||||
log.Printf("Invalid request: %s, %s", r.URL, r.Form)
|
||||
Warning.Printf("Invalid request: %s, %s", r.URL, r.Form)
|
||||
http.Error(w, "Invalid Request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
ip := net.ParseIP(local_ip)
|
||||
if ip == nil {
|
||||
log.Printf("Invalid IP in request: %s", local_ip)
|
||||
Warning.Printf("Invalid IP in request: %s", local_ip)
|
||||
http.Error(w, "Invalid IP in request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
weave_cidr := weave_ipstr + "/" + prefix
|
||||
weave_ip, subnet, err := net.ParseCIDR(weave_cidr)
|
||||
if err != nil {
|
||||
log.Printf("Invalid CIDR in request: %s", weave_cidr)
|
||||
Warning.Printf("Invalid CIDR in request: %s", weave_cidr)
|
||||
http.Error(w, fmt.Sprintf("Invalid CIDR: %s", weave_cidr), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if dns.IsSubDomain(domain, name) {
|
||||
log.Printf("Adding %s (%s) -> %s", name, local_ip, weave_cidr)
|
||||
Info.Printf("Adding %s (%s) -> %s", name, local_ip, weave_cidr)
|
||||
err = db.AddRecord(identifier, name, ip, weave_ip, subnet)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusConflict)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Printf("Ignoring name %s, not in %s", name, domain)
|
||||
Info.Printf("Ignoring name %s, not in %s", name, domain)
|
||||
}
|
||||
case "DELETE":
|
||||
identifier, weave_ipstr, err := parseUrl(r.URL.Path)
|
||||
if identifier == "" || weave_ipstr == "" {
|
||||
log.Printf("Invalid request: %s, %s", r.URL, r.Form)
|
||||
Warning.Printf("Invalid request: %s, %s", r.URL, r.Form)
|
||||
http.Error(w, "Invalid Request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
weave_ip := net.ParseIP(weave_ipstr)
|
||||
if weave_ip == nil {
|
||||
log.Printf("Invalid IP in request: %s", weave_ipstr)
|
||||
Warning.Printf("Invalid IP in request: %s", weave_ipstr)
|
||||
http.Error(w, "Invalid IP in request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
log.Printf("Deleting %s (%s)", identifier, weave_ipstr)
|
||||
Info.Printf("Deleting %s (%s)", identifier, weave_ipstr)
|
||||
err = db.DeleteRecord(identifier, weave_ip)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
default:
|
||||
log.Println("Unexpected http method", r.Method)
|
||||
Warning.Println("Unexpected http method", r.Method)
|
||||
http.Error(w, "Unexpected http method: "+r.Method, http.StatusBadRequest)
|
||||
}
|
||||
})
|
||||
address := fmt.Sprintf(":%d", port)
|
||||
err := http.ListenAndServe(address, nil)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to create http listener: ", err)
|
||||
Error.Fatal("Unable to create http listener: ", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user