Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
695 lines (626 loc) · 20.5 KB
/
Copy pathmain.go
File metadata and controls
695 lines (626 loc) · 20.5 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
package main
import (
"crypto/sha256"
"crypto/subtle"
"embed"
"encoding/hex"
"encoding/xml"
"flag"
"fmt"
"html/template"
"log"
"math"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"unicode"
"unicode/utf8"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
//go:embed static/index.html static/css static/img static/js
var staticFiles embed.FS
// ///
// VersionResponse represents the XML response from the /bigbluebutton/api/ endpoint
type VersionResponse struct {
XMLName xml.Name `xml:"response"`
ReturnCode string `xml:"returncode"`
BBBVersion string `xml:"bbbVersion"`
}
// CreateResponse represents the XML response from the create API endpoint
type CreateResponse struct {
XMLName xml.Name `xml:"response"`
ReturnCode string `xml:"returncode"`
Message string `xml:"message"`
MessageKey string `xml:"messageKey"`
MeetingID string `xml:"meetingID"`
CreateTime string `xml:"createTime"`
}
// Room is a single BBB meeting room entry.
type Room struct {
ID string
Name string
Record bool
WelcomeMessage string
PreUploadedPresentation string
OCSeriesID string
OCDCCreator string
OCAddWebcams string
OCACLReadRoles string
OCACLWriteRoles string
AppendDate bool
}
// Config holds all runtime configuration loaded from environment variables.
type Config struct {
BBBURL *url.URL
BBBSecret string
Rooms []Room
FrontendURL string
UserPassword string
ModeratorPassword string
ListenAddr string
MetricsUsername string
MetricsPassword string
EnableRealIP bool
}
type server struct {
config Config
tmpl *template.Template
httpClient *http.Client
limiter *rateLimiter
}
var (
metricJoinAttempts = prometheus.NewCounter(prometheus.CounterOpts{
Name: "opencast_meet_join_attempts_total",
Help: "Total join form submissions",
})
metricJoinInvalidPw = prometheus.NewCounter(prometheus.CounterOpts{
Name: "opencast_meet_join_invalid_password_total",
Help: "Join attempts with invalid password",
})
metricJoinModerator = prometheus.NewCounter(prometheus.CounterOpts{
Name: "opencast_meet_join_moderator_total",
Help: "Logins with moderator credential",
})
metricJoinViewer = prometheus.NewCounter(prometheus.CounterOpts{
Name: "opencast_meet_join_viewer_total",
Help: "Logins with viewer credential",
})
metricBBBErrors = prometheus.NewCounter(prometheus.CounterOpts{
Name: "opencast_meet_bbb_errors_total",
Help: "Failures from BigBlueButton API calls",
})
metricJoinRateLimited = prometheus.NewCounter(prometheus.CounterOpts{
Name: "opencast_meet_join_rate_limited_total",
Help: "Join attempts rejected because the client IP was rate limited",
})
)
func init() {
prometheus.MustRegister(
metricJoinAttempts,
metricJoinInvalidPw,
metricJoinModerator,
metricJoinViewer,
metricBBBErrors,
metricJoinRateLimited,
)
}
type rateLimiter struct {
mu sync.Mutex
addrs map[string]*addrState
}
// newRateLimiter returns a rate limiter whose stale entries are evicted by a
// background goroutine every rlCleanEvery, keeping the per-request work O(1).
func newRateLimiter() *rateLimiter {
rl := &rateLimiter{addrs: make(map[string]*addrState)}
go func() {
for range time.Tick(rlCleanEvery) {
rl.evictStale()
}
}()
return rl
}
type addrState struct {
failures int
lastSeen time.Time
blockedUntil time.Time
}
// maxNameLength caps the participant name passed to BBB, in characters
// (runes), matching the maxlength attribute of the name input.
const maxNameLength = 100
// validName reports whether name is non-empty, valid UTF-8, at most
// maxNameLength characters long and free of control characters.
func validName(name string) bool {
if name == "" || !utf8.ValidString(name) || utf8.RuneCountInString(name) > maxNameLength {
return false
}
for _, r := range name {
if unicode.IsControl(r) {
return false
}
}
return true
}
const (
rlResetAfter = 10 * time.Minute
rlDelayBase = 2 * time.Second
rlDelayMax = 30 * time.Second
rlCleanEvery = time.Minute
)
// retryAfter reports how long addr must wait before another attempt is allowed.
// It returns 0 when addr is not currently blocked and does not mutate state.
func (rl *rateLimiter) retryAfter(addr string) time.Duration {
rl.mu.Lock()
defer rl.mu.Unlock()
s := rl.addrs[addr]
if s == nil {
return 0
}
if d := time.Until(s.blockedUntil); d > 0 {
return d
}
return 0
}
// recordFailure increments the failure counter for addr and extends its block
// window with exponential backoff (failures × rlDelayBase, capped at rlDelayMax).
func (rl *rateLimiter) recordFailure(addr string) {
rl.mu.Lock()
defer rl.mu.Unlock()
now := time.Now()
s := rl.addrs[addr]
if s == nil || s.lastSeen.Before(now.Add(-rlResetAfter)) {
// New or stale entry the background cleanup has not removed yet:
// start counting from scratch.
s = &addrState{}
rl.addrs[addr] = s
}
s.failures++
s.lastSeen = now
d := time.Duration(s.failures) * rlDelayBase
if d > rlDelayMax {
d = rlDelayMax
}
s.blockedUntil = now.Add(d)
}
// reset clears any tracked state for addr, called after a successful login.
func (rl *rateLimiter) reset(addr string) {
rl.mu.Lock()
defer rl.mu.Unlock()
delete(rl.addrs, addr)
}
func (rl *rateLimiter) activeIPs() int {
rl.mu.Lock()
defer rl.mu.Unlock()
return len(rl.addrs)
}
// evictStale removes entries quiet for longer than rlResetAfter.
func (rl *rateLimiter) evictStale() {
rl.mu.Lock()
defer rl.mu.Unlock()
cutoff := time.Now().Add(-rlResetAfter)
for k, s := range rl.addrs {
if s.lastSeen.Before(cutoff) {
delete(rl.addrs, k)
}
}
}
// clientIP returns the client's IP address. When EnableRealIP is set, the
// first entry of X-Forwarded-For is used. This relies on the outermost reverse
// proxy replacing any client-supplied X-Forwarded-For header (Caddy does so by
// default; nginx needs proxy_set_header X-Forwarded-For $remote_addr or the
// realip module). Invalid values fall back to the socket address.
func (s *server) clientIP(r *http.Request) string {
if s.config.EnableRealIP {
if v := r.Header.Get("X-Forwarded-For"); v != "" {
first := strings.TrimSpace(strings.SplitN(v, ",", 2)[0])
if ip := net.ParseIP(first); ip != nil {
return ip.String()
}
}
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return host
}
// limiterKey maps a client IP to its rate limiter key. IPv6 clients usually
// control a whole /64, so the key is that prefix; otherwise rotating through
// addresses would evade the limiter. IPv4 addresses (including IPv4-mapped
// IPv6) are used in their plain form, unparsable values as is.
func limiterKey(ip string) string {
parsed := net.ParseIP(ip)
if parsed == nil {
return ip
}
if v4 := parsed.To4(); v4 != nil {
return v4.String()
}
return (&net.IPNet{IP: parsed.Mask(net.CIDRMask(64, 128)), Mask: net.CIDRMask(64, 128)}).String()
}
// bbbURL builds a signed BigBlueButton API URL for the given call. The
// checksum is SHA256(call + query + secret) as required by the BBB API.
func bbbURL(cfg Config, call string, params url.Values) string {
sum := sha256.Sum256([]byte(call + params.Encode() + cfg.BBBSecret))
params.Set("checksum", hex.EncodeToString(sum[:]))
u := cfg.BBBURL.JoinPath("api", call)
u.RawQuery = params.Encode()
return u.String()
}
// bbbGet performs a GET request against apiURL and decodes the XML response
// into out.
func bbbGet(client *http.Client, apiURL string, out any) error {
resp, err := client.Get(apiURL)
if err != nil {
return fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
if err := xml.NewDecoder(resp.Body).Decode(out); err != nil {
return fmt.Errorf("failed to parse XML response: %w", err)
}
return nil
}
// getBBBVersion fetches the BigBlueButton server version.
func getBBBVersion(cfg Config, client *http.Client) (*VersionResponse, error) {
var versionResp VersionResponse
if err := bbbGet(client, cfg.BBBURL.JoinPath("api").String(), &versionResp); err != nil {
return nil, err
}
return &versionResp, nil
}
// createMeeting ensures the configured meeting room exists on the BBB server.
// BBB is idempotent on meetingID: returns the existing meeting if already running,
// or creates a fresh one if it has ended.
func createMeeting(cfg Config, room Room, client *http.Client) (*CreateResponse, error) {
params := url.Values{}
params.Add("meetingID", room.ID)
name := room.Name
if room.AppendDate {
name += " | " + time.Now().Format("2006-01-02")
}
params.Add("name", name)
params.Add("muteOnStart", "true")
params.Add("autoStartRecording", "false")
params.Add("record", strconv.FormatBool(room.Record))
params.Add("allowStartStopRecording", strconv.FormatBool(room.Record))
if cfg.FrontendURL != "" {
params.Add("loginURL", cfg.FrontendURL)
params.Add("logoutURL", cfg.FrontendURL)
}
if room.WelcomeMessage != "" {
params.Add("welcome", room.WelcomeMessage)
}
if room.PreUploadedPresentation != "" {
params.Add("preUploadedPresentation", room.PreUploadedPresentation)
}
if room.OCSeriesID != "" {
params.Add("meta_opencast-dc-isPartOf", room.OCSeriesID)
params.Add("meta_opencast-dc-title", name)
}
if room.OCDCCreator != "" {
params.Add("meta_opencast-dc-creator", room.OCDCCreator)
}
if room.OCAddWebcams != "" {
params.Add("meta_opencast-add-webcams", room.OCAddWebcams)
}
if room.OCACLReadRoles != "" {
params.Add("meta_opencast-acl-read-roles", room.OCACLReadRoles)
}
if room.OCACLWriteRoles != "" {
params.Add("meta_opencast-acl-write-roles", room.OCACLWriteRoles)
}
var createResp CreateResponse
if err := bbbGet(client, bbbURL(cfg, "create", params), &createResp); err != nil {
return nil, err
}
return &createResp, nil
}
// generateJoinURL builds a BBB join URL for the given participant name and role.
// createTime binds the link to the current meeting instance so it stops working
// once that meeting has ended.
func generateJoinURL(cfg Config, meetingID, createTime, fullName, role string) string {
params := url.Values{}
params.Add("fullName", fullName)
params.Add("meetingID", meetingID)
params.Add("role", role)
if createTime != "" {
params.Add("createTime", createTime)
}
return bbbURL(cfg, "join", params)
}
// errorMessages maps the short error codes used in the ?error= query parameter
// to the text shown on the login page. Unknown codes render no message, so the
// page cannot be used to display arbitrary attacker-supplied text.
var errorMessages = map[string]string{
"name": "Please enter your name.",
"password": "Invalid password. Please try again.",
"consent": "You must consent to the recording to enter this room.",
"ratelimit": "Too many attempts. Please wait a moment and try again.",
"bbb": "Could not connect to the meeting server. Please try again later.",
"bbbfail": "The meeting server returned an error. Please try again later.",
}
type indexTemplateData struct {
Error string
Name string
Rooms []Room
SelectedRoom string
SelectedRoomRecordable bool
}
func (s *server) indexData(errCode, name, selectedRoom string) indexTemplateData {
room := findRoom(s.config.Rooms, selectedRoom)
return indexTemplateData{
Error: errorMessages[errCode],
Name: name,
Rooms: s.config.Rooms,
SelectedRoom: selectedRoom,
SelectedRoomRecordable: room.Record,
}
}
func (s *server) handleIndex(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
s.tmpl.Execute(w, s.indexData(
r.URL.Query().Get("error"),
r.URL.Query().Get("name"),
r.URL.Query().Get("room"),
))
}
func (s *server) handleJoin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
metricJoinAttempts.Inc()
name := strings.TrimSpace(r.FormValue("name"))
password := r.FormValue("password")
roomID := r.FormValue("room")
ip := limiterKey(s.clientIP(r))
room := findRoom(s.config.Rooms, roomID)
renderError := func(code string) {
redirectURL := "/?error=" + code
if name != "" {
redirectURL += "&name=" + url.QueryEscape(name)
}
if len(s.config.Rooms) > 1 {
redirectURL += "&room=" + url.QueryEscape(room.ID)
}
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
}
if d := s.limiter.retryAfter(ip); d > 0 {
metricJoinRateLimited.Inc()
w.Header().Set("Retry-After", strconv.Itoa(int(math.Ceil(d.Seconds()))))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusTooManyRequests)
_ = s.tmpl.Execute(w, s.indexData("ratelimit", name, room.ID))
return
}
if !validName(name) {
renderError("name")
return
}
var role string
switch {
case subtle.ConstantTimeCompare([]byte(password), []byte(s.config.ModeratorPassword)) == 1:
role = "MODERATOR"
metricJoinModerator.Inc()
case subtle.ConstantTimeCompare([]byte(password), []byte(s.config.UserPassword)) == 1:
role = "VIEWER"
metricJoinViewer.Inc()
default:
s.limiter.recordFailure(ip)
metricJoinInvalidPw.Inc()
renderError("password")
return
}
s.limiter.reset(ip)
if room.Record && r.FormValue("recording_consent") != "on" {
renderError("consent")
return
}
createResp, err := createMeeting(s.config, room, s.httpClient)
if err != nil {
log.Printf("error creating meeting: %v", err)
metricBBBErrors.Inc()
renderError("bbb")
return
}
if createResp.ReturnCode != "SUCCESS" {
log.Printf("BBB create returned non-success: %s / %s", createResp.MessageKey, createResp.Message)
metricBBBErrors.Inc()
renderError("bbbfail")
return
}
joinURL := generateJoinURL(s.config, createResp.MeetingID, createResp.CreateTime, name, role)
http.Redirect(w, r, joinURL, http.StatusFound)
}
// securityHeaders sets conservative browser security headers on every
// response. The page only loads same-origin resources; the data: image source
// is for the inline SVG dropdown arrow in the stylesheet.
// noDirListing wraps a file server so that directory paths return 404 instead
// of a generated listing. Requests for a directory without a trailing slash are
// redirected to the slash form by http.FileServer and then rejected here.
func noDirListing(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
http.NotFound(w, r)
return
}
next.ServeHTTP(w, r)
})
}
func securityHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := w.Header()
h.Set("Content-Security-Policy", "default-src 'self'; img-src 'self' data:; frame-ancestors 'none'")
h.Set("X-Frame-Options", "DENY")
h.Set("X-Content-Type-Options", "nosniff")
h.Set("Referrer-Policy", "strict-origin-when-cross-origin")
next.ServeHTTP(w, r)
})
}
func basicAuthHandler(username, password string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
if !ok ||
subtle.ConstantTimeCompare([]byte(u), []byte(username)) != 1 ||
subtle.ConstantTimeCompare([]byte(p), []byte(password)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="metrics"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
func loadRooms() ([]Room, error) {
var rooms []Room
seen := map[string]int{}
for i := 1; ; i++ {
id := os.Getenv(fmt.Sprintf("ROOM_%d_ID", i))
if id == "" {
break
}
if prev, dup := seen[id]; dup {
return nil, fmt.Errorf("ROOM_%d_ID duplicates ROOM_%d_ID (%q)", i, prev, id)
}
seen[id] = i
name := os.Getenv(fmt.Sprintf("ROOM_%d_NAME", i))
if name == "" {
return nil, fmt.Errorf("ROOM_%d_NAME is not set", i)
}
rooms = append(rooms, Room{
ID: id,
Name: name,
Record: strings.EqualFold(os.Getenv(fmt.Sprintf("ROOM_%d_RECORD", i)), "true"),
WelcomeMessage: os.Getenv(fmt.Sprintf("ROOM_%d_WELCOME_MESSAGE", i)),
PreUploadedPresentation: os.Getenv(fmt.Sprintf("ROOM_%d_PRE_UPLOADED_PRESENTATION", i)),
OCSeriesID: os.Getenv(fmt.Sprintf("ROOM_%d_OC_SERIES", i)),
OCDCCreator: os.Getenv(fmt.Sprintf("ROOM_%d_OC_DC_CREATOR", i)),
OCAddWebcams: os.Getenv(fmt.Sprintf("ROOM_%d_OC_ADD_WEBCAMS", i)),
OCACLReadRoles: os.Getenv(fmt.Sprintf("ROOM_%d_OC_ACL_READ_ROLES", i)),
OCACLWriteRoles: os.Getenv(fmt.Sprintf("ROOM_%d_OC_ACL_WRITE_ROLES", i)),
AppendDate: strings.EqualFold(os.Getenv(fmt.Sprintf("ROOM_%d_APPEND_DATE", i)), "true"),
})
}
if len(rooms) == 0 {
return nil, fmt.Errorf("no rooms configured: set at least ROOM_1_ID and ROOM_1_NAME")
}
return rooms, nil
}
func findRoom(rooms []Room, id string) Room {
for _, r := range rooms {
if r.ID == id {
return r
}
}
return rooms[0]
}
func loadConfig() (Config, error) {
rooms, err := loadRooms()
if err != nil {
return Config{}, err
}
bbbURL, err := url.Parse(os.Getenv("BBB_SERVER_URL"))
if err != nil {
return Config{}, fmt.Errorf("BBB_SERVER_URL is invalid: %w", err)
}
if bbbURL.Scheme == "" || bbbURL.Host == "" {
return Config{}, fmt.Errorf("BBB_SERVER_URL must be an absolute URL, e.g. https://bbb.example.com/bigbluebutton/")
}
return Config{
BBBURL: bbbURL,
BBBSecret: os.Getenv("BBB_SERVER_SECRET"),
Rooms: rooms,
FrontendURL: os.Getenv("APP_FRONTEND_URL"),
UserPassword: os.Getenv("APP_USER_PASSWORD"),
ModeratorPassword: os.Getenv("APP_MODERATOR_PASSWORD"),
ListenAddr: getEnvDefault("APP_LISTEN_ADDR", "127.0.0.1:8080"),
EnableRealIP: strings.EqualFold(os.Getenv("ENABLE_REAL_IP"), "true"),
MetricsUsername: os.Getenv("METRICS_USERNAME"),
MetricsPassword: os.Getenv("METRICS_PASSWORD"),
}, nil
}
func getEnvDefault(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func main() {
log.SetFlags(0) // systemd and container runtimes add their own timestamps
envPath := flag.String("env-path", "", "path to the environment file to load (default: .env in current directory)")
flag.Parse()
if *envPath != "" {
if err := godotenv.Load(*envPath); err != nil {
log.Fatalf("Error loading env file %s: %v", *envPath, err)
}
} else {
_ = godotenv.Load()
}
cfg, err := loadConfig()
if err != nil {
log.Fatalf("Error: %v", err)
}
if cfg.BBBSecret == "" {
log.Fatal("Error: BBB_SERVER_SECRET is not set")
}
if cfg.UserPassword == "" || cfg.ModeratorPassword == "" {
log.Fatal("Error: APP_USER_PASSWORD and APP_MODERATOR_PASSWORD must both be set")
}
if (cfg.MetricsUsername == "") != (cfg.MetricsPassword == "") {
log.Fatal("Error: METRICS_USERNAME and METRICS_PASSWORD must both be set or both be empty")
}
httpClient := &http.Client{Timeout: 10 * time.Second}
// Startup connection check — warn but don't abort if BBB is unreachable.
versionResp, err := getBBBVersion(cfg, httpClient)
if err != nil {
log.Printf("Warning: could not reach BBB server: %v", err)
} else {
log.Printf("Connected to BigBlueButton %s", versionResp.BBBVersion)
}
tmpl, err := template.ParseFS(staticFiles, "static/index.html")
if err != nil {
log.Fatalf("Error parsing template: %v", err)
}
srv := &server{
config: cfg,
tmpl: tmpl,
httpClient: httpClient,
limiter: newRateLimiter(),
}
prometheus.MustRegister(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Name: "opencast_meet_ratelimit_tracked_ips",
Help: "IPs currently tracked by the rate limiter",
},
func() float64 { return float64(srv.limiter.activeIPs()) },
))
http.HandleFunc("/", srv.handleIndex)
http.HandleFunc("/join", srv.handleJoin)
metricsHandler := promhttp.Handler()
if cfg.MetricsUsername != "" {
metricsHandler = basicAuthHandler(cfg.MetricsUsername, cfg.MetricsPassword, metricsHandler)
}
http.Handle("/metrics", metricsHandler)
http.Handle("/static/", noDirListing(http.FileServer(http.FS(staticFiles))))
httpServer := &http.Server{
Addr: cfg.ListenAddr,
Handler: securityHeaders(http.DefaultServeMux),
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
}
log.Printf("Listening on http://%s", cfg.ListenAddr)
if err := httpServer.ListenAndServe(); err != nil {
log.Fatalf("Server error: %v", err)
}
}