Android support for getting network addresses and interfaces (#1056)
* Replace unsupported network functions for android * Refactor default/android net addresses processing * Add default connection values, Refactor netenv * Fix compilation error * Combine network change default/android functions
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
// GetAssignedAddresses returns the assigned IPv4 and IPv6 addresses of the host.
|
||||
func GetAssignedAddresses() (ipv4 []net.IP, ipv6 []net.IP, err error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
addrs, err := osGetInterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -74,7 +74,7 @@ func refreshMyNetworks() error {
|
||||
myNetworksDontRefreshUntil = time.Now().Add(1 * time.Second)
|
||||
|
||||
// Refresh assigned networks.
|
||||
interfaceNetworks, err := net.InterfaceAddrs()
|
||||
interfaceNetworks, err := osGetInterfaceAddrs()
|
||||
if err != nil {
|
||||
// In some cases the system blocks on this call, which piles up to
|
||||
// literally over thousand goroutines wanting to try this again.
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"crypto/sha1"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portbase/log"
|
||||
@@ -61,7 +60,7 @@ serviceLoop:
|
||||
// check network for changes
|
||||
// create hashsum of current network config
|
||||
hasher := sha1.New() //nolint:gosec // not used for security
|
||||
interfaces, err := net.Interfaces()
|
||||
interfaces, err := osGetNetworkInterfaces()
|
||||
if err != nil {
|
||||
log.Warningf("netenv: failed to get interfaces: %s", err)
|
||||
continue
|
||||
|
||||
23
netenv/os_android.go
Normal file
23
netenv/os_android.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package netenv
|
||||
|
||||
import (
|
||||
"github.com/safing/portmaster-android/go/app_interface"
|
||||
"net"
|
||||
)
|
||||
|
||||
func osGetInterfaceAddrs() ([]net.Addr, error) {
|
||||
list, err := app_interface.GetNetworkAddresses()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var netList []net.Addr
|
||||
for _, addr := range list {
|
||||
netList = append(netList, addr.ToIPNet())
|
||||
}
|
||||
|
||||
return netList, nil
|
||||
}
|
||||
|
||||
func osGetNetworkInterfaces() ([]app_interface.NetworkInterface, error) {
|
||||
return app_interface.GetNetworkInterfaces()
|
||||
}
|
||||
15
netenv/os_default.go
Normal file
15
netenv/os_default.go
Normal file
@@ -0,0 +1,15 @@
|
||||
//go:build !android
|
||||
|
||||
package netenv
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
func osGetInterfaceAddrs() ([]net.Addr, error) {
|
||||
return net.InterfaceAddrs()
|
||||
}
|
||||
|
||||
func osGetNetworkInterfaces() ([]net.Interface, error) {
|
||||
return net.Interfaces()
|
||||
}
|
||||
Reference in New Issue
Block a user