ran fine interactively but not as a service. update the logic to work as both again

This commit is contained in:
dovholuknf
2020-04-30 16:14:49 -04:00
parent 85f9df8c49
commit 8a4b8ca9ce
3 changed files with 20 additions and 66 deletions
@@ -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())
}
+19 -20
View File
@@ -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
@@ -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() {