Work on portmaster restructuring
This commit is contained in:
205
network/link.go
205
network/link.go
@@ -3,17 +3,17 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
datastore "github.com/ipfs/go-datastore"
|
||||
|
||||
"github.com/Safing/portbase/database"
|
||||
"github.com/Safing/portbase/database/record"
|
||||
"github.com/Safing/portbase/log"
|
||||
"github.com/Safing/portmaster/network/packet"
|
||||
)
|
||||
|
||||
// FirewallHandler defines the function signature for a firewall handle function
|
||||
type FirewallHandler func(pkt packet.Packet, link *Link)
|
||||
|
||||
var (
|
||||
@@ -22,9 +22,13 @@ var (
|
||||
allLinksLock sync.RWMutex
|
||||
)
|
||||
|
||||
// Link describes an distinct physical connection (e.g. TCP connection) - like an instance - of a Connection
|
||||
// Link describes a distinct physical connection (e.g. TCP connection) - like an instance - of a Connection.
|
||||
type Link struct {
|
||||
database.Base
|
||||
record.Record
|
||||
sync.Mutex
|
||||
|
||||
ID string
|
||||
|
||||
Verdict Verdict
|
||||
Reason string
|
||||
Tunneled bool
|
||||
@@ -32,180 +36,167 @@ type Link struct {
|
||||
Inspect bool
|
||||
Started int64
|
||||
Ended int64
|
||||
connection *Connection
|
||||
RemoteAddress string
|
||||
ActiveInspectors []bool `json:"-" bson:"-"`
|
||||
InspectorData map[uint8]interface{} `json:"-" bson:"-"`
|
||||
|
||||
pktQueue chan packet.Packet
|
||||
firewallHandler FirewallHandler
|
||||
}
|
||||
connection *Connection
|
||||
|
||||
var linkModel *Link // only use this as parameter for database.EnsureModel-like functions
|
||||
|
||||
func init() {
|
||||
database.RegisterModel(linkModel, func() database.Model { return new(Link) })
|
||||
activeInspectors []bool
|
||||
inspectorData map[uint8]interface{}
|
||||
}
|
||||
|
||||
// Connection returns the Connection the Link is part of
|
||||
func (m *Link) Connection() *Connection {
|
||||
return m.connection
|
||||
func (link *Link) Connection() *Connection {
|
||||
return link.connection
|
||||
}
|
||||
|
||||
// FirewallHandlerIsSet returns whether a firewall handler is set or not
|
||||
func (m *Link) FirewallHandlerIsSet() bool {
|
||||
return m.firewallHandler != nil
|
||||
func (link *Link) FirewallHandlerIsSet() bool {
|
||||
return link.firewallHandler != nil
|
||||
}
|
||||
|
||||
// SetFirewallHandler sets the firewall handler for this link
|
||||
func (m *Link) SetFirewallHandler(handler FirewallHandler) {
|
||||
if m.firewallHandler == nil {
|
||||
m.firewallHandler = handler
|
||||
m.pktQueue = make(chan packet.Packet, 1000)
|
||||
go m.packetHandler()
|
||||
func (link *Link) SetFirewallHandler(handler FirewallHandler) {
|
||||
if link.firewallHandler == nil {
|
||||
link.firewallHandler = handler
|
||||
link.pktQueue = make(chan packet.Packet, 1000)
|
||||
go link.packetHandler()
|
||||
return
|
||||
}
|
||||
m.firewallHandler = handler
|
||||
link.firewallHandler = handler
|
||||
}
|
||||
|
||||
// StopFirewallHandler unsets the firewall handler
|
||||
func (m *Link) StopFirewallHandler() {
|
||||
m.pktQueue <- nil
|
||||
func (link *Link) StopFirewallHandler() {
|
||||
link.pktQueue <- nil
|
||||
}
|
||||
|
||||
// HandlePacket queues packet of Link for handling
|
||||
func (m *Link) HandlePacket(pkt packet.Packet) {
|
||||
if m.firewallHandler != nil {
|
||||
m.pktQueue <- pkt
|
||||
func (link *Link) HandlePacket(pkt packet.Packet) {
|
||||
if link.firewallHandler != nil {
|
||||
link.pktQueue <- pkt
|
||||
return
|
||||
}
|
||||
log.Criticalf("network: link %s does not have a firewallHandler, maybe its a copy, dropping packet", m)
|
||||
log.Criticalf("network: link %s does not have a firewallHandler (maybe it's a copy), dropping packet", link)
|
||||
pkt.Drop()
|
||||
}
|
||||
|
||||
// UpdateVerdict sets a new verdict for this link, making sure it does not interfere with previous verdicts
|
||||
func (m *Link) UpdateVerdict(newVerdict Verdict) {
|
||||
if newVerdict > m.Verdict {
|
||||
m.Verdict = newVerdict
|
||||
m.Save()
|
||||
func (link *Link) UpdateVerdict(newVerdict Verdict) {
|
||||
if newVerdict > link.Verdict {
|
||||
link.Verdict = newVerdict
|
||||
link.Save()
|
||||
}
|
||||
}
|
||||
|
||||
// AddReason adds a human readable string as to why a certain verdict was set in regard to this link
|
||||
func (m *Link) AddReason(newReason string) {
|
||||
if m.Reason != "" {
|
||||
m.Reason += " | "
|
||||
func (link *Link) AddReason(newReason string) {
|
||||
link.Lock()
|
||||
defer link.Unlock()
|
||||
|
||||
if link.Reason != "" {
|
||||
link.Reason += " | "
|
||||
}
|
||||
m.Reason += newReason
|
||||
link.Reason += newReason
|
||||
}
|
||||
|
||||
// packetHandler sequentially handles queued packets
|
||||
func (m *Link) packetHandler() {
|
||||
func (link *Link) packetHandler() {
|
||||
for {
|
||||
pkt := <-m.pktQueue
|
||||
pkt := <-link.pktQueue
|
||||
if pkt == nil {
|
||||
break
|
||||
}
|
||||
m.firewallHandler(pkt, m)
|
||||
link.firewallHandler(pkt, link)
|
||||
}
|
||||
m.firewallHandler = nil
|
||||
link.firewallHandler = nil
|
||||
}
|
||||
|
||||
// Create creates a new database entry in the database in the default namespace for this object
|
||||
func (m *Link) Create(name string) error {
|
||||
m.CreateShallow(name)
|
||||
return m.CreateObject(&database.OrphanedLink, name, m)
|
||||
}
|
||||
|
||||
// Create creates a new database entry in the database in the default namespace for this object
|
||||
func (m *Link) CreateShallow(name string) {
|
||||
allLinksLock.Lock()
|
||||
allLinks[name] = m
|
||||
allLinksLock.Unlock()
|
||||
}
|
||||
|
||||
// CreateWithDefaultKey creates a new database entry in the database in the default namespace for this object using the default key
|
||||
func (m *Link) CreateInConnectionNamespace(name string) error {
|
||||
if m.connection != nil {
|
||||
return m.CreateObject(m.connection.GetKey(), name, m)
|
||||
// Save saves the link object in the storage and propagates the change.
|
||||
func (link *Link) Save() error {
|
||||
if link.connection == nil {
|
||||
return errors.New("cannot save link without connection")
|
||||
}
|
||||
return m.CreateObject(&database.OrphanedLink, name, m)
|
||||
|
||||
if link.DatabaseKey() == "" {
|
||||
|
||||
link.SetKey(fmt.Sprintf("network:tree/%d/%s/%s", link.connection.Process().Pid, link.connection.Domain, link.ID))
|
||||
link.CreateMeta()
|
||||
|
||||
dataLock.Lock()
|
||||
defer dataLock.Unlock()
|
||||
|
||||
links[link.ID] = link
|
||||
}
|
||||
|
||||
if link.orphaned && link.connection != nil {
|
||||
p.SetKey()
|
||||
}
|
||||
|
||||
dbController.PushUpdate(link)
|
||||
}
|
||||
|
||||
// Save saves the object to the database (It must have been either already created or loaded from the database)
|
||||
func (m *Link) Save() error {
|
||||
return m.SaveObject(m)
|
||||
// Delete deletes a link from the storage and propagates the change.
|
||||
func (link *Link) Delete() {
|
||||
dataLock.Lock()
|
||||
defer dataLock.Unlock()
|
||||
delete(links, link.ID)
|
||||
link.Lock()
|
||||
defer link.Lock()
|
||||
link.Meta().Delete()
|
||||
dbController.PushUpdate(link)
|
||||
}
|
||||
|
||||
// GetLink fetches a Link from the database from the default namespace for this object
|
||||
func GetLink(name string) (*Link, error) {
|
||||
allLinksLock.RLock()
|
||||
link, ok := allLinks[name]
|
||||
allLinksLock.RUnlock()
|
||||
if !ok {
|
||||
return nil, database.ErrNotFound
|
||||
}
|
||||
return link, nil
|
||||
// return GetLinkFromNamespace(&database.RunningLink, name)
|
||||
}
|
||||
func GetLink(id string) (*Link, bool) {
|
||||
dataLock.RLock()
|
||||
defer dataLock.RUnlock()
|
||||
|
||||
func SaveInCache(link *Link) {
|
||||
|
||||
}
|
||||
|
||||
// GetLinkFromNamespace fetches a Link form the database, but from a custom namespace
|
||||
func GetLinkFromNamespace(namespace *datastore.Key, name string) (*Link, error) {
|
||||
object, err := database.GetAndEnsureModel(namespace, name, linkModel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
model, ok := object.(*Link)
|
||||
if !ok {
|
||||
return nil, database.NewMismatchError(object, linkModel)
|
||||
}
|
||||
return model, nil
|
||||
link, ok := links[id]
|
||||
return link, ok
|
||||
}
|
||||
|
||||
// GetOrCreateLinkByPacket returns the associated Link for a packet and a bool expressing if the Link was newly created
|
||||
func GetOrCreateLinkByPacket(pkt packet.Packet) (*Link, bool) {
|
||||
link, err := GetLink(pkt.GetConnectionID())
|
||||
if err != nil {
|
||||
return CreateLinkFromPacket(pkt), true
|
||||
link, ok := GetLink(pkt.GetConnectionID())
|
||||
if ok {
|
||||
return link, false
|
||||
}
|
||||
return link, false
|
||||
return CreateLinkFromPacket(pkt), true
|
||||
}
|
||||
|
||||
// CreateLinkFromPacket creates a new Link based on Packet. The Link is shallowly saved and SHOULD be saved to the database as soon more information is available
|
||||
// CreateLinkFromPacket creates a new Link based on Packet.
|
||||
func CreateLinkFromPacket(pkt packet.Packet) *Link {
|
||||
link := &Link{
|
||||
ID: pkt.GetConnectionID(),
|
||||
Verdict: UNDECIDED,
|
||||
Started: time.Now().Unix(),
|
||||
RemoteAddress: pkt.FmtRemoteAddress(),
|
||||
}
|
||||
link.CreateShallow(pkt.GetConnectionID())
|
||||
return link
|
||||
}
|
||||
|
||||
// FORMATTING
|
||||
func (m *Link) String() string {
|
||||
if m.connection == nil {
|
||||
return fmt.Sprintf("? <-> %s", m.RemoteAddress)
|
||||
// String returns a string representation of Link.
|
||||
func (link *Link) String() string {
|
||||
if link.connection == nil {
|
||||
return fmt.Sprintf("? <-> %s", link.RemoteAddress)
|
||||
}
|
||||
switch m.connection.Domain {
|
||||
switch link.connection.Domain {
|
||||
case "I":
|
||||
if m.connection.process == nil {
|
||||
return fmt.Sprintf("? <- %s", m.RemoteAddress)
|
||||
if link.connection.process == nil {
|
||||
return fmt.Sprintf("? <- %s", link.RemoteAddress)
|
||||
}
|
||||
return fmt.Sprintf("%s <- %s", m.connection.process.String(), m.RemoteAddress)
|
||||
return fmt.Sprintf("%s <- %s", link.connection.process.String(), link.RemoteAddress)
|
||||
case "D":
|
||||
if m.connection.process == nil {
|
||||
return fmt.Sprintf("? -> %s", m.RemoteAddress)
|
||||
if link.connection.process == nil {
|
||||
return fmt.Sprintf("? -> %s", link.RemoteAddress)
|
||||
}
|
||||
return fmt.Sprintf("%s -> %s", m.connection.process.String(), m.RemoteAddress)
|
||||
return fmt.Sprintf("%s -> %s", link.connection.process.String(), link.RemoteAddress)
|
||||
default:
|
||||
if m.connection.process == nil {
|
||||
return fmt.Sprintf("? -> %s (%s)", m.connection.Domain, m.RemoteAddress)
|
||||
if link.connection.process == nil {
|
||||
return fmt.Sprintf("? -> %s (%s)", link.connection.Domain, link.RemoteAddress)
|
||||
}
|
||||
return fmt.Sprintf("%s to %s (%s)", m.connection.process.String(), m.connection.Domain, m.RemoteAddress)
|
||||
return fmt.Sprintf("%s to %s (%s)", link.connection.process.String(), link.connection.Domain, link.RemoteAddress)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user