2024-07-21 07:57:47 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
2024-07-31 11:47:19 +00:00
|
|
|
"strconv"
|
2024-07-21 07:57:47 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/likexian/doh"
|
|
|
|
dohDns "github.com/likexian/doh/dns"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/xvzc/SpoofDPI/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DnsResolver struct {
|
|
|
|
host string
|
|
|
|
port string
|
|
|
|
enableDoh bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewResolver(config *util.Config) *DnsResolver {
|
|
|
|
return &DnsResolver{
|
|
|
|
host: *config.DnsAddr,
|
2024-07-21 08:12:16 +00:00
|
|
|
port: strconv.Itoa(*config.DnsPort),
|
2024-07-21 07:57:47 +00:00
|
|
|
enableDoh: *config.EnableDoh,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DnsResolver) Lookup(domain string) (string, error) {
|
|
|
|
ipRegex := "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
|
|
|
|
|
|
|
|
if r, _ := regexp.MatchString(ipRegex, domain); r {
|
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.enableDoh {
|
|
|
|
return dohLookup(domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsServer := d.host + ":" + d.port
|
|
|
|
|
|
|
|
msg := new(dns.Msg)
|
|
|
|
msg.SetQuestion(dns.Fqdn(domain), dns.TypeA)
|
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
|
|
|
|
response, _, err := c.Exchange(msg, dnsServer)
|
|
|
|
if err != nil {
|
2024-07-22 11:24:35 +00:00
|
|
|
return "", errors.New("couldn not resolve the domain")
|
2024-07-21 07:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, answer := range response.Answer {
|
|
|
|
if record, ok := answer.(*dns.A); ok {
|
2024-07-31 11:47:19 +00:00
|
|
|
log.Debug("[DNS] resolved ", domain, ": ", record.A.String())
|
2024-07-21 07:57:47 +00:00
|
|
|
return record.A.String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 11:47:19 +00:00
|
|
|
return "", errors.New("no record found")
|
2024-07-21 07:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dohLookup(domain string) (string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
c := doh.Use(doh.CloudflareProvider, doh.GoogleProvider)
|
|
|
|
|
|
|
|
rsp, err := c.Query(ctx, dohDns.Domain(domain), dohDns.TypeA)
|
|
|
|
if err != nil {
|
2024-07-31 11:47:19 +00:00
|
|
|
return "", errors.New("could not resolve the domain")
|
2024-07-21 07:57:47 +00:00
|
|
|
}
|
|
|
|
// doh dns answer
|
|
|
|
answer := rsp.Answer
|
|
|
|
|
|
|
|
// print all answer
|
|
|
|
for _, a := range answer {
|
|
|
|
if a.Type != 1 { // Type == 1 -> A Record
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-07-31 11:47:19 +00:00
|
|
|
log.Debug("[DOH] resolved ", domain, ": ", a.Data)
|
2024-07-21 07:57:47 +00:00
|
|
|
return a.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the client
|
|
|
|
c.Close()
|
|
|
|
|
2024-07-31 11:47:19 +00:00
|
|
|
return "", errors.New("no record found")
|
2024-07-21 07:57:47 +00:00
|
|
|
}
|