From 75b26ec7f4f001a13db2c8dd1fa02d7481fd2b72 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Mon, 17 Feb 2020 17:13:25 +0100 Subject: [feat] initial implementation of windows service --- pkg/helper/windows_service.go | 72 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 pkg/helper/windows_service.go (limited to 'pkg/helper/windows_service.go') diff --git a/pkg/helper/windows_service.go b/pkg/helper/windows_service.go new file mode 100644 index 0000000..b35ba19 --- /dev/null +++ b/pkg/helper/windows_service.go @@ -0,0 +1,72 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +package helper + +import ( + "fmt" + //"strings" + //"time" + + "golang.org/x/sys/windows/svc" + "golang.org/x/sys/windows/svc/debug" + "golang.org/x/sys/windows/svc/eventlog" +) + +var elog debug.Log + +type myservice struct{} + +func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { + const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue + changes <- svc.Status{State: svc.StartPending} + changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} + // TODO use httpBindAddr + go runCommandServer("localhost:7171") +loop: + for { + select { + case c := <-r: + switch c.Cmd { + // TODO start?? + case svc.Interrogate: + changes <- c.CurrentStatus + case svc.Stop, svc.Shutdown: + elog.Info(1, "shutting down service") + break loop + default: + elog.Error(1, fmt.Sprintf("unexpected control request #%d", c)) + } + } + } + changes <- svc.Status{State: svc.StopPending} + return +} + +func runService(name string, isDebug bool) { + var err error + if isDebug { + elog = debug.New(name) + } else { + elog, err = eventlog.Open(name) + if err != nil { + return + } + } + defer elog.Close() + + elog.Info(1, fmt.Sprintf("starting %s service", name)) + run := svc.Run + if isDebug { + run = debug.Run + } + err = run(name, &myservice{}) + if err != nil { + elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err)) + return + } + elog.Info(1, fmt.Sprintf("%s service stopped", name)) +} -- cgit v1.2.3