Skip to content

Commit 8e2e172

Browse files
committed
platform: Implement set underlying networks for android
1 parent 9df2015 commit 8e2e172

File tree

8 files changed

+43
-0
lines changed

8 files changed

+43
-0
lines changed

adapter/network.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ type NetworkInterface struct {
5151
DNSServers []string
5252
Expensive bool
5353
Constrained bool
54+
RawNetwork any
5455
}

experimental/libbox/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func (s *platformInterfaceStub) Interfaces() ([]adapter.NetworkInterface, error)
7878
return nil, os.ErrInvalid
7979
}
8080

81+
func (s *platformInterfaceStub) SetUnderlyingNetworks(networks []adapter.NetworkInterface) error {
82+
return os.ErrInvalid
83+
}
84+
8185
func (s *platformInterfaceStub) UnderNetworkExtension() bool {
8286
return false
8387
}

experimental/libbox/platform.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type PlatformInterface interface {
1717
StartDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
1818
CloseDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
1919
GetInterfaces() (NetworkInterfaceIterator, error)
20+
SetUnderlyingNetworks(networks RawNetworkIterator) error
2021
UnderNetworkExtension() bool
2122
IncludeAllNetworks() bool
2223
ReadWIFIState() *WIFIState
@@ -50,6 +51,8 @@ type NetworkInterface struct {
5051
Type int32
5152
DNSServer StringIterator
5253
Metered bool
54+
55+
RawNetwork RawNetwork
5356
}
5457

5558
type WIFIState struct {
@@ -66,6 +69,11 @@ type NetworkInterfaceIterator interface {
6669
HasNext() bool
6770
}
6871

72+
type RawNetworkIterator interface {
73+
Next() RawNetwork
74+
HasNext() bool
75+
}
76+
6977
type Notification struct {
7078
Identifier string
7179
TypeName string

experimental/libbox/platform/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Interface interface {
1515
OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error)
1616
CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor
1717
Interfaces() ([]adapter.NetworkInterface, error)
18+
SetUnderlyingNetworks(networks []adapter.NetworkInterface) error
1819
UnderNetworkExtension() bool
1920
IncludeAllNetworks() bool
2021
ClearDNSCache()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package libbox
2+
3+
type RawNetwork interface{}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build !android
2+
3+
package libbox
4+
5+
type RawNetwork interface {
6+
stub()
7+
}

experimental/libbox/service.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,18 @@ func (w *platformInterfaceWrapper) Interfaces() ([]adapter.NetworkInterface, err
206206
DNSServers: iteratorToArray[string](netInterface.DNSServer),
207207
Expensive: netInterface.Metered || isDefault && w.isExpensive,
208208
Constrained: isDefault && w.isConstrained,
209+
RawNetwork: netInterface.RawNetwork,
209210
})
210211
}
211212
return interfaces, nil
212213
}
213214

215+
func (w *platformInterfaceWrapper) SetUnderlyingNetworks(networks []adapter.NetworkInterface) error {
216+
return w.iif.SetUnderlyingNetworks(newIterator(common.Map(networks, func(it adapter.NetworkInterface) RawNetwork {
217+
return it.RawNetwork.(RawNetwork)
218+
})))
219+
}
220+
214221
func (w *platformInterfaceWrapper) UnderNetworkExtension() bool {
215222
return w.iif.UnderNetworkExtension()
216223
}

route/network.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ func (r *NetworkManager) UpdateInterfaces() error {
240240
newInterfaces := common.Filter(interfaces, func(it adapter.NetworkInterface) bool {
241241
return it.Flags&net.FlagUp != 0
242242
})
243+
for _, networkInterface := range newInterfaces {
244+
networkInterface.RawNetwork = nil
245+
}
243246
r.networkInterfaces.Store(newInterfaces)
244247
if len(newInterfaces) > 0 && !slices.EqualFunc(oldInterfaces, newInterfaces, func(oldInterface adapter.NetworkInterface, newInterface adapter.NetworkInterface) bool {
245248
return oldInterface.Interface.Index == newInterface.Interface.Index &&
@@ -260,6 +263,15 @@ func (r *NetworkManager) UpdateInterfaces() error {
260263
}
261264
return F.ToString(it.Name, " (", strings.Join(options, ", "), ")")
262265
}), ", "))
266+
if C.IsAndroid {
267+
err = r.platformInterface.SetUnderlyingNetworks(newInterfaces)
268+
if err != nil {
269+
r.logger.Error("set underlying networks: ", err)
270+
}
271+
}
272+
}
273+
for _, networkInterface := range interfaces {
274+
networkInterface.RawNetwork = nil
263275
}
264276
return nil
265277
}

0 commit comments

Comments
 (0)