Fix dependency graph and linter errors

This commit is contained in:
Daniel 2020-04-02 17:09:15 +02:00
parent 180f27307c
commit eec0c37101
23 changed files with 164 additions and 61 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/safing/portbase/log" "github.com/safing/portbase/log"
) )
// Configuration Keys
var ( var (
CfgDevModeKey = "core/devMode" CfgDevModeKey = "core/devMode"
defaultDevMode bool defaultDevMode bool

View file

@ -3,9 +3,14 @@ package core
import ( import (
"fmt" "fmt"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/modules/subsystems" "github.com/safing/portbase/modules/subsystems"
"github.com/safing/portbase/modules" // module dependencies
_ "github.com/safing/portbase/rng"
_ "github.com/safing/portmaster/status"
_ "github.com/safing/portmaster/ui"
_ "github.com/safing/portmaster/updates"
) )
var ( var (
@ -13,7 +18,9 @@ var (
) )
func init() { func init() {
module = modules.Register("core", nil, start, nil, "database", "config", "api", "random", "notifications", "subsystems", "ui", "updates", "status") modules.Register("base", nil, registerDatabases, nil, "database", "config", "random")
module = modules.Register("core", nil, start, nil, "base", "subsystems", "status", "updates", "api", "notifications", "ui")
subsystems.Register( subsystems.Register(
"core", "core",
"Core", "Core",
@ -29,5 +36,5 @@ func start() error {
return fmt.Errorf("failed to start plattform-specific components: %s", err) return fmt.Errorf("failed to start plattform-specific components: %s", err)
} }
return registerDatabases() return nil
} }

View file

@ -10,6 +10,7 @@ import (
_ "github.com/safing/portbase/database/storage/bbolt" _ "github.com/safing/portbase/database/storage/bbolt"
) )
// Default Values (changeable for testing)
var ( var (
DefaultDatabaseStorageType = "bbolt" DefaultDatabaseStorageType = "bbolt"
) )

View file

@ -12,7 +12,10 @@ import (
"github.com/safing/portbase/notifications" "github.com/safing/portbase/notifications"
) )
// Default Values (changeable for testing)
var ( var (
DefaultAPIListenAddress = "127.0.0.1:817"
dataDir string dataDir string
databaseDir string databaseDir string
) )
@ -53,7 +56,7 @@ func globalPrep() error {
} }
// set api listen address // set api listen address
api.SetDefaultAPIListenAddress("127.0.0.1:817") api.SetDefaultAPIListenAddress(DefaultAPIListenAddress)
// set notification persistence // set notification persistence
notifications.SetPersistenceBasePath("core:notifications") notifications.SetPersistenceBasePath("core:notifications")

View file

@ -1,6 +1,18 @@
// package coretest provides a simple unit test setup routine. // Package pmtesting provides a simple unit test setup routine.
// //
// Just include `_ "github.com/safing/portmaster/core/pmtesting"` // Usage:
//
// package name
//
// import (
// "testing"
//
// "github.com/safing/portmaster/core/pmtesting"
// )
//
// func TestMain(m *testing.M) {
// pmtesting.TestMain(m, module)
// }
// //
package pmtesting package pmtesting
@ -29,10 +41,17 @@ func init() {
flag.BoolVar(&printStackOnExit, "print-stack-on-exit", false, "prints the stack before of shutting down") flag.BoolVar(&printStackOnExit, "print-stack-on-exit", false, "prints the stack before of shutting down")
} }
func TestMain(m *testing.M) { // TestMain provides a simple unit test setup routine.
func TestMain(m *testing.M, module *modules.Module) {
// enable module for testing
module.Enable()
// switch databases to memory only // switch databases to memory only
core.DefaultDatabaseStorageType = "hashmap" core.DefaultDatabaseStorageType = "hashmap"
// switch API to high port
core.DefaultAPIListenAddress = "127.0.0.1:10817"
// set log level // set log level
log.SetLogLevel(log.TraceLevel) log.SetLogLevel(log.TraceLevel)
@ -40,19 +59,22 @@ func TestMain(m *testing.M) {
tmpDir := filepath.Join(os.TempDir(), "portmaster-testing") tmpDir := filepath.Join(os.TempDir(), "portmaster-testing")
// initialize data dir // initialize data dir
err := dataroot.Initialize(tmpDir, 0755) err := dataroot.Initialize(tmpDir, 0755)
// start modules
if err == nil {
err = modules.Start()
}
// handle setup error
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup test: %s\n", err) fmt.Fprintf(os.Stderr, "failed to initialize data root: %s\n", err)
printStack()
os.Exit(1) os.Exit(1)
} }
// start modules
var exitCode int
err = modules.Start()
if err != nil {
// starting failed
fmt.Fprintf(os.Stderr, "failed to setup test: %s\n", err)
exitCode = 1
} else {
// run tests // run tests
exitCode := m.Run() exitCode = m.Run()
}
// shutdown // shutdown
_ = modules.Shutdown() _ = modules.Shutdown()
@ -63,7 +85,14 @@ func TestMain(m *testing.M) {
printStack() printStack()
// clean up and exit // clean up and exit
// keep! os.RemoveAll(tmpDir)
// Important: Do not remove tmpDir, as it is used as a cache for updates.
// remove config
_ = os.Remove(filepath.Join(tmpDir, "config.json"))
// remove databases
_ = os.Remove(filepath.Join(tmpDir, "databases.json"))
_ = os.RemoveAll(filepath.Join(tmpDir, "databases"))
os.Exit(exitCode) os.Exit(exitCode)
} }

View file

@ -4,6 +4,7 @@ import (
"github.com/safing/portbase/config" "github.com/safing/portbase/config"
) )
// Configuration Keys
var ( var (
CfgOptionEnableFilterKey = "filter/enable" CfgOptionEnableFilterKey = "filter/enable"

View file

@ -4,13 +4,13 @@ import (
"fmt" "fmt"
"net" "net"
"github.com/safing/portmaster/network/environment" "github.com/safing/portmaster/netenv"
"github.com/safing/portmaster/resolver" "github.com/safing/portmaster/resolver"
) )
func init() { func init() {
resolver.SetLocalAddrFactory(PermittedAddr) resolver.SetLocalAddrFactory(PermittedAddr)
environment.SetLocalAddrFactory(PermittedAddr) netenv.SetLocalAddrFactory(PermittedAddr)
} }
// PermittedAddr returns an already permitted local address for the given network for reliable connectivity. // PermittedAddr returns an already permitted local address for the given network for reliable connectivity.

View file

@ -44,7 +44,7 @@ var (
) )
func init() { func init() {
module = modules.Register("firewall", prep, start, stop, "core", "network", "resolver", "intel", "processes") module = modules.Register("filter", prep, start, stop, "core", "network", "nameserver", "intel")
subsystems.Register( subsystems.Register(
"filter", "filter",
"Privacy Filter", "Privacy Filter",

View file

@ -7,5 +7,5 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
pmtesting.TestMain(m) pmtesting.TestMain(m, module)
} }

View file

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

View file

@ -11,8 +11,8 @@ import (
"github.com/safing/portbase/modules" "github.com/safing/portbase/modules"
"github.com/safing/portmaster/detection/dga" "github.com/safing/portmaster/detection/dga"
"github.com/safing/portmaster/firewall" "github.com/safing/portmaster/firewall"
"github.com/safing/portmaster/netenv"
"github.com/safing/portmaster/network" "github.com/safing/portmaster/network"
"github.com/safing/portmaster/network/environment"
"github.com/safing/portmaster/network/netutils" "github.com/safing/portmaster/network/netutils"
"github.com/safing/portmaster/resolver" "github.com/safing/portmaster/resolver"
@ -30,7 +30,7 @@ var (
) )
func init() { func init() {
module = modules.Register("nameserver", prep, start, stop, "core", "resolver", "network") module = modules.Register("nameserver", prep, start, stop, "core", "resolver", "network", "netenv")
subsystems.Register( subsystems.Register(
"dns", "dns",
"Secure DNS", "Secure DNS",
@ -108,9 +108,9 @@ func handleRequestAsMicroTask(w dns.ResponseWriter, query *dns.Msg) {
} }
} }
func handleRequest(ctx context.Context, w dns.ResponseWriter, query *dns.Msg) error { func handleRequest(ctx context.Context, w dns.ResponseWriter, query *dns.Msg) error { //nolint:gocognit // TODO
// return with server failure if offline // return with server failure if offline
if environment.GetOnlineStatus() == environment.StatusOffline { if netenv.GetOnlineStatus() == netenv.StatusOffline {
returnServerFailure(w, query) returnServerFailure(w, query)
return nil return nil
} }

View file

@ -5,15 +5,13 @@ import (
"net" "net"
"strings" "strings"
"github.com/safing/portmaster/network/environment"
"github.com/miekg/dns"
"github.com/safing/portbase/log" "github.com/safing/portbase/log"
"github.com/safing/portbase/modules" "github.com/safing/portbase/modules"
"github.com/safing/portmaster/netenv"
"github.com/safing/portmaster/intel"
"github.com/safing/portmaster/network/netutils" "github.com/safing/portmaster/network/netutils"
"github.com/safing/portmaster/resolver"
"github.com/miekg/dns"
) )
var ( var (
@ -27,7 +25,7 @@ var (
) )
func init() { func init() {
module = modules.Register("nameserver", initLocalhostRRs, start, stop, "core", "intel", "network") module = modules.Register("nameserver", initLocalhostRRs, start, stop, "core", "resolver", "network", "netenv")
} }
func initLocalhostRRs() error { func initLocalhostRRs() error {
@ -53,7 +51,7 @@ func start() error {
err := dnsServer.ListenAndServe() err := dnsServer.ListenAndServe()
if err != nil { if err != nil {
// check if we are shutting down // check if we are shutting down
if module.ShutdownInProgress() { if module.IsStopping() {
return nil return nil
} }
} }
@ -87,20 +85,20 @@ func handleRequestAsMicroTask(w dns.ResponseWriter, query *dns.Msg) {
return handleRequest(ctx, w, query) return handleRequest(ctx, w, query)
}) })
if err != nil { if err != nil {
log.Warningf("intel: failed to handle dns request: %s", err) log.Warningf("nameserver: failed to handle dns request: %s", err)
} }
} }
func handleRequest(ctx context.Context, w dns.ResponseWriter, query *dns.Msg) error { func handleRequest(ctx context.Context, w dns.ResponseWriter, query *dns.Msg) error {
// return with server failure if offline // return with server failure if offline
if environment.GetOnlineStatus() == environment.StatusOffline { if netenv.GetOnlineStatus() == netenv.StatusOffline {
returnServerFailure(w, query) returnServerFailure(w, query)
return nil return nil
} }
// only process first question, that's how everyone does it. // only process first question, that's how everyone does it.
question := query.Question[0] question := query.Question[0]
q := &intel.Query{ q := &resolver.Query{
FQDN: question.Name, FQDN: question.Name,
QType: dns.Type(question.Qtype), QType: dns.Type(question.Qtype),
} }
@ -157,7 +155,7 @@ func handleRequest(ctx context.Context, w dns.ResponseWriter, query *dns.Msg) er
// TODO: if there are 3 request for the same domain/type in a row, delete all caches of that domain // TODO: if there are 3 request for the same domain/type in a row, delete all caches of that domain
// get intel and RRs // get intel and RRs
rrCache, err := intel.Resolve(ctx, q) rrCache, err := resolver.Resolve(ctx, q)
if err != nil { if err != nil {
// TODO: analyze nxdomain requests, malware could be trying DGA-domains // TODO: analyze nxdomain requests, malware could be trying DGA-domains
tracer.Warningf("nameserver: request for %s%s: %s", q.FQDN, q.QType, err) tracer.Warningf("nameserver: request for %s%s: %s", q.FQDN, q.QType, err)
@ -169,9 +167,9 @@ func handleRequest(ctx context.Context, w dns.ResponseWriter, query *dns.Msg) er
for _, rr := range append(rrCache.Answer, rrCache.Extra...) { for _, rr := range append(rrCache.Answer, rrCache.Extra...) {
switch v := rr.(type) { switch v := rr.(type) {
case *dns.A: case *dns.A:
ipInfo, err := intel.GetIPInfo(v.A.String()) ipInfo, err := resolver.GetIPInfo(v.A.String())
if err != nil { if err != nil {
ipInfo = &intel.IPInfo{ ipInfo = &resolver.IPInfo{
IP: v.A.String(), IP: v.A.String(),
Domains: []string{q.FQDN}, Domains: []string{q.FQDN},
} }
@ -183,9 +181,9 @@ func handleRequest(ctx context.Context, w dns.ResponseWriter, query *dns.Msg) er
} }
} }
case *dns.AAAA: case *dns.AAAA:
ipInfo, err := intel.GetIPInfo(v.AAAA.String()) ipInfo, err := resolver.GetIPInfo(v.AAAA.String())
if err != nil { if err != nil {
ipInfo = &intel.IPInfo{ ipInfo = &resolver.IPInfo{
IP: v.AAAA.String(), IP: v.AAAA.String(),
Domains: []string{q.FQDN}, Domains: []string{q.FQDN},
} }

View file

@ -9,14 +9,12 @@ import (
"strings" "strings"
"syscall" "syscall"
"github.com/safing/portbase/updater" "github.com/safing/portbase/dataroot"
"github.com/safing/portmaster/core/structure"
"github.com/safing/portbase/utils"
"github.com/safing/portbase/info" "github.com/safing/portbase/info"
portlog "github.com/safing/portbase/log" portlog "github.com/safing/portbase/log"
"github.com/safing/portbase/updater"
"github.com/safing/portbase/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -158,15 +156,15 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) {
// remove redundant escape characters and quotes // remove redundant escape characters and quotes
dataDir = strings.Trim(dataDir, `\"`) dataDir = strings.Trim(dataDir, `\"`)
// initialize structure // initialize dataroot
err = structure.Initialize(dataDir, 0755) err = dataroot.Initialize(dataDir, 0755)
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize data root: %s", err) return fmt.Errorf("failed to initialize data root: %s", err)
} }
dataRoot = structure.Root() dataRoot = dataroot.Root()
// initialize registry // initialize registry
err := registry.Initialize(structure.Root().ChildDir("updates", 0755)) err := registry.Initialize(dataRoot.ChildDir("updates", 0755))
if err != nil { if err != nil {
return err return err
} }
@ -187,7 +185,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) {
// logs and warning // logs and warning
if !showShortVersion && !showFullVersion && !strings.Contains(cmd.CommandPath(), " show ") { if !showShortVersion && !showFullVersion && !strings.Contains(cmd.CommandPath(), " show ") {
// set up logs root // set up logs root
logsRoot = structure.NewRootDir("logs", 0777) logsRoot = dataRoot.ChildDir("logs", 0777)
err = logsRoot.Ensure() err = logsRoot.Ensure()
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize logs root: %s", err) return fmt.Errorf("failed to initialize logs root: %s", err)

View file

@ -4,6 +4,7 @@ import (
"github.com/safing/portbase/config" "github.com/safing/portbase/config"
) )
// Configuration Keys
var ( var (
CfgOptionEnableProcessDetectionKey = "core/enableProcessDetection" CfgOptionEnableProcessDetectionKey = "core/enableProcessDetection"
enableProcessDetection config.BoolOption enableProcessDetection config.BoolOption

11
process/module_test.go Normal file
View file

@ -0,0 +1,11 @@
package process
import (
"testing"
"github.com/safing/portmaster/core/pmtesting"
)
func TestMain(m *testing.M) {
pmtesting.TestMain(m, module)
}

View file

@ -4,6 +4,7 @@ import (
"github.com/safing/portbase/config" "github.com/safing/portbase/config"
) )
// Configuration Keys
var ( var (
cfgStringOptions = make(map[string]config.StringOption) cfgStringOptions = make(map[string]config.StringOption)
cfgStringArrayOptions = make(map[string]config.StringArrayOption) cfgStringArrayOptions = make(map[string]config.StringArrayOption)

View file

@ -10,7 +10,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
pmtesting.TestMain(m) pmtesting.TestMain(m, intel.Module)
} }
func testEndpointMatch(t *testing.T, ep Endpoint, entity *intel.Entity, expectedResult EPResult) { func testEndpointMatch(t *testing.T, ep Endpoint, entity *intel.Entity, expectedResult EPResult) {

View file

@ -5,6 +5,7 @@ import (
"github.com/safing/portbase/log" "github.com/safing/portbase/log"
) )
// FindOrCreateLocalProfileByPath returns an existing or new profile for the given application path.
func FindOrCreateLocalProfileByPath(fullPath string) (profile *Profile, new bool, err error) { func FindOrCreateLocalProfileByPath(fullPath string) (profile *Profile, new bool, err error) {
// find local profile // find local profile
it, err := profileDB.Query( it, err := profileDB.Query(

View file

@ -47,3 +47,41 @@ func (profile *Profile) AddFingerprint(fp *Fingerprint) {
profile.Fingerprints = append(profile.Fingerprints, fp) profile.Fingerprints = append(profile.Fingerprints, fp)
} }
*/ */
// TODO: matching
/*
//nolint:deadcode,unused // FIXME
func matchProfile(p *Process, prof *profile.Profile) (score int) {
for _, fp := range prof.Fingerprints {
score += matchFingerprint(p, fp)
}
return
}
//nolint:deadcode,unused // FIXME
func matchFingerprint(p *Process, fp *profile.Fingerprint) (score int) {
if !fp.MatchesOS() {
return 0
}
switch fp.Type {
case "full_path":
if p.Path == fp.Value {
return profile.GetFingerprintWeight(fp.Type)
}
case "partial_path":
// FIXME: if full_path matches, do not match partial paths
return profile.GetFingerprintWeight(fp.Type)
case "md5_sum", "sha1_sum", "sha256_sum":
// FIXME: one sum is enough, check sums in a grouped form, start with the best
sum, err := p.GetExecHash(fp.Type)
if err != nil {
log.Errorf("process: failed to get hash of executable: %s", err)
} else if sum == fp.Value {
return profile.GetFingerprintWeight(fp.Type)
}
}
return 0
}
*/

11
profile/module_test.go Normal file
View file

@ -0,0 +1,11 @@
package profile
import (
"testing"
"github.com/safing/portmaster/core/pmtesting"
)
func TestMain(m *testing.M) {
pmtesting.TestMain(m, module)
}

View file

@ -4,13 +4,10 @@ import (
"github.com/safing/portbase/database" "github.com/safing/portbase/database"
"github.com/safing/portbase/log" "github.com/safing/portbase/log"
"github.com/safing/portbase/modules" "github.com/safing/portbase/modules"
// module dependencies
_ "github.com/safing/portmaster/core"
) )
func init() { func init() {
modules.Register("status", nil, start, stop, "config", "database") modules.Register("status", nil, start, stop, "base")
} }
func start() error { func start() error {

View file

@ -115,8 +115,8 @@ func ServeFileFromBundle(w http.ResponseWriter, r *http.Request, bundleName stri
} }
} }
// set content security policy // TODO: Set content security policy
// FIXME: this breaks the ui client // For some reason, this breaks the ui client
// w.Header().Set("Content-Security-Policy", "default-src 'self'") // w.Header().Set("Content-Security-Policy", "default-src 'self'")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View file

@ -29,7 +29,7 @@ var (
) )
func init() { func init() {
module = modules.Register("updates", registerConfig, start, stop, "config", "database") module = modules.Register("updates", registerConfig, start, stop, "base")
module.RegisterEvent(eventVersionUpdate) module.RegisterEvent(eventVersionUpdate)
module.RegisterEvent(eventResourceUpdate) module.RegisterEvent(eventResourceUpdate)
} }