Skip to content

Commit c82e93c

Browse files
committed
-(dnsforward): fixed sigsegv when protection is disabled
Also, fixed all golint issues ✅ Closes: AdguardTeam#941
1 parent 91a1eb9 commit c82e93c

File tree

6 files changed

+40
-8
lines changed

6 files changed

+40
-8
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ run:
1313
skip-files:
1414
- ".*generated.*"
1515
- dnsfilter/rule_to_regexp.go
16+
- ".*_test.go"
1617

1718

1819
# all available settings of specific linters

dhcpd/check_other_dhcp.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
// CheckIfOtherDHCPServersPresent sends a DHCP request to the specified network interface,
1919
// and waits for a response for a period defined by defaultDiscoverTime
20+
// nolint
2021
func CheckIfOtherDHCPServersPresent(ifaceName string) (bool, error) {
2122
iface, err := net.InterfaceByName(ifaceName)
2223
if err != nil {
@@ -95,16 +96,13 @@ func CheckIfOtherDHCPServersPresent(ifaceName string) (bool, error) {
9596
if c != nil {
9697
defer c.Close()
9798
}
98-
// spew.Dump(c, err)
99-
// spew.Printf("net.ListenUDP returned %v, %v\n", c, err)
10099
if err != nil {
101100
return false, wrapErrPrint(err, "Couldn't listen on :68")
102101
}
103102

104103
// send to 255.255.255.255:67
105104
cm := ipv4.ControlMessage{}
106105
_, err = c.WriteTo(packet, &cm, dstAddr)
107-
// spew.Dump(n, err)
108106
if err != nil {
109107
return false, wrapErrPrint(err, "Couldn't send a packet to %s", dst)
110108
}

dnsfilter/dnsfilter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const (
148148
ReasonRewrite
149149
)
150150

151-
func (i Reason) String() string {
151+
func (r Reason) String() string {
152152
names := []string{
153153
"NotFilteredNotFound",
154154
"NotFilteredWhiteList",
@@ -163,10 +163,10 @@ func (i Reason) String() string {
163163

164164
"Rewrite",
165165
}
166-
if uint(i) >= uint(len(names)) {
166+
if uint(r) >= uint(len(names)) {
167167
return ""
168168
}
169-
return names[i]
169+
return names[r]
170170
}
171171

172172
type dnsFilterContext struct {

dnsforward/dnsforward.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ func (s *Server) handleDNSRequest(p *proxy.Proxy, d *proxy.DNSContext) error {
473473
}
474474

475475
if res.Reason == dnsfilter.ReasonRewrite && len(res.CanonName) != 0 {
476-
477476
d.Req.Question[0] = originalQuestion
478477
d.Res.Question[0] = originalQuestion
479478

@@ -520,7 +519,7 @@ func (s *Server) filterDNSRequest(d *proxy.DNSContext) (*dnsfilter.Result, error
520519
s.RUnlock()
521520

522521
if !protectionEnabled {
523-
return nil, nil
522+
return &dnsfilter.Result{}, nil
524523
}
525524

526525
if host != origHost {

dnsforward/dnsforward_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,39 @@ func TestServer(t *testing.T) {
7878
}
7979
}
8080

81+
func TestServerWithProtectionDisabled(t *testing.T) {
82+
s := createTestServer(t)
83+
s.conf.ProtectionEnabled = false
84+
defer removeDataDir(t)
85+
err := s.Start(nil)
86+
if err != nil {
87+
t.Fatalf("Failed to start server: %s", err)
88+
}
89+
90+
// message over UDP
91+
req := createGoogleATestMessage()
92+
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
93+
client := dns.Client{Net: "udp"}
94+
reply, _, err := client.Exchange(req, addr.String())
95+
if err != nil {
96+
t.Fatalf("Couldn't talk to server %s: %s", addr, err)
97+
}
98+
assertGoogleAResponse(t, reply)
99+
100+
// check query log and stats
101+
log := s.GetQueryLog()
102+
assert.Equal(t, 1, len(log), "Log size")
103+
stats := s.GetStatsTop()
104+
assert.Equal(t, 1, len(stats.Domains), "Top domains length")
105+
assert.Equal(t, 0, len(stats.Blocked), "Top blocked length")
106+
assert.Equal(t, 1, len(stats.Clients), "Top clients length")
107+
108+
err = s.Stop()
109+
if err != nil {
110+
t.Fatalf("DNS server failed to stop: %s", err)
111+
}
112+
}
113+
81114
func TestDotServer(t *testing.T) {
82115
// Prepare the proxy server
83116
_, certPem, keyPem := createServerTLSConfig(t)

home/home.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func Main(version string, channel string) {
6161

6262
// run initializes configuration and runs the AdGuard Home
6363
// run is a blocking method and it won't exit until the service is stopped!
64+
// nolint
6465
func run(args options) {
6566
// config file path can be overridden by command-line arguments:
6667
if args.configFilename != "" {

0 commit comments

Comments
 (0)