Added state perstistance

- Define new state directory to hold the last dns update config and push it into the config, default to state/
- Refactoring, move updaterequest into service and out of webapp. Keep only the form parsing there. Specifically, move the processing code into the service package, it shouldn't be in the webapp.
- Add code to load and save the last updaterequest into a state file (watch out to keep the possibly unhashed password out of it).
- Add a command line option to load all available state files given the current configured user list from the command line.
- Refactor user configuartion loading to separate it from user authentication, claen up the code.
- We now require the username in the config, to make handling it in state updates easier. This will allow easier transition to DB style storage anyway.
- Update .gitignore
This commit is contained in:
2021-09-18 15:38:49 +02:00
parent 9952343cda
commit a23043ba5f
9 changed files with 255 additions and 129 deletions

View File

@ -1,9 +1,12 @@
package webapi
import (
"errors"
"fmt"
"log"
"net"
"net/http"
"net/url"
"gitea.nehmer.net/torben/dyndns/service"
)
@ -52,7 +55,7 @@ func handleUpdate(w http.ResponseWriter, r *http.Request) {
log.Println(service.C.PrettyPrint())
// End DEBUG Output
if err := processUpdateRequest(ur); err != nil {
if err := ur.Process(); err != nil {
log.Printf("failed to process UpdateRequest: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -61,57 +64,38 @@ func handleUpdate(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
}
func processUpdateRequest(ur *UpdateRequest) error {
nfu := service.NewNFTUpdate()
func createUpdateRequestFromForm(form url.Values) (*service.UpdateRequest, error) {
ur := &service.UpdateRequest{}
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)
}
if form.Get("IPv4") != "" {
if ur.IPv4 = net.ParseIP(form.Get("IPv4")); ur.IPv4 == nil {
return nil, errors.New("could not parse IPv4 address")
}
}
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)
}
}
if form.Get("IPv6") != "" {
if ur.IPv6 = net.ParseIP(form.Get("IPv6")); ur.IPv6 == nil {
return nil, errors.New("could not parse IPv6 address")
}
}
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)
ur.UserName = form.Get("UserName")
if ur.UserName == "" {
return nil, errors.New("a UserName must be specified")
}
return nil
ur.Password = form.Get("Password")
if ur.Password == "" {
return nil, errors.New("a Password must be specified")
}
if ip6net := form.Get("IPv6Net"); ip6net != "" {
_, ipnet, err := net.ParseCIDR(ip6net)
if err != nil {
return nil, errors.New("could not parse IPv6Net")
}
ur.IPv6Net = ipnet
}
return ur, nil
}

View File

@ -1,70 +0,0 @@
package webapi
import (
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/url"
"gitea.nehmer.net/torben/dyndns/service"
)
type UpdateRequest struct {
IPv4 net.IP
IPv6 net.IP
UserName string
Password string
IPv6Net *net.IPNet
Config *service.UserConfig
}
func (ur *UpdateRequest) String() string {
return fmt.Sprintf("IPv4: %v, IPv6: %v, UserName: %v, Password: %v, IPv6Net: %v",
ur.IPv4, ur.IPv6, ur.UserName, ur.Password, ur.IPv6Net)
}
func (ur *UpdateRequest) PrettyPrint() string {
s, err := json.MarshalIndent(ur, "", " ")
if err != nil {
log.Fatalf("Failed to pretty print UpdateRequest via JSON: %v", err)
}
return string(s)
}
func createUpdateRequestFromForm(form url.Values) (*UpdateRequest, error) {
ur := &UpdateRequest{}
if form.Get("IPv4") != "" {
if ur.IPv4 = net.ParseIP(form.Get("IPv4")); ur.IPv4 == nil {
return nil, errors.New("could not parse IPv4 address")
}
}
if form.Get("IPv6") != "" {
if ur.IPv6 = net.ParseIP(form.Get("IPv6")); ur.IPv6 == nil {
return nil, errors.New("could not parse IPv6 address")
}
}
ur.UserName = form.Get("UserName")
if ur.UserName == "" {
return nil, errors.New("a UserName must be specified")
}
ur.Password = form.Get("Password")
if ur.Password == "" {
return nil, errors.New("a Password must be specified")
}
if ip6net := form.Get("IPv6Net"); ip6net != "" {
_, ipnet, err := net.ParseCIDR(ip6net)
if err != nil {
return nil, errors.New("could not parse IPv6Net")
}
ur.IPv6Net = ipnet
}
return ur, nil
}