validate.go (3113B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package config 19 20 import ( 21 "errors" 22 "fmt" 23 "strings" 24 25 "github.com/miekg/dns" 26 "github.com/superseriousbusiness/gotosocial/internal/log" 27 ) 28 29 // Validate validates global config settings which don't have defaults, to make sure they are set sensibly. 30 func Validate() error { 31 errs := []error{} 32 33 // host 34 host := GetHost() 35 if host == "" { 36 errs = append(errs, fmt.Errorf("%s must be set", HostFlag())) 37 } 38 39 // accountDomain; only check if host was set, otherwise there's no point 40 if host != "" { 41 switch ad := GetAccountDomain(); ad { 42 case "": 43 SetAccountDomain(GetHost()) 44 default: 45 if !dns.IsSubDomain(ad, host) { 46 //errs = append(errs, fmt.Errorf("%s was %s and %s was %s, but %s is not a valid subdomain of %s", HostFlag(), host, AccountDomainFlag(), ad, host, ad)) 47 log.Warnf(nil, "%s was %s and %s was %s, but %s is not a valid subdomain of %s - apparently this is supposed to be bad, but +Umbrellix permits it.", HostFlag(), host, AccountDomainFlag(), ad, host, ad) 48 } 49 } 50 } 51 52 // protocol 53 switch proto := GetProtocol(); proto { 54 case "https": 55 // no problem 56 break 57 case "http": 58 log.Warnf(nil, "%s was set to 'http'; this should *only* be used for debugging and tests!", ProtocolFlag()) 59 case "": 60 errs = append(errs, fmt.Errorf("%s must be set", ProtocolFlag())) 61 default: 62 errs = append(errs, fmt.Errorf("%s must be set to either http or https, provided value was %s", ProtocolFlag(), proto)) 63 } 64 65 webAssetsBaseDir := GetWebAssetBaseDir() 66 if webAssetsBaseDir == "" { 67 errs = append(errs, fmt.Errorf("%s must be set", WebAssetBaseDirFlag())) 68 } 69 70 tlsChain := GetTLSCertificateChain() 71 tlsKey := GetTLSCertificateKey() 72 tlsChainFlag := TLSCertificateChainFlag() 73 tlsKeyFlag := TLSCertificateKeyFlag() 74 75 if GetLetsEncryptEnabled() && (tlsChain != "" || tlsKey != "") { 76 errs = append(errs, fmt.Errorf("%s cannot be enabled when %s and/or %s are also set", LetsEncryptEnabledFlag(), tlsChainFlag, tlsKeyFlag)) 77 } 78 79 if (tlsChain != "" && tlsKey == "") || (tlsChain == "" && tlsKey != "") { 80 errs = append(errs, fmt.Errorf("%s and %s need to both be set or unset", tlsChainFlag, tlsKeyFlag)) 81 } 82 83 if len(errs) > 0 { 84 errStrings := []string{} 85 for _, err := range errs { 86 errStrings = append(errStrings, err.Error()) 87 } 88 return errors.New(strings.Join(errStrings, "; ")) 89 } 90 91 return nil 92 }