mirror of
https://github.com/safing/portmaster
synced 2025-04-21 11:29:09 +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>
177 lines
4 KiB
Go
177 lines
4 KiB
Go
package navigator
|
|
|
|
import (
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/tevino/abool"
|
|
|
|
"github.com/safing/portmaster/base/api"
|
|
"github.com/safing/portmaster/base/metrics"
|
|
)
|
|
|
|
var metricsRegistered = abool.New()
|
|
|
|
func registerMetrics() (err error) {
|
|
// Only register metrics once.
|
|
if !metricsRegistered.SetToIf(false, true) {
|
|
return nil
|
|
}
|
|
|
|
// Map Stats.
|
|
|
|
_, err = metrics.NewGauge(
|
|
"spn/map/main/latency/all/lowest/seconds",
|
|
nil,
|
|
getLowestLatency,
|
|
&metrics.Options{
|
|
Name: "SPN Map Lowest Latency",
|
|
Permission: api.PermitUser,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = metrics.NewGauge(
|
|
"spn/map/main/latency/fas/lowest/seconds",
|
|
nil,
|
|
getLowestLatencyFromFas,
|
|
&metrics.Options{
|
|
Name: "SPN Map Lowest Latency",
|
|
Permission: api.PermitUser,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = metrics.NewGauge(
|
|
"spn/map/main/capacity/all/highest/bytes",
|
|
nil,
|
|
getHighestCapacity,
|
|
&metrics.Options{
|
|
Name: "SPN Map Lowest Latency",
|
|
Permission: api.PermitUser,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = metrics.NewGauge(
|
|
"spn/map/main/capacity/fas/highest/bytes",
|
|
nil,
|
|
getHighestCapacityFromFas,
|
|
&metrics.Options{
|
|
Name: "SPN Map Lowest Latency",
|
|
Permission: api.PermitUser,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
mapStats *mapMetrics
|
|
mapStatsExpires time.Time
|
|
mapStatsLock sync.Mutex
|
|
mapStatsTTL = 55 * time.Second
|
|
)
|
|
|
|
type mapMetrics struct {
|
|
lowestLatency float64
|
|
lowestForeignASLatency float64
|
|
highestCapacity float64
|
|
highestForeignASCapacity float64
|
|
}
|
|
|
|
func getLowestLatency() float64 { return getMapStats().lowestLatency }
|
|
func getLowestLatencyFromFas() float64 { return getMapStats().lowestForeignASLatency }
|
|
func getHighestCapacity() float64 { return getMapStats().highestCapacity }
|
|
func getHighestCapacityFromFas() float64 { return getMapStats().highestForeignASCapacity }
|
|
|
|
func getMapStats() *mapMetrics {
|
|
mapStatsLock.Lock()
|
|
defer mapStatsLock.Unlock()
|
|
|
|
// Return cache if still valid.
|
|
if time.Now().Before(mapStatsExpires) {
|
|
return mapStats
|
|
}
|
|
|
|
// Refresh.
|
|
mapStats = &mapMetrics{}
|
|
|
|
// Get all pins and home.
|
|
list := Main.pinList(true)
|
|
home, _ := Main.GetHome()
|
|
|
|
// Return empty stats if we have incomplete data.
|
|
if len(list) <= 1 || home == nil {
|
|
mapStatsExpires = time.Now().Add(mapStatsTTL)
|
|
return mapStats
|
|
}
|
|
|
|
// Sort by latency.
|
|
sort.Sort(sortByLowestMeasuredLatency(list))
|
|
// Get lowest latency.
|
|
lowestLatency, _ := list[0].measurements.GetLatency()
|
|
mapStats.lowestLatency = lowestLatency.Seconds()
|
|
// Find best foreign AS latency.
|
|
bestForeignASPin := findFirstForeignASStatsPin(home, list)
|
|
if bestForeignASPin != nil {
|
|
lowestForeignASLatency, _ := bestForeignASPin.measurements.GetLatency()
|
|
mapStats.lowestForeignASLatency = lowestForeignASLatency.Seconds()
|
|
}
|
|
|
|
// Sort by capacity.
|
|
sort.Sort(sortByHighestMeasuredCapacity(list))
|
|
// Get highest capacity.
|
|
highestCapacity, _ := list[0].measurements.GetCapacity()
|
|
mapStats.highestCapacity = float64(highestCapacity) / 8
|
|
// Find best foreign AS capacity.
|
|
bestForeignASPin = findFirstForeignASStatsPin(home, list)
|
|
if bestForeignASPin != nil {
|
|
highestForeignASCapacity, _ := bestForeignASPin.measurements.GetCapacity()
|
|
mapStats.highestForeignASCapacity = float64(highestForeignASCapacity) / 8
|
|
}
|
|
|
|
mapStatsExpires = time.Now().Add(mapStatsTTL)
|
|
return mapStats
|
|
}
|
|
|
|
func findFirstForeignASStatsPin(home *Pin, list []*Pin) *Pin {
|
|
// Find best foreign AS latency.
|
|
for _, pin := range list {
|
|
compared := false
|
|
|
|
// Skip if IPv4 AS matches.
|
|
if home.LocationV4 != nil && pin.LocationV4 != nil {
|
|
if home.LocationV4.AutonomousSystemNumber == pin.LocationV4.AutonomousSystemNumber {
|
|
continue
|
|
}
|
|
compared = true
|
|
}
|
|
|
|
// Skip if IPv6 AS matches.
|
|
if home.LocationV6 != nil && pin.LocationV6 != nil {
|
|
if home.LocationV6.AutonomousSystemNumber == pin.LocationV6.AutonomousSystemNumber {
|
|
continue
|
|
}
|
|
compared = true
|
|
}
|
|
|
|
// Skip if no data was compared
|
|
if !compared {
|
|
continue
|
|
}
|
|
|
|
return pin
|
|
}
|
|
return nil
|
|
}
|