mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package dns
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/metacubex/mihomo/component/resolver"
|
|
|
|
D "github.com/miekg/dns"
|
|
)
|
|
|
|
const (
|
|
SystemDnsFlushTime = 5 * time.Minute
|
|
SystemDnsDeleteTimes = 12 // 12*5 = 60min
|
|
)
|
|
|
|
type systemDnsClient struct {
|
|
disableTimes uint32
|
|
dnsClient
|
|
}
|
|
|
|
type systemClient struct {
|
|
mu sync.Mutex
|
|
dnsClients map[string]*systemDnsClient
|
|
lastFlush time.Time
|
|
defaultNS []dnsClient
|
|
}
|
|
|
|
func (c *systemClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
|
dnsClients, err := c.getDnsClients()
|
|
if err != nil {
|
|
if len(c.defaultNS) > 0 {
|
|
dnsClients = c.defaultNS
|
|
} else {
|
|
return
|
|
}
|
|
}
|
|
msg, _, err = batchExchange(ctx, dnsClients, m)
|
|
return
|
|
}
|
|
|
|
// Address implements dnsClient
|
|
func (c *systemClient) Address() string {
|
|
dnsClients, _ := c.getDnsClients()
|
|
addrs := make([]string, 0, len(dnsClients))
|
|
for _, c := range dnsClients {
|
|
addrs = append(addrs, c.Address())
|
|
}
|
|
return fmt.Sprintf("system(%s)", strings.Join(addrs, ","))
|
|
}
|
|
|
|
var _ dnsClient = (*systemClient)(nil)
|
|
|
|
func newSystemClient() *systemClient {
|
|
return &systemClient{
|
|
dnsClients: map[string]*systemDnsClient{},
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
r := NewResolver(Config{})
|
|
c := newSystemClient()
|
|
c.defaultNS = transform([]NameServer{{Addr: "114.114.114.114:53"}, {Addr: "8.8.8.8:53"}}, nil)
|
|
r.main = []dnsClient{c}
|
|
resolver.SystemResolver = r
|
|
}
|