TUN-3514: Transport logger write to UI when UI is enabled

This commit is contained in:
cthuang 2020-11-09 11:40:48 +00:00
parent 543169c893
commit ebc003d478
3 changed files with 34 additions and 33 deletions

View File

@ -249,7 +249,7 @@ func StartServer(
shutdownC, shutdownC,
graceShutdownC chan struct{}, graceShutdownC chan struct{},
namedTunnel *connection.NamedTunnelConfig, namedTunnel *connection.NamedTunnelConfig,
log logger.Service, generalLogger logger.Service,
isUIEnabled bool, isUIEnabled bool,
) error { ) error {
_ = raven.SetDSN(sentryDSN) _ = raven.SetDSN(sentryDSN)
@ -260,45 +260,45 @@ func StartServer(
dnsReadySignal := make(chan struct{}) dnsReadySignal := make(chan struct{})
if config.GetConfiguration().Source() == "" { if config.GetConfiguration().Source() == "" {
log.Infof(config.ErrNoConfigFile.Error()) generalLogger.Infof(config.ErrNoConfigFile.Error())
} }
if c.IsSet("trace-output") { if c.IsSet("trace-output") {
tmpTraceFile, err := ioutil.TempFile("", "trace") tmpTraceFile, err := ioutil.TempFile("", "trace")
if err != nil { if err != nil {
log.Errorf("Failed to create new temporary file to save trace output: %s", err) generalLogger.Errorf("Failed to create new temporary file to save trace output: %s", err)
} }
defer func() { defer func() {
if err := tmpTraceFile.Close(); err != nil { if err := tmpTraceFile.Close(); err != nil {
log.Errorf("Failed to close trace output file %s with error: %s", tmpTraceFile.Name(), err) generalLogger.Errorf("Failed to close trace output file %s with error: %s", tmpTraceFile.Name(), err)
} }
if err := os.Rename(tmpTraceFile.Name(), c.String("trace-output")); err != nil { if err := os.Rename(tmpTraceFile.Name(), c.String("trace-output")); err != nil {
log.Errorf("Failed to rename temporary trace output file %s to %s with error: %s", tmpTraceFile.Name(), c.String("trace-output"), err) generalLogger.Errorf("Failed to rename temporary trace output file %s to %s with error: %s", tmpTraceFile.Name(), c.String("trace-output"), err)
} else { } else {
err := os.Remove(tmpTraceFile.Name()) err := os.Remove(tmpTraceFile.Name())
if err != nil { if err != nil {
log.Errorf("Failed to remove the temporary trace file %s with error: %s", tmpTraceFile.Name(), err) generalLogger.Errorf("Failed to remove the temporary trace file %s with error: %s", tmpTraceFile.Name(), err)
} }
} }
}() }()
if err := trace.Start(tmpTraceFile); err != nil { if err := trace.Start(tmpTraceFile); err != nil {
log.Errorf("Failed to start trace: %s", err) generalLogger.Errorf("Failed to start trace: %s", err)
return errors.Wrap(err, "Error starting tracing") return errors.Wrap(err, "Error starting tracing")
} }
defer trace.Stop() defer trace.Stop()
} }
buildInfo := buildinfo.GetBuildInfo(version) buildInfo := buildinfo.GetBuildInfo(version)
buildInfo.Log(log) buildInfo.Log(generalLogger)
logClientOptions(c, log) logClientOptions(c, generalLogger)
if c.IsSet("proxy-dns") { if c.IsSet("proxy-dns") {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
errC <- runDNSProxyServer(c, dnsReadySignal, shutdownC, log) errC <- runDNSProxyServer(c, dnsReadySignal, shutdownC, generalLogger)
}() }()
} else { } else {
close(dnsReadySignal) close(dnsReadySignal)
@ -309,24 +309,24 @@ func StartServer(
metricsListener, err := listeners.Listen("tcp", c.String("metrics")) metricsListener, err := listeners.Listen("tcp", c.String("metrics"))
if err != nil { if err != nil {
log.Errorf("Error opening metrics server listener: %s", err) generalLogger.Errorf("Error opening metrics server listener: %s", err)
return errors.Wrap(err, "Error opening metrics server listener") return errors.Wrap(err, "Error opening metrics server listener")
} }
defer metricsListener.Close() defer metricsListener.Close()
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
errC <- metrics.ServeMetrics(metricsListener, shutdownC, log) errC <- metrics.ServeMetrics(metricsListener, shutdownC, generalLogger)
}() }()
go notifySystemd(connectedSignal) go notifySystemd(connectedSignal)
if c.IsSet("pidfile") { if c.IsSet("pidfile") {
go writePidFile(connectedSignal, c.String("pidfile"), log) go writePidFile(connectedSignal, c.String("pidfile"), generalLogger)
} }
cloudflaredID, err := uuid.NewRandom() cloudflaredID, err := uuid.NewRandom()
if err != nil { if err != nil {
log.Errorf("Cannot generate cloudflared ID: %s", err) generalLogger.Errorf("Cannot generate cloudflared ID: %s", err)
return err return err
} }
@ -337,12 +337,12 @@ func StartServer(
}() }()
// update needs to be after DNS proxy is up to resolve equinox server address // update needs to be after DNS proxy is up to resolve equinox server address
if updater.IsAutoupdateEnabled(c, log) { if updater.IsAutoupdateEnabled(c, generalLogger) {
log.Infof("Autoupdate frequency is set to %v", c.Duration("autoupdate-freq")) generalLogger.Infof("Autoupdate frequency is set to %v", c.Duration("autoupdate-freq"))
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
autoupdater := updater.NewAutoUpdater(c.Duration("autoupdate-freq"), &listeners, log) autoupdater := updater.NewAutoUpdater(c.Duration("autoupdate-freq"), &listeners, generalLogger)
errC <- autoupdater.Run(ctx) errC <- autoupdater.Run(ctx)
}() }()
} }
@ -351,33 +351,33 @@ func StartServer(
if dnsProxyStandAlone(c) { if dnsProxyStandAlone(c) {
connectedSignal.Notify() connectedSignal.Notify()
// no grace period, handle SIGINT/SIGTERM immediately // no grace period, handle SIGINT/SIGTERM immediately
return waitToShutdown(&wg, errC, shutdownC, graceShutdownC, 0, log) return waitToShutdown(&wg, errC, shutdownC, graceShutdownC, 0, generalLogger)
} }
url := c.String("url") url := c.String("url")
hostname := c.String("hostname") hostname := c.String("hostname")
if url == hostname && url != "" && hostname != "" { if url == hostname && url != "" && hostname != "" {
errText := "hostname and url shouldn't match. See --help for more information" errText := "hostname and url shouldn't match. See --help for more information"
log.Error(errText) generalLogger.Error(errText)
return fmt.Errorf(errText) return fmt.Errorf(errText)
} }
transportLogger, err := createLogger(c, true, false) transportLogger, err := createLogger(c, true, isUIEnabled)
if err != nil { if err != nil {
return errors.Wrap(err, "error setting up transport logger") return errors.Wrap(err, "error setting up transport logger")
} }
tunnelConfig, ingressRules, err := prepareTunnelConfig(c, buildInfo, version, log, transportLogger, namedTunnel, isUIEnabled) tunnelConfig, ingressRules, err := prepareTunnelConfig(c, buildInfo, version, generalLogger, transportLogger, namedTunnel, isUIEnabled)
if err != nil { if err != nil {
return err return err
} }
ingressRules.StartOrigins(&wg, log, shutdownC, errC) ingressRules.StartOrigins(&wg, generalLogger, shutdownC, errC)
reconnectCh := make(chan origin.ReconnectSignal, 1) reconnectCh := make(chan origin.ReconnectSignal, 1)
if c.IsSet("stdin-control") { if c.IsSet("stdin-control") {
log.Info("Enabling control through stdin") generalLogger.Info("Enabling control through stdin")
go stdinControl(reconnectCh, log) go stdinControl(reconnectCh, generalLogger)
} }
wg.Add(1) wg.Add(1)
@ -398,10 +398,10 @@ func StartServer(
if err != nil { if err != nil {
return err return err
} }
tunnelInfo.LaunchUI(ctx, log, logLevels, tunnelConfig.TunnelEventChan) tunnelInfo.LaunchUI(ctx, generalLogger, transportLogger, logLevels, tunnelConfig.TunnelEventChan)
} }
return waitToShutdown(&wg, errC, shutdownC, graceShutdownC, c.Duration("grace-period"), log) return waitToShutdown(&wg, errC, shutdownC, graceShutdownC, c.Duration("grace-period"), generalLogger)
} }
// forceSetFlag attempts to set the given flag value in the closest context that has it defined // forceSetFlag attempts to set the given flag value in the closest context that has it defined

View File

@ -67,7 +67,7 @@ func NewUIModel(version, hostname, metricsURL string, ing *ingress.Ingress, haCo
func (data *uiModel) LaunchUI( func (data *uiModel) LaunchUI(
ctx context.Context, ctx context.Context,
log logger.Service, generalLogger, transportLogger logger.Service,
logLevels []logger.Level, logLevels []logger.Level,
tunnelEventChan <-chan TunnelEvent, tunnelEventChan <-chan TunnelEvent,
) { ) {
@ -75,7 +75,8 @@ func (data *uiModel) LaunchUI(
// Add TextView as a group to write output to // Add TextView as a group to write output to
logTextView := NewDynamicColorTextView() logTextView := NewDynamicColorTextView()
log.Add(logTextView, logger.NewUIFormatter(time.RFC3339), logLevels...) generalLogger.Add(logTextView, logger.NewUIFormatter(time.RFC3339), logLevels...)
transportLogger.Add(logTextView, logger.NewUIFormatter(time.RFC3339), logLevels...)
// Construct the UI // Construct the UI
palette := palette{ palette := palette{
@ -140,7 +141,7 @@ func (data *uiModel) LaunchUI(
case Connected: case Connected:
data.setConnTableCell(event, connTable, palette) data.setConnTableCell(event, connTable, palette)
case Disconnected, Reconnecting: case Disconnected, Reconnecting:
data.changeConnStatus(event, connTable, log, palette) data.changeConnStatus(event, connTable, generalLogger, palette)
case SetUrl: case SetUrl:
tunnelHostText.SetText(event.Url) tunnelHostText.SetText(event.Url)
data.edgeURL = event.Url data.edgeURL = event.Url
@ -156,7 +157,7 @@ func (data *uiModel) LaunchUI(
go func() { go func() {
if err := app.SetRoot(frame, true).Run(); err != nil { if err := app.SetRoot(frame, true).Run(); err != nil {
log.Errorf("Error launching UI: %s", err) generalLogger.Errorf("Error launching UI: %s", err)
} }
}() }()
} }

View File

@ -24,13 +24,13 @@ func NewObserver(logger logger.Service, tunnelEventChan chan<- ui.TunnelEvent) *
} }
} }
func (o *Observer) logServerInfo(connectionID uint8, location, msg string) { func (o *Observer) logServerInfo(connIndex uint8, location, msg string) {
// If launch-ui flag is set, send connect msg // If launch-ui flag is set, send connect msg
if o.tunnelEventChan != nil { if o.tunnelEventChan != nil {
o.tunnelEventChan <- ui.TunnelEvent{Index: connectionID, EventType: ui.Connected, Location: location} o.tunnelEventChan <- ui.TunnelEvent{Index: connIndex, EventType: ui.Connected, Location: location}
} }
o.Infof(msg) o.Infof(msg)
o.metrics.registerServerLocation(uint8ToString(connectionID), location) o.metrics.registerServerLocation(uint8ToString(connIndex), location)
} }
func (o *Observer) logTrialHostname(registration *tunnelpogs.TunnelRegistration) error { func (o *Observer) logTrialHostname(registration *tunnelpogs.TunnelRegistration) error {