Make dns.strategy take effect in dns exchange

This commit is contained in:
世界 2022-07-11 13:21:17 +08:00
parent a104d18277
commit 3c1190e2c3
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 37 additions and 18 deletions

View file

@ -31,24 +31,32 @@ type Client struct {
cache *cache.LruCache[dnsmessage.Question, *dnsmessage.Message]
disableCache bool
disableExpire bool
strategy C.DomainStrategy
}
func NewClient(options option.DNSClientOptions) *Client {
if options.DisableCache {
return &Client{
disableCache: true,
}
} else {
return &Client{
cache: cache.New[dnsmessage.Question, *dnsmessage.Message](),
client := &Client{
disableCache: options.DisableCache,
disableExpire: options.DisableExpire,
strategy: C.DomainStrategy(options.Strategy),
}
if !options.DisableCache {
client.cache = cache.New[dnsmessage.Question, *dnsmessage.Message]()
}
return client
}
func (c *Client) Exchange(ctx context.Context, transport adapter.DNSTransport, message *dnsmessage.Message) (*dnsmessage.Message, error) {
if len(message.Questions) == 0 {
return nil, E.New("empty query")
if len(message.Questions) != 1 {
responseMessage := dnsmessage.Message{
Header: dnsmessage.Header{
ID: message.ID,
RCode: dnsmessage.RCodeFormatError,
Response: true,
RecursionDesired: true,
},
}
return &responseMessage, nil
}
question := message.Questions[0]
if !c.disableCache {
@ -64,6 +72,18 @@ func (c *Client) Exchange(ctx context.Context, transport adapter.DNSTransport, m
}
return nil, ErrNoRawSupport
}
if question.Type == dnsmessage.TypeA && c.strategy == C.DomainStrategyUseIPv6 || question.Type == dnsmessage.TypeAAAA && c.strategy == C.DomainStrategyUseIPv4 {
responseMessage := dnsmessage.Message{
Header: dnsmessage.Header{
ID: message.ID,
RCode: dnsmessage.RCodeNameError,
Response: true,
RecursionDesired: true,
},
Questions: []dnsmessage.Question{question},
}
return &responseMessage, nil
}
messageId := message.ID
response, err := transport.Exchange(ctx, message)
if err != nil {

View file

@ -12,7 +12,6 @@ type DNSOptions struct {
Servers []DNSServerOptions `json:"servers,omitempty"`
Rules []DNSRule `json:"rules,omitempty"`
Final string `json:"final,omitempty"`
Strategy DomainStrategy `json:"strategy,omitempty"`
DNSClientOptions
}
@ -20,11 +19,11 @@ func (o DNSOptions) Equals(other DNSOptions) bool {
return common.ComparableSliceEquals(o.Servers, other.Servers) &&
common.SliceEquals(o.Rules, other.Rules) &&
o.Final == other.Final &&
o.Strategy == other.Strategy &&
o.DNSClientOptions == other.DNSClientOptions
}
type DNSClientOptions struct {
Strategy DomainStrategy `json:"strategy,omitempty"`
DisableCache bool `json:"disable_cache,omitempty"`
DisableExpire bool `json:"disable_expire,omitempty"`
}