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
package guestagent
import (
"encoding/json"
"errors"
"fmt"
"net/netip"
"net/url"
"os"
"path/filepath"
"strings"
)
const (
DefaultConfigDir = "/etc/socialweb-guest-agent"
DefaultStateDir = "/run/socialweb-guest-agent"
DefaultLoginHelper = "/usr/local/bin/guest-login.sh"
TunnelsFile = "tunnels.json"
SecretsFile = "secrets.json"
AgentFile = "agent.json"
AssociationFile = "association.json"
RequesterDIDFile = "requester-did"
AssociationRequestFile = "association-request.json"
AssociationRefFile = "association-ref"
RequesterAtircEnvFile = "r2-atirc.env"
RequesterGroup = "socialweb-r2"
RequesterDir = "/run/socialweb-r2"
RequesterStartEnvFile = "r2-start.env"
RequesterUnit = "socialweb-r2.service"
RequesterReleaseUnit = "socialweb-r2-release.service"
DefaultSecretsFetchScript = "/usr/local/bin/setup-secrets.sh"
SecretsReadyFile = "secrets.ready"
SecretsRequestFile = "secrets.request"
secretsTag = "secrets"
secretsFallbackSealed = "sealed"
BidderFile = "bidder.json"
CursorFile = "cursor"
STUNServer = "stun:stun.cloudflare.com:3478"
maxTag = 32
associationRefDir = "/etc/socialweb-computer"
)
var ErrConfig = errors.New("guestagent: invalid config")
type Tunnel struct {
Tag string `json:"tag"`
Port int `json:"port"`
}
type Bidder struct {
Base string `json:"base"`
Audience string `json:"audience"`
AcceptPath string `json:"acceptPath"`
WebRTC *WebRTCPort `json:"webrtc,omitempty"`
}
type WebRTCPort struct {
UDPPort int `json:"udpPort"`
HostAddresses []string `json:"hostAddresses"`
}
type Secrets struct {
Fallback string `json:"fallback"`
GetSecrets bool `json:"getSecrets"`
}
type Association struct {
RefFile string `json:"refFile"`
}
type AcceptRef struct {
URI string
CID string
}
type Config struct {
Tunnels []Tunnel
Secrets *Secrets
AgentUnit string
Association *Association
Bidder Bidder
Accept AcceptRef
StateDir string
LoginHelper string
}
func (c Config) Port(tag string) (int, bool) {
for _, t := range c.Tunnels {
if t.Tag == tag {
return t.Port, true
}
}
return 0, false
}
func LoadConfig(configDir, stateDir, loginHelper string) (Config, error) {
cfg := Config{StateDir: stateDir, LoginHelper: loginHelper}
var tunnels struct {
Tunnels []Tunnel `json:"tunnels"`
}
if err := readOptionalJSON(filepath.Join(configDir, TunnelsFile), &tunnels); err != nil {
return cfg, err
}
var secrets Secrets
switch err := readOptionalJSON(filepath.Join(configDir, SecretsFile), &secrets); {
case err != nil:
return cfg, err
case secrets.Fallback == secretsFallbackSealed:
cfg.Secrets = &secrets
case secrets.Fallback != "":
return cfg, fmt.Errorf("%w: %s fallback %q is not %s", ErrConfig, SecretsFile, secrets.Fallback, secretsFallbackSealed)
}
var agent AgentUnit
if err := readOptionalJSON(filepath.Join(configDir, AgentFile), &agent); err != nil {
return cfg, err
}
if agent.Unit != "" && !agentUnitRE.MatchString(agent.Unit) {
return cfg, fmt.Errorf("%w: %s unit %q is not a systemd service name of [a-z0-9-]", ErrConfig, AgentFile, agent.Unit)
}
cfg.AgentUnit = agent.Unit
var association Association
switch err := readOptionalJSON(filepath.Join(configDir, AssociationFile), &association); {
case err != nil:
return cfg, err
case association.RefFile != "":
clean := filepath.Clean(association.RefFile)
if !strings.HasPrefix(association.RefFile, "/") || clean != association.RefFile || !strings.HasPrefix(clean, associationRefDir+"/") {
return cfg, fmt.Errorf("%w: %s refFile %q is not a clean path under %s/", ErrConfig, AssociationFile, association.RefFile, associationRefDir)
}
cfg.Association = &association
}
seen := map[string]bool{}
for _, t := range tunnels.Tunnels {
switch {
case t.Tag == "" || len(t.Tag) > maxTag || !printable(t.Tag):
return cfg, fmt.Errorf("%w: tunnel tag %q is not 1 to %d printable ASCII bytes", ErrConfig, t.Tag, maxTag)
case t.Port < 1 || t.Port > 65535:
return cfg, fmt.Errorf("%w: tunnel %s port %d is not a TCP port", ErrConfig, t.Tag, t.Port)
case seen[t.Tag]:
return cfg, fmt.Errorf("%w: tunnel tag %s appears twice", ErrConfig, t.Tag)
case t.Tag == secretsTag:
return cfg, fmt.Errorf("%w: tunnel tag %s names the secrets key", ErrConfig, t.Tag)
}
seen[t.Tag] = true
cfg.Tunnels = append(cfg.Tunnels, t)
}
if len(cfg.Tunnels) == 0 && cfg.Secrets == nil && cfg.AgentUnit == "" && cfg.Association == nil {
return cfg, fmt.Errorf("%w: %s names no tunnel, %s asks for no sealed secrets fallback, %s names no unit and %s names no ref file", ErrConfig,
TunnelsFile, SecretsFile, AgentFile, AssociationFile)
}
if err := readJSON(filepath.Join(configDir, BidderFile), &cfg.Bidder); err != nil {
return cfg, err
}
base, err := url.Parse(strings.TrimSpace(cfg.Bidder.Base))
if err != nil || (base.Scheme != "http" && base.Scheme != "https") || base.Host == "" {
return cfg, fmt.Errorf("%w: %s base %q is not an http or https URL", ErrConfig, BidderFile, cfg.Bidder.Base)
}
cfg.Bidder.Base = strings.TrimRight(base.String(), "/")
cfg.Bidder.Audience = strings.TrimSpace(cfg.Bidder.Audience)
if cfg.Bidder.Audience == "" {
return cfg, fmt.Errorf("%w: %s has no audience", ErrConfig, BidderFile)
}
if strings.TrimSpace(cfg.Bidder.AcceptPath) == "" {
return cfg, fmt.Errorf("%w: %s has no acceptPath", ErrConfig, BidderFile)
}
if w := cfg.Bidder.WebRTC; w != nil {
if w.UDPPort < 1 || w.UDPPort > 65535 || len(w.HostAddresses) == 0 {
return cfg, fmt.Errorf("%w: %s webrtc names port %d and %d host addresses", ErrConfig, BidderFile, w.UDPPort, len(w.HostAddresses))
}
for _, a := range w.HostAddresses {
if _, err := netip.ParseAddr(strings.TrimSpace(a)); err != nil {
return cfg, fmt.Errorf("%w: %s webrtc host address %q is not an IP address", ErrConfig, BidderFile, a)
}
}
}
var accept struct {
Accept struct {
URI string `json:"uri"`
CID string `json:"cid"`
} `json:"accept"`
}
if err := readJSON(cfg.Bidder.AcceptPath, &accept); err != nil {
return cfg, err
}
cfg.Accept = AcceptRef{URI: strings.TrimSpace(accept.Accept.URI), CID: strings.TrimSpace(accept.Accept.CID)}
if cfg.Accept.URI == "" || cfg.Accept.CID == "" {
return cfg, fmt.Errorf("%w: %s has no accept ref", ErrConfig, cfg.Bidder.AcceptPath)
}
if strings.TrimSpace(cfg.StateDir) == "" || strings.TrimSpace(cfg.LoginHelper) == "" {
return cfg, fmt.Errorf("%w: a state dir and a login helper are required", ErrConfig)
}
return cfg, nil
}
func readOptionalJSON(path string, into any) error {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return nil
}
return readJSON(path, into)
}
func readJSON(path string, into any) error {
raw, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("%w: %v", ErrConfig, err)
}
if err := json.Unmarshal(raw, into); err != nil {
return fmt.Errorf("%w: %s: %v", ErrConfig, path, err)
}
return nil
}
func printable(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] <= ' ' || s[i] > '~' {
return false
}
}
return true
}