Skip to content

Commit 7aef0f6

Browse files
authored
[client] Implement environment variable handling for Android (#4440)
Some features can only be manipulated via environment variables. With this PR, environment variables can be managed from Android.
1 parent dba7ef6 commit 7aef0f6

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

client/android/client.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package android
44

55
import (
66
"context"
7+
"os"
78
"slices"
89
"sync"
910

@@ -83,7 +84,8 @@ func NewClient(cfgFile string, androidSDKVersion int, deviceName string, uiVersi
8384
}
8485

8586
// Run start the internal client. It is a blocker function
86-
func (c *Client) Run(urlOpener URLOpener, dns *DNSList, dnsReadyListener DnsReadyListener) error {
87+
func (c *Client) Run(urlOpener URLOpener, dns *DNSList, dnsReadyListener DnsReadyListener, envList *EnvList) error {
88+
exportEnvList(envList)
8789
cfg, err := profilemanager.UpdateOrCreateConfig(profilemanager.ConfigInput{
8890
ConfigPath: c.cfgFile,
8991
})
@@ -118,7 +120,8 @@ func (c *Client) Run(urlOpener URLOpener, dns *DNSList, dnsReadyListener DnsRead
118120

119121
// RunWithoutLogin we apply this type of run function when the backed has been started without UI (i.e. after reboot).
120122
// In this case make no sense handle registration steps.
121-
func (c *Client) RunWithoutLogin(dns *DNSList, dnsReadyListener DnsReadyListener) error {
123+
func (c *Client) RunWithoutLogin(dns *DNSList, dnsReadyListener DnsReadyListener, envList *EnvList) error {
124+
exportEnvList(envList)
122125
cfg, err := profilemanager.UpdateOrCreateConfig(profilemanager.ConfigInput{
123126
ConfigPath: c.cfgFile,
124127
})
@@ -249,3 +252,14 @@ func (c *Client) SetConnectionListener(listener ConnectionListener) {
249252
func (c *Client) RemoveConnectionListener() {
250253
c.recorder.RemoveConnectionListener()
251254
}
255+
256+
func exportEnvList(list *EnvList) {
257+
if list == nil {
258+
return
259+
}
260+
for k, v := range list.AllItems() {
261+
if err := os.Setenv(k, v); err != nil {
262+
log.Errorf("could not set env variable %s: %v", k, err)
263+
}
264+
}
265+
}

client/android/env_list.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package android
2+
3+
import "github.com/netbirdio/netbird/client/internal/peer"
4+
5+
var (
6+
// EnvKeyNBForceRelay Exported for Android java client
7+
EnvKeyNBForceRelay = peer.EnvKeyNBForceRelay
8+
)
9+
10+
// EnvList wraps a Go map for export to Java
11+
type EnvList struct {
12+
data map[string]string
13+
}
14+
15+
// NewEnvList creates a new EnvList
16+
func NewEnvList() *EnvList {
17+
return &EnvList{data: make(map[string]string)}
18+
}
19+
20+
// Put adds a key-value pair
21+
func (el *EnvList) Put(key, value string) {
22+
el.data[key] = value
23+
}
24+
25+
// Get retrieves a value by key
26+
func (el *EnvList) Get(key string) string {
27+
return el.data[key]
28+
}
29+
30+
func (el *EnvList) AllItems() map[string]string {
31+
return el.data
32+
}

client/internal/peer/conn.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"math/rand"
77
"net"
88
"net/netip"
9-
"os"
109
"runtime"
1110
"sync"
1211
"time"
@@ -174,7 +173,7 @@ func (conn *Conn) Open(engineCtx context.Context) error {
174173
conn.handshaker = NewHandshaker(conn.Log, conn.config, conn.signaler, conn.workerICE, conn.workerRelay)
175174

176175
conn.handshaker.AddOnNewOfferListener(conn.workerRelay.OnNewOffer)
177-
if os.Getenv("NB_FORCE_RELAY") != "true" {
176+
if !isForceRelayed() {
178177
conn.handshaker.AddOnNewOfferListener(conn.workerICE.OnNewOffer)
179178
}
180179

client/internal/peer/env.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package peer
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
const (
9+
EnvKeyNBForceRelay = "NB_FORCE_RELAY"
10+
)
11+
12+
func isForceRelayed() bool {
13+
return strings.EqualFold(os.Getenv(EnvKeyNBForceRelay), "true")
14+
}

0 commit comments

Comments
 (0)