From c1f24d8f0ee648ed268b98b037da037a2f8e9fc5 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 25 Oct 2023 18:07:45 +0800 Subject: [PATCH] chore: code cleanup --- dns/client.go | 4 ---- dns/dhcp.go | 9 --------- dns/doh.go | 5 ----- dns/doq.go | 5 ----- dns/rcode.go | 6 +----- dns/resolver.go | 19 +++---------------- dns/util.go | 2 +- 7 files changed, 5 insertions(+), 45 deletions(-) diff --git a/dns/client.go b/dns/client.go index 89f083aa..f228b08e 100644 --- a/dns/client.go +++ b/dns/client.go @@ -48,10 +48,6 @@ func (c *client) Address() string { return c.addr } -func (c *client) Exchange(m *D.Msg) (*D.Msg, error) { - return c.ExchangeContext(context.Background(), m) -} - func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error) { var ( ip netip.Addr diff --git a/dns/dhcp.go b/dns/dhcp.go index 7d420d89..e0373ab4 100644 --- a/dns/dhcp.go +++ b/dns/dhcp.go @@ -11,8 +11,6 @@ import ( "github.com/Dreamacro/clash/common/atomic" "github.com/Dreamacro/clash/component/dhcp" "github.com/Dreamacro/clash/component/iface" - "github.com/Dreamacro/clash/component/resolver" - D "github.com/miekg/dns" ) @@ -46,13 +44,6 @@ func (d *dhcpClient) Address() string { return strings.Join(addrs, ",") } -func (d *dhcpClient) Exchange(m *D.Msg) (msg *D.Msg, err error) { - ctx, cancel := context.WithTimeout(context.Background(), resolver.DefaultDNSTimeout) - defer cancel() - - return d.ExchangeContext(ctx, m) -} - func (d *dhcpClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) { clients, err := d.resolve(ctx) if err != nil { diff --git a/dns/doh.go b/dns/doh.go index 0d84fc4f..488e9025 100644 --- a/dns/doh.go +++ b/dns/doh.go @@ -157,11 +157,6 @@ func (doh *dnsOverHTTPS) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D. return msg, err } -// Exchange implements the Upstream interface for *dnsOverHTTPS. -func (doh *dnsOverHTTPS) Exchange(m *D.Msg) (*D.Msg, error) { - return doh.ExchangeContext(context.Background(), m) -} - // Close implements the Upstream interface for *dnsOverHTTPS. func (doh *dnsOverHTTPS) Close() (err error) { doh.clientMu.Lock() diff --git a/dns/doq.go b/dns/doq.go index afa8259a..76da913f 100644 --- a/dns/doq.go +++ b/dns/doq.go @@ -134,11 +134,6 @@ func (doq *dnsOverQUIC) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.M return msg, err } -// Exchange implements the Upstream interface for *dnsOverQUIC. -func (doq *dnsOverQUIC) Exchange(m *D.Msg) (msg *D.Msg, err error) { - return doq.ExchangeContext(context.Background(), m) -} - // Close implements the Upstream interface for *dnsOverQUIC. func (doq *dnsOverQUIC) Close() (err error) { doq.connMu.Lock() diff --git a/dns/rcode.go b/dns/rcode.go index 61fc8d72..9777d2e7 100644 --- a/dns/rcode.go +++ b/dns/rcode.go @@ -39,16 +39,12 @@ type rcodeClient struct { var _ dnsClient = rcodeClient{} -func (r rcodeClient) Exchange(m *D.Msg) (*D.Msg, error) { +func (r rcodeClient) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error) { m.Response = true m.Rcode = r.rcode return m, nil } -func (r rcodeClient) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error) { - return r.Exchange(m) -} - func (r rcodeClient) Address() string { return r.addr } diff --git a/dns/resolver.go b/dns/resolver.go index 4696515f..f0183c16 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -23,7 +23,6 @@ import ( ) type dnsClient interface { - Exchange(m *D.Msg) (msg *D.Msg, err error) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) Address() string } @@ -136,11 +135,6 @@ func (r *Resolver) shouldIPFallback(ip netip.Addr) bool { return false } -// Exchange a batch of dns request, and it use cache -func (r *Resolver) Exchange(m *D.Msg) (msg *D.Msg, err error) { - return r.ExchangeContext(context.Background(), m) -} - // ExchangeContext a batch of dns request with context.Context, and it use cache func (r *Resolver) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) { if len(m.Question) == 0 { @@ -210,10 +204,10 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M } if matched := r.matchPolicy(m); len(matched) != 0 { - result, cache, err = r.batchExchange(ctx, matched, m) + result, cache, err = batchExchange(ctx, matched, m) return } - result, cache, err = r.batchExchange(ctx, r.main, m) + result, cache, err = batchExchange(ctx, r.main, m) return } @@ -255,13 +249,6 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M return } -func (r *Resolver) batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, cache bool, err error) { - ctx, cancel := context.WithTimeout(ctx, resolver.DefaultDNSTimeout) - defer cancel() - - return batchExchange(ctx, clients, m) -} - func (r *Resolver) matchPolicy(m *D.Msg) []dnsClient { if r.policy == nil { return nil @@ -385,7 +372,7 @@ func (r *Resolver) lookupIP(ctx context.Context, host string, dnsType uint16) (i func (r *Resolver) asyncExchange(ctx context.Context, client []dnsClient, msg *D.Msg) <-chan *result { ch := make(chan *result, 1) go func() { - res, _, err := r.batchExchange(ctx, client, msg) + res, _, err := batchExchange(ctx, client, msg) ch <- &result{Msg: res, Error: err} }() return ch diff --git a/dns/util.go b/dns/util.go index 433ea9e2..6c196517 100644 --- a/dns/util.go +++ b/dns/util.go @@ -306,7 +306,7 @@ func batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.M domain := msgToDomain(m) for _, client := range clients { if _, isRCodeClient := client.(rcodeClient); isRCodeClient { - msg, err = client.Exchange(m) + msg, err = client.ExchangeContext(ctx, m) return msg, false, err } client := client // shadow define client to ensure the value captured by the closure will not be changed in the next loop