mirror of
https://github.com/safing/portmaster
synced 2025-09-13 16:29:42 +00:00
Revamp Profile Domains and Ports to Endpoints and ServiceEndpoints
This commit is contained in:
parent
4017de7dac
commit
bde81d815d
13 changed files with 417 additions and 249 deletions
72
intel/reverse.go
Normal file
72
intel/reverse.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package intel
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/Safing/portbase/log"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// ResolveIPAndValidate finds (reverse DNS), validates (forward DNS) and returns the domain name assigned to the given IP.
|
||||
func ResolveIPAndValidate(ip string, securityLevel uint8) (domain string, err error) {
|
||||
// get reversed DNS address
|
||||
rQ, err := dns.ReverseAddr(ip)
|
||||
if err != nil {
|
||||
log.Tracef("intel: failed to get reverse address of %s: %s", ip, err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
// get PTR record
|
||||
rrCache := Resolve(rQ, dns.Type(dns.TypePTR), securityLevel)
|
||||
if rrCache == nil {
|
||||
return "", errors.New("querying for PTR record failed (may be NXDomain)")
|
||||
}
|
||||
|
||||
// get result from record
|
||||
var ptrName string
|
||||
for _, rr := range rrCache.Answer {
|
||||
ptrRec, ok := rr.(*dns.PTR)
|
||||
if ok {
|
||||
ptrName = ptrRec.Ptr
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// check for nxDomain
|
||||
if ptrName == "" {
|
||||
return "", errors.New("no PTR record for IP (nxDomain)")
|
||||
}
|
||||
|
||||
log.Infof("ptrName: %s", ptrName)
|
||||
|
||||
// get forward record
|
||||
if strings.Contains(ip, ":") {
|
||||
rrCache = Resolve(ptrName, dns.Type(dns.TypeAAAA), securityLevel)
|
||||
} else {
|
||||
rrCache = Resolve(ptrName, dns.Type(dns.TypeA), securityLevel)
|
||||
}
|
||||
if rrCache == nil {
|
||||
return "", errors.New("querying for A/AAAA record failed (may be NXDomain)")
|
||||
}
|
||||
|
||||
// check for matching A/AAAA record
|
||||
log.Infof("rr: %s", rrCache)
|
||||
for _, rr := range rrCache.Answer {
|
||||
switch v := rr.(type) {
|
||||
case *dns.A:
|
||||
log.Infof("A: %s", v.A.String())
|
||||
if ip == v.A.String() {
|
||||
return ptrName, nil
|
||||
}
|
||||
case *dns.AAAA:
|
||||
log.Infof("AAAA: %s", v.AAAA.String())
|
||||
if ip == v.AAAA.String() {
|
||||
return ptrName, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// no match
|
||||
return "", errors.New("validation failed")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue