mirror of
https://github.com/safing/portmaster
synced 2025-09-04 19:49:15 +00:00
Rename intel to resolver
This commit is contained in:
parent
f270ccc21f
commit
55033404d4
18 changed files with 181 additions and 19 deletions
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Package intel is responsible for fetching intelligence data, including DNS, on remote entities.
|
package resolver is responsible for fetching intelligence data, including DNS, on remote entities.
|
||||||
|
|
||||||
DNS Servers
|
DNS Servers
|
||||||
|
|
||||||
|
@ -27,4 +27,4 @@ All other domains are resolved using search scopes and all available resolvers.
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
package intel
|
package resolver
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
|
"github.com/safing/portmaster/intel"
|
||||||
|
|
||||||
// module dependencies
|
// module dependencies
|
||||||
_ "github.com/safing/portmaster/core"
|
_ "github.com/safing/portmaster/core"
|
||||||
|
@ -16,10 +17,12 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
module = modules.Register("intel", prep, start, nil, "core", "network")
|
module = modules.Register("resolver", prep, start, nil, "core", "network")
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prep() error {
|
||||||
|
intel.SetReverseResolver(ResolveIPAndValidate)
|
||||||
|
|
||||||
return prepConfig()
|
return prepConfig()
|
||||||
}
|
}
|
||||||
|
|
6
resolver/main_test.go
Normal file
6
resolver/main_test.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package resolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
// portmaster tests helper
|
||||||
|
_ "github.com/safing/portmaster/core/pmtesting"
|
||||||
|
)
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -313,7 +313,7 @@ func listenForDNSPackets(conn *net.UDPConn, messages chan *dns.Msg) error {
|
||||||
for {
|
for {
|
||||||
n, err := conn.Read(buf)
|
n, err := conn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if module.ShutdownInProgress() {
|
if module.IsStopping() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
log.Debugf("intel: failed to read packet: %s", err)
|
log.Debugf("intel: failed to read packet: %s", err)
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
// DISABLE TESTING FOR NOW: find a way to have tests with the module system
|
// DISABLE TESTING FOR NOW: find a way to have tests with the module system
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
153
resolver/resolver.go
Normal file
153
resolver/resolver.go
Normal file
|
@ -0,0 +1,153 @@
|
||||||
|
package resolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/miekg/dns"
|
||||||
|
"github.com/safing/portbase/log"
|
||||||
|
"github.com/safing/portmaster/network/environment"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DNS Resolver Attributes
|
||||||
|
const (
|
||||||
|
ServerTypeDNS = "dns"
|
||||||
|
ServerTypeTCP = "tcp"
|
||||||
|
ServerTypeDoT = "dot"
|
||||||
|
ServerTypeDoH = "doh"
|
||||||
|
|
||||||
|
ServerSourceConfigured = "config"
|
||||||
|
ServerSourceAssigned = "dhcp"
|
||||||
|
ServerSourceMDNS = "mdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Resolver holds information about an active resolver.
|
||||||
|
type Resolver struct {
|
||||||
|
// Server config url (and ID)
|
||||||
|
Server string
|
||||||
|
|
||||||
|
// Parsed config
|
||||||
|
ServerType string
|
||||||
|
ServerAddress string
|
||||||
|
ServerIP net.IP
|
||||||
|
ServerIPScope int8
|
||||||
|
ServerPort uint16
|
||||||
|
|
||||||
|
// Special Options
|
||||||
|
VerifyDomain string
|
||||||
|
Search []string
|
||||||
|
SkipFQDN string
|
||||||
|
|
||||||
|
Source string
|
||||||
|
|
||||||
|
// logic interface
|
||||||
|
Conn ResolverConn
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the URL representation of the resolver.
|
||||||
|
func (resolver *Resolver) String() string {
|
||||||
|
return resolver.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolverConn is an interface to implement different types of query backends.
|
||||||
|
type ResolverConn interface {
|
||||||
|
Query(ctx context.Context, q *Query) (*RRCache, error)
|
||||||
|
MarkFailed()
|
||||||
|
LastFail() time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// BasicResolverConn implements ResolverConn for standard dns clients.
|
||||||
|
type BasicResolverConn struct {
|
||||||
|
sync.Mutex // for lastFail
|
||||||
|
|
||||||
|
resolver *Resolver
|
||||||
|
clientManager *clientManager
|
||||||
|
lastFail time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkFailed marks the resolver as failed.
|
||||||
|
func (brc *BasicResolverConn) MarkFailed() {
|
||||||
|
if !environment.Online() {
|
||||||
|
// don't mark failed if we are offline
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
brc.Lock()
|
||||||
|
defer brc.Unlock()
|
||||||
|
brc.lastFail = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// LastFail returns the internal lastfail value while locking the Resolver.
|
||||||
|
func (brc *BasicResolverConn) LastFail() time.Time {
|
||||||
|
brc.Lock()
|
||||||
|
defer brc.Unlock()
|
||||||
|
return brc.lastFail
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query executes the given query against the resolver.
|
||||||
|
func (brc *BasicResolverConn) Query(ctx context.Context, q *Query) (*RRCache, error) {
|
||||||
|
// convenience
|
||||||
|
resolver := brc.resolver
|
||||||
|
|
||||||
|
// create query
|
||||||
|
dnsQuery := new(dns.Msg)
|
||||||
|
dnsQuery.SetQuestion(q.FQDN, uint16(q.QType))
|
||||||
|
|
||||||
|
// start
|
||||||
|
var reply *dns.Msg
|
||||||
|
var err error
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
|
||||||
|
// log query time
|
||||||
|
// qStart := time.Now()
|
||||||
|
reply, _, err = brc.clientManager.getDNSClient().Exchange(dnsQuery, resolver.ServerAddress)
|
||||||
|
// log.Tracef("intel: query to %s took %s", resolver.Server, time.Now().Sub(qStart))
|
||||||
|
|
||||||
|
// error handling
|
||||||
|
if err != nil {
|
||||||
|
log.Tracer(ctx).Tracef("intel: query to %s encountered error: %s", resolver.Server, err)
|
||||||
|
|
||||||
|
// TODO: handle special cases
|
||||||
|
// 1. connect: network is unreachable
|
||||||
|
// 2. timeout
|
||||||
|
|
||||||
|
// hint network environment at failed connection
|
||||||
|
environment.ReportFailedConnection()
|
||||||
|
|
||||||
|
// temporary error
|
||||||
|
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
|
||||||
|
log.Tracer(ctx).Tracef("intel: retrying to resolve %s%s with %s, error is temporary", q.FQDN, q.QType, resolver.Server)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// permanent error
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// no error
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
// FIXME: mark as failed
|
||||||
|
}
|
||||||
|
|
||||||
|
// hint network environment at successful connection
|
||||||
|
environment.ReportSuccessfulConnection()
|
||||||
|
|
||||||
|
new := &RRCache{
|
||||||
|
Domain: q.FQDN,
|
||||||
|
Question: q.QType,
|
||||||
|
Answer: reply.Answer,
|
||||||
|
Ns: reply.Ns,
|
||||||
|
Extra: reply.Extra,
|
||||||
|
Server: resolver.Server,
|
||||||
|
ServerScope: resolver.ServerIPScope,
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: check if reply.Answer is valid
|
||||||
|
return new, nil
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
|
@ -1,4 +1,4 @@
|
||||||
package intel
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
Loading…
Add table
Reference in a new issue