2021-08-23 18:41:32 +00:00
|
|
|
package webapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"gitea.nehmer.net/torben/dyndns/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
router.HandleFunc("/update", handleUpdate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleUpdate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ur, err := createUpdateRequestFromForm(r.Form)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uc, err := service.LoadConfigForUser(ur.UserName, ur.Password)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*service.UnauthorizedError); ok {
|
|
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
|
|
} else {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
log.Printf("Authorization failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:44:52 +00:00
|
|
|
ur.Config = uc
|
|
|
|
|
|
|
|
// Start DEBUG Output
|
|
|
|
log.Println("Final request")
|
|
|
|
log.Println(ur.PrettyPrint())
|
|
|
|
for _, other := range uc.Others {
|
|
|
|
log.Printf("Other %s, IID %v, merged: %v",
|
|
|
|
other.Hostname, other.V6IID, other.ConvertIIDToAddress(ur.IPv6Net))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("WebAPI Config:")
|
|
|
|
log.Println(C.PrettyPrint())
|
|
|
|
log.Println("Service Config:")
|
|
|
|
log.Println(service.C.PrettyPrint())
|
|
|
|
// End DEBUG Output
|
|
|
|
|
|
|
|
if err := processUpdateRequest(ur); err != nil {
|
|
|
|
log.Printf("failed to process UpdateRequest: %v", err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2021-08-23 18:41:32 +00:00
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
fmt.Fprintln(w, "OK")
|
2021-08-24 14:44:52 +00:00
|
|
|
}
|
2021-08-23 18:41:32 +00:00
|
|
|
|
2021-08-24 14:44:52 +00:00
|
|
|
func processUpdateRequest(ur *UpdateRequest) error {
|
|
|
|
nfu := service.NewNFTUpdate()
|
|
|
|
|
|
|
|
if err := service.DNSUpdateEntry(ur.Config.Domain, ur.Config.Router.Hostname, ur.IPv4, ur.IPv6); err != nil {
|
|
|
|
return fmt.Errorf("failed to update router DNS: %v", err)
|
2021-08-23 18:41:32 +00:00
|
|
|
}
|
2021-08-24 14:44:52 +00:00
|
|
|
if ur.Config.Router.NFT.Table != "" {
|
|
|
|
if ur.IPv4 != nil && ur.Config.Router.NFT.Set4 != "" {
|
|
|
|
if err := nfu.AddIP(ur.Config.Router.NFT.Table, ur.Config.Router.NFT.Set4, ur.IPv4); err != nil {
|
|
|
|
return fmt.Errorf("failed to update IPv4 Router NFT setup: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ur.IPv6 != nil && ur.Config.Router.NFT.Set6 != "" {
|
|
|
|
if err := nfu.AddIP(ur.Config.Router.NFT.Table, ur.Config.Router.NFT.Set6, ur.IPv6); err != nil {
|
|
|
|
return fmt.Errorf("failed to update IPv6 Router NFT setup: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, other := range ur.Config.Others {
|
|
|
|
fullV6IP := other.ConvertIIDToAddress(ur.IPv6Net)
|
|
|
|
if other.RegisterV4 {
|
|
|
|
if err := service.DNSUpdateEntry(ur.Config.Domain, other.Hostname, ur.IPv4, fullV6IP); err != nil {
|
|
|
|
return fmt.Errorf("failed to update DNS for host %s: %v", other.Hostname, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := service.DNSUpdateEntry(ur.Config.Domain, other.Hostname, nil, fullV6IP); err != nil {
|
|
|
|
return fmt.Errorf("failed to update DNS for host %s: %v", other.Hostname, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if other.NFT.Table != "" {
|
|
|
|
if other.NFT.Set6 != "" {
|
|
|
|
if err := nfu.AddIP(other.NFT.Table, other.NFT.Set6, fullV6IP); err != nil {
|
|
|
|
return fmt.Errorf("failed to update IPv6 NFT setup for host %s: %v", other.Hostname, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if other.RegisterV4 && other.NFT.Set4 != "" {
|
|
|
|
if err := nfu.AddIP(other.NFT.Table, other.NFT.Set4, ur.IPv4); err != nil {
|
|
|
|
return fmt.Errorf("failed to update IPv6 NFT setup for host %s: %v", other.Hostname, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Summary of collected NFT Updates:")
|
|
|
|
log.Println(nfu.PrettyPrint())
|
|
|
|
|
|
|
|
if err := nfu.Process(); err != nil {
|
|
|
|
return fmt.Errorf("failed to update NFT Setup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-08-23 18:41:32 +00:00
|
|
|
}
|