Skip to content

Commit

Permalink
all: imp code, skel
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Aug 19, 2024
1 parent 76d3cd6 commit 8fec401
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
19 changes: 9 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Makefile. Bump this number every time a significant change is made to
# this Makefile.
#
# AdGuard-Project-Version: 5
# AdGuard-Project-Version: 6

# Don't name these macros "GO" etc., because GNU Make apparently makes
# them exported environment variables with the literal value of
Expand All @@ -33,13 +33,12 @@ VERSION = 0

ENV = env\
BRANCH="$(BRANCH)"\
COMMIT='$(COMMIT)'\
DIST_DIR='$(DIST_DIR)'\
GO="$(GO.MACRO)"\
GOAMD64='$(GOAMD64)'\
GOPROXY='$(GOPROXY)'\
GOTOOLCHAIN='$(GOTOOLCHAIN)'\
GOTELEMETRY='$(GOTELEMETRY)'\
GOTOOLCHAIN='$(GOTOOLCHAIN)'\
OUT='$(OUT)'\
PATH="$${PWD}/bin:$$( "$(GO.MACRO)" env GOPATH )/bin:$${PATH}"\
RACE='$(RACE)'\
Expand All @@ -62,13 +61,13 @@ init: ; git config core.hooksPath ./scripts/hooks

test: go-test

go-build: ; $(ENV) "$(SHELL)" ./scripts/make/go-build.sh
go-deps: ; $(ENV) "$(SHELL)" ./scripts/make/go-deps.sh
go-lint: ; $(ENV) "$(SHELL)" ./scripts/make/go-lint.sh
go-test: ; $(ENV) RACE='1' "$(SHELL)" ./scripts/make/go-test.sh
go-tools: ; $(ENV) "$(SHELL)" ./scripts/make/go-tools.sh

go-upd-tools: ; $(ENV) "$(SHELL)" ./scripts/make/go-upd-tools.sh
go-build: ; $(ENV) "$(SHELL)" ./scripts/make/go-build.sh
go-deps: ; $(ENV) "$(SHELL)" ./scripts/make/go-deps.sh
go-env: ; $(ENV) "$(GO.MACRO)" env
go-lint: ; $(ENV) "$(SHELL)" ./scripts/make/go-lint.sh
go-test: ; $(ENV) RACE='1' "$(SHELL)" ./scripts/make/go-test.sh
go-tools: ; $(ENV) "$(SHELL)" ./scripts/make/go-tools.sh
go-upd-tools: ; $(ENV) "$(SHELL)" ./scripts/make/go-upd-tools.sh

go-check: go-tools go-lint go-test

Expand Down
10 changes: 8 additions & 2 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ func createProxyConfig(
l *slog.Logger,
options *Options,
) (conf *proxy.Config, err error) {
reqHdlr, err := handler.NewDefault(ctx, &handler.DefaultConfig{
hostsFiles, err := options.hostsFiles(ctx, l)
if err != nil {
// Don't wrap the error since it's informative enough as is.
return nil, err
}

reqHdlr, err := handler.NewDefault(&handler.DefaultConfig{
Logger: l.With(slogutil.KeyPrefix, "default_handler"),
// TODO(e.burkov): Use the configured message constructor.
MessageConstructor: dnsmsg.DefaultMessageConstructor{},
HaltIPv6: options.IPv6Disabled,
HostsFiles: options.hostsFiles(ctx, l),
HostsFiles: hostsFiles,
FileSystem: osutil.RootDirFS(),
})
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,21 @@ func parseOptions() (opts *Options, exitCode int, err error) {

// hostsFiles returns the list of hosts files to resolve from. It's empty if
// resolving from hosts files is disabled.
func (opts *Options) hostsFiles(ctx context.Context, l *slog.Logger) (paths []string) {
func (opts *Options) hostsFiles(ctx context.Context, l *slog.Logger) (paths []string, err error) {
if !opts.HostsFileEnabled {
return nil
return nil, nil
}

if len(opts.HostsFiles) > 0 {
return opts.HostsFiles
return opts.HostsFiles, nil
}

paths, err := hostsfile.DefaultHostsPaths()
paths, err = hostsfile.DefaultHostsPaths()
if err != nil {
l.ErrorContext(ctx, "getting default hosts files", "err", err)
return nil, fmt.Errorf("getting default hosts files: %w", err)
}

l.DebugContext(ctx, "hosts files are not specified, using default", "paths", paths)

return paths
return paths, nil
}
2 changes: 1 addition & 1 deletion internal/handler/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Default struct {
}

// NewDefault creates a new [Default] handler.
func NewDefault(ctx context.Context, conf *DefaultConfig) (d *Default, err error) {
func NewDefault(conf *DefaultConfig) (d *Default, err error) {
hosts, err := readHosts(conf.FileSystem, conf.HostsFiles)
if err != nil {
return nil, err
Expand Down
14 changes: 6 additions & 8 deletions internal/handler/default_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ func TestDefault_haltAAAA(t *testing.T) {
t.Run("disabled", func(t *testing.T) {
t.Parallel()

ctx := testutil.ContextWithTimeout(t, defaultTimeout)
hdlr, err := NewDefault(ctx, &DefaultConfig{
hdlr, err := NewDefault(&DefaultConfig{
Logger: slogutil.NewDiscardLogger(),
MessageConstructor: messages,
HaltIPv6: false,
FileSystem: testdata,
})
require.NoError(t, err)

ctx = testutil.ContextWithTimeout(t, defaultTimeout)
ctx := testutil.ContextWithTimeout(t, defaultTimeout)

assert.Nil(t, hdlr.haltAAAA(ctx, reqA))
assert.Nil(t, hdlr.haltAAAA(ctx, reqAAAA))
Expand All @@ -70,16 +69,15 @@ func TestDefault_haltAAAA(t *testing.T) {
t.Run("enabled", func(t *testing.T) {
t.Parallel()

ctx := testutil.ContextWithTimeout(t, defaultTimeout)
hdlr, err := NewDefault(ctx, &DefaultConfig{
hdlr, err := NewDefault(&DefaultConfig{
Logger: slogutil.NewDiscardLogger(),
MessageConstructor: messages,
HaltIPv6: true,
FileSystem: testdata,
})
require.NoError(t, err)

ctx = testutil.ContextWithTimeout(t, defaultTimeout)
ctx := testutil.ContextWithTimeout(t, defaultTimeout)

assert.Nil(t, hdlr.haltAAAA(ctx, reqA))
assert.Equal(t, nodataResp, hdlr.haltAAAA(ctx, reqAAAA))
Expand All @@ -92,8 +90,7 @@ func TestDefault_resolveFromHosts(t *testing.T) {
// TODO(e.burkov): Use the one from [dnsproxytest].
messages := dnsmsg.DefaultMessageConstructor{}

ctx := testutil.ContextWithTimeout(t, defaultTimeout)
hdlr, err := NewDefault(ctx, &DefaultConfig{
hdlr, err := NewDefault(&DefaultConfig{
MessageConstructor: messages,
FileSystem: testdata,
Logger: slogutil.NewDiscardLogger(),
Expand Down Expand Up @@ -190,6 +187,7 @@ func TestDefault_resolveFromHosts(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ctx := testutil.ContextWithTimeout(t, defaultTimeout)
resp := hdlr.resolveFromHosts(ctx, tc.req)
if tc.wantAns == nil {
assert.Nil(t, resp)
Expand Down

0 comments on commit 8fec401

Please sign in to comment.