Skip to content

Commit

Permalink
platform: Implement set underlying networks for android
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 22, 2024
1 parent f0f3a45 commit eb4a184
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions adapter/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ type NetworkInterface struct {
DNSServers []string
Expensive bool
Constrained bool
RawNetwork any
}
4 changes: 4 additions & 0 deletions experimental/libbox/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (s *platformInterfaceStub) Interfaces() ([]adapter.NetworkInterface, error)
return nil, os.ErrInvalid
}

func (s *platformInterfaceStub) SetUnderlyingNetworks(networks []adapter.NetworkInterface) error {
return os.ErrInvalid
}

func (s *platformInterfaceStub) UnderNetworkExtension() bool {
return false
}
Expand Down
8 changes: 8 additions & 0 deletions experimental/libbox/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type PlatformInterface interface {
StartDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
CloseDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
GetInterfaces() (NetworkInterfaceIterator, error)
SetUnderlyingNetworks(networks RawNetworkIterator) error
UnderNetworkExtension() bool
IncludeAllNetworks() bool
ReadWIFIState() *WIFIState
Expand Down Expand Up @@ -50,6 +51,8 @@ type NetworkInterface struct {
Type int32
DNSServer StringIterator
Metered bool

RawNetwork RawNetwork
}

type WIFIState struct {
Expand All @@ -66,6 +69,11 @@ type NetworkInterfaceIterator interface {
HasNext() bool
}

type RawNetworkIterator interface {
Next() RawNetwork
HasNext() bool
}

type Notification struct {
Identifier string
TypeName string
Expand Down
1 change: 1 addition & 0 deletions experimental/libbox/platform/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Interface interface {
OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error)
CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor
Interfaces() ([]adapter.NetworkInterface, error)
SetUnderlyingNetworks(networks []adapter.NetworkInterface) error
UnderNetworkExtension() bool
IncludeAllNetworks() bool
ClearDNSCache()
Expand Down
3 changes: 3 additions & 0 deletions experimental/libbox/raw_network_android.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package libbox

type RawNetwork interface{}
7 changes: 7 additions & 0 deletions experimental/libbox/raw_network_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !android

package libbox

type RawNetwork interface {
stub()
}
7 changes: 7 additions & 0 deletions experimental/libbox/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,18 @@ func (w *platformInterfaceWrapper) Interfaces() ([]adapter.NetworkInterface, err
DNSServers: iteratorToArray[string](netInterface.DNSServer),
Expensive: netInterface.Metered || isDefault && w.isExpensive,
Constrained: isDefault && w.isConstrained,
RawNetwork: netInterface.RawNetwork,
})
}
return interfaces, nil
}

func (w *platformInterfaceWrapper) SetUnderlyingNetworks(networks []adapter.NetworkInterface) error {
return w.iif.SetUnderlyingNetworks(newIterator(common.Map(networks, func(it adapter.NetworkInterface) RawNetwork {
return it.RawNetwork.(RawNetwork)
})))
}

func (w *platformInterfaceWrapper) UnderNetworkExtension() bool {
return w.iif.UnderNetworkExtension()
}
Expand Down
12 changes: 12 additions & 0 deletions route/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ func (r *NetworkManager) UpdateInterfaces() error {
newInterfaces := common.Filter(interfaces, func(it adapter.NetworkInterface) bool {
return it.Flags&net.FlagUp != 0
})
for _, networkInterface := range newInterfaces {
networkInterface.RawNetwork = nil
}
r.networkInterfaces.Store(newInterfaces)
if len(newInterfaces) > 0 && !slices.EqualFunc(oldInterfaces, newInterfaces, func(oldInterface adapter.NetworkInterface, newInterface adapter.NetworkInterface) bool {
return oldInterface.Interface.Index == newInterface.Interface.Index &&
Expand All @@ -260,6 +263,15 @@ func (r *NetworkManager) UpdateInterfaces() error {
}
return F.ToString(it.Name, " (", strings.Join(options, ", "), ")")
}), ", "))
if C.IsAndroid {
err = r.platformInterface.SetUnderlyingNetworks(newInterfaces)
if err != nil {
r.logger.Error("set underlying networks: ", err)
}
}
}
for _, networkInterface := range interfaces {
networkInterface.RawNetwork = nil
}
return nil
}
Expand Down

0 comments on commit eb4a184

Please sign in to comment.