-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[client] Android debug bundle support #5888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
b317825
1d792f0
a35ecf9
f01c1ee
1c2275e
0920e6a
aa1c194
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import ( | |
|
|
||
| "github.com/netbirdio/netbird/client/iface/device" | ||
| "github.com/netbirdio/netbird/client/internal" | ||
| "github.com/netbirdio/netbird/client/internal/debug" | ||
| "github.com/netbirdio/netbird/client/internal/dns" | ||
| "github.com/netbirdio/netbird/client/internal/listener" | ||
| "github.com/netbirdio/netbird/client/internal/peer" | ||
|
|
@@ -26,6 +27,7 @@ import ( | |
| "github.com/netbirdio/netbird/formatter" | ||
| "github.com/netbirdio/netbird/route" | ||
| "github.com/netbirdio/netbird/shared/management/domain" | ||
| types "github.com/netbirdio/netbird/upload-server/types" | ||
| ) | ||
|
|
||
| // ConnectionListener export internal Listener for mobile | ||
|
|
@@ -69,6 +71,8 @@ type Client struct { | |
| networkChangeListener listener.NetworkChangeListener | ||
|
|
||
| connectClient *internal.ConnectClient | ||
| config *profilemanager.Config | ||
| cacheDir string | ||
| } | ||
|
|
||
| // NewClient instantiate a new Client | ||
|
|
@@ -93,6 +97,7 @@ func (c *Client) Run(platformFiles PlatformFiles, urlOpener URLOpener, isAndroid | |
|
|
||
| cfgFile := platformFiles.ConfigurationFilePath() | ||
| stateFile := platformFiles.StateFilePath() | ||
| cacheDir := platformFiles.CacheDir() | ||
|
|
||
| log.Infof("Starting client with config: %s, state: %s", cfgFile, stateFile) | ||
|
|
||
|
|
@@ -124,8 +129,10 @@ func (c *Client) Run(platformFiles PlatformFiles, urlOpener URLOpener, isAndroid | |
|
|
||
| // todo do not throw error in case of cancelled context | ||
| ctx = internal.CtxInitState(ctx) | ||
| c.config = cfg | ||
| c.cacheDir = cacheDir | ||
| c.connectClient = internal.NewConnectClient(ctx, cfg, c.recorder) | ||
| return c.connectClient.RunOnAndroid(c.tunAdapter, c.iFaceDiscover, c.networkChangeListener, slices.Clone(dns.items), dnsReadyListener, stateFile) | ||
| return c.connectClient.RunOnAndroid(c.tunAdapter, c.iFaceDiscover, c.networkChangeListener, slices.Clone(dns.items), dnsReadyListener, stateFile, cacheDir) | ||
| } | ||
|
|
||
| // RunWithoutLogin we apply this type of run function when the backed has been started without UI (i.e. after reboot). | ||
|
|
@@ -135,6 +142,7 @@ func (c *Client) RunWithoutLogin(platformFiles PlatformFiles, dns *DNSList, dnsR | |
|
|
||
| cfgFile := platformFiles.ConfigurationFilePath() | ||
| stateFile := platformFiles.StateFilePath() | ||
| cacheDir := platformFiles.CacheDir() | ||
|
|
||
| log.Infof("Starting client without login with config: %s, state: %s", cfgFile, stateFile) | ||
|
|
||
|
|
@@ -157,8 +165,10 @@ func (c *Client) RunWithoutLogin(platformFiles PlatformFiles, dns *DNSList, dnsR | |
|
|
||
| // todo do not throw error in case of cancelled context | ||
| ctx = internal.CtxInitState(ctx) | ||
| c.config = cfg | ||
| c.cacheDir = cacheDir | ||
| c.connectClient = internal.NewConnectClient(ctx, cfg, c.recorder) | ||
| return c.connectClient.RunOnAndroid(c.tunAdapter, c.iFaceDiscover, c.networkChangeListener, slices.Clone(dns.items), dnsReadyListener, stateFile) | ||
| return c.connectClient.RunOnAndroid(c.tunAdapter, c.iFaceDiscover, c.networkChangeListener, slices.Clone(dns.items), dnsReadyListener, stateFile, cacheDir) | ||
| } | ||
|
|
||
| // Stop the internal client and free the resources | ||
|
|
@@ -185,6 +195,71 @@ func (c *Client) RenewTun(fd int) error { | |
| return e.RenewTun(fd) | ||
| } | ||
|
|
||
| // DebugBundle generates a debug bundle, uploads it, and returns the upload key. | ||
| // It works both with and without a running engine. | ||
| func (c *Client) DebugBundle(platformFiles PlatformFiles, anonymize bool) (string, error) { | ||
| cfg := c.config | ||
| cacheDir := c.cacheDir | ||
|
|
||
| // If the engine hasn't been started, load config from disk | ||
| if cfg == nil { | ||
| var err error | ||
| cfg, err = profilemanager.UpdateOrCreateConfig(profilemanager.ConfigInput{ | ||
| ConfigPath: platformFiles.ConfigurationFilePath(), | ||
| }) | ||
| if err != nil { | ||
| return "", fmt.Errorf("load config: %w", err) | ||
| } | ||
| cacheDir = platformFiles.CacheDir() | ||
| } | ||
|
|
||
| deps := debug.GeneratorDependencies{ | ||
| InternalConfig: cfg, | ||
| StatusRecorder: c.recorder, | ||
| TempDir: cacheDir, | ||
| } | ||
|
Comment on lines
+221
to
+240
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🤖 Prompt for AI Agents
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Android, addStateFile() calls NewServiceManager("").GetStatePath() which resolves to /var/lib/netbird/state.json (the Linux default), not the app's actual state file path that Java passes via PlatformFiles.StateFilePath(). The bundle silently skips the state file since that path doesn't exist on Android.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Would you like me to open a GitHub issue to track this? It would capture the context: ✏️ Learnings added
🧠 Learnings used |
||
|
|
||
| if c.connectClient != nil { | ||
| resp, err := c.connectClient.GetLatestSyncResponse() | ||
| if err != nil { | ||
| log.Warnf("get latest sync response: %v", err) | ||
| } | ||
| deps.SyncResponse = resp | ||
|
|
||
| if e := c.connectClient.Engine(); e != nil { | ||
| if cm := e.GetClientMetrics(); cm != nil { | ||
| deps.ClientMetrics = cm | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bundleGenerator := debug.NewBundleGenerator( | ||
| deps, | ||
| debug.BundleConfig{ | ||
| Anonymize: anonymize, | ||
| IncludeSystemInfo: true, | ||
| }, | ||
| ) | ||
|
|
||
| path, err := bundleGenerator.Generate() | ||
| if err != nil { | ||
| return "", fmt.Errorf("generate debug bundle: %w", err) | ||
| } | ||
| defer func() { | ||
| if err := os.Remove(path); err != nil { | ||
| log.Errorf("failed to remove debug bundle file: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| key, err := debug.UploadDebugBundle(context.Background(), types.DefaultBundleURL, cfg.ManagementURL.String(), path) | ||
| if err != nil { | ||
| return "", fmt.Errorf("upload debug bundle: %w", err) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| log.Infof("debug bundle uploaded with key %s", key) | ||
| return key, nil | ||
| } | ||
|
|
||
| // SetTraceLogLevel configure the logger to trace level | ||
| func (c *Client) SetTraceLogLevel() { | ||
| log.SetLevel(log.TraceLevel) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| //go:build android | ||
|
|
||
| package debug | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| func (g *BundleGenerator) addPlatformLog() error { | ||
| cmd := exec.Command("/system/bin/logcat", "-d") | ||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| return fmt.Errorf("run logcat: %w", err) | ||
| } | ||
|
|
||
| var logReader *strings.Reader | ||
| if g.anonymize { | ||
| anonymized := g.anonymizer.AnonymizeString(string(out)) | ||
|
pappz marked this conversation as resolved.
Outdated
|
||
| logReader = strings.NewReader(anonymized) | ||
| } else { | ||
| logReader = strings.NewReader(string(out)) | ||
| } | ||
|
|
||
| if err := g.addFileToZip(logReader, "logcat.txt"); err != nil { | ||
| return fmt.Errorf("add logcat to zip: %w", err) | ||
| } | ||
|
|
||
| log.Debugf("added logcat output to debug bundle (%d bytes)", len(out)) | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| //go:build !android | ||
|
|
||
| package debug | ||
|
|
||
| import ( | ||
| "slices" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
|
|
||
| "github.com/netbirdio/netbird/util" | ||
| ) | ||
|
|
||
| func (g *BundleGenerator) addPlatformLog() error { | ||
| if g.logPath != "" && !slices.Contains(util.SpecialLogs, g.logPath) { | ||
| if err := g.addLogfile(); err != nil { | ||
| log.Errorf("failed to add log file to debug bundle: %v", err) | ||
| if err := g.trySystemdLogFallback(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } else if err := g.trySystemdLogFallback(); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.