First working implementation
- implemented update request processing - separated host and domain name in config (req. for DNS updates) - enhanced NFT update validation - cleaned up example user - cleand up logging in update request - moved appropriate config into user request struct
This commit is contained in:
		@@ -36,23 +36,82 @@ func handleUpdate(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	w.Header().Set("Content-Type", "text/plain")
 | 
			
		||||
	fmt.Fprintln(w, "OK")
 | 
			
		||||
	fmt.Fprintln(w, ur.PrettyPrint())
 | 
			
		||||
	log.Println(ur.PrettyPrint())
 | 
			
		||||
	log.Println(ur.IPv6Net.IP)
 | 
			
		||||
	log.Println(ur.IPv6Net.Mask)
 | 
			
		||||
	log.Printf("Request PW: %s, Config PW: %s", ur.Password, uc.PassWord)
 | 
			
		||||
	fmt.Fprintln(w, "Unmarshalled userconfig:")
 | 
			
		||||
	fmt.Fprintln(w, uc.PrettyPrint())
 | 
			
		||||
	fmt.Fprintln(w, "WebAPI Config:")
 | 
			
		||||
	fmt.Fprintln(w, C.PrettyPrint())
 | 
			
		||||
	fmt.Fprintln(w, "Service Config:")
 | 
			
		||||
	fmt.Fprintln(w, service.C.PrettyPrint())
 | 
			
		||||
 | 
			
		||||
	for _, other := range uc.Others {
 | 
			
		||||
		log.Printf("Other %s, IID %v, UR V6NEt: %v, merged: %v",
 | 
			
		||||
			other.DNS, other.V6IID, ur.IPv6Net,
 | 
			
		||||
			other.ConvertIIDToAddress(ur.IPv6Net))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
	}
 | 
			
		||||
	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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,8 @@ import (
 | 
			
		||||
	"log"
 | 
			
		||||
	"net"
 | 
			
		||||
	"net/url"
 | 
			
		||||
 | 
			
		||||
	"gitea.nehmer.net/torben/dyndns/service"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type UpdateRequest struct {
 | 
			
		||||
@@ -15,6 +17,7 @@ type UpdateRequest struct {
 | 
			
		||||
	UserName string
 | 
			
		||||
	Password string
 | 
			
		||||
	IPv6Net  *net.IPNet
 | 
			
		||||
	Config   *service.UserConfig
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (ur *UpdateRequest) String() string {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user