2020-05-08 15:39:37 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2021-04-07 13:36:38 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2020-05-08 15:39:37 +00:00
|
|
|
)
|
|
|
|
|
2021-01-20 14:27:53 +00:00
|
|
|
// IPFromAddr gets IP address from addr.
|
|
|
|
func IPFromAddr(addr net.Addr) (ip net.IP) {
|
2020-05-14 09:57:41 +00:00
|
|
|
switch addr := addr.(type) {
|
|
|
|
case *net.UDPAddr:
|
2021-01-13 13:56:05 +00:00
|
|
|
return addr.IP
|
2020-05-14 09:57:41 +00:00
|
|
|
case *net.TCPAddr:
|
2021-01-13 13:56:05 +00:00
|
|
|
return addr.IP
|
2020-05-14 09:57:41 +00:00
|
|
|
}
|
2021-01-13 13:56:05 +00:00
|
|
|
return nil
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 13:56:05 +00:00
|
|
|
// IPStringFromAddr extracts IP address from net.Addr.
|
2020-05-08 15:39:37 +00:00
|
|
|
// Note: we can't use net.SplitHostPort(a.String()) because of IPv6 zone:
|
2020-10-30 10:32:02 +00:00
|
|
|
// https://github.com/AdguardTeam/AdGuardHome/internal/issues/1261
|
2021-01-20 14:27:53 +00:00
|
|
|
func IPStringFromAddr(addr net.Addr) (ipStr string) {
|
|
|
|
if ip := IPFromAddr(addr); ip != nil {
|
2021-01-13 13:56:05 +00:00
|
|
|
return ip.String()
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
2021-01-13 13:56:05 +00:00
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-01-13 13:56:05 +00:00
|
|
|
func stringArrayDup(a []string) []string {
|
|
|
|
a2 := make([]string, len(a))
|
|
|
|
copy(a2, a)
|
|
|
|
return a2
|
2020-05-08 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find value in a sorted array
|
|
|
|
func findSorted(ar []string, val string) int {
|
|
|
|
i := sort.SearchStrings(ar, val)
|
|
|
|
if i == len(ar) || ar[i] != val {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func isWildcard(host string) bool {
|
|
|
|
return len(host) >= 2 &&
|
|
|
|
host[0] == '*' && host[1] == '.'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return TRUE if host name matches a wildcard pattern
|
|
|
|
func matchDomainWildcard(host, wildcard string) bool {
|
|
|
|
return isWildcard(wildcard) &&
|
|
|
|
strings.HasSuffix(host, wildcard[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return TRUE if client's SNI value matches DNS names from certificate
|
|
|
|
func matchDNSName(dnsNames []string, sni string) bool {
|
2021-04-07 13:36:38 +00:00
|
|
|
if aghnet.ValidateDomainName(sni) != nil {
|
2020-05-08 15:39:37 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-04-07 13:36:38 +00:00
|
|
|
|
2020-05-08 15:39:37 +00:00
|
|
|
if findSorted(dnsNames, sni) != -1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dn := range dnsNames {
|
|
|
|
if matchDomainWildcard(sni, dn) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-09-22 12:04:17 +00:00
|
|
|
|
|
|
|
// Is not comment
|
|
|
|
func isUpstream(line string) bool {
|
|
|
|
return !strings.HasPrefix(line, "#")
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterOutComments(lines []string) []string {
|
|
|
|
var filtered []string
|
|
|
|
for _, l := range lines {
|
|
|
|
if isUpstream(l) {
|
|
|
|
filtered = append(filtered, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filtered
|
|
|
|
}
|