Changed to new URL scheme with container identifier
Implementation matches design.md better: - http action changes from POST /add to PUT /name - each record now has the container identifier
This commit is contained in:
1 parent
50d343e2fb
commit
6bff651e87
4 files changed
+49
-20
No files matched your search
+31
-13
@@ -1,30 +1,48 @@
|
||||
package weavedns
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Parse a URL of the form PUT /name/<identifier>/<ip-address>
|
||||
func parseUrl(url string) (identifier string, ipaddr string, err error) {
|
||||
parts := strings.Split(url, "/")
|
||||
if len(parts) != 4 {
|
||||
return "", "", errors.New("Unable to parse url: " + url)
|
||||
}
|
||||
return parts[2], parts[3], nil
|
||||
}
|
||||
|
||||
func ListenHttp(db Zone, port int) {
|
||||
http.HandleFunc("/add", func(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.FormValue("name")
|
||||
addr := r.FormValue("ip")
|
||||
weave_cidr := r.FormValue("weave_cidr")
|
||||
if name != "" && addr != "" && weave_cidr != "" {
|
||||
ip := net.ParseIP(addr)
|
||||
http.HandleFunc("/name/", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "PUT":
|
||||
identifier, weave_ipstr, err := parseUrl(r.URL.Path)
|
||||
name := r.FormValue("fqdn")
|
||||
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)
|
||||
return
|
||||
}
|
||||
ip := net.ParseIP(local_ip)
|
||||
if ip == nil {
|
||||
log.Printf("Invalid IP in request: %s", local_ip)
|
||||
return
|
||||
}
|
||||
weave_cidr := weave_ipstr + "/" + prefix
|
||||
weave_ip, subnet, err := net.ParseCIDR(weave_cidr)
|
||||
if err == nil && ip != nil {
|
||||
log.Printf("Adding %s (%s) -> %s", name, addr, weave_cidr)
|
||||
db.AddRecord(name, ip, weave_ip, subnet)
|
||||
if err == nil {
|
||||
log.Printf("Adding %s (%s) -> %s", name, local_ip, weave_cidr)
|
||||
db.AddRecord(identifier, name, ip, weave_ip, subnet)
|
||||
} else if err != nil {
|
||||
log.Printf("Invalid CIDR in request: %s", weave_cidr)
|
||||
} else {
|
||||
log.Printf("Invalid IP in request: %s", addr)
|
||||
}
|
||||
} else {
|
||||
log.Printf("Invalid request: %s, %s, %s", name, addr, weave_cidr)
|
||||
}
|
||||
})
|
||||
address := fmt.Sprintf(":%d", port)
|
||||
|
||||
Reference in new issue
Block a user