2019-11-19 11:55:02 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-30 12:48:58 -06:00
|
|
|
"fmt"
|
2019-11-19 11:55:02 -07:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/kardianos/service"
|
2020-06-30 16:53:30 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-11-19 11:55:02 -07:00
|
|
|
"github.com/slackhq/nebula"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
2019-11-19 11:55:02 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var logger service.Logger
|
|
|
|
|
|
|
|
type program struct {
|
|
|
|
configPath *string
|
|
|
|
configTest *bool
|
|
|
|
build string
|
2020-09-18 08:20:09 -06:00
|
|
|
control *nebula.Control
|
2019-11-19 11:55:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *program) Start(s service.Service) error {
|
2019-11-28 09:25:34 -07:00
|
|
|
// Start should not block.
|
2020-09-18 08:20:09 -06:00
|
|
|
logger.Info("Nebula service starting.")
|
2019-11-19 11:55:02 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
l := logrus.New()
|
2021-04-30 14:04:47 -06:00
|
|
|
HookLogger(l)
|
2021-03-26 08:46:30 -06:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
c := config.NewC(l)
|
|
|
|
err := c.Load(*p.configPath)
|
2020-06-30 12:48:58 -06:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load config: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
p.control, err = nebula.Main(c, *p.configTest, Build, l, nil)
|
2020-09-18 08:20:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.control.Start()
|
|
|
|
return nil
|
2019-11-19 11:55:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *program) Stop(s service.Service) error {
|
2019-11-23 08:32:10 -07:00
|
|
|
logger.Info("Nebula service stopping.")
|
2020-09-18 08:20:09 -06:00
|
|
|
p.control.Stop()
|
2019-11-19 11:55:02 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:45:15 -07:00
|
|
|
func fileExists(filename string) bool {
|
|
|
|
_, err := os.Stat(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-19 11:55:02 -07:00
|
|
|
func doService(configPath *string, configTest *bool, build string, serviceFlag *string) {
|
|
|
|
if *configPath == "" {
|
|
|
|
ex, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
*configPath = filepath.Dir(ex) + "/config.yaml"
|
2022-12-19 10:45:15 -07:00
|
|
|
if !fileExists(*configPath) {
|
|
|
|
*configPath = filepath.Dir(ex) + "/config.yml"
|
|
|
|
}
|
2019-11-19 11:55:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
svcConfig := &service.Config{
|
|
|
|
Name: "Nebula",
|
|
|
|
DisplayName: "Nebula Network Service",
|
|
|
|
Description: "Nebula network connectivity daemon for encrypted communications",
|
|
|
|
Arguments: []string{"-service", "run", "-config", *configPath},
|
|
|
|
}
|
|
|
|
|
|
|
|
prg := &program{
|
|
|
|
configPath: configPath,
|
|
|
|
configTest: configTest,
|
|
|
|
build: build,
|
|
|
|
}
|
|
|
|
|
2021-04-30 14:04:47 -06:00
|
|
|
// Here are what the different loggers are doing:
|
|
|
|
// - `log` is the standard go log utility, meant to be used while the process is still attached to stdout/stderr
|
|
|
|
// - `logger` is the service log utility that may be attached to a special place depending on OS (Windows will have it attached to the event log)
|
|
|
|
// - above, in `Run` we create a `logrus.Logger` which is what nebula expects to use
|
2019-11-19 11:55:02 -07:00
|
|
|
s, err := service.New(prg, svcConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := make(chan error, 5)
|
|
|
|
logger, err = s.Logger(errs)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
err := <-errs
|
|
|
|
if err != nil {
|
2021-04-30 14:04:47 -06:00
|
|
|
// Route any errors from the system logger to stdout as a best effort to notice issues there
|
2019-11-19 11:55:02 -07:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
switch *serviceFlag {
|
|
|
|
case "run":
|
|
|
|
err = s.Run()
|
|
|
|
if err != nil {
|
2021-04-30 14:04:47 -06:00
|
|
|
// Route any errors to the system logger
|
2019-11-19 11:55:02 -07:00
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err := service.Control(s, *serviceFlag)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Valid actions: %q\n", service.ControlAction)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|