Save time.Duration as seconds instead of nanoseconds

This commit is contained in:
nemunaire 2023-02-24 16:49:32 +01:00
parent 251b40a961
commit 1711a4736e
6 changed files with 169 additions and 15 deletions

View File

@ -40,18 +40,19 @@ import (
"git.happydns.org/happydomain/model"
"git.happydns.org/happydomain/services"
"git.happydns.org/happydomain/services/common"
"git.happydns.org/happydomain/utils"
)
type Origin struct {
Ns string `json:"mname" happydomain:"label=Name Server,placeholder=ns0,required,description=The domain name of the name server that was the original or primary source of data for this zone."`
Mbox string `json:"rname" happydomain:"label=Contact Email,required,description=A <domain-name> which specifies the mailbox of the person responsible for this zone."`
Serial uint32 `json:"serial" happydomain:"label=Zone Serial,required,description=The unsigned 32 bit version number of the original copy of the zone. Zone transfers preserve this value. This value wraps and should be compared using sequence space arithmetic."`
Refresh time.Duration `json:"refresh" happydomain:"label=Slave Refresh Time,required,description=The time interval before the zone should be refreshed by others name servers than the primary."`
Retry time.Duration `json:"retry" happydomain:"label=Retry Interval on failed refresh,required,description=The time interval a slave name server should elapse before a failed refresh should be retried."`
Expire time.Duration `json:"expire" happydomain:"label=Authoritative Expiry,required,description=Time value that specifies the upper limit on the time interval that can elapse before the zone is no longer authoritative."`
Negttl time.Duration `json:"nxttl" happydomain:"label=Negative Caching Time,required,description=Maximal time a resolver should cache a negative authoritative answer (such as NXDOMAIN ...)."`
NameServers []string `json:"ns" happydomain:"label=Zone's Name Servers"`
Ns string `json:"mname" happydomain:"label=Name Server,placeholder=ns0,required,description=The domain name of the name server that was the original or primary source of data for this zone."`
Mbox string `json:"rname" happydomain:"label=Contact Email,required,description=A <domain-name> which specifies the mailbox of the person responsible for this zone."`
Serial uint32 `json:"serial" happydomain:"label=Zone Serial,required,description=The unsigned 32 bit version number of the original copy of the zone. Zone transfers preserve this value. This value wraps and should be compared using sequence space arithmetic."`
Refresh common.Duration `json:"refresh" happydomain:"label=Slave Refresh Time,required,description=The time interval before the zone should be refreshed by others name servers than the primary."`
Retry common.Duration `json:"retry" happydomain:"label=Retry Interval on failed refresh,required,description=The time interval a slave name server should elapse before a failed refresh should be retried."`
Expire common.Duration `json:"expire" happydomain:"label=Authoritative Expiry,required,description=Time value that specifies the upper limit on the time interval that can elapse before the zone is no longer authoritative."`
Negttl common.Duration `json:"nxttl" happydomain:"label=Negative Caching Time,required,description=Maximal time a resolver should cache a negative authoritative answer (such as NXDOMAIN ...)."`
NameServers []string `json:"ns" happydomain:"label=Zone's Name Servers"`
}
func (s *Origin) GetNbResources() int {
@ -104,10 +105,10 @@ func origin_analyze(a *svcs.Analyzer) error {
Ns: soa.Ns,
Mbox: soa.Mbox,
Serial: soa.Serial,
Refresh: time.Duration(soa.Refresh) * time.Second,
Retry: time.Duration(soa.Retry) * time.Second,
Expire: time.Duration(soa.Expire) * time.Second,
Negttl: time.Duration(soa.Minttl) * time.Second,
Refresh: common.Duration(time.Duration(soa.Refresh) * time.Second),
Retry: common.Duration(time.Duration(soa.Retry) * time.Second),
Expire: common.Duration(time.Duration(soa.Expire) * time.Second),
Negttl: common.Duration(time.Duration(soa.Minttl) * time.Second),
}
a.UseRR(

View File

@ -0,0 +1,70 @@
// Copyright or © or Copr. happyDNS (2023)
//
// contact@happydomain.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package common
import (
"encoding/json"
"fmt"
"time"
)
type Duration time.Duration
func (d Duration) Seconds() float64 {
return time.Duration(d).Seconds()
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Seconds())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
*d = Duration(time.Duration(value) * time.Second)
return nil
case string:
v, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(v)
return nil
default:
return fmt.Errorf("invalid duration")
}
}

View File

@ -0,0 +1,82 @@
// Copyright or © or Copr. happyDNS (2023)
//
// contact@happydomain.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package database
import (
"bytes"
"fmt"
"log"
)
func migrateFrom3(s *LevelDBStorage) (err error) {
err = migrateFrom3_records(s)
if err != nil {
return
}
err = s.Tidy()
if err != nil {
return
}
return
}
func migrateFrom3_records(s *LevelDBStorage) (err error) {
TypeStr := []byte("\"_svctype\":\"abstract.Origin\"")
iter := s.search("domain.zone-")
for iter.Next() {
zonestr, err := s.db.Get(iter.Key(), nil)
if err != nil {
return fmt.Errorf("unable to find/decode %s: %w", iter.Key(), err)
}
if bytes.Contains(zonestr, TypeStr) {
migstr := zonestr
migstr = bytes.Replace(migstr, []byte("000000000,\"retry\":"), []byte(",\"retry\":"), 1)
migstr = bytes.Replace(migstr, []byte("000000000,\"expire\":"), []byte(",\"expire\":"), 1)
migstr = bytes.Replace(migstr, []byte("000000000,\"nxttl\":"), []byte(",\"nxttl\":"), 1)
migstr = bytes.Replace(migstr, []byte("000000000,\"ns\":"), []byte(",\"ns\":"), 1)
if !bytes.Equal(migstr, zonestr) {
err = s.db.Put(iter.Key(), migstr, nil)
if err != nil {
return fmt.Errorf("unable to write %s: %w", iter.Key(), err)
}
log.Printf("Migrating v2 -> v3: %s (contains Origin)...", iter.Key())
}
}
}
return
}

View File

@ -42,6 +42,7 @@ var migrations []LevelDBMigrationFunc = []LevelDBMigrationFunc{
migrateFrom0,
migrateFrom1,
migrateFrom2,
migrateFrom3,
}
func (s *LevelDBStorage) DoMigration() (err error) {

View File

@ -18,7 +18,7 @@
export let value: any;
let unit: string|null = null;
$: unit = specs.type === 'time.Duration' ? 's' : null
$: unit = specs.type === 'time.Duration' || specs.type === 'common.Duration' ? 's' : null
let inputtype: InputType = "text";
$: if (specs.type && (specs.type.startsWith("uint") || specs.type.startsWith("int"))) inputtype = "number";
@ -29,7 +29,7 @@
if (specs.type == "int8" || specs.type == "uint8") inputmax = 255;
else if (specs.type == "int16" || specs.type == "uint16") inputmax = 65536;
else if (specs.type == "int" || specs.type == "uint" || specs.type == "int32" || specs.type == "uint32") inputmax = 2147483647;
else if (specs.type == 'time.Duration' || specs.type == "int64" || specs.type == "uint64") inputmax = 9007199254740991;
else if (specs.type == 'time.Duration' || specs.type == 'common.Duration' || specs.type == "int64" || specs.type == "uint64") inputmax = 9007199254740991;
else inputmax = undefined;
if (inputmax) {

View File

@ -8,6 +8,6 @@ export function fillUndefinedValues(value: any, spec: Field) {
if (spec.default !== undefined) value[spec.id] = spec.default;
else if (vartype == "[]uint8") value[spec.id] = "";
else if (vartype.startsWith("[]")) value[spec.id] = [];
else if (vartype != "string" && !vartype.startsWith("uint") && !vartype.startsWith("int") && vartype != "time.Duration" && vartype != "net.IP") value[spec.id] = { };
else if (vartype != "string" && !vartype.startsWith("uint") && !vartype.startsWith("int") && vartype != "net.IP" && vartype != "time.Duration" && vartype != "common.Duration") value[spec.id] = { };
}
}