chore: code cleanup
This commit is contained in:
parent
fc5a3cf80c
commit
c1f24d8f0e
7 changed files with 5 additions and 45 deletions
|
@ -48,10 +48,6 @@ func (c *client) Address() string {
|
||||||
return c.addr
|
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) {
|
func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error) {
|
||||||
var (
|
var (
|
||||||
ip netip.Addr
|
ip netip.Addr
|
||||||
|
|
|
@ -11,8 +11,6 @@ import (
|
||||||
"github.com/Dreamacro/clash/common/atomic"
|
"github.com/Dreamacro/clash/common/atomic"
|
||||||
"github.com/Dreamacro/clash/component/dhcp"
|
"github.com/Dreamacro/clash/component/dhcp"
|
||||||
"github.com/Dreamacro/clash/component/iface"
|
"github.com/Dreamacro/clash/component/iface"
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
|
||||||
|
|
||||||
D "github.com/miekg/dns"
|
D "github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -46,13 +44,6 @@ func (d *dhcpClient) Address() string {
|
||||||
return strings.Join(addrs, ",")
|
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) {
|
func (d *dhcpClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
||||||
clients, err := d.resolve(ctx)
|
clients, err := d.resolve(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -157,11 +157,6 @@ func (doh *dnsOverHTTPS) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.
|
||||||
return msg, err
|
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.
|
// Close implements the Upstream interface for *dnsOverHTTPS.
|
||||||
func (doh *dnsOverHTTPS) Close() (err error) {
|
func (doh *dnsOverHTTPS) Close() (err error) {
|
||||||
doh.clientMu.Lock()
|
doh.clientMu.Lock()
|
||||||
|
|
|
@ -134,11 +134,6 @@ func (doq *dnsOverQUIC) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.M
|
||||||
return msg, err
|
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.
|
// Close implements the Upstream interface for *dnsOverQUIC.
|
||||||
func (doq *dnsOverQUIC) Close() (err error) {
|
func (doq *dnsOverQUIC) Close() (err error) {
|
||||||
doq.connMu.Lock()
|
doq.connMu.Lock()
|
||||||
|
|
|
@ -39,16 +39,12 @@ type rcodeClient struct {
|
||||||
|
|
||||||
var _ dnsClient = rcodeClient{}
|
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.Response = true
|
||||||
m.Rcode = r.rcode
|
m.Rcode = r.rcode
|
||||||
return m, nil
|
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 {
|
func (r rcodeClient) Address() string {
|
||||||
return r.addr
|
return r.addr
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type dnsClient interface {
|
type dnsClient interface {
|
||||||
Exchange(m *D.Msg) (msg *D.Msg, err error)
|
|
||||||
ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error)
|
ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error)
|
||||||
Address() string
|
Address() string
|
||||||
}
|
}
|
||||||
|
@ -136,11 +135,6 @@ func (r *Resolver) shouldIPFallback(ip netip.Addr) bool {
|
||||||
return false
|
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
|
// 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) {
|
func (r *Resolver) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
||||||
if len(m.Question) == 0 {
|
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 {
|
if matched := r.matchPolicy(m); len(matched) != 0 {
|
||||||
result, cache, err = r.batchExchange(ctx, matched, m)
|
result, cache, err = batchExchange(ctx, matched, m)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
result, cache, err = r.batchExchange(ctx, r.main, m)
|
result, cache, err = batchExchange(ctx, r.main, m)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,13 +249,6 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M
|
||||||
return
|
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 {
|
func (r *Resolver) matchPolicy(m *D.Msg) []dnsClient {
|
||||||
if r.policy == nil {
|
if r.policy == nil {
|
||||||
return 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 {
|
func (r *Resolver) asyncExchange(ctx context.Context, client []dnsClient, msg *D.Msg) <-chan *result {
|
||||||
ch := make(chan *result, 1)
|
ch := make(chan *result, 1)
|
||||||
go func() {
|
go func() {
|
||||||
res, _, err := r.batchExchange(ctx, client, msg)
|
res, _, err := batchExchange(ctx, client, msg)
|
||||||
ch <- &result{Msg: res, Error: err}
|
ch <- &result{Msg: res, Error: err}
|
||||||
}()
|
}()
|
||||||
return ch
|
return ch
|
||||||
|
|
|
@ -306,7 +306,7 @@ func batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.M
|
||||||
domain := msgToDomain(m)
|
domain := msgToDomain(m)
|
||||||
for _, client := range clients {
|
for _, client := range clients {
|
||||||
if _, isRCodeClient := client.(rcodeClient); isRCodeClient {
|
if _, isRCodeClient := client.(rcodeClient); isRCodeClient {
|
||||||
msg, err = client.Exchange(m)
|
msg, err = client.ExchangeContext(ctx, m)
|
||||||
return msg, false, err
|
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
|
client := client // shadow define client to ensure the value captured by the closure will not be changed in the next loop
|
||||||
|
|
Loading…
Reference in a new issue