mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-10 20:05:55 +02:00
87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
// 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.
|
|
|
|
//go:build windows
|
|
// +build windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
log "github.com/sirupsen/logrus"
|
|
"golang.org/x/sys/windows/svc"
|
|
)
|
|
|
|
type crowdsec_winservice struct {
|
|
config *csconfig.Config
|
|
}
|
|
|
|
func (m *crowdsec_winservice) 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}
|
|
fasttick := time.Tick(500 * time.Millisecond)
|
|
slowtick := time.Tick(2 * time.Second)
|
|
tick := fasttick
|
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
|
go WindowsRun()
|
|
|
|
loop:
|
|
for {
|
|
select {
|
|
case <-tick:
|
|
|
|
case c := <-r:
|
|
switch c.Cmd {
|
|
case svc.Interrogate:
|
|
changes <- c.CurrentStatus
|
|
case svc.Stop, svc.Shutdown:
|
|
changes <- svc.Status{State: svc.StopPending}
|
|
err := shutdown(nil, m.config)
|
|
if err != nil {
|
|
log.Errorf("Error while shutting down: %s", err)
|
|
//don't return, we still want to notify windows that we are stopped ?
|
|
}
|
|
break loop
|
|
case svc.Pause:
|
|
changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted}
|
|
tick = slowtick
|
|
case svc.Continue:
|
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
|
tick = fasttick
|
|
default:
|
|
log.Errorf("unexpected control request #%d", c)
|
|
}
|
|
}
|
|
}
|
|
changes <- svc.Status{State: svc.Stopped}
|
|
return
|
|
}
|
|
|
|
func runService(name string) {
|
|
cConfig, err := csconfig.NewConfig(flags.ConfigFile, flags.DisableAgent, flags.DisableAPI)
|
|
if err != nil {
|
|
log.Fatalf(err.Error())
|
|
}
|
|
if err := LoadConfig(cConfig); err != nil {
|
|
log.Fatalf(err.Error())
|
|
}
|
|
// Configure logging
|
|
if err = types.SetDefaultLoggerConfig(cConfig.Common.LogMedia, cConfig.Common.LogDir, *cConfig.Common.LogLevel,
|
|
cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs); err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
log.Infof("starting %s service", name)
|
|
|
|
winsvc := crowdsec_winservice{config: cConfig}
|
|
|
|
err = svc.Run(name, &winsvc)
|
|
if err != nil {
|
|
log.Errorf("%s service failed: %s", name, err)
|
|
return
|
|
}
|
|
log.Infof("%s service stopped", name)
|
|
}
|