Fix tests and linters

This commit is contained in:
Daniel
2022-02-02 12:48:42 +01:00
parent f2fcad4d11
commit 60d8664e7b
171 changed files with 944 additions and 874 deletions

View File

@@ -7,6 +7,7 @@ import (
"strings"
"github.com/miekg/dns"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/nameserver/nsutil"
)

View File

@@ -8,12 +8,13 @@ import (
"strings"
"sync"
"golang.org/x/net/publicsuffix"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/intel/filterlists"
"github.com/safing/portmaster/intel/geoip"
"github.com/safing/portmaster/network/netutils"
"github.com/safing/portmaster/status"
"golang.org/x/net/publicsuffix"
)
// Entity describes a remote endpoint in many different ways.
@@ -21,7 +22,7 @@ import (
// functions performs locking. The caller MUST ENSURE
// proper locking and synchronization when accessing
// any properties of Entity.
type Entity struct {
type Entity struct { //nolint:maligned
sync.Mutex
// lists exist for most entity information and
@@ -319,7 +320,7 @@ func (e *Entity) getDomainLists(ctx context.Context) {
log.Tracer(ctx).Tracef("intel: loading domain list for %s", domain)
e.loadDomainListOnce.Do(func() {
var domainsToInspect = []string{domain}
domainsToInspect := []string{domain}
if e.checkCNAMEs && len(e.CNAME) > 0 {
log.Tracer(ctx).Tracef("intel: CNAME filtering enabled, checking %v too", e.CNAME)

View File

@@ -6,9 +6,10 @@ import (
"strings"
"sync"
"github.com/tannerryan/ring"
"github.com/safing/portbase/database/record"
"github.com/safing/portbase/log"
"github.com/tannerryan/ring"
)
var defaultFilter = newScopedBloom()

View File

@@ -4,9 +4,9 @@ import (
"fmt"
"sync"
"github.com/safing/portbase/database"
"github.com/hashicorp/go-version"
"github.com/safing/portbase/database"
"github.com/safing/portbase/database/record"
)

View File

@@ -9,12 +9,13 @@ import (
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/safing/portbase/database"
"github.com/safing/portbase/database/record"
"github.com/safing/portbase/log"
"github.com/safing/portbase/updater"
"github.com/safing/portmaster/updates"
"golang.org/x/sync/errgroup"
)
const (
@@ -46,13 +47,11 @@ var (
filterListsLoaded chan struct{}
)
var (
cache = database.NewInterface(&database.Options{
Local: true,
Internal: true,
CacheSize: 2 ^ 8,
})
)
var cache = database.NewInterface(&database.Options{
Local: true,
Internal: true,
CacheSize: 2 ^ 8,
})
// getFileFunc is the function used to get a file from
// the updater. It's basically updates.GetFile and used
@@ -85,7 +84,9 @@ func processListFile(ctx context.Context, filter *scopedBloom, file *updater.Fil
if err != nil {
return err
}
defer f.Close()
defer func() {
_ = f.Close()
}()
values := make(chan *listEntry, 100)
records := make(chan record.Record, 100)

View File

@@ -4,6 +4,7 @@ import (
"compress/gzip"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
@@ -57,7 +58,7 @@ func decodeFile(ctx context.Context, r io.Reader, ch chan<- *listEntry) error {
entryCount++
length, readErr := binary.ReadUvarint(reader)
if readErr != nil {
if readErr == io.EOF {
if errors.Is(readErr, io.EOF) {
return nil
}
return fmt.Errorf("failed to load varint entity length: %w", readErr)
@@ -66,7 +67,7 @@ func decodeFile(ctx context.Context, r io.Reader, ch chan<- *listEntry) error {
blob := make([]byte, length)
_, readErr = io.ReadFull(reader, blob)
if readErr != nil {
if readErr == io.EOF {
if errors.Is(readErr, io.EOF) {
// there shouldn't be an EOF here because
// we actually got a length above. Return
// ErrUnexpectedEOF instead of just EOF.

View File

@@ -163,7 +163,7 @@ func getListIndexFromCache() (*ListIndexFile, error) {
}
var (
// listIndexUpdate must only be used by updateListIndex
// listIndexUpdate must only be used by updateListIndex.
listIndexUpdate *updater.File
listIndexUpdateLock sync.Mutex
)
@@ -232,9 +232,8 @@ func updateListIndex() error {
// a slice of distinct source IDs.
func ResolveListIDs(ids []string) ([]string, error) {
index, err := getListIndexFromCache()
if err != nil {
if err == database.ErrNotFound {
if errors.Is(err, database.ErrNotFound) {
if err := updateListIndex(); err != nil {
return nil, err
}

View File

@@ -33,7 +33,7 @@ func lookupBlockLists(entity, value string) ([]string, error) {
// log.Debugf("intel/filterlists: searching for entries with %s", key)
entry, err := getEntityRecordByKey(key)
if err != nil {
if err == database.ErrNotFound {
if errors.Is(err, database.ErrNotFound) {
return nil, nil
}
log.Errorf("intel/filterlists: failed to get entries for key %s: %s", key, err)
@@ -103,7 +103,6 @@ func LookupIPv4String(ipv4 string) ([]string, error) {
// LookupIPv4 is like LookupIPv4String but accepts a net.IP.
func LookupIPv4(ipv4 net.IP) ([]string, error) {
ip := ipv4.To4()
if ip == nil {
return nil, errors.New("invalid IPv4")

View File

@@ -4,22 +4,20 @@ import (
"context"
"fmt"
"github.com/tevino/abool"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/portmaster/netenv"
"github.com/safing/portmaster/updates"
"github.com/tevino/abool"
)
var (
module *modules.Module
)
var module *modules.Module
const (
filterlistsDisabled = "filterlists:disabled"
filterlistsUpdateFailed = "filterlists:update-failed"
filterlistsStaleDataSurvived = "filterlists:staledata"
filterlistsUpdateInProgress = "filterlists:update-in-progress"
)
// booleans mainly used to decouple the module

View File

@@ -24,17 +24,17 @@ func getEntityRecordByKey(key string) (*entityRecord, error) {
}
if r.IsWrapped() {
new := &entityRecord{}
if err := record.Unwrap(r, new); err != nil {
newER := &entityRecord{}
if err := record.Unwrap(r, newER); err != nil {
return nil, err
}
return new, nil
return newER, nil
}
e, ok := r.(*entityRecord)
newER, ok := r.(*entityRecord)
if !ok {
return nil, fmt.Errorf("record not of type *entityRecord, but %T", r)
}
return e, nil
return newER, nil
}

View File

@@ -2,17 +2,19 @@ package filterlists
import (
"context"
"errors"
"fmt"
"sort"
"time"
"github.com/hashicorp/go-version"
"github.com/tevino/abool"
"github.com/safing/portbase/database"
"github.com/safing/portbase/database/query"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/updater"
"github.com/tevino/abool"
)
var updateInProgress = abool.New()
@@ -21,7 +23,6 @@ var updateInProgress = abool.New()
// error state is correctly set or resolved.
func tryListUpdate(ctx context.Context) error {
err := performUpdate(ctx)
if err != nil {
// Check if the module already has a failure status set. If not, set a
// generic one with the returned error.
@@ -123,7 +124,7 @@ func performUpdate(ctx context.Context) error {
module.Warning(
filterlistsStaleDataSurvived,
"Filter Lists May Overblock",
fmt.Sprintf("The Portmaster failed to delete outdated filter list data. Filtering capabilities are fully available, but overblocking may occur. Error: %s", err.Error()),
fmt.Sprintf("The Portmaster failed to delete outdated filter list data. Filtering capabilities are fully available, but overblocking may occur. Error: %s", err.Error()), //nolint:misspell // overblocking != overclocking
)
return fmt.Errorf("failed to cleanup stale cache records: %w", err)
}
@@ -137,7 +138,7 @@ func performUpdate(ctx context.Context) error {
log.Infof("intel/filterlists: successfully migrated cache database to %s", highestVersion.Version())
}
// The list update suceeded, resolve any states.
// The list update succeeded, resolve any states.
module.Resolve("")
return nil
}
@@ -178,7 +179,7 @@ func getUpgradableFiles() ([]*updater.File, error) {
if intermediateFile == nil || intermediateFile.UpgradeAvailable() || !cacheDBInUse {
var err error
intermediateFile, err = getFile(intermediateListFilePath)
if err != nil && err != updater.ErrNotFound {
if err != nil && !errors.Is(err, updater.ErrNotFound) {
return nil, err
}
@@ -191,7 +192,7 @@ func getUpgradableFiles() ([]*updater.File, error) {
if urgentFile == nil || urgentFile.UpgradeAvailable() || !cacheDBInUse {
var err error
urgentFile, err = getFile(urgentListFilePath)
if err != nil && err != updater.ErrNotFound {
if err != nil && !errors.Is(err, updater.ErrNotFound) {
return nil, err
}
@@ -216,7 +217,7 @@ func resolveUpdateOrder(updateOrder []*updater.File) ([]*updater.File, error) {
var err error
cacheDBVersion, err = getCacheDatabaseVersion()
if err != nil {
if err != database.ErrNotFound {
if !errors.Is(err, database.ErrNotFound) {
log.Errorf("intel/filterlists: failed to get cache database version: %s", err)
}
cacheDBVersion, _ = version.NewSemver("v0.0.0")
@@ -247,12 +248,14 @@ func resolveUpdateOrder(updateOrder []*updater.File) ([]*updater.File, error) {
type byAscVersion []*updater.File
func (fs byAscVersion) Len() int { return len(fs) }
func (fs byAscVersion) Less(i, j int) bool {
vi, _ := version.NewSemver(fs[i].Version())
vj, _ := version.NewSemver(fs[j].Version())
return vi.LessThan(vj)
}
func (fs byAscVersion) Swap(i, j int) {
fi := fs[i]
fj := fs[j]

View File

@@ -58,7 +58,7 @@ func (ub *updateBroadcaster) ReplaceDatabase(db *geoIPDB) {
defer ub.rw.Unlock()
if ub.db != nil {
ub.db.Close()
_ = ub.db.Close()
}
ub.db = db
ub.notifyWaiters()
@@ -101,7 +101,7 @@ type updateWorker struct {
// waiting nil is returned.
func (upd *updateWorker) GetReader(v6 bool, wait bool) *maxminddb.Reader {
// check which updateBroadcaster we need to use
var ub *updateBroadcaster = &upd.v4
ub := &upd.v4
if v6 {
ub = &upd.v6
}

View File

@@ -5,8 +5,9 @@ import (
"net"
"strings"
"github.com/safing/portbase/utils"
"github.com/umahmood/haversine"
"github.com/safing/portbase/utils"
)
const (
@@ -27,6 +28,7 @@ type Location struct {
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
}
// Coordinates holds geographic coordinates and their estimated accuracy.
type Coordinates struct {
AccuracyRadius uint16 `maxminddb:"accuracy_radius"`
Latitude float64 `maxminddb:"latitude"`
@@ -199,6 +201,8 @@ var unknownASOrgNames = []string{
"undefined", // Programmatic unknown value.
}
// ASOrgUnknown return whether the given AS Org string actually is meant to
// mean that the AS Org is unknown.
func ASOrgUnknown(asOrg string) bool {
return utils.StringInSlice(
unknownASOrgNames,

View File

@@ -6,6 +6,8 @@ import (
)
func TestPrimitiveNetworkProximity(t *testing.T) {
t.Parallel()
ip4_1 := net.ParseIP("1.1.1.1")
ip4_2 := net.ParseIP("1.1.1.2")
ip4_3 := net.ParseIP("255.255.255.0")

View File

@@ -6,6 +6,8 @@ import (
)
func TestLocationLookup(t *testing.T) {
t.Parallel()
ip1 := net.ParseIP("81.2.69.142")
loc1, err := GetLocation(ip1)
if err != nil {
@@ -53,8 +55,8 @@ func TestLocationLookup(t *testing.T) {
dist3 := loc1.EstimateNetworkProximity(loc3)
dist4 := loc1.EstimateNetworkProximity(loc4)
t.Logf("proximity %s <> %s: %d", ip1, ip2, dist1)
t.Logf("proximity %s <> %s: %d", ip2, ip3, dist2)
t.Logf("proximity %s <> %s: %d", ip1, ip3, dist3)
t.Logf("proximity %s <> %s: %d", ip1, ip4, dist4)
t.Logf("proximity %s <> %s: %.2f", ip1, ip2, dist1)
t.Logf("proximity %s <> %s: %.2f", ip2, ip3, dist2)
t.Logf("proximity %s <> %s: %.2f", ip1, ip3, dist3)
t.Logf("proximity %s <> %s: %.2f", ip1, ip4, dist4)
}

View File

@@ -4,10 +4,8 @@ import (
"github.com/safing/portbase/modules"
)
var (
// Module of this package. Export needed for testing of the endpoints package.
Module *modules.Module
)
// Module of this package. Export needed for testing of the endpoints package.
var Module *modules.Module
func init() {
Module = modules.Register("intel", nil, nil, nil, "geoip", "filterlists")

View File

@@ -4,9 +4,7 @@ import (
"context"
)
var (
reverseResolver func(ctx context.Context, ip string, securityLevel uint8) (domain string, err error)
)
var reverseResolver func(ctx context.Context, ip string, securityLevel uint8) (domain string, err error)
// SetReverseResolver allows the resolver module to register a function to allow reverse resolving IPs to domains.
func SetReverseResolver(fn func(ctx context.Context, ip string, securityLevel uint8) (domain string, err error)) {