Work on portmaster restructuring

This commit is contained in:
Daniel
2018-11-28 16:17:46 +01:00
parent 5bdb021c88
commit be8a1d1739
13 changed files with 301 additions and 191 deletions

View File

@@ -3,6 +3,7 @@ package process
import (
"fmt"
"sync"
"time"
"github.com/Safing/portbase/database"
"github.com/tevino/abool"
@@ -16,10 +17,6 @@ var (
dbControllerFlag = abool.NewBool(false)
)
func makeProcessKey(pid int) string {
return fmt.Sprintf("network:tree/%d", pid)
}
// GetProcessFromStorage returns a process from the internal storage.
func GetProcessFromStorage(pid int) (*Process, bool) {
processesLock.RLock()
@@ -48,13 +45,18 @@ func (p *Process) Save() {
defer p.Unlock()
if p.DatabaseKey() == "" {
p.SetKey(makeProcessKey(p.Pid))
p.SetKey(fmt.Sprintf("network:tree/%d", p.Pid))
p.CreateMeta()
}
processesLock.RLock()
_, ok := processes[p.Pid]
processesLock.RUnlock()
if !ok {
processesLock.Lock()
defer processesLock.Unlock()
processes[p.Pid] = p
processesLock.Unlock()
}
if dbControllerFlag.IsSet() {
@@ -62,6 +64,33 @@ func (p *Process) Save() {
}
}
// Delete deletes a process from the storage and propagates the change.
func (p *Process) Delete() {
processesLock.Lock()
defer processesLock.Unlock()
delete(processes, p.Pid)
p.Lock()
defer p.Lock()
p.Meta().Delete()
if dbControllerFlag.IsSet() {
dbController.PushUpdate(p)
}
}
// CleanProcessStorage cleans the storage from old processes.
func CleanProcessStorage(thresholdDuration time.Duration) {
processesLock.Lock()
defer processesLock.Unlock()
threshold := time.Now().Add(-thresholdDuration).Unix()
for _, p := range processes {
if p.FirstConnectionEstablished < threshold && p.ConnectionCount == 0 {
p.Delete()
}
}
}
// SetDBController sets the database controller and allows the package to push database updates on a save. It must be set by the package that registers the "network" database.
func SetDBController(controller *database.Controller) {
dbController = controller

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"runtime"
"sync"
"time"
processInfo "github.com/shirou/gopsutil/process"
@@ -32,14 +33,38 @@ type Process struct {
Name string
Icon string
// Icon is a path to the icon and is either prefixed "f:" for filepath, "d:" for database cache path or "c:"/"a:" for a the icon key to fetch it from a company / authoritative node and cache it in its own cache.
FirstConnectionEstablished int64
LastConnectionEstablished int64
ConnectionCount uint
}
// Strings returns a string represenation of process
func (m *Process) String() string {
if m == nil {
func (p *Process) String() string {
if p == nil {
return "?"
}
return fmt.Sprintf("%s:%s:%d", m.UserName, m.Path, m.Pid)
return fmt.Sprintf("%s:%s:%d", p.UserName, p.Path, p.Pid)
}
// AddConnection increases the connection counter and the last connection timestamp.
func (p *Process) AddConnection() {
p.Lock()
defer p.Unlock()
p.ConnectionCount++
p.LastConnectionEstablished = time.Now().Unix()
if p.FirstConnectionEstablished == 0 {
p.FirstConnectionEstablished = p.LastConnectionEstablished
}
}
// RemoveConnection lowers the connection counter by one.
func (p *Process) RemoveConnection() {
p.Lock()
defer p.Unlock()
if p.ConnectionCount > 0 {
p.ConnectionCount--
}
}
// GetOrFindProcess returns the process for the given PID.

View File

@@ -1,6 +1,7 @@
package process
var (
// UnknownProcess is used when a process cannot be found.
UnknownProcess = &Process{
UserID: -1,
UserName: "Unknown",