This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
131 lines (118 loc) · 3.43 KB
/
main.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"github.com/cloudradar-monitoring/plexus/api"
"github.com/cloudradar-monitoring/plexus/config"
"github.com/cloudradar-monitoring/plexus/control"
"github.com/cloudradar-monitoring/plexus/handler"
"github.com/cloudradar-monitoring/plexus/logger/zerologger"
"github.com/cloudradar-monitoring/plexus/server"
"github.com/cloudradar-monitoring/plexus/verify"
)
func main() {
app := cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "path to the config",
Required: true,
},
},
Commands: []*cli.Command{serve, verifyConfig},
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var verifyConfig = &cli.Command{
Name: "verify-config",
Description: "Verifies the plexus config and checks if everything should work.",
Action: func(c *cli.Context) error {
if !verify.Verify(c.String("config")) {
os.Exit(1)
}
return nil
},
}
var serve = &cli.Command{
Name: "serve",
Description: "Serves plexus",
Action: func(c *cli.Context) error {
cfg, errs := config.Get(c.String("config"))
handle, err := zerologger.Init(cfg.LogLevel.AsZeroLogLevel(), cfg.LogFile)
if err != nil {
return err
}
defer handle.Close()
exit := false
for _, err := range errs {
log.WithLevel(err.Level).Msg(err.Msg)
exit = exit || err.Level == zerolog.FatalLevel || err.Level == zerolog.PanicLevel
}
if exit {
os.Exit(1)
}
log.Debug().Interface("config", cfg).Msg("Using")
mc, err := control.Connect(cfg.AsControlConfig(), zerologger.Get())
if err != nil {
return fmt.Errorf("could not connect to meshcentral: %s", err)
}
serverInfo, err := mc.ServerInfo()
if err != nil {
mc.Close()
return fmt.Errorf("could not get server id: %s", err)
}
log.Info().Str("serverID", serverInfo.AgentHash).Msg("MeshControl: Authenticated")
if err := mc.DeleteMeshes(); err != nil {
return fmt.Errorf("could not clean old meshes: %s", err)
}
mc.Close()
var auth handler.AuthChecker = func(rw http.ResponseWriter, r *http.Request) bool {
user, password, _ := r.BasicAuth()
if cfg.AuthUser != "" || cfg.AuthPass != "" {
if cfg.AuthUser != user || cfg.AuthPass != password {
rw.Header().Add("WWW-Authenticate", `Basic realm="Plexus Session", charset="UTF-8"`)
api.WriteJSONError(rw, http.StatusUnauthorized, "invalid username / password")
return false
}
}
return true
}
opt := &handler.Options{
ControlConfig: cfg.AsControlConfig(),
PairingConfig: cfg.AsPairingConfig(),
Auth: auth,
Log: zerologger.Get(),
Prefix: cfg.PathPrefix,
AllowSessionCredentials: true,
}
r := mux.NewRouter()
r.Use(accessLog)
handler.Register(r, opt)
log.Info().Str("addr", cfg.ServerAddress).Msg("Start listening")
return server.Start(r, cfg.ServerAddress, cfg.TLSCertFile, cfg.TLSKeyFile)
},
}
func accessLog(h http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
start := time.Now()
h.ServeHTTP(rw, r)
duration := time.Since(start)
log.Debug().
Str("host", r.Host).
Str("ip", r.RemoteAddr).
Str("method", r.Method).
Str("path", r.URL.Path).
Str("duration", duration.String()).
Msg("HTTP")
})
}