diff --git a/pkg/doh/dial.go b/pkg/doh/dial.go index 55c0c675..84c9e748 100644 --- a/pkg/doh/dial.go +++ b/pkg/doh/dial.go @@ -6,6 +6,7 @@ import ( "net" "sync" + "github.com/qdm12/dns/v2/internal/picker" "github.com/qdm12/dns/v2/internal/server" "github.com/qdm12/dns/v2/pkg/provider" ) @@ -28,7 +29,7 @@ func newDoHDial(settings ResolverSettings) (dial server.Dial) { }, } - picker := settings.Picker + picker := picker.New() return func(ctx context.Context, _, _ string) (conn net.Conn, err error) { // Pick DoH server pseudo-randomly from the chosen providers diff --git a/pkg/doh/interfaces.go b/pkg/doh/interfaces.go index c7452ac7..ab8a071b 100644 --- a/pkg/doh/interfaces.go +++ b/pkg/doh/interfaces.go @@ -2,7 +2,6 @@ package doh import ( "github.com/miekg/dns" - "github.com/qdm12/dns/v2/pkg/provider" ) type Middleware interface { @@ -27,7 +26,3 @@ type Logger interface { type Warner interface { Warn(s string) } - -type Picker interface { - DoHServer(servers []provider.DoHServer) provider.DoHServer -} diff --git a/pkg/doh/settings.go b/pkg/doh/settings.go index 88dacfbc..231f384d 100644 --- a/pkg/doh/settings.go +++ b/pkg/doh/settings.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/qdm12/dns/v2/internal/picker" metricsnoop "github.com/qdm12/dns/v2/pkg/doh/metrics/noop" lognoop "github.com/qdm12/dns/v2/pkg/log/noop" "github.com/qdm12/dns/v2/pkg/provider" @@ -42,11 +41,6 @@ type ResolverSettings struct { // Metrics is the metrics interface to record metric data. // It defaults to a No-Op metrics implementation. Metrics Metrics - // Picker is the picker to use for each upstream call to pick - // a server from a pool of servers. It must be thread safe. - // It defaults to a fast thread safe pseudo random picker - // with uniform distribution. - Picker Picker } func (s *ServerSettings) SetDefaults() { @@ -62,7 +56,6 @@ func (s *ResolverSettings) SetDefaults() { const defaultTimeout = 5 * time.Second s.Timeout = gosettings.DefaultComparable(s.Timeout, defaultTimeout) s.Metrics = gosettings.DefaultComparable[Metrics](s.Metrics, metricsnoop.New()) - s.Picker = gosettings.DefaultComparable[Picker](s.Picker, picker.New()) } var ( diff --git a/pkg/doh/settings_test.go b/pkg/doh/settings_test.go index 7b2f3576..ab63a208 100644 --- a/pkg/doh/settings_test.go +++ b/pkg/doh/settings_test.go @@ -4,7 +4,6 @@ import ( "testing" "time" - "github.com/qdm12/dns/v2/internal/picker" metrics "github.com/qdm12/dns/v2/pkg/doh/metrics/noop" log "github.com/qdm12/dns/v2/pkg/log/noop" "github.com/qdm12/dns/v2/pkg/provider" @@ -16,14 +15,12 @@ func Test_ServerSettings_SetDefaults(t *testing.T) { metrics := metrics.New() logger := log.New() - picker := picker.New() s := ServerSettings{ Middlewares: []Middleware{}, Logger: logger, Resolver: ResolverSettings{ Metrics: metrics, - Picker: picker, }, } s.SetDefaults() @@ -41,7 +38,6 @@ func Test_ServerSettings_SetDefaults(t *testing.T) { IPVersion: "ipv4", Timeout: 5 * time.Second, Metrics: metrics, - Picker: picker, }, ListeningAddress: ptrTo(":53"), } diff --git a/pkg/dot/dial.go b/pkg/dot/dial.go index c0ff5135..f6f5bba5 100644 --- a/pkg/dot/dial.go +++ b/pkg/dot/dial.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "net" + "github.com/qdm12/dns/v2/internal/picker" "github.com/qdm12/dns/v2/internal/server" "github.com/qdm12/dns/v2/pkg/provider" ) @@ -22,7 +23,7 @@ func newDoTDial(settings ResolverSettings) (dial server.Dial) { Timeout: settings.Timeout, } - picker := settings.Picker + picker := picker.New() ipv6 := settings.IPVersion == "ipv6" return func(ctx context.Context, _, _ string) (net.Conn, error) { @@ -47,7 +48,7 @@ func newDoTDial(settings ResolverSettings) (dial server.Dial) { } } -func pickNameAddress(picker Picker, servers []provider.DoTServer, +func pickNameAddress(picker *picker.Picker, servers []provider.DoTServer, ipv6 bool) (name, address string) { server := picker.DoTServer(servers) addrPort := picker.DoTAddrPort(server, ipv6) diff --git a/pkg/dot/dial_test.go b/pkg/dot/dial_test.go index e80c3a68..1d3f0df2 100644 --- a/pkg/dot/dial_test.go +++ b/pkg/dot/dial_test.go @@ -3,27 +3,42 @@ package dot import ( "testing" - "github.com/golang/mock/gomock" + "github.com/qdm12/dns/v2/internal/picker" "github.com/qdm12/dns/v2/pkg/provider" "github.com/stretchr/testify/assert" ) func Test_pickNameAddress(t *testing.T) { t.Parallel() - ctrl := gomock.NewController(t) - picker := NewMockPicker(ctrl) + picker := picker.New() servers := []provider.DoTServer{ provider.Cloudflare().DoT, provider.Google().DoT, } const ipv6 = true - picker.EXPECT().DoTServer(servers).Return(servers[0]) - picker.EXPECT().DoTAddrPort(servers[0], ipv6).Return(servers[0].IPv6[0]) - name, address := pickNameAddress(picker, servers, ipv6) - assert.Equal(t, "cloudflare-dns.com", name) - assert.Equal(t, "[2606:4700:4700::1111]:853", address) + found := false + for _, server := range servers { + if server.Name != name { + continue + } + ips := server.IPv4 + if ipv6 { + ips = append(ips, server.IPv6...) + } + for _, addrPort := range ips { + if addrPort.String() == address { + found = true + break + } + } + if found { + break + } + } + + assert.True(t, found) } diff --git a/pkg/dot/interfaces.go b/pkg/dot/interfaces.go index c2d39e4b..f4b3bad6 100644 --- a/pkg/dot/interfaces.go +++ b/pkg/dot/interfaces.go @@ -1,10 +1,7 @@ package dot import ( - "net/netip" - "github.com/miekg/dns" - "github.com/qdm12/dns/v2/pkg/provider" ) type Middleware interface { @@ -28,8 +25,3 @@ type Logger interface { type Warner interface { Warn(s string) } - -type Picker interface { - DoTServer(servers []provider.DoTServer) provider.DoTServer - DoTAddrPort(server provider.DoTServer, ipv6 bool) netip.AddrPort -} diff --git a/pkg/dot/mocks_generate_test.go b/pkg/dot/mocks_generate_test.go index 10429002..93f1e5f9 100644 --- a/pkg/dot/mocks_generate_test.go +++ b/pkg/dot/mocks_generate_test.go @@ -1,4 +1,4 @@ package dot -//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Metrics,Logger,Picker +//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Metrics,Logger //go:generate mockgen -destination=mocks_local_test.go -package $GOPACKAGE -source interfaces_test.go diff --git a/pkg/dot/mocks_test.go b/pkg/dot/mocks_test.go index a831e16f..0f05436f 100644 --- a/pkg/dot/mocks_test.go +++ b/pkg/dot/mocks_test.go @@ -1,15 +1,13 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: github.com/qdm12/dns/v2/pkg/dot (interfaces: Metrics,Logger,Picker) +// Source: github.com/qdm12/dns/v2/pkg/dot (interfaces: Metrics,Logger) // Package dot is a generated GoMock package. package dot import ( - netip "net/netip" reflect "reflect" gomock "github.com/golang/mock/gomock" - provider "github.com/qdm12/dns/v2/pkg/provider" ) // MockMetrics is a mock of Metrics interface. @@ -129,54 +127,3 @@ func (mr *MockLoggerMockRecorder) Warn(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warn", reflect.TypeOf((*MockLogger)(nil).Warn), arg0) } - -// MockPicker is a mock of Picker interface. -type MockPicker struct { - ctrl *gomock.Controller - recorder *MockPickerMockRecorder -} - -// MockPickerMockRecorder is the mock recorder for MockPicker. -type MockPickerMockRecorder struct { - mock *MockPicker -} - -// NewMockPicker creates a new mock instance. -func NewMockPicker(ctrl *gomock.Controller) *MockPicker { - mock := &MockPicker{ctrl: ctrl} - mock.recorder = &MockPickerMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockPicker) EXPECT() *MockPickerMockRecorder { - return m.recorder -} - -// DoTAddrPort mocks base method. -func (m *MockPicker) DoTAddrPort(arg0 provider.DoTServer, arg1 bool) netip.AddrPort { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DoTAddrPort", arg0, arg1) - ret0, _ := ret[0].(netip.AddrPort) - return ret0 -} - -// DoTAddrPort indicates an expected call of DoTAddrPort. -func (mr *MockPickerMockRecorder) DoTAddrPort(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DoTAddrPort", reflect.TypeOf((*MockPicker)(nil).DoTAddrPort), arg0, arg1) -} - -// DoTServer mocks base method. -func (m *MockPicker) DoTServer(arg0 []provider.DoTServer) provider.DoTServer { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DoTServer", arg0) - ret0, _ := ret[0].(provider.DoTServer) - return ret0 -} - -// DoTServer indicates an expected call of DoTServer. -func (mr *MockPickerMockRecorder) DoTServer(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DoTServer", reflect.TypeOf((*MockPicker)(nil).DoTServer), arg0) -} diff --git a/pkg/dot/settings.go b/pkg/dot/settings.go index 48bb728d..39a12c8c 100644 --- a/pkg/dot/settings.go +++ b/pkg/dot/settings.go @@ -6,7 +6,6 @@ import ( "os" "time" - "github.com/qdm12/dns/v2/internal/picker" metricsnoop "github.com/qdm12/dns/v2/pkg/dot/metrics/noop" lognoop "github.com/qdm12/dns/v2/pkg/log/noop" "github.com/qdm12/dns/v2/pkg/provider" @@ -50,11 +49,6 @@ type ResolverSettings struct { // Metrics is the metrics interface to record metric data. // It defaults to a No-Op metrics implementation. Metrics Metrics - // Picker is the picker to use for each upstream call to pick - // a server and/or IP address. It must be thread safe. - // It defaults to a fast thread safe pseudo random picker - // with uniform distribution. - Picker Picker } func (s *ServerSettings) SetDefaults() { @@ -73,7 +67,6 @@ func (s *ResolverSettings) SetDefaults() { s.IPVersion = gosettings.DefaultComparable(s.IPVersion, "ipv4") s.Warner = gosettings.DefaultComparable[Warner](s.Warner, lognoop.New()) s.Metrics = gosettings.DefaultComparable[Metrics](s.Metrics, metricsnoop.New()) - s.Picker = gosettings.DefaultComparable[Picker](s.Picker, picker.New()) } var (