Torben Nehmer
73b7a3d1ee
- Always merge state again, so that we keep the missing information from the not updated protocol
150 lines
4.2 KiB
Go
150 lines
4.2 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
)
|
|
|
|
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) MergeOldStateFile() {
|
|
filename := ur.Config.GetStateFileName()
|
|
|
|
if _, err := os.Stat(filename); err != nil {
|
|
log.Printf("No old state file %s found, skip merging: %v", filename, err)
|
|
}
|
|
|
|
// We have a state file present, load it and merge into current state
|
|
// so that we don't loose any IPv4/v6 data during partial updates.
|
|
|
|
oldState, err := LoadStateFile(ur.Config)
|
|
if err != nil {
|
|
log.Printf("Failed to load old state file %s to merge while saving, skipping it: %v",
|
|
filename, err)
|
|
return
|
|
}
|
|
|
|
if ur.IPv4 == nil {
|
|
ur.IPv4 = oldState.IPv4
|
|
}
|
|
if ur.IPv6 == nil {
|
|
ur.IPv6 = oldState.IPv6
|
|
ur.IPv6Net = oldState.IPv6Net
|
|
}
|
|
}
|
|
|
|
func (ur *UpdateRequest) SaveStateFile() error {
|
|
ur.MergeOldStateFile()
|
|
|
|
filename := ur.Config.GetStateFileName()
|
|
data, err := json.MarshalIndent(ur, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to convert state for user %s to JSON: %v",
|
|
ur.UserName, err)
|
|
}
|
|
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 ur.IPv6 != nil {
|
|
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 != "" && ur.IPv6 != nil {
|
|
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 != "" && ur.IPv4 != nil {
|
|
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
|
|
}
|