| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "sort" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "time" |
| 13 | |
| 14 | "gopkg.in/yaml.v3" |
| 15 | ) |
| 16 | |
| 17 | // Field beschrijft één instelling die de webinterface, de API en BlueGit |
| 18 | // kunnen lezen en wijzigen. Alles wat in Fields staat, is overal hetzelfde |
| 19 | // zichtbaar; alleen velden zonder Readonly zijn instelbaar. |
| 20 | type Field struct { |
| 21 | Key string `json:"key"` |
| 22 | Label string `json:"label"` |
| 23 | Help string `json:"help"` |
| 24 | // Kind is duration, int of text. |
| 25 | Kind string `json:"kind"` |
| 26 | // Restart: historisch, "pas na een herstart". Wordt niet meer gebruikt; |
| 27 | // listen geldt meteen. |
| 28 | Restart bool `json:"restart,omitempty"` |
| 29 | // Readonly: alleen tonen, niet wijzigen (zoals data_dir). |
| 30 | Readonly bool `json:"readonly,omitempty"` |
| 31 | // Placeholder is een voorbeeld in het invoerveld. |
| 32 | Placeholder string `json:"placeholder,omitempty"` |
| 33 | } |
| 34 | |
| 35 | // Fields zijn de instelbare velden, in de volgorde van de webinterface. |
| 36 | var Fields = []Field{ |
| 37 | {Key: "data_dir", Label: "Gegevensmap", Help: "Hier staan settings.yml, de kopie, de archieven en het beheertoken. Dit pad zet je bij het starten (--data-dir of BLUEGIT_BACKUP_DATA_DIR); in de webinterface kan het niet, omdat de instellingen zelf in deze map staan.", Kind: "text", Readonly: true}, |
| 38 | {Key: "public_url", Label: "Publiek adres", Help: "Waar BlueGit deze server bereikt. Een domein of een IP-adres mag, bijvoorbeeld http://backup.lan:8080 of http://192.168.1.50:8080. IPv6 tussen hakjes: http://[2001:db8::1]:8080.", Kind: "text", Placeholder: "http://192.168.1.50:8080"}, |
| 39 | {Key: "archive_interval", Label: "Archiefinterval", Help: "Zo vaak wordt de hele kopie gecomprimeerd opgeslagen (bijvoorbeeld 24h, 12h, 7d).", Kind: "duration"}, |
| 40 | {Key: "retention_days", Label: "Bewaartermijn (dagen)", Help: "Archieven ouder dan dit gaan weg. 0 = nooit op leeftijd. Het nieuwste blijft altijd staan.", Kind: "int"}, |
| 41 | {Key: "min_free_mb", Label: "Minimaal vrij (MB)", Help: "Minder vrije schijfruimte dan dit: het oudste archief gaat weg.", Kind: "int"}, |
| 42 | {Key: "rate_limit_kib", Label: "Downloadsnelheid (KiB/s)", Help: "Zo snel wordt BlueGit hoogstens gelezen. 0 = onbeperkt.", Kind: "int"}, |
| 43 | {Key: "full_sync_interval", Label: "Volledige controle", Help: "Ook zonder melding zo vaak alles nalopen.", Kind: "duration"}, |
| 44 | {Key: "min_sync_interval", Label: "Minimale tijd tussen syncs", Help: "Na een melding wacht de server minstens zo lang sinds de vorige sync.", Kind: "duration"}, |
| 45 | {Key: "listen", Label: "Luisteradres", Help: "Adres en poort van de webinterface en API. Een wijziging geldt meteen. Lukt binden niet, dan blijft het oude adres in gebruik en krijg je een foutmelding.", Kind: "text", Placeholder: "0.0.0.0:8080"}, |
| 46 | } |
| 47 | |
| 48 | // FieldByKey zoekt een veld. |
| 49 | func FieldByKey(key string) (Field, bool) { |
| 50 | for _, f := range Fields { |
| 51 | if f.Key == key { |
| 52 | return f, true |
| 53 | } |
| 54 | } |
| 55 | return Field{}, false |
| 56 | } |
| 57 | |
| 58 | // Get geeft de waarde van key als tekst. |
| 59 | func (c Config) Get(key string) string { |
| 60 | switch key { |
| 61 | case "listen": |
| 62 | return c.Listen |
| 63 | case "public_url": |
| 64 | return c.PublicURL |
| 65 | case "data_dir": |
| 66 | return c.DataDir |
| 67 | case "archive_interval": |
| 68 | return FormatDuration(c.ArchiveInterval.Duration) |
| 69 | case "retention_days": |
| 70 | return strconv.Itoa(c.RetentionDays) |
| 71 | case "min_free_mb": |
| 72 | return strconv.FormatInt(c.MinFreeMB, 10) |
| 73 | case "rate_limit_kib": |
| 74 | return strconv.FormatInt(c.RateLimitKiB, 10) |
| 75 | case "full_sync_interval": |
| 76 | return FormatDuration(c.FullSyncInterval.Duration) |
| 77 | case "min_sync_interval": |
| 78 | return FormatDuration(c.MinSyncInterval.Duration) |
| 79 | } |
| 80 | return "" |
| 81 | } |
| 82 | |
| 83 | // FormatDuration schrijft 24h als "24h" en 30m als "30m". |
| 84 | func FormatDuration(d time.Duration) string { |
| 85 | if d%(24*time.Hour) == 0 && d >= 48*time.Hour { |
| 86 | return fmt.Sprintf("%dd", d/(24*time.Hour)) |
| 87 | } |
| 88 | switch { |
| 89 | case d == 0: |
| 90 | return "0s" |
| 91 | case d%time.Hour == 0: |
| 92 | return fmt.Sprintf("%dh", d/time.Hour) |
| 93 | case d%time.Minute == 0: |
| 94 | return fmt.Sprintf("%dm", d/time.Minute) |
| 95 | case d%time.Second == 0: |
| 96 | return fmt.Sprintf("%ds", d/time.Second) |
| 97 | } |
| 98 | return d.String() |
| 99 | } |
| 100 | |
| 101 | // ParseDuration leest een Go-duur of een aantal dagen ("7d"). |
| 102 | func ParseDuration(s string) (time.Duration, error) { |
| 103 | s = strings.TrimSpace(s) |
| 104 | if strings.HasSuffix(s, "d") { |
| 105 | n, err := strconv.Atoi(strings.TrimSuffix(s, "d")) |
| 106 | if err == nil && n >= 0 { |
| 107 | return time.Duration(n) * 24 * time.Hour, nil |
| 108 | } |
| 109 | } |
| 110 | d, err := time.ParseDuration(s) |
| 111 | if err != nil { |
| 112 | return 0, fmt.Errorf("ongeldige duur %q", s) |
| 113 | } |
| 114 | return d, nil |
| 115 | } |
| 116 | |
| 117 | // Set zet key op val. Alleen velden uit Fields. |
| 118 | func (c *Config) Set(key, val string) error { |
| 119 | f, ok := FieldByKey(key) |
| 120 | if !ok { |
| 121 | return fmt.Errorf("onbekende instelling %q", key) |
| 122 | } |
| 123 | if f.Readonly { |
| 124 | return fmt.Errorf("%s is niet instelbaar in de webinterface of API", key) |
| 125 | } |
| 126 | val = strings.TrimSpace(val) |
| 127 | switch f.Kind { |
| 128 | case "duration": |
| 129 | d, err := ParseDuration(val) |
| 130 | if err != nil { |
| 131 | return fmt.Errorf("%s: %w", key, err) |
| 132 | } |
| 133 | switch key { |
| 134 | case "archive_interval": |
| 135 | c.ArchiveInterval.Duration = d |
| 136 | case "full_sync_interval": |
| 137 | c.FullSyncInterval.Duration = d |
| 138 | case "min_sync_interval": |
| 139 | c.MinSyncInterval.Duration = d |
| 140 | } |
| 141 | case "int": |
| 142 | n, err := strconv.ParseInt(val, 10, 64) |
| 143 | if err != nil || n < 0 { |
| 144 | return fmt.Errorf("%s: geef een geheel getal van 0 of meer", key) |
| 145 | } |
| 146 | switch key { |
| 147 | case "retention_days": |
| 148 | c.RetentionDays = int(n) |
| 149 | case "min_free_mb": |
| 150 | c.MinFreeMB = n |
| 151 | case "rate_limit_kib": |
| 152 | c.RateLimitKiB = n |
| 153 | } |
| 154 | default: |
| 155 | switch key { |
| 156 | case "public_url": |
| 157 | u, err := ParsePublicURL(val) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | c.PublicURL = u |
| 162 | case "listen": |
| 163 | if _, _, err := net.SplitHostPort(val); err != nil { |
| 164 | return fmt.Errorf("listen: verwacht host:poort, bijvoorbeeld 0.0.0.0:8080") |
| 165 | } |
| 166 | c.Listen = val |
| 167 | } |
| 168 | } |
| 169 | return nil |
| 170 | } |
| 171 | |
| 172 | // Apply zet een reeks instellingen in één keer. Bij een fout verandert er niets. |
| 173 | func (c *Config) Apply(set map[string]string) error { |
| 174 | next := *c |
| 175 | keys := make([]string, 0, len(set)) |
| 176 | for k := range set { |
| 177 | keys = append(keys, k) |
| 178 | } |
| 179 | sort.Strings(keys) |
| 180 | for _, k := range keys { |
| 181 | if err := next.Set(k, set[k]); err != nil { |
| 182 | return err |
| 183 | } |
| 184 | } |
| 185 | if err := next.Validate(); err != nil { |
| 186 | return err |
| 187 | } |
| 188 | *c = next |
| 189 | return nil |
| 190 | } |
| 191 | |
| 192 | // SettingsFile bewaart wat via de webinterface, API of BlueGit is ingesteld. |
| 193 | const SettingsFile = "settings.yml" |
| 194 | |
| 195 | // LegacyImportMarker wordt gezet nadat een oude config.yml één keer is |
| 196 | // overgenomen. Daarna wint de webinterface. |
| 197 | const LegacyImportMarker = "config.yml.imported" |
| 198 | |
| 199 | // LegacyConfigCopy is een kopie van /etc/bluegit-backup/config.yml die het |
| 200 | // pakket bij een upgrade wegzet, voor het geval het origineel verdwijnt. |
| 201 | const LegacyConfigCopy = "legacy-config.yml" |
| 202 | |
| 203 | // LoadSettings leest data_dir/settings.yml (ontbreekt: leeg). |
| 204 | func LoadSettings(dataDir string) (map[string]string, error) { |
| 205 | out := map[string]string{} |
| 206 | b, err := os.ReadFile(filepath.Join(dataDir, SettingsFile)) |
| 207 | if errors.Is(err, os.ErrNotExist) { |
| 208 | return out, nil |
| 209 | } |
| 210 | if err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | if err := yaml.Unmarshal(b, &out); err != nil { |
| 214 | return nil, fmt.Errorf("%s: %w", SettingsFile, err) |
| 215 | } |
| 216 | return out, nil |
| 217 | } |
| 218 | |
| 219 | // SaveSettings schrijft data_dir/settings.yml. |
| 220 | func SaveSettings(dataDir string, set map[string]string) error { |
| 221 | b, err := yaml.Marshal(set) |
| 222 | if err != nil { |
| 223 | return err |
| 224 | } |
| 225 | b = append([]byte("# Door de webinterface, API of BlueGit ingesteld.\n"), b...) |
| 226 | tmp := filepath.Join(dataDir, SettingsFile+".tmp") |
| 227 | if err := os.WriteFile(tmp, b, 0o600); err != nil { |
| 228 | return err |
| 229 | } |
| 230 | return os.Rename(tmp, filepath.Join(dataDir, SettingsFile)) |
| 231 | } |