Skip to content

Commit

Permalink
Slightly improved startup by parallelization of tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
lietu committed Nov 20, 2019
1 parent 3fb054f commit ef9c39b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 40 deletions.
10 changes: 9 additions & 1 deletion better-dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
Expand Down Expand Up @@ -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()
}

Expand Down
6 changes: 6 additions & 0 deletions server/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
52 changes: 34 additions & 18 deletions shared/dnsservers_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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()
}
59 changes: 38 additions & 21 deletions shared/dnsservers_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
log "github.com/sirupsen/logrus"
"os/exec"
"strings"
"sync"
)

var interfaces = []string{}
Expand Down Expand Up @@ -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()
}

0 comments on commit ef9c39b

Please sign in to comment.