2024-05-07 12:28:30 +02:00
|
|
|
//go:build android
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2024-10-02 18:24:22 +02:00
|
|
|
"github.com/netbirdio/netbird/client/iface/device"
|
2023-03-17 10:37:27 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal"
|
2023-07-14 21:56:22 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/dns"
|
2023-11-02 19:04:33 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/listener"
|
2023-03-17 10:37:27 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2023-03-24 08:40:39 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/stdnet"
|
2023-03-17 10:37:27 +01:00
|
|
|
"github.com/netbirdio/netbird/client/system"
|
|
|
|
"github.com/netbirdio/netbird/formatter"
|
2024-05-07 12:28:30 +02:00
|
|
|
"github.com/netbirdio/netbird/util/net"
|
2023-03-17 10:37:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectionListener export internal Listener for mobile
|
|
|
|
type ConnectionListener interface {
|
|
|
|
peer.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// TunAdapter export internal TunAdapter for mobile
|
|
|
|
type TunAdapter interface {
|
2024-10-02 18:24:22 +02:00
|
|
|
device.TunAdapter
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 08:40:39 +01:00
|
|
|
// IFaceDiscover export internal IFaceDiscover for mobile
|
|
|
|
type IFaceDiscover interface {
|
2023-04-13 17:00:01 +02:00
|
|
|
stdnet.ExternalIFaceDiscover
|
2023-03-24 08:40:39 +01:00
|
|
|
}
|
|
|
|
|
2023-11-02 19:04:33 +01:00
|
|
|
// NetworkChangeListener export internal NetworkChangeListener for mobile
|
|
|
|
type NetworkChangeListener interface {
|
|
|
|
listener.NetworkChangeListener
|
2023-05-31 18:25:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
// DnsReadyListener export internal dns ReadyListener for mobile
|
|
|
|
type DnsReadyListener interface {
|
|
|
|
dns.ReadyListener
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
func init() {
|
|
|
|
formatter.SetLogcatFormatter(log.StandardLogger())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client struct manage the life circle of background service
|
|
|
|
type Client struct {
|
2023-11-02 19:04:33 +01:00
|
|
|
cfgFile string
|
2024-10-02 18:24:22 +02:00
|
|
|
tunAdapter device.TunAdapter
|
2023-11-02 19:04:33 +01:00
|
|
|
iFaceDiscover IFaceDiscover
|
|
|
|
recorder *peer.Status
|
|
|
|
ctxCancel context.CancelFunc
|
|
|
|
ctxCancelLock *sync.Mutex
|
|
|
|
deviceName string
|
2024-05-31 17:26:56 +02:00
|
|
|
uiVersion string
|
2023-11-02 19:04:33 +01:00
|
|
|
networkChangeListener listener.NetworkChangeListener
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient instantiate a new Client
|
2024-05-31 17:26:56 +02:00
|
|
|
func NewClient(cfgFile, deviceName string, uiVersion string, tunAdapter TunAdapter, iFaceDiscover IFaceDiscover, networkChangeListener NetworkChangeListener) *Client {
|
2024-05-07 12:28:30 +02:00
|
|
|
net.SetAndroidProtectSocketFn(tunAdapter.ProtectSocket)
|
2023-03-17 10:37:27 +01:00
|
|
|
return &Client{
|
2023-11-02 19:04:33 +01:00
|
|
|
cfgFile: cfgFile,
|
|
|
|
deviceName: deviceName,
|
2024-05-31 17:26:56 +02:00
|
|
|
uiVersion: uiVersion,
|
2023-11-02 19:04:33 +01:00
|
|
|
tunAdapter: tunAdapter,
|
|
|
|
iFaceDiscover: iFaceDiscover,
|
|
|
|
recorder: peer.NewRecorder(""),
|
|
|
|
ctxCancelLock: &sync.Mutex{},
|
|
|
|
networkChangeListener: networkChangeListener,
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run start the internal client. It is a blocker function
|
2023-07-14 21:56:22 +02:00
|
|
|
func (c *Client) Run(urlOpener URLOpener, dns *DNSList, dnsReadyListener DnsReadyListener) error {
|
2023-03-17 10:37:27 +01:00
|
|
|
cfg, err := internal.UpdateOrCreateConfig(internal.ConfigInput{
|
|
|
|
ConfigPath: c.cfgFile,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.recorder.UpdateManagementAddress(cfg.ManagementURL.String())
|
2024-02-24 12:41:13 +01:00
|
|
|
c.recorder.UpdateRosenpass(cfg.RosenpassEnabled, cfg.RosenpassPermissive)
|
2023-03-17 10:37:27 +01:00
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
//nolint
|
|
|
|
ctxWithValues := context.WithValue(context.Background(), system.DeviceNameCtxKey, c.deviceName)
|
2024-05-31 17:26:56 +02:00
|
|
|
//nolint
|
|
|
|
ctxWithValues = context.WithValue(ctxWithValues, system.UiVersionCtxKey, c.uiVersion)
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
c.ctxCancelLock.Lock()
|
|
|
|
ctx, c.ctxCancel = context.WithCancel(ctxWithValues)
|
|
|
|
defer c.ctxCancel()
|
|
|
|
c.ctxCancelLock.Unlock()
|
|
|
|
|
|
|
|
auth := NewAuthWithConfig(ctx, cfg)
|
2023-03-28 09:57:23 +02:00
|
|
|
err = auth.login(urlOpener)
|
2023-03-17 10:37:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo do not throw error in case of cancelled context
|
|
|
|
ctx = internal.CtxInitState(ctx)
|
2024-05-10 10:47:16 +02:00
|
|
|
connectClient := internal.NewConnectClient(ctx, cfg, c.recorder)
|
|
|
|
return connectClient.RunOnAndroid(c.tunAdapter, c.iFaceDiscover, c.networkChangeListener, dns.items, dnsReadyListener)
|
2023-08-11 11:51:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunWithoutLogin we apply this type of run function when the backed has been started without UI (i.e. after reboot).
|
|
|
|
// In this case make no sense handle registration steps.
|
|
|
|
func (c *Client) RunWithoutLogin(dns *DNSList, dnsReadyListener DnsReadyListener) error {
|
|
|
|
cfg, err := internal.UpdateOrCreateConfig(internal.ConfigInput{
|
|
|
|
ConfigPath: c.cfgFile,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.recorder.UpdateManagementAddress(cfg.ManagementURL.String())
|
2024-02-24 12:41:13 +01:00
|
|
|
c.recorder.UpdateRosenpass(cfg.RosenpassEnabled, cfg.RosenpassPermissive)
|
2023-08-11 11:51:39 +02:00
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
//nolint
|
|
|
|
ctxWithValues := context.WithValue(context.Background(), system.DeviceNameCtxKey, c.deviceName)
|
|
|
|
c.ctxCancelLock.Lock()
|
|
|
|
ctx, c.ctxCancel = context.WithCancel(ctxWithValues)
|
|
|
|
defer c.ctxCancel()
|
|
|
|
c.ctxCancelLock.Unlock()
|
|
|
|
|
|
|
|
// todo do not throw error in case of cancelled context
|
|
|
|
ctx = internal.CtxInitState(ctx)
|
2024-05-10 10:47:16 +02:00
|
|
|
connectClient := internal.NewConnectClient(ctx, cfg, c.recorder)
|
|
|
|
return connectClient.RunOnAndroid(c.tunAdapter, c.iFaceDiscover, c.networkChangeListener, dns.items, dnsReadyListener)
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the internal client and free the resources
|
|
|
|
func (c *Client) Stop() {
|
|
|
|
c.ctxCancelLock.Lock()
|
|
|
|
defer c.ctxCancelLock.Unlock()
|
|
|
|
if c.ctxCancel == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ctxCancel()
|
|
|
|
}
|
|
|
|
|
2023-06-09 09:55:31 +02:00
|
|
|
// SetTraceLogLevel configure the logger to trace level
|
|
|
|
func (c *Client) SetTraceLogLevel() {
|
|
|
|
log.SetLevel(log.TraceLevel)
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:30:38 +01:00
|
|
|
// SetInfoLogLevel configure the logger to info level
|
|
|
|
func (c *Client) SetInfoLogLevel() {
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
// PeersList return with the list of the PeerInfos
|
|
|
|
func (c *Client) PeersList() *PeerInfoArray {
|
|
|
|
|
|
|
|
fullStatus := c.recorder.GetFullStatus()
|
|
|
|
|
|
|
|
peerInfos := make([]PeerInfo, len(fullStatus.Peers))
|
|
|
|
for n, p := range fullStatus.Peers {
|
|
|
|
pi := PeerInfo{
|
|
|
|
p.IP,
|
|
|
|
p.FQDN,
|
|
|
|
p.ConnStatus.String(),
|
|
|
|
}
|
|
|
|
peerInfos[n] = pi
|
|
|
|
}
|
|
|
|
return &PeerInfoArray{items: peerInfos}
|
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
// OnUpdatedHostDNS update the DNS servers addresses for root zones
|
|
|
|
func (c *Client) OnUpdatedHostDNS(list *DNSList) error {
|
|
|
|
dnsServer, err := dns.GetServerDns()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsServer.OnUpdatedHostDNSServer(list.items)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:59:13 +02:00
|
|
|
// SetConnectionListener set the network connection listener
|
|
|
|
func (c *Client) SetConnectionListener(listener ConnectionListener) {
|
|
|
|
c.recorder.SetConnectionListener(listener)
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveConnectionListener remove connection listener
|
2023-04-03 16:59:13 +02:00
|
|
|
func (c *Client) RemoveConnectionListener() {
|
|
|
|
c.recorder.RemoveConnectionListener()
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|