Skip to content

Commit

Permalink
Pull request 386: 430-fix-system-hosts-file-path
Browse files Browse the repository at this point in the history
Closes #430.

Squashed commit of the following:

commit 9f9870f
Author: Stanislav Chzhen <[email protected]>
Date:   Fri Dec 27 17:16:17 2024 +0300

    handler: imp docs

commit f80639a
Author: Stanislav Chzhen <[email protected]>
Date:   Fri Dec 27 16:49:41 2024 +0300

    handler: imp tests

commit ef2dd46
Author: Stanislav Chzhen <[email protected]>
Date:   Fri Dec 27 16:37:59 2024 +0300

    handler: imp code

commit 0f4241d
Author: Stanislav Chzhen <[email protected]>
Date:   Fri Dec 27 16:00:08 2024 +0300

    handler: fix ptr

commit 64cc591
Author: Stanislav Chzhen <[email protected]>
Date:   Fri Dec 27 14:22:15 2024 +0300

    all: imp code

commit d16be8a
Author: Stanislav Chzhen <[email protected]>
Date:   Thu Dec 26 22:01:30 2024 +0300

    all: fix system hosts file path
  • Loading branch information
schzhn committed Dec 27, 2024
1 parent ed4be08 commit 712d38b
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 15 deletions.
3 changes: 1 addition & 2 deletions internal/cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/hostsfile"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/osutil"
Expand Down Expand Up @@ -518,7 +517,7 @@ func (conf *configuration) hostsFiles(ctx context.Context, l *slog.Logger) (path
return conf.HostsFiles, nil
}

paths, err = hostsfile.DefaultHostsPaths()
paths, err = proxynetutil.DefaultHostsPaths()
if err != nil {
return nil, fmt.Errorf("getting default hosts files: %w", err)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/handler/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ type messageConstructor interface {
NewCompressedResponse(req *dns.Msg, code int) (resp *dns.Msg)

// NewPTRAnswer creates a new resource record for PTR response with the
// given FQDN and PTR domain.
NewPTRAnswer(fqdn, ptrDomain string) (ans *dns.PTR)
// given FQDN and PTR domain. Arguments must be fully qualified domain
// names.
NewPTRAnswer(fqdn, ptrFQDN string) (ans *dns.PTR)

// NewIPResponse creates a new A/AAAA response message for req with the
// given IP addresses. All IP addresses must be of the same family.
Expand Down Expand Up @@ -48,10 +49,10 @@ func (defaultConstructor) NewCompressedResponse(req *dns.Msg, code int) (resp *d

// NewPTRAnswer implements the [messageConstructor] interface for
// [defaultConstructor].
func (defaultConstructor) NewPTRAnswer(fqdn, ptrDomain string) (ans *dns.PTR) {
func (defaultConstructor) NewPTRAnswer(fqdn, ptrFQDN string) (ans *dns.PTR) {
return &dns.PTR{
Hdr: hdr(fqdn, dns.TypePTR),
Ptr: ptrDomain,
Ptr: dns.Fqdn(ptrFQDN),
}
}

Expand Down
16 changes: 8 additions & 8 deletions internal/handler/default_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func TestDefault_resolveFromHosts(t *testing.T) {
})

const (
domainV4 = "ipv4.domain.example"
domainV6 = "ipv6.domain.example"
fqdnV4 = "ipv4.domain.example."
fqdnV6 = "ipv6.domain.example."
)

var (
Expand All @@ -118,26 +118,26 @@ func TestDefault_resolveFromHosts(t *testing.T) {
}{{
wantAns: &dns.A{
Hdr: dns.RR_Header{
Name: domainV4,
Name: fqdnV4,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 10,
},
A: addrV4.AsSlice(),
},
req: (&dns.Msg{}).SetQuestion(domainV4, dns.TypeA),
req: (&dns.Msg{}).SetQuestion(fqdnV4, dns.TypeA),
name: "success_a",
}, {
wantAns: &dns.AAAA{
Hdr: dns.RR_Header{
Name: domainV6,
Name: fqdnV6,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 10,
},
AAAA: addrV6.AsSlice(),
},
req: (&dns.Msg{}).SetQuestion(domainV6, dns.TypeAAAA),
req: (&dns.Msg{}).SetQuestion(fqdnV6, dns.TypeAAAA),
name: "success_aaaa",
}, {
wantAns: &dns.PTR{
Expand All @@ -147,7 +147,7 @@ func TestDefault_resolveFromHosts(t *testing.T) {
Class: dns.ClassINET,
Ttl: 10,
},
Ptr: domainV4,
Ptr: fqdnV4,
},
req: (&dns.Msg{}).SetQuestion(reversedV4, dns.TypePTR),
name: "success_ptr_v4",
Expand All @@ -159,7 +159,7 @@ func TestDefault_resolveFromHosts(t *testing.T) {
Class: dns.ClassINET,
Ttl: 10,
},
Ptr: domainV6,
Ptr: fqdnV6,
},
req: (&dns.Msg{}).SetQuestion(reversedV6, dns.TypePTR),
name: "success_ptr_v6",
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (h *Default) resolveFromHosts(ctx context.Context, req *dns.Msg) (resp *dns
resp = h.messages.NewCompressedResponse(req, dns.RcodeSuccess)
name = req.Question[0].Name
for _, ptr := range ptrs {
resp.Answer = append(resp.Answer, h.messages.NewPTRAnswer(name, ptr))
resp.Answer = append(resp.Answer, h.messages.NewPTRAnswer(name, dns.Fqdn(ptr)))
}
default:
h.logger.DebugContext(ctx, "no hosts records found", "name", name, "qtype", q.Qtype)
Expand Down
9 changes: 9 additions & 0 deletions internal/netutil/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package netutil

// DefaultHostsPaths returns the slice of default paths to system hosts files.
//
// TODO(s.chzhen): Since [fs.FS] is no longer needed, update the
// [hostsfile.DefaultHostsPaths] from golibs.
func DefaultHostsPaths() (paths []string, err error) {
return defaultHostsPaths()
}
21 changes: 21 additions & 0 deletions internal/netutil/paths_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build unix

package netutil

import "github.com/AdguardTeam/golibs/hostsfile"

// defaultHostsPaths returns default paths to hosts files for UNIX.
func defaultHostsPaths() (paths []string, err error) {
paths, err = hostsfile.DefaultHostsPaths()
if err != nil {
// Should not happen because error is always nil.
panic(err)
}

res := make([]string, 0, len(paths))
for _, p := range paths {
res = append(res, "/"+p)
}

return res, nil
}
22 changes: 22 additions & 0 deletions internal/netutil/paths_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build windows

package netutil

import (
"fmt"
"path"

"golang.org/x/sys/windows"
)

// defaultHostsPaths returns default paths to hosts files for Windows.
func defaultHostsPaths() (paths []string, err error) {
sysDir, err := windows.GetSystemDirectory()
if err != nil {
return []string{}, fmt.Errorf("getting system directory: %w", err)
}

p := path.Join(sysDir, "drivers", "etc", "hosts")

return []string{p}, nil
}

0 comments on commit 712d38b

Please sign in to comment.