2023-03-17 10:37:27 +01:00
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/internal"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2023-05-31 18:25:24 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/routemanager"
|
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"
|
|
|
|
"github.com/netbirdio/netbird/iface"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectionListener export internal Listener for mobile
|
|
|
|
type ConnectionListener interface {
|
|
|
|
peer.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// TunAdapter export internal TunAdapter for mobile
|
|
|
|
type TunAdapter interface {
|
|
|
|
iface.TunAdapter
|
|
|
|
}
|
|
|
|
|
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-05-31 18:25:24 +02:00
|
|
|
// RouteListener export internal RouteListener for mobile
|
|
|
|
type RouteListener interface {
|
|
|
|
routemanager.RouteListener
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
cfgFile string
|
|
|
|
tunAdapter iface.TunAdapter
|
2023-03-24 08:40:39 +01:00
|
|
|
iFaceDiscover IFaceDiscover
|
2023-03-17 10:37:27 +01:00
|
|
|
recorder *peer.Status
|
|
|
|
ctxCancel context.CancelFunc
|
|
|
|
ctxCancelLock *sync.Mutex
|
|
|
|
deviceName string
|
2023-05-31 18:25:24 +02:00
|
|
|
routeListener routemanager.RouteListener
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient instantiate a new Client
|
2023-05-31 18:25:24 +02:00
|
|
|
func NewClient(cfgFile, deviceName string, tunAdapter TunAdapter, iFaceDiscover IFaceDiscover, routeListener RouteListener) *Client {
|
2023-03-17 10:37:27 +01:00
|
|
|
lvl, _ := log.ParseLevel("trace")
|
|
|
|
log.SetLevel(lvl)
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
cfgFile: cfgFile,
|
|
|
|
deviceName: deviceName,
|
|
|
|
tunAdapter: tunAdapter,
|
2023-03-24 08:40:39 +01:00
|
|
|
iFaceDiscover: iFaceDiscover,
|
2023-03-17 10:37:27 +01:00
|
|
|
recorder: peer.NewRecorder(""),
|
|
|
|
ctxCancelLock: &sync.Mutex{},
|
2023-05-31 18:25:24 +02:00
|
|
|
routeListener: routeListener,
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run start the internal client. It is a blocker function
|
|
|
|
func (c *Client) Run(urlOpener URLOpener) error {
|
|
|
|
cfg, err := internal.UpdateOrCreateConfig(internal.ConfigInput{
|
|
|
|
ConfigPath: c.cfgFile,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.recorder.UpdateManagementAddress(cfg.ManagementURL.String())
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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)
|
2023-05-31 18:25:24 +02:00
|
|
|
return internal.RunClient(ctx, cfg, c.recorder, c.tunAdapter, c.iFaceDiscover, c.routeListener)
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(),
|
|
|
|
p.Direct,
|
|
|
|
}
|
|
|
|
peerInfos[n] = pi
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PeerInfoArray{items: peerInfos}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|