mirror of
https://github.com/safing/portmaster
synced 2025-04-21 03:19:10 +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>
184 lines
4.6 KiB
Go
184 lines
4.6 KiB
Go
package netenv
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/safing/portmaster/base/log"
|
|
"github.com/safing/portmaster/base/utils/osdetail"
|
|
)
|
|
|
|
// Gateways returns the currently active gateways.
|
|
func Gateways() []net.IP {
|
|
defaultIf := getDefaultInterface()
|
|
if defaultIf == nil {
|
|
return nil
|
|
}
|
|
|
|
// Collect gateways.
|
|
var gw []net.IP
|
|
if defaultIf.IPv4DefaultGateway != nil {
|
|
gw = append(gw, defaultIf.IPv4DefaultGateway)
|
|
}
|
|
if defaultIf.IPv6DefaultGateway != nil {
|
|
gw = append(gw, defaultIf.IPv6DefaultGateway)
|
|
}
|
|
|
|
return gw
|
|
}
|
|
|
|
// Nameservers returns the currently active nameservers.
|
|
func Nameservers() []Nameserver {
|
|
defaultIf := getDefaultInterface()
|
|
if defaultIf == nil {
|
|
return nil
|
|
}
|
|
|
|
// Compile search list.
|
|
var search []string
|
|
if defaultIf.DNSServerConfig != nil {
|
|
if defaultIf.DNSServerConfig.Suffix != "" {
|
|
search = append(search, defaultIf.DNSServerConfig.Suffix)
|
|
}
|
|
if len(defaultIf.DNSServerConfig.SuffixSearchList) > 0 {
|
|
search = append(search, defaultIf.DNSServerConfig.SuffixSearchList...)
|
|
}
|
|
}
|
|
|
|
// Compile nameservers.
|
|
var ns []Nameserver
|
|
for _, nsIP := range defaultIf.DNSServer {
|
|
ns = append(ns, Nameserver{
|
|
IP: nsIP,
|
|
Search: search,
|
|
})
|
|
}
|
|
|
|
return ns
|
|
}
|
|
|
|
const (
|
|
defaultInterfaceRecheck = 2 * time.Second
|
|
)
|
|
|
|
var (
|
|
defaultInterface *defaultNetInterface
|
|
defaultInterfaceLock sync.Mutex
|
|
defaultInterfaceNetworkChangedFlag = GetNetworkChangedFlag()
|
|
)
|
|
|
|
type defaultNetInterface struct {
|
|
InterfaceIndex string
|
|
IPv6Address net.IP
|
|
IPv4Address net.IP
|
|
IPv6DefaultGateway net.IP
|
|
IPv4DefaultGateway net.IP
|
|
DNSServer []net.IP
|
|
DNSServerConfig *dnsServerConfig
|
|
}
|
|
|
|
type dnsServerConfig struct {
|
|
Suffix string
|
|
SuffixSearchList []string
|
|
}
|
|
|
|
func getDefaultInterface() *defaultNetInterface {
|
|
defaultInterfaceLock.Lock()
|
|
defer defaultInterfaceLock.Unlock()
|
|
// Check if the network changed, if not, return cache.
|
|
if !defaultInterfaceNetworkChangedFlag.IsSet() {
|
|
return defaultInterface
|
|
}
|
|
defaultInterfaceNetworkChangedFlag.Refresh()
|
|
|
|
// Get interface data from Windows.
|
|
interfaceData, err := osdetail.RunPowershellCmd("Get-NetRoute -DestinationPrefix '0.0.0.0/0' | Select-Object -First 1 | Get-NetIPConfiguration | Format-List")
|
|
if err != nil {
|
|
log.Warningf("netenv: failed to get interface data: %s", err)
|
|
return nil
|
|
}
|
|
|
|
// TODO: It would be great to get this as json. Powershell can do this,
|
|
// but it just spits out lots of weird data instead of the same strings
|
|
// seen in the list.
|
|
newIf := &defaultNetInterface{}
|
|
|
|
// Scan data for needed fields.
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(interfaceData))
|
|
scanner.Split(bufio.ScanLines)
|
|
var segmentKey, segmentValue, previousKey string
|
|
for scanner.Scan() {
|
|
segments := strings.SplitN(scanner.Text(), " : ", 2)
|
|
|
|
// Check what the line gives us.
|
|
switch len(segments) {
|
|
case 2:
|
|
// This is a new key and value.
|
|
segmentKey = strings.TrimSpace(segments[0])
|
|
segmentValue = strings.TrimSpace(segments[1])
|
|
previousKey = segmentKey
|
|
case 1:
|
|
// This is another value for the previous key.
|
|
segmentKey = previousKey
|
|
segmentValue = strings.TrimSpace(segments[0])
|
|
default:
|
|
continue
|
|
}
|
|
|
|
// Ignore empty lines.
|
|
if segmentValue == "" {
|
|
continue
|
|
}
|
|
|
|
// Parse and assign value to struct.
|
|
switch segmentKey {
|
|
case "InterfaceIndex":
|
|
newIf.InterfaceIndex = segmentValue
|
|
case "IPv6Address":
|
|
newIf.IPv6Address = net.ParseIP(segmentValue)
|
|
case "IPv4Address":
|
|
newIf.IPv4Address = net.ParseIP(segmentValue)
|
|
case "IPv6DefaultGateway":
|
|
newIf.IPv6DefaultGateway = net.ParseIP(segmentValue)
|
|
case "IPv4DefaultGateway":
|
|
newIf.IPv4DefaultGateway = net.ParseIP(segmentValue)
|
|
case "DNSServer":
|
|
newIP := net.ParseIP(segmentValue)
|
|
if newIP != nil {
|
|
newIf.DNSServer = append(newIf.DNSServer, newIP)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get Search Scopes for this interface.
|
|
if newIf.InterfaceIndex != "" {
|
|
dnsConfigData, err := osdetail.RunPowershellCmd(fmt.Sprintf(
|
|
"Get-DnsClient -InterfaceIndex %s | ConvertTo-Json -Depth 1",
|
|
newIf.InterfaceIndex,
|
|
))
|
|
if err != nil {
|
|
log.Warningf("netenv: failed to get dns server config data: %s", err)
|
|
} else {
|
|
// Parse data into struct.
|
|
dnsConfig := &dnsServerConfig{}
|
|
err := json.Unmarshal([]byte(dnsConfigData), dnsConfig)
|
|
if err != nil {
|
|
log.Warningf("netenv: failed to get dns server config data: %s", err)
|
|
} else {
|
|
newIf.DNSServerConfig = dnsConfig
|
|
}
|
|
}
|
|
} else {
|
|
log.Warning("netenv: could not get dns server config data, because default interface index is missing")
|
|
}
|
|
|
|
// Assign new value to cache and return.
|
|
defaultInterface = newIf
|
|
return defaultInterface
|
|
}
|