diff options
author | Keuin <[email protected]> | 2024-03-09 20:19:35 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2024-03-09 20:20:20 +0800 |
commit | b933083d20b3db4a3d6a8134efe312eb6ff3d8e2 (patch) | |
tree | 2a86151c77b1eed1596b5f50ce824659e3c1fc22 /cmd/cmd.go |
Diffstat (limited to 'cmd/cmd.go')
-rw-r--r-- | cmd/cmd.go | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/cmd/cmd.go b/cmd/cmd.go new file mode 100644 index 0000000..8374124 --- /dev/null +++ b/cmd/cmd.go @@ -0,0 +1,101 @@ +package main + +import ( + "context" + "errors" + "fmt" + "github.com/akamensky/argparse" + "github.com/bombsimon/logrusr/v4" + "github.com/go-logr/logr" + "github.com/hit-mc/observatory/config" + "github.com/hit-mc/observatory/observatory" + "github.com/sirupsen/logrus" + "net/http" + "os" + "os/signal" + "time" +) + +func main() { + parser := argparse.NewParser("observatory", "Minecraft tunnel observatory") + configFile := parser.String("c", "config", &argparse.Options{ + Required: true, + Help: "Config file to use", + Default: "config.toml", + }) + isServer := parser.Flag("", "server", &argparse.Options{ + Required: false, + Help: "Run program as server (stats collector)", + Default: false, + }) + isClient := parser.Flag("", "client", &argparse.Options{ + Required: false, + Help: "Run program as client (stats observer&reporter)", + Default: false, + }) + err := parser.Parse(os.Args) + if err != nil { + fmt.Print(parser.Usage(err)) + return + } + if !*isServer && !*isClient { + fmt.Println("You must specify either --server or --client in CLI arguments.") + os.Exit(1) + } + + ctx, cancel := context.WithCancel(context.Background()) + + logger := logrusr.New(logrus.New()) + go func() { + logger := logger.WithName("SignalHandler") + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + logger.Info("stopping") + cancel() + }() + + if *isServer { + runCollector(ctx, *configFile, logger) + } else { + runObserver(ctx, *configFile, logger) + } +} + +func runCollector(ctx context.Context, configFile string, logger logr.Logger) { + cfg, err := config.Read[config.Server](configFile) + if err != nil { + panic(fmt.Errorf("error reading config file `%v`: %w", configFile, err)) + } + + handshakeTimeout := time.Duration(cfg.HandshakeTimeout) * time.Millisecond + s := observatory.NewCollector(cfg.Listen, handshakeTimeout, logger, cfg.Token, cfg.Targets) + err = s.Run(ctx) + if err != nil && !errors.Is(err, http.ErrServerClosed) { + panic(err) + } +} + +func runObserver(ctx context.Context, configFile string, logger logr.Logger) { + cfg, err := config.Read[config.Client](configFile) + if err != nil { + panic(fmt.Errorf("error reading config file `%v`: %w", configFile, err)) + } + + sendBuf := cfg.SendBuffer + if sendBuf < 0 { + sendBuf = 0 + } + + reconnInterval := cfg.ReconnectInterval + if reconnInterval < 0 { + reconnInterval = 0 + } + + reportConnTimeout := cfg.ReportConnectTimeout + if reportConnTimeout < 0 { + reportConnTimeout = 0 + } + + observatory.RunClient(ctx, cfg, logger) +} |