Skip to content

Commit 9f84165

Browse files
authored
[client] Add netstack support for Android cli (#4319)
1 parent 3488a51 commit 9f84165

File tree

6 files changed

+38
-5
lines changed

6 files changed

+38
-5
lines changed

client/iface/device/device_netstack.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build !android
2-
// +build !android
3-
41
package device
52

63
import (
@@ -47,7 +44,7 @@ func NewNetstackDevice(name string, address wgaddr.Address, wgPort int, key stri
4744
}
4845
}
4946

50-
func (t *TunNetstackDevice) Create() (WGConfigurer, error) {
47+
func (t *TunNetstackDevice) create() (WGConfigurer, error) {
5148
log.Info("create nbnetstack tun interface")
5249

5350
// TODO: get from service listener runtime IP
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 device
4+
5+
func (t *TunNetstackDevice) Create(routes []string, dns string, searchDomains []string) (WGConfigurer, error) {
6+
return t.create()
7+
}
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 device
4+
5+
func (t *TunNetstackDevice) Create() (WGConfigurer, error) {
6+
return t.create()
7+
}

client/iface/iface_new_android.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package iface
33
import (
44
"github.com/netbirdio/netbird/client/iface/bind"
55
"github.com/netbirdio/netbird/client/iface/device"
6+
"github.com/netbirdio/netbird/client/iface/netstack"
67
"github.com/netbirdio/netbird/client/iface/wgaddr"
78
"github.com/netbirdio/netbird/client/iface/wgproxy"
89
)
@@ -16,6 +17,15 @@ func NewWGIFace(opts WGIFaceOpts) (*WGIface, error) {
1617

1718
iceBind := bind.NewICEBind(opts.TransportNet, opts.FilterFn, wgAddress)
1819

20+
if netstack.IsEnabled() {
21+
wgIFace := &WGIface{
22+
userspaceBind: true,
23+
tun: device.NewNetstackDevice(opts.IFaceName, wgAddress, opts.WGPort, opts.WGPrivKey, opts.MTU, iceBind, netstack.ListenAddr()),
24+
wgProxyFactory: wgproxy.NewUSPFactory(iceBind),
25+
}
26+
return wgIFace, nil
27+
}
28+
1929
wgIFace := &WGIface{
2030
userspaceBind: true,
2131
tun: device.NewTunDevice(wgAddress, opts.WGPort, opts.WGPrivKey, opts.MTU, iceBind, opts.MobileArgs.TunAdapter, opts.DisableDNS),

client/internal/stdnet/stdnet.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sync"
1010
"time"
1111

12+
"github.com/netbirdio/netbird/client/iface/netstack"
1213
"github.com/pion/transport/v3"
1314
"github.com/pion/transport/v3/stdnet"
1415
)
@@ -32,9 +33,15 @@ type Net struct {
3233
// NewNetWithDiscover creates a new StdNet instance.
3334
func NewNetWithDiscover(iFaceDiscover ExternalIFaceDiscover, disallowList []string) (*Net, error) {
3435
n := &Net{
35-
iFaceDiscover: newMobileIFaceDiscover(iFaceDiscover),
3636
interfaceFilter: InterfaceFilter(disallowList),
3737
}
38+
// current ExternalIFaceDiscover implement in android-client https://github.dev/netbirdio/android-client
39+
// so in android cli use pionDiscover
40+
if netstack.IsEnabled() {
41+
n.iFaceDiscover = pionDiscover{}
42+
} else {
43+
newMobileIFaceDiscover(iFaceDiscover)
44+
}
3845
return n, n.UpdateInterfaces()
3946
}
4047

util/net/protectsocket_android.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"sync"
66
"syscall"
7+
8+
"github.com/netbirdio/netbird/client/iface/netstack"
79
)
810

911
var (
@@ -19,6 +21,9 @@ func SetAndroidProtectSocketFn(fn func(fd int32) bool) {
1921

2022
// ControlProtectSocket is a Control function that sets the fwmark on the socket
2123
func ControlProtectSocket(_, _ string, c syscall.RawConn) error {
24+
if netstack.IsEnabled() {
25+
return nil
26+
}
2227
var aErr error
2328
err := c.Control(func(fd uintptr) {
2429
androidProtectSocketLock.Lock()

0 commit comments

Comments
 (0)