Просмотр исходного кода

Implement caching dns resolver

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
10b78322a3

+ 0
- 1
antireplay/noop.go Просмотреть файл

5
 type noop struct{}
5
 type noop struct{}
6
 
6
 
7
 func (n noop) SeenBefore(_ []byte) bool { return false }
7
 func (n noop) SeenBefore(_ []byte) bool { return false }
8
-func (n noop) Shutdown()                {}
9
 
8
 
10
 func NewNoop() mtglib.AntiReplayCache {
9
 func NewNoop() mtglib.AntiReplayCache {
11
 	return noop{}
10
 	return noop{}

+ 0
- 2
antireplay/noop_test.go Просмотреть файл

18
 	suite.False(filter.SeenBefore([]byte{4, 5, 6}))
18
 	suite.False(filter.SeenBefore([]byte{4, 5, 6}))
19
 	suite.False(filter.SeenBefore([]byte{1, 2, 3}))
19
 	suite.False(filter.SeenBefore([]byte{1, 2, 3}))
20
 	suite.False(filter.SeenBefore([]byte{4, 5, 6}))
20
 	suite.False(filter.SeenBefore([]byte{4, 5, 6}))
21
-
22
-	filter.Shutdown()
23
 }
21
 }
24
 
22
 
25
 func TestNoop(t *testing.T) {
23
 func TestNoop(t *testing.T) {

+ 0
- 2
antireplay/stable_bloom_filter.go Просмотреть файл

20
 	return s.filter.TestAndAdd(digest)
20
 	return s.filter.TestAndAdd(digest)
21
 }
21
 }
22
 
22
 
23
-func (s *stableBloomFilter) Shutdown() {}
24
-
25
 func NewStableBloomFilter(byteSize uint, errorRate float64) mtglib.AntiReplayCache {
23
 func NewStableBloomFilter(byteSize uint, errorRate float64) mtglib.AntiReplayCache {
26
 	sf := boom.NewDefaultStableBloomFilter(byteSize*8, errorRate) // nolint: gomnd
24
 	sf := boom.NewDefaultStableBloomFilter(byteSize*8, errorRate) // nolint: gomnd
27
 	sf.SetHash(xxhash.New64())
25
 	sf.SetHash(xxhash.New64())

+ 0
- 2
antireplay/stable_bloom_filter_test.go Просмотреть файл

18
 	suite.False(filter.SeenBefore([]byte{4, 5, 6}))
18
 	suite.False(filter.SeenBefore([]byte{4, 5, 6}))
19
 	suite.True(filter.SeenBefore([]byte{1, 2, 3}))
19
 	suite.True(filter.SeenBefore([]byte{1, 2, 3}))
20
 	suite.True(filter.SeenBefore([]byte{4, 5, 6}))
20
 	suite.True(filter.SeenBefore([]byte{4, 5, 6}))
21
-
22
-	filter.Shutdown()
23
 }
21
 }
24
 
22
 
25
 func TestStableBloomFilter(t *testing.T) {
23
 func TestStableBloomFilter(t *testing.T) {

+ 0
- 6
cli/proxy.go Просмотреть файл

59
 		PreferIP:           c.Config.PreferIP.Value(mtglib.DefaultPreferIP),
59
 		PreferIP:           c.Config.PreferIP.Value(mtglib.DefaultPreferIP),
60
 	}
60
 	}
61
 
61
 
62
-	defer func() {
63
-		opts.AntiReplayCache.Shutdown()
64
-		opts.IPBlocklist.Shutdown()
65
-		opts.EventStream.Shutdown()
66
-	}()
67
-
68
 	if opts.Concurrency == 0 {
62
 	if opts.Concurrency == 0 {
69
 		opts.Concurrency = mtglib.DefaultConcurrency
63
 		opts.Concurrency = mtglib.DefaultConcurrency
70
 	}
64
 	}

+ 5
- 5
events/event_stream.go Просмотреть файл

9
 	"github.com/OneOfOne/xxhash"
9
 	"github.com/OneOfOne/xxhash"
10
 )
10
 )
11
 
11
 
12
-type eventStream struct {
12
+type EventStream struct {
13
 	ctx       context.Context
13
 	ctx       context.Context
14
 	ctxCancel context.CancelFunc
14
 	ctxCancel context.CancelFunc
15
 	chans     []chan mtglib.Event
15
 	chans     []chan mtglib.Event
16
 }
16
 }
17
 
17
 
18
-func (e eventStream) Send(ctx context.Context, evt mtglib.Event) {
18
+func (e EventStream) Send(ctx context.Context, evt mtglib.Event) {
19
 	var chanNo uint32
19
 	var chanNo uint32
20
 
20
 
21
 	if streamID := evt.StreamID(); streamID != "" {
21
 	if streamID := evt.StreamID(); streamID != "" {
31
 	}
31
 	}
32
 }
32
 }
33
 
33
 
34
-func (e eventStream) Shutdown() {
34
+func (e EventStream) Shutdown() {
35
 	e.ctxCancel()
35
 	e.ctxCancel()
36
 }
36
 }
37
 
37
 
38
-func NewEventStream(observerFactories []ObserverFactory) mtglib.EventStream {
38
+func NewEventStream(observerFactories []ObserverFactory) EventStream {
39
 	if len(observerFactories) == 0 {
39
 	if len(observerFactories) == 0 {
40
 		observerFactories = append(observerFactories, NewNoopObserver)
40
 		observerFactories = append(observerFactories, NewNoopObserver)
41
 	}
41
 	}
42
 
42
 
43
 	ctx, cancel := context.WithCancel(context.Background())
43
 	ctx, cancel := context.WithCancel(context.Background())
44
-	rv := eventStream{
44
+	rv := EventStream{
45
 		ctx:       ctx,
45
 		ctx:       ctx,
46
 		ctxCancel: cancel,
46
 		ctxCancel: cancel,
47
 		chans:     make([]chan mtglib.Event, runtime.NumCPU()),
47
 		chans:     make([]chan mtglib.Event, runtime.NumCPU()),

+ 1
- 1
events/event_stream_test.go Просмотреть файл

19
 	ctxCancel     context.CancelFunc
19
 	ctxCancel     context.CancelFunc
20
 	observerMock1 *ObserverMock
20
 	observerMock1 *ObserverMock
21
 	observerMock2 *ObserverMock
21
 	observerMock2 *ObserverMock
22
-	stream        mtglib.EventStream
22
+	stream        events.EventStream
23
 }
23
 }
24
 
24
 
25
 func (suite *EventStreamTestSuite) SetupTest() {
25
 func (suite *EventStreamTestSuite) SetupTest() {

+ 0
- 1
events/noop.go Просмотреть файл

9
 type noop struct{}
9
 type noop struct{}
10
 
10
 
11
 func (n noop) Send(ctx context.Context, evt mtglib.Event) {}
11
 func (n noop) Send(ctx context.Context, evt mtglib.Event) {}
12
-func (n noop) Shutdown()                                  {}
13
 
12
 
14
 func NewNoopStream() mtglib.EventStream {
13
 func NewNoopStream() mtglib.EventStream {
15
 	return noop{}
14
 	return noop{}

+ 0
- 2
events/noop_test.go Просмотреть файл

70
 			stream.Send(suite.ctx, value)
70
 			stream.Send(suite.ctx, value)
71
 		})
71
 		})
72
 	}
72
 	}
73
-
74
-	stream.Shutdown()
75
 }
73
 }
76
 
74
 
77
 func (suite *NoopTestSuite) TestObserver() {
75
 func (suite *NoopTestSuite) TestObserver() {

+ 1
- 0
go.mod Просмотреть файл

9
 	github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
9
 	github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
10
 	github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6
10
 	github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6
11
 	github.com/d4l3k/messagediff v1.2.1 // indirect
11
 	github.com/d4l3k/messagediff v1.2.1 // indirect
12
+	github.com/dgraph-io/ristretto v0.0.3 // indirect
12
 	github.com/jarcoal/httpmock v1.0.8
13
 	github.com/jarcoal/httpmock v1.0.8
13
 	github.com/kentik/patricia v0.0.0-20201202224819-f9447a6e25f1
14
 	github.com/kentik/patricia v0.0.0-20201202224819-f9447a6e25f1
14
 	github.com/libp2p/go-reuseport v0.0.2
15
 	github.com/libp2p/go-reuseport v0.0.2

+ 7
- 0
go.sum Просмотреть файл

2
 cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2
 cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
3
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4
 github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
4
 github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
5
+github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
5
 github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8=
6
 github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8=
6
 github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
7
 github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
7
 github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
8
 github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
38
 github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
39
 github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
39
 github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
40
 github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
40
 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
41
 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
42
+github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
43
+github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
41
 github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
44
 github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
42
 github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
45
 github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
43
 github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
46
 github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
55
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
58
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
56
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
59
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
57
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
60
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
61
+github.com/dgraph-io/ristretto v0.0.3 h1:jh22xisGBjrEVnRZ1DVTpBVQm0Xndu8sMl0CWDzSIBI=
62
+github.com/dgraph-io/ristretto v0.0.3/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
58
 github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
63
 github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
64
+github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
59
 github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
65
 github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
60
 github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
66
 github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
61
 github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
67
 github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
277
 github.com/smira/go-statsd v1.3.2/go.mod h1:1srXJ9/pbnN04G8f4F1jUzsGOnwkPKXciyqpewGlkC4=
283
 github.com/smira/go-statsd v1.3.2/go.mod h1:1srXJ9/pbnN04G8f4F1jUzsGOnwkPKXciyqpewGlkC4=
278
 github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
284
 github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
279
 github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
285
 github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
286
+github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
280
 github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
287
 github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
281
 github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
288
 github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
282
 github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
289
 github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=

+ 0
- 1
ipblocklist/noop.go Просмотреть файл

9
 type noop struct{}
9
 type noop struct{}
10
 
10
 
11
 func (n noop) Contains(ip net.IP) bool { return false }
11
 func (n noop) Contains(ip net.IP) bool { return false }
12
-func (n noop) Shutdown()               {}
13
 
12
 
14
 func NewNoop() mtglib.IPBlocklist {
13
 func NewNoop() mtglib.IPBlocklist {
15
 	return noop{}
14
 	return noop{}

+ 0
- 3
mtglib/init.go Просмотреть файл

35
 
35
 
36
 type AntiReplayCache interface {
36
 type AntiReplayCache interface {
37
 	SeenBefore(data []byte) bool
37
 	SeenBefore(data []byte) bool
38
-	Shutdown()
39
 }
38
 }
40
 
39
 
41
 type IPBlocklist interface {
40
 type IPBlocklist interface {
42
 	Contains(net.IP) bool
41
 	Contains(net.IP) bool
43
-	Shutdown()
44
 }
42
 }
45
 
43
 
46
 type Event interface {
44
 type Event interface {
50
 
48
 
51
 type EventStream interface {
49
 type EventStream interface {
52
 	Send(context.Context, Event)
50
 	Send(context.Context, Event)
53
-	Shutdown()
54
 }
51
 }
55
 
52
 
56
 type TimeAttackDetector interface {
53
 type TimeAttackDetector interface {

+ 88
- 0
network/dns_resolver.go Просмотреть файл

1
+package network
2
+
3
+import (
4
+	"net/http"
5
+	"time"
6
+
7
+	doh "github.com/babolivier/go-doh-client"
8
+	"github.com/dgraph-io/ristretto"
9
+)
10
+
11
+const (
12
+	dnsResolverSize     = 1024 * 1024 // 1mb
13
+	dnsResolverKeepTime = 10 * time.Minute
14
+)
15
+
16
+type dnsResolver struct {
17
+	resolver doh.Resolver
18
+	cache    *ristretto.Cache
19
+}
20
+
21
+func (d dnsResolver) LookupA(hostname string) []string {
22
+	key := "\x00." + hostname
23
+
24
+	if value, ok := d.cache.Get(key); ok {
25
+		return value.([]string)
26
+	}
27
+
28
+	var ips []string
29
+
30
+	if recs, _, err := d.resolver.LookupA(hostname); err == nil {
31
+		for _, v := range recs {
32
+			ips = append(ips, v.IP4)
33
+		}
34
+
35
+		d.cache.SetWithTTL(key, ips, 0, dnsResolverKeepTime)
36
+	}
37
+
38
+	return ips
39
+}
40
+
41
+func (d dnsResolver) LookupAAAA(hostname string) []string {
42
+	key := "\x01." + hostname
43
+
44
+	if value, ok := d.cache.Get(key); ok {
45
+		return value.([]string)
46
+	}
47
+
48
+	var ips []string
49
+
50
+	if recs, _, err := d.resolver.LookupAAAA(hostname); err == nil {
51
+		for _, v := range recs {
52
+			ips = append(ips, v.IP6)
53
+		}
54
+
55
+		d.cache.SetWithTTL(key, ips, 0, dnsResolverKeepTime)
56
+	}
57
+
58
+	return ips
59
+}
60
+
61
+func newDNSResolver(hostname string, httpClient *http.Client) dnsResolver {
62
+	cache, err := ristretto.NewCache(&ristretto.Config{
63
+		NumCounters: 10 * dnsResolverSize, // nolint: gomnd // taken from official doc as a best practice value
64
+		MaxCost:     dnsResolverSize,
65
+		BufferItems: 64, // nolint: gomnd // taken from official doc as a best practice value
66
+		Cost: func(value interface{}) int64 {
67
+			var cost int64
68
+
69
+			for _, v := range value.([]string) {
70
+				cost += int64(len([]byte(v)))
71
+			}
72
+
73
+			return cost
74
+		},
75
+	})
76
+	if err != nil {
77
+		panic(err)
78
+	}
79
+
80
+	return dnsResolver{
81
+		resolver: doh.Resolver{
82
+			Host:       hostname,
83
+			Class:      doh.IN,
84
+			HTTPClient: httpClient,
85
+		},
86
+		cache: cache,
87
+	}
88
+}

+ 50
- 0
network/dns_resolver_internal_test.go Просмотреть файл

1
+package network
2
+
3
+import (
4
+	"net"
5
+	"net/http"
6
+	"testing"
7
+	"time"
8
+
9
+	"github.com/stretchr/testify/suite"
10
+)
11
+
12
+type DNSResolverTestSuite struct {
13
+	suite.Suite
14
+
15
+	d dnsResolver
16
+}
17
+
18
+func (suite *DNSResolverTestSuite) TestLookupA() {
19
+	suite.d.LookupA("google.com")
20
+	time.Sleep(10 * time.Millisecond)
21
+
22
+	addrs := suite.d.LookupA("google.com")
23
+
24
+	for _, v := range addrs {
25
+		suite.NotEmpty(v)
26
+		suite.NotNil(net.ParseIP(v).To4())
27
+	}
28
+}
29
+
30
+func (suite *DNSResolverTestSuite) TestLookupAAAA() {
31
+	suite.d.LookupAAAA("google.com")
32
+	time.Sleep(10 * time.Millisecond)
33
+
34
+	addrs := suite.d.LookupAAAA("google.com")
35
+
36
+	for _, v := range addrs {
37
+		suite.NotEmpty(v)
38
+		suite.Nil(net.ParseIP(v).To4())
39
+		suite.NotNil(net.ParseIP(v).To16())
40
+	}
41
+}
42
+
43
+func (suite *DNSResolverTestSuite) SetupTest() {
44
+	suite.d = newDNSResolver("1.1.1.1", &http.Client{})
45
+}
46
+
47
+func TestDNSResolver(t *testing.T) {
48
+	t.Parallel()
49
+	suite.Run(t, &DNSResolverTestSuite{})
50
+}

+ 11
- 21
network/network.go Просмотреть файл

10
 	"time"
10
 	"time"
11
 
11
 
12
 	"github.com/9seconds/mtg/v2/mtglib"
12
 	"github.com/9seconds/mtg/v2/mtglib"
13
-	doh "github.com/babolivier/go-doh-client"
14
 )
13
 )
15
 
14
 
16
 type networkHTTPTransport struct {
15
 type networkHTTPTransport struct {
26
 
25
 
27
 type network struct {
26
 type network struct {
28
 	dialer      Dialer
27
 	dialer      Dialer
29
-	dns         doh.Resolver
28
+	dns         dnsResolver
30
 	httpTimeout time.Duration
29
 	httpTimeout time.Duration
31
 	userAgent   string
30
 	userAgent   string
32
 }
31
 }
84
 		go func() {
83
 		go func() {
85
 			defer wg.Done()
84
 			defer wg.Done()
86
 
85
 
87
-			if recs, _, err := n.dns.LookupA(address); err == nil {
88
-				mutex.Lock()
89
-				defer mutex.Unlock()
86
+			resolved := n.dns.LookupA(address)
90
 
87
 
91
-				for _, v := range recs {
92
-					ips = append(ips, v.IP4)
93
-				}
94
-			}
88
+			mutex.Lock()
89
+			ips = append(ips, resolved...)
90
+			mutex.Unlock()
95
 		}()
91
 		}()
96
 	}
92
 	}
97
 
93
 
102
 		go func() {
98
 		go func() {
103
 			defer wg.Done()
99
 			defer wg.Done()
104
 
100
 
105
-			if recs, _, err := n.dns.LookupAAAA(address); err == nil {
106
-				mutex.Lock()
107
-				defer mutex.Unlock()
101
+			resolved := n.dns.LookupAAAA(address)
108
 
102
 
109
-				for _, v := range recs {
110
-					ips = append(ips, v.IP6)
111
-				}
112
-			}
103
+			mutex.Lock()
104
+			ips = append(ips, resolved...)
105
+			mutex.Unlock()
113
 		}()
106
 		}()
114
 	}
107
 	}
115
 
108
 
140
 		dialer:      dialer,
133
 		dialer:      dialer,
141
 		httpTimeout: httpTimeout,
134
 		httpTimeout: httpTimeout,
142
 		userAgent:   userAgent,
135
 		userAgent:   userAgent,
143
-		dns: doh.Resolver{
144
-			Host:       dohHostname,
145
-			Class:      doh.IN,
146
-			HTTPClient: makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext),
147
-		},
136
+		dns: newDNSResolver(dohHostname,
137
+			makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext)),
148
 	}, nil
138
 	}, nil
149
 }
139
 }
150
 
140
 

Загрузка…
Отмена
Сохранить