You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.0 KiB
Go
137 lines
3.0 KiB
Go
8 months ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
|
|
||
8 months ago
|
package libtailscale
|
||
8 months ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"path/filepath"
|
||
8 months ago
|
"runtime/debug"
|
||
8 months ago
|
"time"
|
||
|
|
||
|
"tailscale.com/logpolicy"
|
||
|
"tailscale.com/logtail"
|
||
|
"tailscale.com/logtail/filch"
|
||
|
"tailscale.com/net/interfaces"
|
||
|
"tailscale.com/smallzstd"
|
||
|
"tailscale.com/types/logger"
|
||
|
"tailscale.com/types/logid"
|
||
|
"tailscale.com/util/clientmetric"
|
||
|
"tailscale.com/util/must"
|
||
|
)
|
||
|
|
||
|
const defaultMTU = 1280 // minimalMTU from wgengine/userspace.go
|
||
|
|
||
|
const (
|
||
|
logPrefKey = "privatelogid"
|
||
|
loginMethodPrefKey = "loginmethod"
|
||
|
customLoginServerPrefKey = "customloginserver"
|
||
|
)
|
||
|
|
||
8 months ago
|
func newApp(dataDir string, appCtx AppContext) Application {
|
||
8 months ago
|
a := &App{
|
||
8 months ago
|
dataDir: dataDir,
|
||
|
appCtx: appCtx,
|
||
8 months ago
|
}
|
||
8 months ago
|
a.ready.Add(1)
|
||
8 months ago
|
|
||
8 months ago
|
a.store = newStateStore(a.appCtx)
|
||
8 months ago
|
interfaces.RegisterInterfaceGetter(a.getInterfaces)
|
||
|
go func() {
|
||
8 months ago
|
defer func() {
|
||
|
if p := recover(); p != nil {
|
||
|
log.Printf("panic in runBackend %s: %s", p, debug.Stack())
|
||
|
panic(p)
|
||
|
}
|
||
|
}()
|
||
|
|
||
8 months ago
|
ctx := context.Background()
|
||
|
if err := a.runBackend(ctx); err != nil {
|
||
|
fatalErr(err)
|
||
|
}
|
||
|
}()
|
||
8 months ago
|
|
||
|
return a
|
||
8 months ago
|
}
|
||
|
|
||
|
func fatalErr(err error) {
|
||
|
// TODO: expose in UI.
|
||
|
log.Printf("fatal error: %v", err)
|
||
|
}
|
||
|
|
||
|
// osVersion returns android.os.Build.VERSION.RELEASE. " [nogoogle]" is appended
|
||
|
// if Google Play services are not compiled in.
|
||
|
func (a *App) osVersion() string {
|
||
8 months ago
|
version, err := a.appCtx.GetOSVersion()
|
||
8 months ago
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return version
|
||
|
}
|
||
|
|
||
|
// modelName return the MANUFACTURER + MODEL from
|
||
|
// android.os.Build.
|
||
|
func (a *App) modelName() string {
|
||
8 months ago
|
model, err := a.appCtx.GetModelName()
|
||
8 months ago
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return model
|
||
|
}
|
||
|
|
||
|
func (a *App) isChromeOS() bool {
|
||
8 months ago
|
isChromeOS, err := a.appCtx.IsChromeOS()
|
||
8 months ago
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
8 months ago
|
return isChromeOS
|
||
8 months ago
|
}
|
||
|
|
||
|
// SetupLogs sets up remote logging.
|
||
|
func (b *backend) setupLogs(logDir string, logID logid.PrivateID, logf logger.Logf) {
|
||
|
if b.netMon == nil {
|
||
|
panic("netMon must be created prior to SetupLogs")
|
||
|
}
|
||
|
transport := logpolicy.NewLogtailTransport(logtail.DefaultHost, b.netMon, log.Printf)
|
||
|
|
||
|
logcfg := logtail.Config{
|
||
|
Collection: logtail.CollectionNode,
|
||
|
PrivateID: logID,
|
||
|
Stderr: log.Writer(),
|
||
|
MetricsDelta: clientmetric.EncodeLogTailMetricsDelta,
|
||
|
IncludeProcID: true,
|
||
|
IncludeProcSequence: true,
|
||
|
NewZstdEncoder: func() logtail.Encoder {
|
||
|
return must.Get(smallzstd.NewEncoder(nil))
|
||
|
},
|
||
|
HTTPC: &http.Client{Transport: transport},
|
||
|
}
|
||
|
logcfg.FlushDelayFn = func() time.Duration { return 2 * time.Minute }
|
||
|
|
||
|
filchOpts := filch.Options{
|
||
|
ReplaceStderr: true,
|
||
|
}
|
||
|
|
||
|
var filchErr error
|
||
|
if logDir != "" {
|
||
|
logPath := filepath.Join(logDir, "ipn.log.")
|
||
|
logcfg.Buffer, filchErr = filch.New(logPath, filchOpts)
|
||
|
}
|
||
|
|
||
|
b.logger = logtail.NewLogger(logcfg, logf)
|
||
|
|
||
|
log.SetFlags(0)
|
||
|
log.SetOutput(b.logger)
|
||
|
|
||
|
log.Printf("goSetupLogs: success")
|
||
|
|
||
|
if logDir == "" {
|
||
|
log.Printf("SetupLogs: no logDir, storing logs in memory")
|
||
|
}
|
||
|
if filchErr != nil {
|
||
|
log.Printf("SetupLogs: filch setup failed: %v", filchErr)
|
||
|
}
|
||
|
}
|