-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig.go
More file actions
156 lines (127 loc) · 3.96 KB
/
Copy pathconfig.go
File metadata and controls
156 lines (127 loc) · 3.96 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
package main
import (
"encoding/base64"
"os"
"strings"
"time"
"github.com/goccy/go-yaml"
"github.com/DropMorePackets/berghain"
)
type Config struct {
Secret Secret `yaml:"secret"`
Listen string `yaml:"listen"`
Default FrontendConfig `yaml:"default"`
Frontend map[string]FrontendConfig `yaml:"frontend"`
}
type Secret []byte
func init() {
yaml.RegisterCustomUnmarshaler((*Secret).UnmarshalYAML)
}
func (s *Secret) UnmarshalYAML(b []byte) error {
ba, err := base64.StdEncoding.DecodeString(string(b))
if err != nil {
return err
}
*s = ba
return nil
}
type FrontendConfig struct {
Levels []LevelConfig `yaml:"levels"`
TrustedDomains []string `yaml:"trusted_domains"`
}
func (fc FrontendConfig) AsBerghain(s []byte) *berghain.Berghain {
b := berghain.NewBerghain(s)
for _, c := range fc.Levels {
b.Levels = append(b.Levels, c.AsLevelConfig())
}
b.TrustedDomains = fc.TrustedDomains
return b
}
type LevelConfig struct {
Countdown *int `yaml:"countdown"`
Duration time.Duration `yaml:"duration"`
Type string `yaml:"type"`
// Captcha settings, required for the turnstile, hcaptcha and
// recaptcha types.
Sitekey string `yaml:"sitekey"`
Secret string `yaml:"secret"`
// VerifyURL overrides the provider siteverify endpoint,
// e.g. for regional endpoints or tests.
VerifyURL string `yaml:"verify_url"`
// SkipHostnameCheck disables binding the provider-reported hostname
// to the request identity. Provider test keys report a fixed
// hostname, so tests need this; production setups do not.
SkipHostnameCheck bool `yaml:"skip_hostname_check"`
}
func (c LevelConfig) AsLevelConfig() *berghain.LevelConfig {
var lc berghain.LevelConfig
lc.Duration = c.Duration
if c.Countdown == nil {
// no level specific countdown was provided
lc.Countdown = 3
} else if *c.Countdown > 9 {
// template string currently only allows one digit
// and JavaScript does not allow zero padding of integers in JSON
Fatal("countdown too high, cannot proceed", "countdown_have", *c.Countdown, "countdown_max", 9)
} else {
lc.Countdown = *c.Countdown
}
switch c.Type {
case "none":
lc.Type = berghain.ValidationTypeNone
case "pow":
lc.Type = berghain.ValidationTypePOW
case "turnstile":
lc.Type = berghain.ValidationTypeTurnstile
case "hcaptcha":
lc.Type = berghain.ValidationTypeHCaptcha
case "recaptcha":
lc.Type = berghain.ValidationTypeReCaptcha
default:
Fatal("unknown validation type", "validator", c.Type)
}
switch lc.Type {
case berghain.ValidationTypeTurnstile, berghain.ValidationTypeHCaptcha, berghain.ValidationTypeReCaptcha:
if c.Sitekey == "" || c.Secret == "" {
Fatal("captcha types require a sitekey and a secret", "validator", c.Type)
}
lc.CaptchaSitekey = c.Sitekey
lc.CaptchaSecret = c.Secret
lc.CaptchaVerifyURL = c.VerifyURL
lc.CaptchaSkipHostnameCheck = c.SkipHostnameCheck
default:
if c.Sitekey != "" || c.Secret != "" || c.VerifyURL != "" || c.SkipHostnameCheck {
Fatal("sitekey, secret, verify_url and skip_hostname_check are only valid for captcha types", "validator", c.Type)
}
}
return &lc
}
func loadConfig() Config {
if configPath == "" {
Fatal("missing config path", "path", configPath)
}
f, err := os.Open(configPath)
if err != nil {
Fatal("failed opening config", "path", configPath, "error", err)
}
var c Config
if err := yaml.NewDecoder(f).Decode(&c); err != nil {
Fatal("failed reading config", "path", configPath, "error", err)
}
return c
}
// ParseListener parses the listen string and returns the network type and address
func ParseListener(listen string) (network, address string) {
// old default
if listen == "" {
return "unix", "./spop.sock"
}
if strings.HasPrefix(listen, "unix://") {
return "unix", strings.TrimPrefix(listen, "unix://")
}
if strings.HasPrefix(listen, "tcp://") {
return "tcp", strings.TrimPrefix(listen, "tcp://")
}
// old default behaviour
return "unix", listen
}