Merge pull request #1401 from gravitl/bugfix_v0.14.5_etc_hosts_lockfile

Bugfix v0.14.5 etc hosts lockfile
This commit is contained in:
dcarns 2022-07-21 12:14:21 -04:00 committed by GitHub
commit fde9aa041a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,9 @@ package functions
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt"
"os"
"runtime" "runtime"
"strings" "strings"
"time" "time"
@ -243,9 +246,21 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
func setHostDNS(dns, iface string, windows bool) error { func setHostDNS(dns, iface string, windows bool) error {
etchosts := "/etc/hosts" etchosts := "/etc/hosts"
temp := os.TempDir()
lockfile := temp + "/netclient-lock"
if windows { if windows {
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts" etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
lockfile = temp + "\\netclient-lock"
} }
if _, err := os.Stat(lockfile); !errors.Is(err, os.ErrNotExist) {
return errors.New("/etc/hosts file is locked .... aborting")
}
lock, err := os.Create(lockfile)
if err != nil {
return fmt.Errorf("could not create lock file %w", err)
}
lock.Close()
defer os.Remove(lockfile)
dnsdata := strings.NewReader(dns) dnsdata := strings.NewReader(dns)
profile, err := parser.ParseProfile(dnsdata) profile, err := parser.ParseProfile(dnsdata)
if err != nil { if err != nil {
@ -268,9 +283,21 @@ func setHostDNS(dns, iface string, windows bool) error {
func removeHostDNS(iface string, windows bool) error { func removeHostDNS(iface string, windows bool) error {
etchosts := "/etc/hosts" etchosts := "/etc/hosts"
temp := os.TempDir()
lockfile := temp + "/netclient-lock"
if windows { if windows {
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts" etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
lockfile = temp + "\\netclient-lock"
} }
if _, err := os.Stat(lockfile); !errors.Is(err, os.ErrNotExist) {
return errors.New("/etc/hosts file is locked .... aborting")
}
lock, err := os.Create(lockfile)
if err != nil {
return fmt.Errorf("could not create lock file %w", err)
}
lock.Close()
defer os.Remove(lockfile)
hosts, err := file.NewFile(etchosts) hosts, err := file.NewFile(etchosts)
if err != nil { if err != nil {
return err return err