|
@@ -2,6 +2,9 @@ package functions
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "os"
|
|
|
"runtime"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -243,9 +246,20 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
|
|
|
|
|
|
func setHostDNS(dns, iface string, windows bool) error {
|
|
|
etchosts := "/etc/hosts"
|
|
|
+ temp := os.TempDir()
|
|
|
+ lockfile := temp + "netclient-lock"
|
|
|
if windows {
|
|
|
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
|
|
|
}
|
|
|
+ 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)
|
|
|
profile, err := parser.ParseProfile(dnsdata)
|
|
|
if err != nil {
|
|
@@ -268,9 +282,20 @@ func setHostDNS(dns, iface string, windows bool) error {
|
|
|
|
|
|
func removeHostDNS(iface string, windows bool) error {
|
|
|
etchosts := "/etc/hosts"
|
|
|
+ temp := os.TempDir()
|
|
|
+ lockfile := temp + "netclient-lock"
|
|
|
if windows {
|
|
|
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
|
|
|
}
|
|
|
+ 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)
|
|
|
if err != nil {
|
|
|
return err
|