Give "#" the highest priority

This commit is contained in:
j2rong4cn 2025-05-09 15:46:29 +08:00
parent eafb352eaf
commit 905443c309

View File

@ -68,44 +68,45 @@ func filterIP(ips []net.Address, option dns.IPOption) []net.Address {
return filtered return filtered
} }
func (h *StaticHosts) lookupInternal(domain string) []net.Address { func (h *StaticHosts) lookupInternal(domain string) ([]net.Address, error) {
ips := make([]net.Address, 0) ips := make([]net.Address, 0)
found := false found := false
for _, id := range h.matchers.Match(domain) { for _, id := range h.matchers.Match(domain) {
for _, v := range h.ips[id] {
if err, ok := v.(dns.RCodeError); ok {
if uint16(err) == 0 {
return nil, dns.ErrEmptyResponse
}
return nil, err
}
}
ips = append(ips, h.ips[id]...) ips = append(ips, h.ips[id]...)
found = true found = true
} }
if !found { if !found {
return nil return nil, nil
} }
return ips return ips, nil
} }
func (h *StaticHosts) lookup(domain string, option dns.IPOption, maxDepth int) ([]net.Address, error) { func (h *StaticHosts) lookup(domain string, option dns.IPOption, maxDepth int) ([]net.Address, error) {
switch addrs := h.lookupInternal(domain); { switch addrs, err := h.lookupInternal(domain); {
case err != nil:
return nil, err
case len(addrs) == 0: // Not recorded in static hosts, return nil case len(addrs) == 0: // Not recorded in static hosts, return nil
return addrs, nil return addrs, nil
case len(addrs) == 1: case len(addrs) == 1 && addrs[0].Family().IsDomain(): // Try to unwrap domain
if err, ok := addrs[0].(dns.RCodeError); ok { errors.LogDebug(context.Background(), "found replaced domain: ", domain, " -> ", addrs[0].Domain(), ". Try to unwrap it")
if uint16(err) == 0 { if maxDepth > 0 {
return nil, dns.ErrEmptyResponse unwrapped, err := h.lookup(addrs[0].Domain(), option, maxDepth-1)
if err != nil {
return nil, err
} }
return nil, err if unwrapped != nil {
} return unwrapped, nil
if addrs[0].Family().IsDomain() { // Try to unwrap domain
errors.LogDebug(context.Background(), "found replaced domain: ", domain, " -> ", addrs[0].Domain(), ". Try to unwrap it")
if maxDepth > 0 {
unwrapped, err := h.lookup(addrs[0].Domain(), option, maxDepth-1)
if err != nil {
return nil, err
}
if unwrapped != nil {
return unwrapped, nil
}
} }
return addrs, nil
} }
fallthrough return addrs, nil
default: // IP record found, return a non-nil IP array default: // IP record found, return a non-nil IP array
return filterIP(addrs, option), nil return filterIP(addrs, option), nil
} }