From 8a4b8ca9ce93f4d42e16cf701b705b30e460b2b9 Mon Sep 17 00:00:00 2001 From: dovholuknf <46322585+dovholuknf@users.noreply.github.com> Date: Thu, 30 Apr 2020 16:14:49 -0400 Subject: [PATCH] ran fine interactively but not as a service. update the logic to work as both again --- wintun-testing/ziti-tunnel/config/config.go | 46 ------------------- wintun-testing/ziti-tunnel/main.go | 39 ++++++++-------- wintun-testing/ziti-tunnel/service/service.go | 1 + 3 files changed, 20 insertions(+), 66 deletions(-) diff --git a/wintun-testing/ziti-tunnel/config/config.go b/wintun-testing/ziti-tunnel/config/config.go index 3751e3f7..85e04b98 100644 --- a/wintun-testing/ziti-tunnel/config/config.go +++ b/wintun-testing/ziti-tunnel/config/config.go @@ -1,13 +1,7 @@ package config import ( - "io" "os" - "strings" - - "github.com/michaelquigley/pfxlog" - "github.com/sirupsen/logrus" - "gopkg.in/natefinch/lumberjack.v2" ) func File() string { @@ -20,43 +14,3 @@ func Path() string { func LogFile() string { return Path() + "ziti-tunneler.log" } - -func ParseLevel(lvl string) logrus.Level { - switch strings.ToLower(lvl) { - case "panic": - return logrus.PanicLevel - case "fatal": - return logrus.FatalLevel - case "error": - return logrus.ErrorLevel - case "warn", "warning": - return logrus.WarnLevel - case "info": - return logrus.InfoLevel - case "debug": - return logrus.DebugLevel - case "trace": - return logrus.TraceLevel - default: - logrus.Warnf("level not recognized: %s. Using Info", lvl) - return logrus.InfoLevel - } -} - -func InitLogger(level string) { - logLevel := ParseLevel(level) - logrus.SetLevel(logLevel) - - _ = os.Remove(LogFile()) //reset the log on startup - multiWriter := io.MultiWriter(&lumberjack.Logger{ - Filename: LogFile(), - MaxSize: 1, // megabytes - MaxBackups: 2, - MaxAge: 30, //days - Compress: false, // disabled by default - }, os.Stdout) - - logrus.SetOutput(multiWriter) - logrus.SetFormatter(pfxlog.NewFormatter()) - pfxlog.Logger().Infof("Logger initialized. Log file located at: %s", LogFile()) -} diff --git a/wintun-testing/ziti-tunnel/main.go b/wintun-testing/ziti-tunnel/main.go index 0cf5e651..06d1fce6 100644 --- a/wintun-testing/ziti-tunnel/main.go +++ b/wintun-testing/ziti-tunnel/main.go @@ -3,14 +3,13 @@ package main import ( "fmt" + "golang.org/x/sys/windows/svc/debug" + "golang.org/x/sys/windows/svc/eventlog" "os" "strings" "wintun-testing/ziti-tunnel/globals" "golang.org/x/sys/windows/svc" - "golang.org/x/sys/windows/svc/debug" - "golang.org/x/sys/windows/svc/eventlog" - "wintun-testing/ziti-tunnel/service" ) @@ -19,32 +18,32 @@ var log = globals.Logger() func main() { globals.InitLogger("debug") - isIntSess, err := svc.IsAnInteractiveSession() - if err != nil { - log.Fatalf("failed to determine if we are running in an interactive session: %v", err) - } - - // if not interactive that means this is probably running as a service via services + // passing no arguments is an indicator that this is excpecting to be run 'as a service'. + // using arg count instead of svc.IsAnInteractiveSession() as svc.IsAnInteractiveSession() + // seems to return false even when run in an interactive shell as via `psexec -i -s cmd.exe` hasArgs := len(os.Args) > 1 - if !isIntSess && !hasArgs { - service.RunService(false) - log.Info("service has completed") - return - } - // all this code below is to either support installing/removing the service or testing it - if len(os.Args) < 2 { - usage("no command specified") - } - - if !isIntSess { + var err error + if hasArgs { globals.Elog = debug.New(service.SvcName) } else { globals.Elog, err = eventlog.Open(service.SvcName) if err != nil { return } + log.Info("running as service") + service.RunService(false) + log.Info("service has completed") + return } + + log.Info("running interactively") + + // all this code below is to either support installing/removing the service or testing it + if len(os.Args) < 2 { + usage("no command specified") + } + defer globals.Elog.Close() elog := globals.Elog diff --git a/wintun-testing/ziti-tunnel/service/service.go b/wintun-testing/ziti-tunnel/service/service.go index d740ade5..10e442f4 100644 --- a/wintun-testing/ziti-tunnel/service/service.go +++ b/wintun-testing/ziti-tunnel/service/service.go @@ -15,6 +15,7 @@ type zitiService struct{} func (m *zitiService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { changes <- svc.Status{State: svc.StartPending} + log.Errorf("the main loop exited with an unexpected error: %v") control := make(chan string) mainLoop := make(chan struct{}) go func() {