github.com/crowdsecurity/crowdsec@v1.6.1/cmd/crowdsec/run_in_svc_windows.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"runtime/pprof"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  	"golang.org/x/sys/windows/svc"
     9  
    10  	"github.com/crowdsecurity/go-cs-lib/trace"
    11  	"github.com/crowdsecurity/go-cs-lib/version"
    12  
    13  	"github.com/crowdsecurity/crowdsec/pkg/csconfig"
    14  	"github.com/crowdsecurity/crowdsec/pkg/database"
    15  )
    16  
    17  func StartRunSvc() error {
    18  	const svcName = "CrowdSec"
    19  	const svcDescription = "Crowdsec IPS/IDS"
    20  
    21  	defer trace.CatchPanic("crowdsec/StartRunSvc")
    22  
    23  	// Always try to stop CPU profiling to avoid passing flags around
    24  	// It's a noop if profiling is not enabled
    25  	defer pprof.StopCPUProfile()
    26  
    27  	isRunninginService, err := svc.IsWindowsService()
    28  	if err != nil {
    29  		return fmt.Errorf("failed to determine if we are running in windows service mode: %w", err)
    30  	}
    31  	if isRunninginService {
    32  		return runService(svcName)
    33  	}
    34  
    35  	if flags.WinSvc == "Install" {
    36  		err = installService(svcName, svcDescription)
    37  		if err != nil {
    38  			return fmt.Errorf("failed to %s %s: %w", flags.WinSvc, svcName, err)
    39  		}
    40  	} else if flags.WinSvc == "Remove" {
    41  		err = removeService(svcName)
    42  		if err != nil {
    43  			return fmt.Errorf("failed to %s %s: %w", flags.WinSvc, svcName, err)
    44  		}
    45  	} else if flags.WinSvc == "Start" {
    46  		err = startService(svcName)
    47  		if err != nil {
    48  			return fmt.Errorf("failed to %s %s: %w", flags.WinSvc, svcName, err)
    49  		}
    50  	} else if flags.WinSvc == "Stop" {
    51  		err = controlService(svcName, svc.Stop, svc.Stopped)
    52  		if err != nil {
    53  			return fmt.Errorf("failed to %s %s: %w", flags.WinSvc, svcName, err)
    54  		}
    55  	} else if flags.WinSvc == "" {
    56  		return WindowsRun()
    57  	} else {
    58  		return fmt.Errorf("Invalid value for winsvc parameter: %s", flags.WinSvc)
    59  	}
    60  	return nil
    61  }
    62  
    63  func WindowsRun() error {
    64  	var (
    65  		cConfig *csconfig.Config
    66  		err     error
    67  	)
    68  
    69  	cConfig, err = LoadConfig(flags.ConfigFile, flags.DisableAgent, flags.DisableAPI, false)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	log.Infof("Crowdsec %s", version.String())
    75  
    76  	agentReady := make(chan bool, 1)
    77  
    78  	// Enable profiling early
    79  	if cConfig.Prometheus != nil {
    80  		var dbClient *database.Client
    81  		var err error
    82  
    83  		if cConfig.DbConfig != nil {
    84  			dbClient, err = database.NewClient(cConfig.DbConfig)
    85  
    86  			if err != nil {
    87  				return fmt.Errorf("unable to create database client: %w", err)
    88  			}
    89  		}
    90  		registerPrometheus(cConfig.Prometheus)
    91  		go servePrometheus(cConfig.Prometheus, dbClient, agentReady)
    92  	}
    93  	return Serve(cConfig, agentReady)
    94  }