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:
120
service/updaterequest.go
Normal file
120
service/updaterequest.go
Normal file
@ -0,0 +1,120 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
type UpdateRequest struct {
|
||||
IPv4 net.IP
|
||||
IPv6 net.IP
|
||||
UserName string
|
||||
Password string `json:"-"`
|
||||
IPv6Net *net.IPNet
|
||||
Config *UserConfig `json:"-"`
|
||||
}
|
||||
|
||||
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 (ur *UpdateRequest) SaveStateFile() error {
|
||||
data, err := json.MarshalIndent(ur, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to convert state for user %s to JSON: %v",
|
||||
ur.UserName, err)
|
||||
}
|
||||
filename := ur.Config.GetStateFileName()
|
||||
if err = ioutil.WriteFile(filename, data, 0640); err != nil {
|
||||
return fmt.Errorf("failed to to write sate file for user %s to file %s: %v",
|
||||
ur.UserName, filename, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadStateFile(uc *UserConfig) (*UpdateRequest, error) {
|
||||
filename := uc.GetStateFileName()
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read state file %s for user %s, %v",
|
||||
uc.UserName, filename, err)
|
||||
}
|
||||
var ur UpdateRequest
|
||||
if err = json.Unmarshal(data, &ur); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal state JSON for user %s: %v",
|
||||
uc.UserName, err)
|
||||
}
|
||||
ur.Config = uc
|
||||
return &ur, nil
|
||||
}
|
||||
|
||||
func (ur *UpdateRequest) Process() error {
|
||||
nfu := NewNFTUpdate()
|
||||
|
||||
if err := 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 := 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 := 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)
|
||||
}
|
||||
|
||||
if err := ur.SaveStateFile(); err != nil {
|
||||
return fmt.Errorf("failed to save state file: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user