2024-06-13 13:24:24 +02:00
|
|
|
//go:build (linux && !android) || freebsd
|
2024-01-30 09:58:56 +01:00
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
2024-01-30 09:58:56 +01:00
|
|
|
)
|
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
type ShutdownState struct {
|
|
|
|
ManagerType osManagerType
|
|
|
|
DNSAddress netip.Addr
|
|
|
|
WgIface string
|
|
|
|
}
|
2024-01-30 09:58:56 +01:00
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
func (s *ShutdownState) Name() string {
|
|
|
|
return "dns_state"
|
|
|
|
}
|
2024-01-30 09:58:56 +01:00
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
func (s *ShutdownState) Cleanup() error {
|
|
|
|
manager, err := newHostManagerFromType(s.WgIface, s.ManagerType)
|
2024-01-30 09:58:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create previous host manager: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
if err := manager.restoreUncleanShutdownDNS(&s.DNSAddress); err != nil {
|
|
|
|
return fmt.Errorf("restore unclean shutdown dns: %w", err)
|
2024-01-30 09:58:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
// TODO: move file contents to state manager
|
|
|
|
func createUncleanShutdownIndicator(sourcePath string, dnsAddressStr string, stateManager *statemanager.Manager) error {
|
|
|
|
dnsAddress, err := netip.ParseAddr(dnsAddressStr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parse dns address %s: %w", dnsAddressStr, err)
|
|
|
|
}
|
|
|
|
|
2024-01-30 09:58:56 +01:00
|
|
|
dir := filepath.Dir(fileUncleanShutdownResolvConfLocation)
|
|
|
|
if err := os.MkdirAll(dir, os.FileMode(0755)); err != nil {
|
|
|
|
return fmt.Errorf("create dir %s: %w", dir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := copyFile(sourcePath, fileUncleanShutdownResolvConfLocation); err != nil {
|
|
|
|
return fmt.Errorf("create %s: %w", sourcePath, err)
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
state := &ShutdownState{
|
|
|
|
ManagerType: fileManager,
|
|
|
|
DNSAddress: dnsAddress,
|
2024-01-30 09:58:56 +01:00
|
|
|
}
|
2024-10-24 10:53:46 +02:00
|
|
|
if err := stateManager.UpdateState(state); err != nil {
|
|
|
|
return fmt.Errorf("update state: %w", err)
|
2024-01-30 09:58:56 +01:00
|
|
|
}
|
2024-10-24 10:53:46 +02:00
|
|
|
|
2024-01-30 09:58:56 +01:00
|
|
|
return nil
|
|
|
|
}
|