diff --git a/internal/confmigrate/migrations_test.go b/internal/confmigrate/migrations_test.go index 5e9ea657..513646e8 100644 --- a/internal/confmigrate/migrations_test.go +++ b/internal/confmigrate/migrations_test.go @@ -96,7 +96,7 @@ func TestUpgradeSchema5to6(t *testing.T) { }, want: yobj{ "clients": yarr{yobj{ - "ids": []string{"127.0.0.1"}, + "ids": yarr{"127.0.0.1"}, "ip": "127.0.0.1", }}, "schema_version": newSchemaVer, @@ -109,7 +109,7 @@ func TestUpgradeSchema5to6(t *testing.T) { }, want: yobj{ "clients": yarr{yobj{ - "ids": []string{"mac"}, + "ids": yarr{"mac"}, "mac": "mac", }}, "schema_version": newSchemaVer, @@ -122,7 +122,7 @@ func TestUpgradeSchema5to6(t *testing.T) { }, want: yobj{ "clients": yarr{yobj{ - "ids": []string{"127.0.0.1", "mac"}, + "ids": yarr{"127.0.0.1", "mac"}, "ip": "127.0.0.1", "mac": "mac", }}, diff --git a/internal/confmigrate/v10.go b/internal/confmigrate/v10.go index 81641897..0cf10c2f 100644 --- a/internal/confmigrate/v10.go +++ b/internal/confmigrate/v10.go @@ -34,36 +34,47 @@ func migrateTo10(diskConf yobj) (err error) { diskConf["schema_version"] = 10 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } const quicPort = 784 - for _, upsField := range []string{ - "upstream_dns", - "local_ptr_upstreams", - } { - var ups yarr - ups, ok, err = fieldVal[yarr](dns, upsField) - if err != nil { + ups, ok, err := fieldVal[yarr](dns, "upstream_dns") + if err != nil { + return err + } else if ok { + if err = addQUICPorts(ups, quicPort); err != nil { return err - } else if !ok { - continue } - var u string - for i, uVal := range ups { - u, ok = uVal.(string) - if !ok { - return fmt.Errorf("unexpected type of upstream field: %T", uVal) - } + dns["upstream_dns"] = ups + } - ups[i] = addQUICPort(u, quicPort) + ups, ok, err = fieldVal[yarr](dns, "local_ptr_upstreams") + if err != nil { + return err + } else if ok { + if err = addQUICPorts(ups, quicPort); err != nil { + return err } - dns[upsField] = ups + + dns["local_ptr_upstreams"] = ups + } + + return nil +} + +// addQUICPorts inserts a port into each QUIC upstream's hostname in ups if +// those are missing. +func addQUICPorts(ups yarr, port int) (err error) { + for i, uVal := range ups { + u, ok := uVal.(string) + if !ok { + return fmt.Errorf("unexpected type of upstream field: %T", uVal) + } + + ups[i] = addQUICPort(u, port) } return nil diff --git a/internal/confmigrate/v12.go b/internal/confmigrate/v12.go index 078f8bc6..6703561a 100644 --- a/internal/confmigrate/v12.go +++ b/internal/confmigrate/v12.go @@ -21,18 +21,18 @@ func migrateTo12(diskConf yobj) (err error) { diskConf["schema_version"] = 12 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } const field = "querylog_interval" qlogIvl, ok, err := fieldVal[int](dns, field) - if err != nil { - return err - } else if !ok { + if !ok { + if err != nil { + return err + } + // Set the initial value from home.initConfig function. qlogIvl = 90 } diff --git a/internal/confmigrate/v13.go b/internal/confmigrate/v13.go index 27afa25d..a69592bf 100644 --- a/internal/confmigrate/v13.go +++ b/internal/confmigrate/v13.go @@ -19,17 +19,13 @@ func migrateTo13(diskConf yobj) (err error) { diskConf["schema_version"] = 13 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } dhcp, ok, err := fieldVal[yobj](diskConf, "dhcp") - if err != nil { + if !ok { return err - } else if !ok { - return nil } return moveSameVal[string](dns, dhcp, "local_domain_name") diff --git a/internal/confmigrate/v14.go b/internal/confmigrate/v14.go index 1f5812f8..d259a2a6 100644 --- a/internal/confmigrate/v14.go +++ b/internal/confmigrate/v14.go @@ -31,35 +31,32 @@ func migrateTo14(diskConf yobj) (err error) { diskConf["schema_version"] = 14 persistent, ok, err := fieldVal[yarr](diskConf, "clients") - if err != nil { - return err - } else if !ok { + if !ok { + if err != nil { + return err + } + persistent = yarr{} } - var rdnsSrc bool + runtimeClients := yobj{ + "whois": true, + "arp": true, + "rdns": false, + "dhcp": true, + "hosts": true, + } + diskConf["clients"] = yobj{ + "persistent": persistent, + "runtime_sources": runtimeClients, + } + dns, ok, err := fieldVal[yobj](diskConf, "dns") if err != nil { return err - } else if ok { - rdnsSrc, ok, err = fieldVal[bool](dns, "resolve_clients") - if err != nil { - return err - } else if ok { - delete(dns, "resolve_clients") - } + } else if !ok { + return nil } - diskConf["clients"] = yobj{ - "persistent": persistent, - "runtime_sources": yobj{ - "whois": true, - "arp": true, - "rdns": rdnsSrc, - "dhcp": true, - "hosts": true, - }, - } - - return nil + return moveVal[bool](dns, runtimeClients, "resolve_clients", "rdns") } diff --git a/internal/confmigrate/v15.go b/internal/confmigrate/v15.go index 6cd0e3e6..904ef110 100644 --- a/internal/confmigrate/v15.go +++ b/internal/confmigrate/v15.go @@ -30,10 +30,8 @@ func migrateTo15(diskConf yobj) (err error) { diskConf["schema_version"] = 15 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } qlog := map[string]any{ @@ -43,17 +41,12 @@ func migrateTo15(diskConf yobj) (err error) { "interval": "2160h", "size_memory": 1000, } - err = coalesceError( - moveVal[bool](dns, qlog, "querylog_enabled", "enabled"), - moveVal[bool](dns, qlog, "querylog_file_enabled", "file_enabled"), - moveVal[string](dns, qlog, "querylog_interval", "interval"), - moveVal[int](dns, qlog, "querylog_size_memory", "size_memory"), - ) - if err != nil { - return err - } - diskConf["querylog"] = qlog - return nil + return coalesceError( + moveVal[bool](dns, qlog, "querylog_enabled", "enabled"), + moveVal[bool](dns, qlog, "querylog_file_enabled", "file_enabled"), + moveVal[any](dns, qlog, "querylog_interval", "interval"), + moveVal[int](dns, qlog, "querylog_size_memory", "size_memory"), + ) } diff --git a/internal/confmigrate/v16.go b/internal/confmigrate/v16.go index 3722a53d..b174d4cb 100644 --- a/internal/confmigrate/v16.go +++ b/internal/confmigrate/v16.go @@ -47,10 +47,8 @@ func migrateTo16(diskConf yobj) (err error) { diskConf["schema_version"] = 16 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } stats := yobj{ @@ -58,24 +56,23 @@ func migrateTo16(diskConf yobj) (err error) { "interval": 1, "ignored": yarr{}, } + diskConf["statistics"] = stats const field = "statistics_interval" statsIvl, ok, err := fieldVal[int](dns, field) - if err != nil { + if !ok { return err - } else if ok { - if statsIvl == 0 { - // Set the interval to the default value of one day to make sure - // that it passes the validations. - stats["enabled"] = false - } else { - stats["interval"] = statsIvl - } - delete(dns, field) } - diskConf["statistics"] = stats + if statsIvl == 0 { + // Set the interval to the default value of one day to make sure + // that it passes the validations. + stats["enabled"] = false + } else { + stats["interval"] = statsIvl + } + delete(dns, field) return nil } diff --git a/internal/confmigrate/v17.go b/internal/confmigrate/v17.go index 97dc3b3d..a3a04460 100644 --- a/internal/confmigrate/v17.go +++ b/internal/confmigrate/v17.go @@ -22,10 +22,8 @@ func migrateTo17(diskConf yobj) (err error) { diskConf["schema_version"] = 17 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } const field = "edns_client_subnet" diff --git a/internal/confmigrate/v18.go b/internal/confmigrate/v18.go index cc90b007..28c55723 100644 --- a/internal/confmigrate/v18.go +++ b/internal/confmigrate/v18.go @@ -26,10 +26,8 @@ func migrateTo18(diskConf yobj) (err error) { diskConf["schema_version"] = 18 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } safeSearch := yobj{ @@ -41,7 +39,6 @@ func migrateTo18(diskConf yobj) (err error) { "yandex": true, "youtube": true, } - dns["safe_search"] = safeSearch return moveVal[bool](dns, safeSearch, "safesearch_enabled", "enabled") diff --git a/internal/confmigrate/v19.go b/internal/confmigrate/v19.go index 317e44c2..6fbb6d4f 100644 --- a/internal/confmigrate/v19.go +++ b/internal/confmigrate/v19.go @@ -34,10 +34,8 @@ func migrateTo19(diskConf yobj) (err error) { diskConf["schema_version"] = 19 clients, ok, err := fieldVal[yobj](diskConf, "clients") - if err != nil { + if !ok { return err - } else if !ok { - return nil } persistent, ok, _ := fieldVal[yarr](clients, "persistent") diff --git a/internal/confmigrate/v20.go b/internal/confmigrate/v20.go index 64813ce6..e86c36c7 100644 --- a/internal/confmigrate/v20.go +++ b/internal/confmigrate/v20.go @@ -25,10 +25,8 @@ func migrateTo20(diskConf yobj) (err error) { diskConf["schema_version"] = 20 stats, ok, err := fieldVal[yobj](diskConf, "statistics") - if err != nil { + if !ok { return err - } else if !ok { - return nil } const field = "interval" diff --git a/internal/confmigrate/v21.go b/internal/confmigrate/v21.go index fd074da7..afda11bf 100644 --- a/internal/confmigrate/v21.go +++ b/internal/confmigrate/v21.go @@ -28,10 +28,8 @@ func migrateTo21(diskConf yobj) (err error) { const field = "blocked_services" dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } svcs := yobj{ diff --git a/internal/confmigrate/v22.go b/internal/confmigrate/v22.go index 70bb23f4..4d5de732 100644 --- a/internal/confmigrate/v22.go +++ b/internal/confmigrate/v22.go @@ -36,17 +36,13 @@ func migrateTo22(diskConf yobj) (err error) { const field = "blocked_services" clients, ok, err := fieldVal[yobj](diskConf, "clients") - if err != nil { + if !ok { return err - } else if !ok { - return nil } persistent, ok, err := fieldVal[yarr](clients, "persistent") - if err != nil { + if !ok { return err - } else if !ok { - return nil } for i, p := range persistent { diff --git a/internal/confmigrate/v23.go b/internal/confmigrate/v23.go index 34cee315..6cbcbcf5 100644 --- a/internal/confmigrate/v23.go +++ b/internal/confmigrate/v23.go @@ -27,10 +27,8 @@ func migrateTo23(diskConf yobj) (err error) { diskConf["schema_version"] = 23 bindHost, ok, err := fieldVal[string](diskConf, "bind_host") - if err != nil { + if !ok { return err - } else if !ok { - return nil } bindHostAddr, err := netip.ParseAddr(bindHost) diff --git a/internal/confmigrate/v25.go b/internal/confmigrate/v25.go index 25ab7858..e574610e 100644 --- a/internal/confmigrate/v25.go +++ b/internal/confmigrate/v25.go @@ -18,14 +18,13 @@ func migrateTo25(diskConf yobj) (err error) { diskConf["schema_version"] = 25 httpObj, ok, err := fieldVal[yobj](diskConf, "http") - if err != nil { + if !ok { return err - } else if !ok { - return nil } pprofObj := yobj{ - "port": 6060, + "enabled": false, + "port": 6060, } err = moveVal[bool](diskConf, pprofObj, "debug_pprof", "enabled") diff --git a/internal/confmigrate/v26.go b/internal/confmigrate/v26.go index 88e10140..5ead91df 100644 --- a/internal/confmigrate/v26.go +++ b/internal/confmigrate/v26.go @@ -73,10 +73,8 @@ func migrateTo26(diskConf yobj) (err error) { diskConf["schema_version"] = 26 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } filteringObj := yobj{} diff --git a/internal/confmigrate/v3.go b/internal/confmigrate/v3.go index 0f522b51..8220c5df 100644 --- a/internal/confmigrate/v3.go +++ b/internal/confmigrate/v3.go @@ -14,24 +14,18 @@ package confmigrate // 'bootstrap_dns': // - '1.1.1.1' // # … -func migrateTo3(diskConf yobj) error { +func migrateTo3(diskConf yobj) (err error) { diskConf["schema_version"] = 3 dnsConfig, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } bootstrapDNS, ok, err := fieldVal[any](dnsConfig, "bootstrap_dns") - if err != nil { - return err - } else if !ok { - return nil + if ok { + dnsConfig["bootstrap_dns"] = yarr{bootstrapDNS} } - dnsConfig["bootstrap_dns"] = yarr{bootstrapDNS} - - return nil + return err } diff --git a/internal/confmigrate/v4.go b/internal/confmigrate/v4.go index 8ea8155e..467cc05c 100644 --- a/internal/confmigrate/v4.go +++ b/internal/confmigrate/v4.go @@ -6,20 +6,21 @@ package confmigrate // 'schema_version': 3 // 'clients': // - # … +// # … // // # AFTER: // 'schema_version': 4 // 'clients': // - 'use_global_blocked_services': true // # … +// # … func migrateTo4(diskConf yobj) (err error) { diskConf["schema_version"] = 4 clients, ok, _ := fieldVal[yarr](diskConf, "clients") if ok { for i := range clients { - var c yobj - if c, ok = clients[i].(yobj); ok { + if c, isYobj := clients[i].(yobj); isYobj { c["use_global_blocked_services"] = true } } diff --git a/internal/confmigrate/v5.go b/internal/confmigrate/v5.go index aed006d0..7bdd4d00 100644 --- a/internal/confmigrate/v5.go +++ b/internal/confmigrate/v5.go @@ -30,10 +30,8 @@ func migrateTo5(diskConf yobj) (err error) { } pass, ok, err := fieldVal[string](diskConf, "auth_pass") - if err != nil { + if !ok { return err - } else if !ok { - return nil } delete(diskConf, "auth_pass") diff --git a/internal/confmigrate/v6.go b/internal/confmigrate/v6.go index 2767a4c0..aa741ba9 100644 --- a/internal/confmigrate/v6.go +++ b/internal/confmigrate/v6.go @@ -10,24 +10,26 @@ import "fmt" // - # … // 'ip': '127.0.0.1' // 'mac': 'AA:AA:AA:AA:AA:AA' +// # … // # … // // # AFTER: // 'schema_version': 6 // 'clients': // - # … +// 'ip': '127.0.0.1' +// 'mac': 'AA:AA:AA:AA:AA:AA' // 'ids': // - '127.0.0.1' // - 'AA:AA:AA:AA:AA:AA' +// # … // # … func migrateTo6(diskConf yobj) (err error) { diskConf["schema_version"] = 6 clients, ok, err := fieldVal[yarr](diskConf, "clients") - if err != nil { + if !ok { return err - } else if !ok { - return nil } for i, client := range clients { @@ -37,22 +39,14 @@ func migrateTo6(diskConf yobj) (err error) { return fmt.Errorf("unexpected type of client at index %d: %T", i, client) } - var ids []string - - var ip string - ip, _, err = fieldVal[string](c, "ip") - if err != nil { - return fmt.Errorf("client at index %d: %w", i, err) - } else if ip != "" { - ids = append(ids, ip) - } - - var mac string - mac, _, err = fieldVal[string](c, "mac") - if err != nil { - return fmt.Errorf("client at index %d: %w", i, err) - } else if mac != "" { - ids = append(ids, mac) + ids := yarr{} + for _, id := range []string{"ip", "mac"} { + val, _, valErr := fieldVal[string](c, id) + if valErr != nil { + return fmt.Errorf("client at index %d: %w", i, valErr) + } else if val != "" { + ids = append(ids, val) + } } c["ids"] = ids diff --git a/internal/confmigrate/v7.go b/internal/confmigrate/v7.go index 904bb88d..1db348bd 100644 --- a/internal/confmigrate/v7.go +++ b/internal/confmigrate/v7.go @@ -28,11 +28,11 @@ package confmigrate // 'lease_duration': 86400 // 'icmp_timeout_msec': 1000 // # … -func migrateTo7(diskConf yobj) error { +func migrateTo7(diskConf yobj) (err error) { diskConf["schema_version"] = 7 - dhcp, ok, err := fieldVal[yobj](diskConf, "dhcp") - if err != nil || !ok { + dhcp, ok, _ := fieldVal[yobj](diskConf, "dhcp") + if !ok { return nil } diff --git a/internal/confmigrate/v8.go b/internal/confmigrate/v8.go index 50c31fd4..f8075dfb 100644 --- a/internal/confmigrate/v8.go +++ b/internal/confmigrate/v8.go @@ -20,17 +20,13 @@ func migrateTo8(diskConf yobj) (err error) { diskConf["schema_version"] = 8 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } bindHost, ok, err := fieldVal[string](dns, "bind_host") - if err != nil { + if !ok { return err - } else if !ok { - return nil } delete(dns, "bind_host") diff --git a/internal/confmigrate/v9.go b/internal/confmigrate/v9.go index eba74ec7..b8fc9a72 100644 --- a/internal/confmigrate/v9.go +++ b/internal/confmigrate/v9.go @@ -19,10 +19,8 @@ func migrateTo9(diskConf yobj) (err error) { diskConf["schema_version"] = 9 dns, ok, err := fieldVal[yobj](diskConf, "dns") - if err != nil { + if !ok { return err - } else if !ok { - return nil } return moveVal[string](dns, dns, "autohost_tld", "local_domain_name") diff --git a/internal/confmigrate/yaml.go b/internal/confmigrate/yaml.go index 3e097b52..778cd1fd 100644 --- a/internal/confmigrate/yaml.go +++ b/internal/confmigrate/yaml.go @@ -1,6 +1,8 @@ package confmigrate -import "fmt" +import ( + "fmt" +) type ( // yarr is the convenience alias for YAML array. diff --git a/scripts/make/go-lint.sh b/scripts/make/go-lint.sh index 9a2fc2a5..f08fcd47 100644 --- a/scripts/make/go-lint.sh +++ b/scripts/make/go-lint.sh @@ -177,7 +177,6 @@ run_linter gocyclo --over 10 . gocognit_paths="\ ./internal/aghnet/ 20 ./internal/querylog/ 20 -./internal/confmigrate/ 19 ./internal/dnsforward/ 19 ./internal/home/ 19 ./internal/aghtls/ 18 @@ -194,6 +193,7 @@ gocognit_paths="\ ./internal/aghrenameio/ 10 ./internal/arpdb/ 10 ./internal/client/ 10 +./internal/confmigrate/ 10 ./internal/dhcpsvc 10 ./internal/filtering/hashprefix/ 10 ./internal/filtering/rulelist/ 10