134 lines
4.2 KiB
Go
134 lines
4.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/netip"
|
|
"os"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
UploadDir string
|
|
MaxPasteBytes int64
|
|
MaxFileBytes int64
|
|
Expiries []Expiry
|
|
NormalizeMIMETypes bool
|
|
BannedMIMETypes []string
|
|
PoWDifficulty int
|
|
ReportOptions []string
|
|
|
|
DatabasePath string
|
|
// public host like "https://bin.zip", NOT the address minbin listens on
|
|
Host string
|
|
Addr string
|
|
IPAdressSource string
|
|
TrustedProxies []netip.Prefix
|
|
BannedClientASNs map[uint32]struct{}
|
|
}
|
|
|
|
func LoadConfig() (Config, error) {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
log.Fatal("Error loading .env file")
|
|
}
|
|
|
|
pasteSizeSetting := envOrDefault("MINIBIN_MAX_PASTE_KIB", "1024")
|
|
maxPasteKiB, err := strconv.ParseInt(pasteSizeSetting, 10, 64)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("failed to parse MINIBIN_MAX_PASTE_KIB: %w", err)
|
|
}
|
|
if maxPasteKiB <= 0 {
|
|
return Config{}, fmt.Errorf("MINIBIN_MAX_PASTE_KIB must be greater than 0")
|
|
}
|
|
|
|
fileSizeSetting := envOrDefault("MINIBIN_MAX_FILE_KIB", "1024")
|
|
maxFileKiB, err := strconv.ParseInt(fileSizeSetting, 10, 64)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("failed to parse MINIBIN_MAX_FILE_KIB: %w", err)
|
|
}
|
|
if maxFileKiB <= 0 {
|
|
return Config{}, fmt.Errorf("MINIBIN_MAX_FILE_KIB must be greater than 0")
|
|
}
|
|
|
|
expiriesSetting := envOrDefault("MINIBIN_EXPIRIES", "1h,12h,1d,7d,2w,1mo,6mo,1y,5y,0")
|
|
var expiries []Expiry
|
|
for entry := range strings.SplitSeq(expiriesSetting, ",") {
|
|
expiry, err := ParseExpiry(entry)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("MINIBIN_EXPIRIES: %w", err)
|
|
}
|
|
expiries = append(expiries, expiry)
|
|
}
|
|
|
|
powDifficultySetting := envOrDefault("MINIBIN_POW_DIFFICULTY", "10")
|
|
powDifficulty, err := strconv.Atoi(powDifficultySetting)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("failed to parse MINIBIN_POW_DIFFICULTY")
|
|
}
|
|
reportOptionsSetting := envOrDefault("MINIBIN_REPORT_OPTIONS", "Malware (including links),CSAM/CSEM,Copyright Infringement,Fraud")
|
|
reportOptions := strings.Split(reportOptionsSetting, ",")
|
|
if len(reportOptions) == 0 {
|
|
return Config{}, fmt.Errorf("no valid report options set")
|
|
}
|
|
ipAddressSource := envOrDefault("MINIBIN_IP_ADDRESS_SOURCE", "remote")
|
|
if !slices.Contains([]string{"remote", "cloudflare", "xff"}, ipAddressSource) {
|
|
return Config{}, fmt.Errorf("%s is not a valid MINIBIN_IP_ADDRESS_SOURCE option", ipAddressSource)
|
|
}
|
|
var trustedProxies []netip.Prefix
|
|
if rawTrusted := envOrDefault("MINIBIN_TRUSTED_PROXIES", ""); rawTrusted != "" {
|
|
for entry := range strings.SplitSeq(rawTrusted, ",") {
|
|
entry = strings.TrimSpace(entry)
|
|
prefix, err := netip.ParsePrefix(entry)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("MINIBIN_TRUSTED_PROXIES: invalid cidr %q: %w", entry, err)
|
|
}
|
|
trustedProxies = append(trustedProxies, prefix)
|
|
}
|
|
}
|
|
bannedAsns := make(map[uint32]struct{})
|
|
if rawAsn := envOrDefault("MINIBIN_BANNED_ASNS", ""); rawAsn != "" {
|
|
for entry := range strings.SplitSeq(rawAsn, ",") {
|
|
entry = strings.TrimSpace(entry)
|
|
if entry == "" {
|
|
continue
|
|
}
|
|
asn, err := strconv.ParseUint(entry, 10, 32)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("MINIBIN_BANNED_ASNS: invalid number %q: %w", entry, err)
|
|
}
|
|
bannedAsns[uint32(asn)] = struct{}{}
|
|
}
|
|
}
|
|
|
|
return Config{
|
|
UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"),
|
|
MaxPasteBytes: maxPasteKiB * 1024,
|
|
MaxFileBytes: maxFileKiB * 1024,
|
|
Expiries: expiries,
|
|
NormalizeMIMETypes: envOrDefault("MINIBIN_NORMALIZE_MIME_TYPES", "true") == "true",
|
|
BannedMIMETypes: strings.Split(envOrDefault("MINIBIN_BANNED_MIME_TYPES", ""), ","),
|
|
PoWDifficulty: powDifficulty,
|
|
ReportOptions: reportOptions,
|
|
|
|
DatabasePath: envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite"),
|
|
Host: envOrDefault("MINIBIN_HOST", "http://localhost:8080"),
|
|
Addr: envOrDefault("MINIBIN_ADDR", "localhost:8080"),
|
|
IPAdressSource: ipAddressSource,
|
|
TrustedProxies: trustedProxies,
|
|
BannedClientASNs: bannedAsns,
|
|
}, nil
|
|
}
|
|
|
|
func envOrDefault(key, fallback string) string {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
return value
|
|
}
|