Add support for network service
This commit is contained in:
@@ -2,11 +2,14 @@ package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/safing/portmaster/network/state"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/safing/portmaster/network/packet"
|
||||
"github.com/safing/portmaster/network/state"
|
||||
"github.com/safing/portmaster/profile"
|
||||
)
|
||||
|
||||
// GetProcessByConnection returns the process that owns the described connection.
|
||||
@@ -41,3 +44,39 @@ func GetProcessByConnection(ctx context.Context, pktInfo *packet.Info) (process
|
||||
|
||||
return process, connInbound, nil
|
||||
}
|
||||
|
||||
func GetNetworkHost(ctx context.Context, remoteIP net.IP) (process *Process, err error) {
|
||||
now := time.Now().Unix()
|
||||
networkHost := &Process{
|
||||
Name: fmt.Sprintf("Network Host %s", remoteIP),
|
||||
UserName: "Unknown",
|
||||
UserID: -255,
|
||||
Pid: -255,
|
||||
ParentPid: -255,
|
||||
Path: fmt.Sprintf("net:%s", remoteIP),
|
||||
FirstSeen: now,
|
||||
LastSeen: now,
|
||||
}
|
||||
|
||||
// Get the (linked) local profile.
|
||||
networkHostProfile, err := profile.GetNetworkHostProfile(remoteIP.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Assign profile to process.
|
||||
networkHost.LocalProfileKey = networkHostProfile.Key()
|
||||
networkHost.profile = networkHostProfile.LayeredProfile()
|
||||
|
||||
if networkHostProfile.Name == "" {
|
||||
// Assign name and save.
|
||||
networkHostProfile.Name = networkHost.Name
|
||||
|
||||
err := networkHostProfile.Save()
|
||||
if err != nil {
|
||||
log.Warningf("process: failed to save profile %s: %s", networkHostProfile.ScopedID(), err)
|
||||
}
|
||||
}
|
||||
|
||||
return networkHost, nil
|
||||
}
|
||||
|
||||
@@ -2,11 +2,16 @@ package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/safing/portbase/log"
|
||||
"github.com/safing/portmaster/profile"
|
||||
)
|
||||
|
||||
var (
|
||||
ownPID = os.Getpid()
|
||||
)
|
||||
|
||||
// GetProfile finds and assigns a profile set to the process.
|
||||
func (p *Process) GetProfile(ctx context.Context) (changed bool, err error) {
|
||||
// Update profile metadata outside of *Process lock.
|
||||
@@ -31,6 +36,8 @@ func (p *Process) GetProfile(ctx context.Context) (changed bool, err error) {
|
||||
profileID = profile.UnidentifiedProfileID
|
||||
case SystemProcessID:
|
||||
profileID = profile.SystemProfileID
|
||||
case ownPID:
|
||||
profileID = profile.PortmasterProfileID
|
||||
}
|
||||
|
||||
// Get the (linked) local profile.
|
||||
@@ -56,7 +63,7 @@ func (p *Process) UpdateProfileMetadata() {
|
||||
}
|
||||
|
||||
// Update metadata of profile.
|
||||
metadataUpdated := localProfile.UpdateMetadata(p.Name)
|
||||
metadataUpdated := localProfile.UpdateMetadata(p.Name, p.Path)
|
||||
|
||||
// Mark profile as used.
|
||||
profileChanged := localProfile.MarkUsed()
|
||||
|
||||
Reference in New Issue
Block a user