mirror of
https://github.com/safing/portmaster
synced 2025-09-01 18:19:12 +00:00
* Move portbase into monorepo * Add new simple module mgr * [WIP] Switch to new simple module mgr * Add StateMgr and more worker variants * [WIP] Switch more modules * [WIP] Switch more modules * [WIP] swtich more modules * [WIP] switch all SPN modules * [WIP] switch all service modules * [WIP] Convert all workers to the new module system * [WIP] add new task system to module manager * [WIP] Add second take for scheduling workers * [WIP] Add FIXME for bugs in new scheduler * [WIP] Add minor improvements to scheduler * [WIP] Add new worker scheduler * [WIP] Fix more bug related to new module system * [WIP] Fix start handing of the new module system * [WIP] Improve startup process * [WIP] Fix minor issues * [WIP] Fix missing subsystem in settings * [WIP] Initialize managers in constructor * [WIP] Move module event initialization to constrictors * [WIP] Fix setting for enabling and disabling the SPN module * [WIP] Move API registeration into module construction * [WIP] Update states mgr for all modules * [WIP] Add CmdLine operation support * Add state helper methods to module group and instance * Add notification and module status handling to status package * Fix starting issues * Remove pilot widget and update security lock to new status data * Remove debug logs * Improve http server shutdown * Add workaround for cleanly shutting down firewall+netquery * Improve logging * Add syncing states with notifications for new module system * Improve starting, stopping, shutdown; resolve FIXMEs/TODOs * [WIP] Fix most unit tests * Review new module system and fix minor issues * Push shutdown and restart events again via API * Set sleep mode via interface * Update example/template module * [WIP] Fix spn/cabin unit test * Remove deprecated UI elements * Make log output more similar for the logging transition phase * Switch spn hub and observer cmds to new module system * Fix log sources * Make worker mgr less error prone * Fix tests and minor issues * Fix observation hub * Improve shutdown and restart handling * Split up big connection.go source file * Move varint and dsd packages to structures repo * Improve expansion test * Fix linter warnings * Fix interception module on windows * Fix linter errors --------- Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
175 lines
4 KiB
Go
175 lines
4 KiB
Go
package crew
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/safing/portmaster/base/log"
|
|
"github.com/safing/portmaster/service/mgr"
|
|
"github.com/safing/portmaster/service/network"
|
|
"github.com/safing/portmaster/service/network/packet"
|
|
"github.com/safing/portmaster/spn/navigator"
|
|
)
|
|
|
|
const (
|
|
stickyTTL = 1 * time.Hour
|
|
)
|
|
|
|
var (
|
|
stickyIPs = make(map[string]*stickyHub)
|
|
stickyDomains = make(map[string]*stickyHub)
|
|
stickyLock sync.Mutex
|
|
)
|
|
|
|
type stickyHub struct {
|
|
Pin *navigator.Pin
|
|
Route *navigator.Route
|
|
LastSeen time.Time
|
|
Avoid bool
|
|
}
|
|
|
|
func (sh *stickyHub) isExpired() bool {
|
|
return time.Now().Add(-stickyTTL).After(sh.LastSeen)
|
|
}
|
|
|
|
func makeStickyIPKey(conn *network.Connection) string {
|
|
if p := conn.Process().Profile(); p != nil {
|
|
return fmt.Sprintf(
|
|
"%s/%s>%s",
|
|
p.LocalProfile().Source,
|
|
p.LocalProfile().ID,
|
|
conn.Entity.IP,
|
|
)
|
|
}
|
|
|
|
return "?>" + string(conn.Entity.IP)
|
|
}
|
|
|
|
func makeStickyDomainKey(conn *network.Connection) string {
|
|
if p := conn.Process().Profile(); p != nil {
|
|
return fmt.Sprintf(
|
|
"%s/%s>%s",
|
|
p.LocalProfile().Source,
|
|
p.LocalProfile().ID,
|
|
conn.Entity.Domain,
|
|
)
|
|
}
|
|
|
|
return "?>" + conn.Entity.Domain
|
|
}
|
|
|
|
func getStickiedHub(conn *network.Connection) (sticksTo *stickyHub) {
|
|
stickyLock.Lock()
|
|
defer stickyLock.Unlock()
|
|
|
|
// Check if IP is sticky.
|
|
sticksTo = stickyIPs[makeStickyIPKey(conn)] // byte comparison
|
|
if sticksTo != nil && !sticksTo.isExpired() {
|
|
sticksTo.LastSeen = time.Now()
|
|
}
|
|
|
|
// If the IP did not stick and we have a domain, check if that sticks.
|
|
if sticksTo == nil && conn.Entity.Domain != "" {
|
|
sticksTo, ok := stickyDomains[makeStickyDomainKey(conn)]
|
|
if ok && !sticksTo.isExpired() {
|
|
sticksTo.LastSeen = time.Now()
|
|
}
|
|
}
|
|
|
|
// If nothing sticked, return now.
|
|
if sticksTo == nil {
|
|
return nil
|
|
}
|
|
|
|
// Get intel from map before locking pin to avoid simultaneous locking.
|
|
mapIntel := navigator.Main.GetIntel()
|
|
|
|
// Lock Pin for checking.
|
|
sticksTo.Pin.Lock()
|
|
defer sticksTo.Pin.Unlock()
|
|
|
|
// Check if the stickied Hub supports the needed IP version.
|
|
switch {
|
|
case conn.IPVersion == packet.IPv4 && sticksTo.Pin.EntityV4 == nil:
|
|
// Connection is IPv4, but stickied Hub has no IPv4.
|
|
return nil
|
|
case conn.IPVersion == packet.IPv6 && sticksTo.Pin.EntityV6 == nil:
|
|
// Connection is IPv4, but stickied Hub has no IPv4.
|
|
return nil
|
|
}
|
|
|
|
// Disregard stickied Hub if it is disregard with the current options.
|
|
matcher := conn.TunnelOpts.Destination.Matcher(mapIntel)
|
|
if !matcher(sticksTo.Pin) {
|
|
return nil
|
|
}
|
|
|
|
// Return fully checked stickied Hub.
|
|
return sticksTo
|
|
}
|
|
|
|
func (t *Tunnel) stickDestinationToHub() {
|
|
stickyLock.Lock()
|
|
defer stickyLock.Unlock()
|
|
|
|
// Stick to IP.
|
|
ipKey := makeStickyIPKey(t.connInfo)
|
|
stickyIPs[ipKey] = &stickyHub{
|
|
Pin: t.dstPin,
|
|
Route: t.route,
|
|
LastSeen: time.Now(),
|
|
}
|
|
log.Infof("spn/crew: sticking %s to %s", ipKey, t.dstPin.Hub)
|
|
|
|
// Stick to Domain, if present.
|
|
if t.connInfo.Entity.Domain != "" {
|
|
domainKey := makeStickyDomainKey(t.connInfo)
|
|
stickyDomains[domainKey] = &stickyHub{
|
|
Pin: t.dstPin,
|
|
Route: t.route,
|
|
LastSeen: time.Now(),
|
|
}
|
|
log.Infof("spn/crew: sticking %s to %s", domainKey, t.dstPin.Hub)
|
|
}
|
|
}
|
|
|
|
func (t *Tunnel) avoidDestinationHub() {
|
|
stickyLock.Lock()
|
|
defer stickyLock.Unlock()
|
|
|
|
// Stick to Hub/IP Pair.
|
|
ipKey := makeStickyIPKey(t.connInfo)
|
|
stickyIPs[ipKey] = &stickyHub{
|
|
Pin: t.dstPin,
|
|
LastSeen: time.Now(),
|
|
Avoid: true,
|
|
}
|
|
log.Warningf("spn/crew: avoiding %s for %s", t.dstPin.Hub, ipKey)
|
|
}
|
|
|
|
func cleanStickyHubs(ctx *mgr.WorkerCtx) error {
|
|
stickyLock.Lock()
|
|
defer stickyLock.Unlock()
|
|
|
|
for _, stickyRegistry := range []map[string]*stickyHub{stickyIPs, stickyDomains} {
|
|
for key, stickedEntry := range stickyRegistry {
|
|
if stickedEntry.isExpired() {
|
|
delete(stickyRegistry, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func clearStickyHubs() {
|
|
stickyLock.Lock()
|
|
defer stickyLock.Unlock()
|
|
|
|
for _, stickyRegistry := range []map[string]*stickyHub{stickyIPs, stickyDomains} {
|
|
for key := range stickyRegistry {
|
|
delete(stickyRegistry, key)
|
|
}
|
|
}
|
|
}
|