Clean up linter errors

This commit is contained in:
Daniel
2019-11-07 16:13:22 +01:00
parent 35c7f4955b
commit f75fc7d162
50 changed files with 402 additions and 334 deletions

View File

@@ -1,6 +1,7 @@
package network
import (
"context"
"time"
"github.com/safing/portbase/log"
@@ -11,7 +12,8 @@ var (
cleanerTickDuration = 10 * time.Second
deleteLinksAfterEndedThreshold = 5 * time.Minute
deleteCommsWithoutLinksThreshhold = 3 * time.Minute
lastEstablishedUpdateThreshold = 30 * time.Second
mtSaveLink = "save network link"
)
func cleaner() {
@@ -68,12 +70,17 @@ func cleanLinks() (activeComms map[string]struct{}) {
link.Ended = now
link.Unlock()
log.Tracef("network.clean: marked %s as ended", link.DatabaseKey())
go link.save()
// save
linkToSave := link
module.StartMicroTask(&mtSaveLink, func(ctx context.Context) error {
linkToSave.saveAndLog()
return nil
})
}
}
return
return activeComms
}
func cleanComms(activeLinks map[string]struct{}) (activeComms map[string]struct{}) {

View File

@@ -18,6 +18,7 @@ import (
)
// Communication describes a logical connection between a process and a domain.
//nolint:maligned // TODO: fix alignment
type Communication struct {
record.Base
sync.Mutex
@@ -288,7 +289,10 @@ func (comm *Communication) SaveIfNeeded() {
comm.Unlock()
if save {
comm.save()
err := comm.save()
if err != nil {
log.Warningf("network: failed to save comm %s: %s", comm, err)
}
}
}

View File

@@ -32,7 +32,7 @@ type StorageInterface struct {
func (s *StorageInterface) Get(key string) (record.Record, error) {
splitted := strings.Split(key, "/")
switch splitted[0] {
switch splitted[0] { //nolint:gocritic // TODO: implement full key space
case "tree":
switch len(splitted) {
case 2:

View File

@@ -15,12 +15,14 @@ var (
module *modules.Module
)
// InitSubModule initializes module specific things with the given module. Intended to be used as part of the "network" module.
func InitSubModule(m *modules.Module) {
module = m
module.RegisterEvent(networkChangedEvent)
module.RegisterEvent(onlineStatusChangedEvent)
}
// StartSubModule starts module specific things with the given module. Intended to be used as part of the "network" module.
func StartSubModule() error {
if module == nil {
return errors.New("not initialized")

View File

@@ -0,0 +1,35 @@
package environment
import (
"os"
"testing"
"github.com/safing/portbase/modules"
"github.com/safing/portmaster/core"
)
func TestMain(m *testing.M) {
// setup
tmpDir, err := core.InitForTesting()
if err != nil {
panic(err)
}
// setup package
netModule := modules.Register("network", nil, nil, nil, "core")
InitSubModule(netModule)
err = StartSubModule()
if err != nil {
panic(err)
}
// run tests
rv := m.Run()
// teardown
core.StopTesting()
_ = os.RemoveAll(tmpDir)
// exit with test run return value
os.Exit(rv)
}

View File

@@ -95,7 +95,7 @@ func (l *Location) EstimateNetworkProximity(to *Location) (proximity int) {
}
}
return //nolint:nakedreturn
return //nolint:nakedret
}
// PrimitiveNetworkProximity calculates the numerical distance between two IP addresses. Returns a proximity value between 0 (far away) and 100 (nearby).

View File

@@ -3,7 +3,6 @@ package geoip
import (
"context"
"fmt"
"time"
"github.com/safing/portbase/modules"
)
@@ -22,22 +21,12 @@ func start() error {
return fmt.Errorf("goeip: failed to load databases: %s", err)
}
module.RegisterEventHook(
return module.RegisterEventHook(
"updates",
"resource update",
"upgrade databases",
upgradeDatabases,
)
// TODO: replace with update subscription
module.NewTask("update databases", func(ctx context.Context, task *modules.Task) {
dbFileLock.Lock()
defer dbFileLock.Unlock()
}).Repeat(10 * time.Minute).MaxDelay(1 * time.Hour)
return nil
}
func upgradeDatabases(_ context.Context, _ interface{}) error {

View File

@@ -1,6 +1,7 @@
package network
import (
"context"
"errors"
"fmt"
"sync"
@@ -14,11 +15,8 @@ import (
// FirewallHandler defines the function signature for a firewall handle function
type FirewallHandler func(pkt packet.Packet, link *Link)
var (
linkTimeout = 10 * time.Minute
)
// Link describes a distinct physical connection (e.g. TCP connection) - like an instance - of a Connection.
//nolint:maligned // TODO: fix alignment
type Link struct {
record.Base
sync.Mutex
@@ -75,7 +73,13 @@ func (link *Link) SetFirewallHandler(handler FirewallHandler) {
if link.firewallHandler == nil {
link.firewallHandler = handler
link.pktQueue = make(chan packet.Packet, 1000)
go link.packetHandler()
// start handling
module.StartWorker("", func(ctx context.Context) error {
link.packetHandler()
return nil
})
return
}
link.firewallHandler = handler
@@ -98,8 +102,13 @@ func (link *Link) HandlePacket(pkt packet.Packet) {
link.pktQueue <- pkt
return
}
log.Warningf("network: link %s does not have a firewallHandler, dropping packet", link)
pkt.Drop()
err := pkt.Drop()
if err != nil {
log.Warningf("network: failed to drop packet %s: %s", pkt, err)
}
}
// Accept accepts the link and adds the given reason.
@@ -195,41 +204,48 @@ func (link *Link) ApplyVerdict(pkt packet.Packet) {
link.Lock()
defer link.Unlock()
var err error
if link.VerdictPermanent {
switch link.Verdict {
case VerdictAccept:
pkt.PermanentAccept()
err = pkt.PermanentAccept()
case VerdictBlock:
pkt.PermanentBlock()
err = pkt.PermanentBlock()
case VerdictDrop:
pkt.PermanentDrop()
err = pkt.PermanentDrop()
case VerdictRerouteToNameserver:
pkt.RerouteToNameserver()
err = pkt.RerouteToNameserver()
case VerdictRerouteToTunnel:
pkt.RerouteToTunnel()
err = pkt.RerouteToTunnel()
default:
pkt.Drop()
err = pkt.Drop()
}
} else {
switch link.Verdict {
case VerdictAccept:
pkt.Accept()
err = pkt.Accept()
case VerdictBlock:
pkt.Block()
err = pkt.Block()
case VerdictDrop:
pkt.Drop()
err = pkt.Drop()
case VerdictRerouteToNameserver:
pkt.RerouteToNameserver()
err = pkt.RerouteToNameserver()
case VerdictRerouteToTunnel:
pkt.RerouteToTunnel()
err = pkt.RerouteToTunnel()
default:
pkt.Drop()
err = pkt.Drop()
}
}
if err != nil {
log.Warningf("network: failed to apply link verdict to packet %s: %s", pkt, err)
}
}
// SaveWhenFinished marks the Link for saving after all current actions are finished.
func (link *Link) SaveWhenFinished() {
// FIXME: check if we should lock here
link.saveWhenFinished = true
}
@@ -243,11 +259,19 @@ func (link *Link) SaveIfNeeded() {
link.Unlock()
if save {
link.save()
link.saveAndLog()
}
}
// Save saves the link object in the storage and propagates the change.
// saveAndLog saves the link object in the storage and propagates the change. It does not return an error, but logs it.
func (link *Link) saveAndLog() {
err := link.save()
if err != nil {
log.Warningf("network: failed to save link %s: %s", link, err)
}
}
// save saves the link object in the storage and propagates the change.
func (link *Link) save() error {
// update link
link.Lock()