forked from SagerNet/sing-box
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bef8c18
commit 6713bba
Showing
4 changed files
with
87 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package route | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
|
||
dns "github.com/sagernet/sing-dns" | ||
) | ||
|
||
type StaticDNSEntry struct { | ||
IPv4 *netip.Addr | ||
IPv6 *netip.Addr | ||
} | ||
|
||
func createEntries(items map[string][]string) map[string]StaticDNSEntry { | ||
entries := make(map[string]StaticDNSEntry) | ||
|
||
for domain, ips := range items { | ||
entry := StaticDNSEntry{} | ||
|
||
for _, ipString := range ips { | ||
ip, err := netip.ParseAddr(ipString) | ||
if err != nil { | ||
fmt.Printf("Invalid IP address for domain %s: %s\n", domain, ipString) | ||
continue | ||
} | ||
|
||
if ip.Is4() { | ||
entry.IPv4 = &ip | ||
} else { | ||
entry.IPv6 = &ip | ||
} | ||
} | ||
entries[domain] = entry | ||
} | ||
|
||
return entries | ||
} | ||
|
||
func lookupStaticIP(domain string, strategy uint8, entries map[string]StaticDNSEntry) ([]netip.Addr, error) { | ||
|
||
if staticDns, ok := entries[domain]; ok { | ||
addrs := []netip.Addr{} | ||
switch strategy { | ||
case dns.DomainStrategyUseIPv4: | ||
if staticDns.IPv4 != nil { | ||
addrs = append(addrs, *staticDns.IPv4) | ||
} | ||
case dns.DomainStrategyUseIPv6: | ||
if staticDns.IPv6 != nil { | ||
addrs = append(addrs, *staticDns.IPv6) | ||
} | ||
case dns.DomainStrategyPreferIPv6: | ||
if staticDns.IPv6 != nil { | ||
addrs = append(addrs, *staticDns.IPv6) | ||
} | ||
if staticDns.IPv4 != nil { | ||
addrs = append(addrs, *staticDns.IPv4) | ||
} | ||
default: | ||
if staticDns.IPv4 != nil { | ||
addrs = append(addrs, *staticDns.IPv4) | ||
} | ||
if staticDns.IPv6 != nil { | ||
addrs = append(addrs, *staticDns.IPv6) | ||
} | ||
} | ||
return addrs, nil | ||
} else { | ||
return nil, fmt.Errorf("NotFound") | ||
} | ||
|
||
} |