Merge branch 'develop' of github.com:vlabo/portmaster into develop

This commit is contained in:
Vladimir 2022-04-13 19:31:09 +03:00
commit 4a7ab13bf5

View file

@ -2,8 +2,13 @@ package resolver
import ( import (
"context" "context"
"encoding/base64"
"errors" "errors"
"fmt"
"io/ioutil"
"net" "net"
"net/http"
"strings"
"time" "time"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -40,7 +45,7 @@ func (pr *PlainResolver) Query(ctx context.Context, q *Query) (*RRCache, error)
// create query // create query
dnsQuery := new(dns.Msg) dnsQuery := new(dns.Msg)
dnsQuery.SetQuestion(q.FQDN, uint16(q.QType)) dnsQuery.SetQuestion(q.FQDN, uint16(q.QType))
var reply *dns.Msg
// get timeout from context and config // get timeout from context and config
var timeout time.Duration var timeout time.Duration
if deadline, ok := ctx.Deadline(); !ok { if deadline, ok := ctx.Deadline(); !ok {
@ -52,32 +57,51 @@ func (pr *PlainResolver) Query(ctx context.Context, q *Query) (*RRCache, error)
timeout = defaultRequestTimeout timeout = defaultRequestTimeout
} }
// create client if strings.HasPrefix(pr.resolver.ServerAddress, "https:") {
dnsClient := &dns.Client{ buf, err := dnsQuery.Pack()
Timeout: timeout,
Dialer: &net.Dialer{
Timeout: timeout,
LocalAddr: getLocalAddr("udp"),
},
}
// query server if err != nil {
reply, ttl, err := dnsClient.Exchange(dnsQuery, pr.resolver.ServerAddress) return nil, err
log.Tracer(ctx).Tracef("resolver: query took %s", ttl)
// error handling
if err != nil {
// Hint network environment at failed connection if err is not a timeout.
var nErr net.Error
if errors.As(err, &nErr) && !nErr.Timeout() {
netenv.ReportFailedConnection()
} }
return nil, err b64dns := base64.RawStdEncoding.EncodeToString(buf)
} url := fmt.Sprintf("%s/dns-query?dns=%s", pr.resolver.ServerAddress, b64dns)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
reply := new(dns.Msg)
reply.Unpack(body)
} else {
// create client
dnsClient := &dns.Client{
Timeout: timeout,
Dialer: &net.Dialer{
Timeout: timeout,
LocalAddr: getLocalAddr("udp"),
},
}
// check if blocked // query server
if pr.resolver.IsBlockedUpstream(reply) { reply, ttl, err := dnsClient.Exchange(dnsQuery, pr.resolver.ServerAddress)
return nil, &BlockedUpstreamError{pr.resolver.Info.DescriptiveName()} log.Tracer(ctx).Tracef("resolver: query took %s", ttl)
// error handling
if err != nil {
// Hint network environment at failed connection if err is not a timeout.
var nErr net.Error
if errors.As(err, &nErr) && !nErr.Timeout() {
netenv.ReportFailedConnection()
}
return nil, err
}
// check if blocked
if pr.resolver.IsBlockedUpstream(reply) {
return nil, &BlockedUpstreamError{pr.resolver.Info.DescriptiveName()}
}
} }
// hint network environment at successful connection // hint network environment at successful connection