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
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package config
import (
"fmt"
"net/netip"
"net/url"
"strings"
"github.com/miekg/dns"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/language"
"github.com/superseriousbusiness/gotosocial/internal/log"
)
// Validate validates global config settings.
func Validate() error {
// Gather all validation errors in
// easily readable format for admins.
var (
errs gtserror.MultiError
errf = func(format string, a ...any) {
errs = append(errs, fmt.Errorf(format, a...))
}
)
// `host`
host := GetHost()
if host == "" {
errf("%s must be set", HostFlag())
}
// If `account-domain` and `host`
// are set, `host` must be a valid
// subdomain of `account-domain`.
if host != "" {
ad := GetAccountDomain()
if ad == "" {
// `account-domain` not set, fall
// back by setting it to `host`.
SetAccountDomain(GetHost())
} else if !dns.IsSubDomain(ad, host) {
errf(
"%s %s is not a valid subdomain of %s %s",
AccountDomainFlag(), ad, HostFlag(), host,
)
}
}
// Ensure `protocol` sensibly set.
switch proto := GetProtocol(); proto {
case "https":
// No problem.
case "http":
log.Warnf(
nil,
"%s was set to 'http'; this should *only* be used for debugging and tests!",
ProtocolFlag(),
)
case "":
errf("%s must be set", ProtocolFlag())
default:
errf(
"%s must be set to either http or https, provided value was %s",
ProtocolFlag(), proto,
)
}
// `federation-mode` should be
// "blocklist" or "allowlist".
switch fediMode := GetInstanceFederationMode(); fediMode {
case InstanceFederationModeBlocklist, InstanceFederationModeAllowlist:
// No problem.
case "":
errf("%s must be set", InstanceFederationModeFlag())
default:
errf(
"%s must be set to either blocklist or allowlist, provided value was %s",
InstanceFederationModeFlag(), fediMode,
)
}
// Parse `instance-languages`, and
// set enriched version into config.
parsedLangs, err := language.InitLangs(GetInstanceLanguages().TagStrs())
if err != nil {
errf(
"%s could not be parsed as an array of valid BCP47 language tags: %v",
InstanceLanguagesFlag(), err,
)
} else {
// Parsed successfully, put enriched
// versions in config immediately.
SetInstanceLanguages(parsedLangs)
}
// `instance-stats-mode` should be
// "", "zero", "serve", or "baffle"
switch statsMode := GetInstanceStatsMode(); statsMode {
case InstanceStatsModeDefault, InstanceStatsModeZero, InstanceStatsModeServe, InstanceStatsModeBaffle:
// No problem.
default:
errf(
"%s must be set to empty string, zero, serve, or baffle, provided value was %s",
InstanceFederationModeFlag(), statsMode,
)
}
// `web-assets-base-dir`.
webAssetsBaseDir := GetWebAssetBaseDir()
if webAssetsBaseDir == "" {
errf("%s must be set", WebAssetBaseDirFlag())
}
// `storage-s3-redirect-url`
if s3RedirectURL := GetStorageS3RedirectURL(); s3RedirectURL != "" {
if strings.HasSuffix(s3RedirectURL, "/") {
errf(
"%s must not end with a trailing slash",
StorageS3RedirectURLFlag(),
)
}
if url, err := url.Parse(s3RedirectURL); err != nil {
errf(
"%s invalid: %w",
StorageS3RedirectURLFlag(), err,
)
} else if url.Scheme != "https" && url.Scheme != "http" {
errf(
"%s scheme must be https or http",
StorageS3RedirectURLFlag(),
)
}
}
// Custom / LE TLS settings.
//
// Only one of custom certs or LE can be set,
// and if using custom certs then all relevant
// values must be provided.
var (
tlsChain = GetTLSCertificateChain()
tlsKey = GetTLSCertificateKey()
tlsChainFlag = TLSCertificateChainFlag()
tlsKeyFlag = TLSCertificateKeyFlag()
)
if GetLetsEncryptEnabled() && (tlsChain != "" || tlsKey != "") {
errf(
"%s cannot be true when %s and/or %s are also set",
LetsEncryptEnabledFlag(), tlsChainFlag, tlsKeyFlag,
)
}
if (tlsChain != "" && tlsKey == "") || (tlsChain == "" && tlsKey != "") {
errf(
"%s and %s need to both be set or unset",
tlsChainFlag, tlsKeyFlag,
)
}
// Parse `advanced-rate-limit-exceptions` and set
// parsed versions on config to avoid reparsing calls.
rles := GetAdvancedRateLimitExceptions()
rlesParsed := make([]netip.Prefix, 0, len(rles))
for _, rle := range rles {
parsed, err := netip.ParsePrefix(rle)
if err != nil {
errf(
"invalid entry %s in %s: %w",
rle, AdvancedRateLimitExceptionsFlag(), err,
)
continue
}
rlesParsed = append(rlesParsed, parsed)
}
SetAdvancedRateLimitExceptionsParsed(rlesParsed)
return errs.Combine()
}
|