mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 06:25:44 +03:00
ae8de95d89
Merge in DNS/adguard-home from 2234-move-to-internal to master Squashed commit of the following: commit d26a288cabeac86f9483fab307677b1027c78524 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Oct 30 12:44:18 2020 +0300 * all: move internal Go packages to internal/ Closes #2234.
102 lines
2 KiB
Go
102 lines
2 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"net"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/AdguardTeam/golibs/utils"
|
|
)
|
|
|
|
// GetIPString is a helper function that extracts IP address from net.Addr
|
|
func GetIPString(addr net.Addr) string {
|
|
switch addr := addr.(type) {
|
|
case *net.UDPAddr:
|
|
return addr.IP.String()
|
|
case *net.TCPAddr:
|
|
return addr.IP.String()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func stringArrayDup(a []string) []string {
|
|
a2 := make([]string, len(a))
|
|
copy(a2, a)
|
|
return a2
|
|
}
|
|
|
|
// Get IP address from net.Addr object
|
|
// Note: we can't use net.SplitHostPort(a.String()) because of IPv6 zone:
|
|
// https://github.com/AdguardTeam/AdGuardHome/internal/issues/1261
|
|
func ipFromAddr(a net.Addr) string {
|
|
switch addr := a.(type) {
|
|
case *net.UDPAddr:
|
|
return addr.IP.String()
|
|
case *net.TCPAddr:
|
|
return addr.IP.String()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Get IP address from net.Addr
|
|
func getIP(addr net.Addr) net.IP {
|
|
switch addr := addr.(type) {
|
|
case *net.UDPAddr:
|
|
return addr.IP
|
|
case *net.TCPAddr:
|
|
return addr.IP
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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 {
|
|
if utils.IsValidHostname(sni) != nil {
|
|
return false
|
|
}
|
|
if findSorted(dnsNames, sni) != -1 {
|
|
return true
|
|
}
|
|
|
|
for _, dn := range dnsNames {
|
|
if matchDomainWildcard(sni, dn) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// 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
|
|
}
|