nuclei/pkg/types/interfaces.go

166 lines
3.5 KiB
Go
Raw Normal View History

2020-12-24 06:43:18 +00:00
// Taken from https://github.com/spf13/cast.
package types
import (
javascript protocol for scripting (includes 15+ proto libs) (#4109) * rebase js-layer PR from @ice3man543 * package restructuring * working * fix duplicated event & matcher status * fix lint error * fix response field * add new functions * multiple minor improvements * fix incorrect stats in js protocol * sort output metadata in cli * remove temp files * remove dead code * add unit and integration test * fix lint error * add jsdoclint using llm * fix error in test * add js lint using llm * generate docs of libs * llm lint * remove duplicated docs * update generated docs * update prompt in doclint * update docs * temp disable version check test * fix unit test and add retry * fix panic in it * update and move jsdocs * updated jsdocs * update docs * update container platform in test * dir restructure and adding docs * add api_reference and remove markdown docs * fix imports * add javascript design and contribution docs * add js protocol documentation * update integration test and docs * update doc ext mdx->md * minor update to docs * new integration test and more * move go libs and add docs * gen new net docs and more * final docs update * add new devtool * use fastdialer * fix build fail * use fastdialer + network sandbox support * add reserved keyword 'Port' * update Port to new syntax * misc update * always enable templatectx in js protocol * move docs to 'js-proto-docs' repo * remove scrapefuncs binary --------- Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
2023-09-16 10:32:17 +00:00
"bytes"
2022-03-23 15:26:05 +00:00
"encoding/hex"
2020-12-24 06:43:18 +00:00
"fmt"
"strconv"
"strings"
2022-03-23 15:26:05 +00:00
"github.com/asaskevich/govalidator"
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
2020-12-24 06:43:18 +00:00
)
// JSONScalarToString converts an interface coming from json to string
// Inspired from: https://github.com/cli/cli/blob/09b09810dd812e3ede54b59ad9d6912b946ac6c5/pkg/export/template.go#L72
func JSONScalarToString(input interface{}) (string, error) {
switch tt := input.(type) {
case string:
return ToString(tt), nil
case float64:
return ToString(tt), nil
case nil:
return ToString(tt), nil
case bool:
return ToString(tt), nil
default:
return "", fmt.Errorf("cannot convert type to string: %v", tt)
}
}
2020-12-24 06:43:18 +00:00
// ToString converts an interface to string in a quick way
func ToString(data interface{}) string {
switch s := data.(type) {
case nil:
return ""
2020-12-24 06:43:18 +00:00
case string:
return s
case bool:
return strconv.FormatBool(s)
case float64:
return strconv.FormatFloat(s, 'f', -1, 64)
case float32:
return strconv.FormatFloat(float64(s), 'f', -1, 32)
case int:
return strconv.Itoa(s)
case int64:
return strconv.FormatInt(s, 10)
case int32:
return strconv.Itoa(int(s))
case int16:
return strconv.FormatInt(int64(s), 10)
case int8:
return strconv.FormatInt(int64(s), 10)
case uint:
return strconv.FormatUint(uint64(s), 10)
case uint64:
2021-02-26 07:43:11 +00:00
return strconv.FormatUint(s, 10)
2020-12-24 06:43:18 +00:00
case uint32:
return strconv.FormatUint(uint64(s), 10)
case uint16:
return strconv.FormatUint(uint64(s), 10)
case uint8:
return strconv.FormatUint(uint64(s), 10)
case []byte:
return string(s)
case severity.Holder:
return s.Severity.String()
case severity.Severity:
return s.String()
2020-12-24 06:43:18 +00:00
case fmt.Stringer:
return s.String()
case error:
return s.Error()
default:
return fmt.Sprintf("%v", data)
}
}
// ToStringNSlice converts an interface to string in a quick way or to a slice with strings
// if the input is a slice of interfaces.
func ToStringNSlice(data interface{}) interface{} {
switch s := data.(type) {
case []interface{}:
var a []string
for _, v := range s {
a = append(a, ToString(v))
}
return a
default:
return ToString(data)
}
}
2022-03-23 15:26:05 +00:00
func ToHexOrString(data interface{}) string {
switch s := data.(type) {
case string:
if govalidator.IsASCII(s) {
return s
}
return hex.Dump([]byte(s))
case []byte:
return hex.Dump(s)
default:
return fmt.Sprintf("%v", data)
}
}
2020-12-24 06:43:18 +00:00
// ToStringSlice casts an interface to a []string type.
func ToStringSlice(i interface{}) []string {
var a []string
switch v := i.(type) {
case []interface{}:
for _, u := range v {
a = append(a, ToString(u))
}
return a
case []string:
return v
case string:
return strings.Fields(v)
default:
return nil
}
}
javascript protocol for scripting (includes 15+ proto libs) (#4109) * rebase js-layer PR from @ice3man543 * package restructuring * working * fix duplicated event & matcher status * fix lint error * fix response field * add new functions * multiple minor improvements * fix incorrect stats in js protocol * sort output metadata in cli * remove temp files * remove dead code * add unit and integration test * fix lint error * add jsdoclint using llm * fix error in test * add js lint using llm * generate docs of libs * llm lint * remove duplicated docs * update generated docs * update prompt in doclint * update docs * temp disable version check test * fix unit test and add retry * fix panic in it * update and move jsdocs * updated jsdocs * update docs * update container platform in test * dir restructure and adding docs * add api_reference and remove markdown docs * fix imports * add javascript design and contribution docs * add js protocol documentation * update integration test and docs * update doc ext mdx->md * minor update to docs * new integration test and more * move go libs and add docs * gen new net docs and more * final docs update * add new devtool * use fastdialer * fix build fail * use fastdialer + network sandbox support * add reserved keyword 'Port' * update Port to new syntax * misc update * always enable templatectx in js protocol * move docs to 'js-proto-docs' repo * remove scrapefuncs binary --------- Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
2023-09-16 10:32:17 +00:00
// ToByteSlice casts an interface to a []byte type.
func ToByteSlice(i interface{}) []byte {
switch v := i.(type) {
case []byte:
return v
case []string:
return []byte(strings.Join(v, ""))
case string:
return []byte(v)
case []interface{}:
var buff bytes.Buffer
for _, u := range v {
buff.WriteString(ToString(u))
}
return buff.Bytes()
default:
strValue := ToString(i)
return []byte(strValue)
}
}
2020-12-24 06:43:18 +00:00
// ToStringMap casts an interface to a map[string]interface{} type.
func ToStringMap(i interface{}) map[string]interface{} {
var m = map[string]interface{}{}
switch v := i.(type) {
case map[interface{}]interface{}:
for k, val := range v {
m[ToString(k)] = val
}
return m
case map[string]interface{}:
return v
default:
return nil
}
}