diff --git a/better-dns.go b/better-dns.go index 73d1f7d..43b10c9 100644 --- a/better-dns.go +++ b/better-dns.go @@ -12,6 +12,7 @@ import ( "os/signal" "strconv" "strings" + "sync" "syscall" "time" ) @@ -91,10 +92,17 @@ func loadLists() { "https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext", } + wg := &sync.WaitGroup{} for i := range urls { - blockFromURL(urls[i]) + wg.Add(1) + go func(url string) { + blockFromURL(url) + wg.Done() + }(urls[i]) } + wg.Wait() + server.LogLists() } diff --git a/server/filter.go b/server/filter.go index 26ed64f..7a43ba9 100644 --- a/server/filter.go +++ b/server/filter.go @@ -5,10 +5,12 @@ import ( "github.com/miekg/dns" log "github.com/sirupsen/logrus" "net" + "sync" ) var blockedEntries = map[string]*shared.BlockEntry{} var listEntries = map[string]int64{} +var blockListMutex = &sync.Mutex{} func filter(req *dns.Msg) *shared.BlockEntry { question := req.Question[0] @@ -63,6 +65,10 @@ func AddBlockedEntry(name string, list string) { name = name + "." } + // Called from multiple goroutines so making the map and list processing safe + blockListMutex.Lock() + defer blockListMutex.Unlock() + blockedEntries[name] = &shared.BlockEntry{Src: list} old, ok := listEntries[list] diff --git a/shared/dnsservers_darwin.go b/shared/dnsservers_darwin.go index a8abda6..59f5657 100644 --- a/shared/dnsservers_darwin.go +++ b/shared/dnsservers_darwin.go @@ -21,7 +21,7 @@ func RememberDnsServers() { for _, line := range lines { line := strings.TrimSpace(line) - // Configuration for interface "Local Area Connection* 1" + // Hardware Port: Ethernet if strings.HasPrefix(line, "Hardware Port: ") { parts := strings.SplitN(line, ": ", 2) interfaces = append(interfaces, parts[1]) @@ -32,28 +32,44 @@ func RememberDnsServers() { } func UpdateDnsServers() { + wg := &sync.WaitGroup{} for _, iface := range interfaces { - cmd := exec.Command("networksetup", "-setdnsservers", iface, "127.0.0.1") - output, err := cmd.CombinedOutput() - log.Error(strings.TrimSpace(string(output[:]))) - if err != nil { - log.Errorf("Error setting %s DNS servers: %s", iface, err) - } else { - log.Debugf("%s now using 127.0.0.1 for DNS", iface) - } + wg.Add(1) + + go func(iface string) { + cmd := exec.Command("networksetup", "-setdnsservers", iface, "127.0.0.1") + output, err := cmd.CombinedOutput() + log.Error(strings.TrimSpace(string(output[:]))) + if err != nil { + log.Errorf("Error setting %s DNS servers: %s", iface, err) + } else { + log.Debugf("%s now using 127.0.0.1 for DNS", iface) + } + wg.Done() + }(iface) } + + wg.Wait() } func RestoreDnsServers() { + wg := &sync.WaitGroup{} for _, iface := range interfaces { - cmd := exec.Command("networksetup", "-setdnsservers", iface, "empty") - output, err := cmd.CombinedOutput() - if err != nil { - log.Errorf("Error restoring %s DNS servers: %s", iface, err) - log.Errorf("Try manually with: networksetup -setdnsservers %s empty", iface) - log.Error(strings.TrimSpace(string(output[:]))) - } else { - log.Debugf("%s now using DNS servers set by DHCP", iface) - } + wg.Add(1) + + go func(iface string) { + cmd := exec.Command("networksetup", "-setdnsservers", iface, "empty") + output, err := cmd.CombinedOutput() + if err != nil { + log.Errorf("Error restoring %s DNS servers: %s", iface, err) + log.Errorf("Try manually with: networksetup -setdnsservers %s empty", iface) + log.Error(strings.TrimSpace(string(output[:]))) + } else { + log.Debugf("%s now using DNS servers set by DHCP", iface) + } + wg.Done() + }(iface) } + + wg.Wait() } diff --git a/shared/dnsservers_windows.go b/shared/dnsservers_windows.go index 1075694..16e4b26 100644 --- a/shared/dnsservers_windows.go +++ b/shared/dnsservers_windows.go @@ -5,6 +5,7 @@ import ( log "github.com/sirupsen/logrus" "os/exec" "strings" + "sync" ) var interfaces = []string{} @@ -43,32 +44,48 @@ func RememberDnsServers() { } func UpdateDnsServers() { + wg := &sync.WaitGroup{} for _, iface := range interfaces { - iface = fmt.Sprintf("\"%s\"", iface) - ps := fmt.Sprintf("Set-DnsClientServerAddress -InterfaceAlias %s -ServerAddresses (\"127.0.0.1\")", iface) - cmd := exec.Command("powershell.exe", "-Command", ps) - output, err := cmd.CombinedOutput() - log.Error(strings.TrimSpace(string(output[:]))) - if err != nil { - log.Errorf("Error setting %s DNS servers: %s", iface, err) - } else { - log.Debugf("%s now using 127.0.0.1 for DNS", iface) - } + wg.Add(1) + + go func(iface string) { + iface = fmt.Sprintf("\"%s\"", iface) + ps := fmt.Sprintf("Set-DnsClientServerAddress -InterfaceAlias %s -ServerAddresses (\"127.0.0.1\")", iface) + cmd := exec.Command("powershell.exe", "-Command", ps) + output, err := cmd.CombinedOutput() + if err != nil { + log.Errorf("Error setting %s DNS servers: %s", iface, err) + log.Errorf("Output was: %s", output) + } else { + log.Debugf("%s now using 127.0.0.1 for DNS", iface) + } + wg.Done() + }(iface) } + + wg.Wait() } func RestoreDnsServers() { + wg := &sync.WaitGroup{} for _, iface := range interfaces { - iface = fmt.Sprintf("\"%s\"", iface) - ps := fmt.Sprintf("Set-DnsClientServerAddress -InterfaceAlias %s -ResetServerAddresses", iface) - cmd := exec.Command("powershell.exe", "-Command", ps) - output, err := cmd.CombinedOutput() - if err != nil { - log.Errorf("Error restoring %s DNS servers: %s", iface, err) - log.Errorf("Try manually with: netsh interface ipv4 set dnsservers name=%s source=dhcp", iface) - log.Error(strings.TrimSpace(string(output[:]))) - } else { - log.Debugf("%s now using DNS servers set by DHCP", iface) - } + wg.Add(1) + + go func(iface string) { + iface = fmt.Sprintf("\"%s\"", iface) + ps := fmt.Sprintf("Set-DnsClientServerAddress -InterfaceAlias %s -ResetServerAddresses", iface) + cmd := exec.Command("powershell.exe", "-Command", ps) + output, err := cmd.CombinedOutput() + if err != nil { + log.Errorf("Error restoring %s DNS servers: %s", iface, err) + log.Errorf("Try manually with: netsh interface ipv4 set dnsservers name=%s source=dhcp", iface) + log.Error(strings.TrimSpace(string(output[:]))) + } else { + log.Debugf("%s now using DNS servers set by DHCP", iface) + } + wg.Done() + }(iface) } + + wg.Wait() }