diff --git a/certificates/auth/interface.go b/certificates/auth/interface.go index 9481234..9df67ad 100644 --- a/certificates/auth/interface.go +++ b/certificates/auth/interface.go @@ -59,11 +59,15 @@ func List() []ClientAuth { } } -func Parse(s string) ClientAuth { +func cleanString(s string) string { s = strings.ToLower(s) s = strings.Replace(s, "\"", "", -1) s = strings.Replace(s, "'", "", -1) - s = strings.TrimSpace(s) + return strings.TrimSpace(s) +} + +func Parse(s string) ClientAuth { + s = cleanString(s) switch { case strings.Contains(s, strict) || (strings.Contains(s, require) && strings.Contains(s, verify)): diff --git a/certificates/auth/models.go b/certificates/auth/models.go index 3d349a1..c054827 100644 --- a/certificates/auth/models.go +++ b/certificates/auth/models.go @@ -28,6 +28,7 @@ package auth import ( "reflect" + "strings" libmap "github.com/go-viper/mapstructure/v2" ) @@ -48,13 +49,19 @@ func ViperDecoderHook() libmap.DecodeHookFuncType { } // Check if the target type matches the expected one - if to != reflect.TypeOf(z) { + if to.Kind() != reflect.Int { return data, nil } + if strings.Contains(cleanString(t), none) { + return NoClientCert, nil + } + // Format/decode/parse the data and return the new value if e := z.unmarshall([]byte(t)); e != nil { - return nil, e + return data, nil + } else if z == NoClientCert { + return data, nil } else { return z, nil } diff --git a/certificates/ca/models.go b/certificates/ca/models.go index 935cbf4..6a12f35 100644 --- a/certificates/ca/models.go +++ b/certificates/ca/models.go @@ -79,13 +79,13 @@ func ViperDecoderHook() libmap.DecodeHookFuncType { } // Check if the target type matches the expected one - if to != reflect.TypeOf(z) { + if to.Kind() != reflect.Interface { return data, nil } // Format/decode/parse the data and return the new value if e := z.unMarshall([]byte(t)); e != nil { - return nil, e + return data, nil } else { return z, nil } diff --git a/certificates/certs/encode.go b/certificates/certs/encode.go index 260a3f6..6ff736d 100644 --- a/certificates/certs/encode.go +++ b/certificates/certs/encode.go @@ -38,13 +38,13 @@ import ( func (o *Certif) unMarshall(p []byte) error { if o.UnmarshalJSON(p) == nil { return nil - } else if o.UnmarshalYAML(&yaml.Node{Value: string(p)}) != nil { + } else if o.UnmarshalYAML(&yaml.Node{Value: string(p)}) == nil { return nil - } else if o.UnmarshalTOML(p) != nil { + } else if o.UnmarshalTOML(p) == nil { return nil - } else if o.UnmarshalCBOR(p) != nil { + } else if o.UnmarshalCBOR(p) == nil { return nil - } else if o.UnmarshalText(p) != nil { + } else if o.UnmarshalText(p) == nil { return nil } diff --git a/certificates/certs/models.go b/certificates/certs/models.go index 2995691..b2d840b 100644 --- a/certificates/certs/models.go +++ b/certificates/certs/models.go @@ -28,6 +28,7 @@ package certs import ( "crypto/tls" + "encoding/json" "reflect" libmap "github.com/go-viper/mapstructure/v2" @@ -80,28 +81,28 @@ func (o *Certif) GetCerts() []string { func ViperDecoderHook() libmap.DecodeHookFuncType { return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) { var ( - z = &Certif{c: tls.Certificate{}} - t string - k bool + ok bool + z = &Certif{c: tls.Certificate{}} ) - // Check if the data type matches the expected one - if from.Kind() != reflect.String { - return data, nil - } else if t, k = data.(string); !k { - return data, nil + if _, ok = data.(map[string]interface{}); ok { + if p, e := json.Marshal(data); e != nil { + return data, nil + } else if e = z.UnmarshalJSON(p); e != nil { + return data, nil + } else { + return z, nil + } + } else if _, ok = data.(string); !ok { + if p, e := json.Marshal(data); e != nil { + return data, nil + } else if e = z.UnmarshalJSON(p); e != nil { + return data, nil + } else { + return z, nil + } } - // Check if the target type matches the expected one - if to != reflect.TypeOf(z) { - return data, nil - } - - // Format/decode/parse the data and return the new value - if e := z.unMarshall([]byte(t)); e != nil { - return nil, e - } else { - return z, nil - } + return data, nil } } diff --git a/certificates/cipher/models.go b/certificates/cipher/models.go index f62c4c2..2bb0b87 100644 --- a/certificates/cipher/models.go +++ b/certificates/cipher/models.go @@ -77,13 +77,15 @@ func ViperDecoderHook() libmap.DecodeHookFuncType { } // Check if the target type matches the expected one - if to != reflect.TypeOf(z) { + if to.Kind() != reflect.Uint16 { return data, nil } // Format/decode/parse the data and return the new value if e := z.unmarshall([]byte(t)); e != nil { - return nil, e + return data, nil + } else if z == Unknown { + return data, nil } else { return z, nil } diff --git a/certificates/curves/models.go b/certificates/curves/models.go index 8a5a87a..ecbfc8e 100644 --- a/certificates/curves/models.go +++ b/certificates/curves/models.go @@ -63,13 +63,15 @@ func ViperDecoderHook() libmap.DecodeHookFuncType { } // Check if the target type matches the expected one - if to != reflect.TypeOf(z) { + if to.Kind() != reflect.Uint16 { return data, nil } // Format/decode/parse the data and return the new value if e := z.unmarshall([]byte(t)); e != nil { - return nil, e + return data, nil + } else if z == Unknown { + return data, nil } else { return z, nil } diff --git a/certificates/tlsversion/models.go b/certificates/tlsversion/models.go index fe6bd72..ebb891d 100644 --- a/certificates/tlsversion/models.go +++ b/certificates/tlsversion/models.go @@ -63,13 +63,15 @@ func ViperDecoderHook() libmap.DecodeHookFuncType { } // Check if the target type matches the expected one - if to != reflect.TypeOf(z) { + if to.Kind() != reflect.Int { return data, nil } // Format/decode/parse the data and return the new value if e := z.unmarshall([]byte(t)); e != nil { - return nil, e + return data, nil + } else if z == VersionUnknown { + return data, nil } else { return z, nil } diff --git a/duration/big/interface.go b/duration/big/interface.go index 8dc16d5..91d2e5a 100644 --- a/duration/big/interface.go +++ b/duration/big/interface.go @@ -79,10 +79,15 @@ func ParseDuration(d time.Duration) Duration { } func ParseFloat64(f float64) Duration { - const mx float64 = math.MaxInt64 + const ( + mx float64 = math.MaxInt64 + mi = -mx + ) if f > mx { return Duration(math.MaxInt64) + } else if f < mi { + return Duration(-math.MaxInt64) } else { return Duration(math.Round(f)) } diff --git a/duration/interface.go b/duration/interface.go index b0716bd..5b77da9 100644 --- a/duration/interface.go +++ b/duration/interface.go @@ -63,10 +63,15 @@ func ParseDuration(d time.Duration) Duration { } func ParseFloat64(f float64) Duration { - const mx float64 = math.MaxInt64 + const ( + mx float64 = math.MaxInt64 + mi = -mx + ) if f > mx { return Duration(math.MaxInt64) + } else if f < mi { + return Duration(-math.MaxInt64) } else { return Duration(math.Round(f)) } diff --git a/semaphore/interface.go b/semaphore/interface.go index f241ecf..ef63e7c 100644 --- a/semaphore/interface.go +++ b/semaphore/interface.go @@ -38,6 +38,8 @@ type Semaphore interface { context.Context semtps.Sem semtps.Progress + + Clone() Semaphore } func MaxSimultaneous() int { diff --git a/semaphore/semaphore.go b/semaphore/semaphore.go index 5340044..31f4a29 100644 --- a/semaphore/semaphore.go +++ b/semaphore/semaphore.go @@ -56,6 +56,13 @@ func (o *sem) Weighted() int64 { return o.s.Weighted() } +func (o *sem) Clone() Semaphore { + return &sem{ + s: o.s.New(), + m: o.m, + } +} + func (o *sem) New() semtps.Sem { return o.s.New() }