this has taken quite a while (8 ish hours) but reports and proof of work challenges are completely implemented the proof of work challenges are bit iffy and could be gpu accelerated which is bad, so i plan to eventually switch to something memory hard like argon2id references used for pow: https://dev.to/ratmd/simple-proof-of-work-in-javascript-3kgm https://anubis.techaro.lol/docs/design/how-anubis-works https://anubis.techaro.lol/docs/design/why-proof-of-work as well as some other posts or repos found when searching
99 lines
2.9 KiB
Go
99 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"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
|
|
}
|
|
|
|
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", "18")
|
|
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")
|
|
}
|
|
|
|
log.Println(len(reportOptions))
|
|
|
|
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"),
|
|
}, nil
|
|
}
|
|
|
|
func envOrDefault(key, fallback string) string {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
return value
|
|
}
|