mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-07 20:38:51 +08:00
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:
commit
fde9aa041a
1 changed files with 27 additions and 0 deletions
|
@ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue