summaryrefslogtreecommitdiff
path: root/cmd/cmd.go
blob: 83741240554f7e8ed38cb63774a9a2e3851ccf94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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)
}