github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/dns/dns_types.go (about)

     1  package dns
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"strings"
     7  
     8  	"github.com/alecthomas/jsonschema"
     9  )
    10  
    11  // DNSRequestType is the type of the method specified
    12  type DNSRequestType int
    13  
    14  // name:DNSRequestType
    15  const (
    16  	// name:A
    17  	A DNSRequestType = iota + 1
    18  	// name:NS
    19  	NS
    20  	// name:DS
    21  	DS
    22  	// name:CNAME
    23  	CNAME
    24  	// name:SOA
    25  	SOA
    26  	// name:PTR
    27  	PTR
    28  	// name:MX
    29  	MX
    30  	// name:TXT
    31  	TXT
    32  	// name:AAAA
    33  	AAAA
    34  	// name:CAA
    35  	CAA
    36  	// name:TLSA
    37  	TLSA
    38  	// name:ANY
    39  	ANY
    40  	limit
    41  )
    42  
    43  // DNSRequestTypeMapping is a table for conversion of method from string.
    44  var DNSRequestTypeMapping = map[DNSRequestType]string{
    45  	A:     "A",
    46  	NS:    "NS",
    47  	DS:    "DS",
    48  	CNAME: "CNAME",
    49  	SOA:   "SOA",
    50  	PTR:   "PTR",
    51  	MX:    "MX",
    52  	TXT:   "TXT",
    53  	AAAA:  "AAAA",
    54  	CAA:   "CAA",
    55  	TLSA:  "TLSA",
    56  	ANY:   "ANY",
    57  }
    58  
    59  // GetSupportedDNSRequestTypes returns list of supported types
    60  func GetSupportedDNSRequestTypes() []DNSRequestType {
    61  	var result []DNSRequestType
    62  	for index := DNSRequestType(1); index < limit; index++ {
    63  		result = append(result, index)
    64  	}
    65  	return result
    66  }
    67  
    68  func toDNSRequestTypes(valueToMap string) (DNSRequestType, error) {
    69  	normalizedValue := normalizeValue(valueToMap)
    70  	for key, currentValue := range DNSRequestTypeMapping {
    71  		if normalizedValue == currentValue {
    72  			return key, nil
    73  		}
    74  	}
    75  	return -1, errors.New("Invalid DNS request type: " + valueToMap)
    76  }
    77  
    78  func normalizeValue(value string) string {
    79  	return strings.TrimSpace(strings.ToUpper(value))
    80  }
    81  
    82  func (t DNSRequestType) String() string {
    83  	return DNSRequestTypeMapping[t]
    84  }
    85  
    86  // DNSRequestTypeHolder is used to hold internal type of the DNS type
    87  type DNSRequestTypeHolder struct {
    88  	DNSRequestType DNSRequestType `mapping:"true"`
    89  }
    90  
    91  func (holder DNSRequestTypeHolder) String() string {
    92  	return holder.DNSRequestType.String()
    93  }
    94  
    95  func (holder DNSRequestTypeHolder) JSONSchemaType() *jsonschema.Type {
    96  	gotType := &jsonschema.Type{
    97  		Type:        "string",
    98  		Title:       "type of DNS request to make",
    99  		Description: "Type is the type of DNS request to make",
   100  	}
   101  	for _, types := range GetSupportedDNSRequestTypes() {
   102  		gotType.Enum = append(gotType.Enum, types.String())
   103  	}
   104  	return gotType
   105  }
   106  
   107  func (holder *DNSRequestTypeHolder) UnmarshalYAML(unmarshal func(interface{}) error) error {
   108  	var marshalledTypes string
   109  	if err := unmarshal(&marshalledTypes); err != nil {
   110  		return err
   111  	}
   112  
   113  	computedType, err := toDNSRequestTypes(marshalledTypes)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	holder.DNSRequestType = computedType
   119  	return nil
   120  }
   121  
   122  func (holder *DNSRequestTypeHolder) UnmarshalJSON(data []byte) error {
   123  	s := strings.Trim(string(data), `"`)
   124  	if s == "" {
   125  		return nil
   126  	}
   127  	computedType, err := toDNSRequestTypes(s)
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	holder.DNSRequestType = computedType
   133  	return nil
   134  }
   135  
   136  func (holder *DNSRequestTypeHolder) MarshalJSON() ([]byte, error) {
   137  	return json.Marshal(holder.DNSRequestType.String())
   138  }
   139  
   140  func (holder DNSRequestTypeHolder) MarshalYAML() (interface{}, error) {
   141  	return holder.DNSRequestType.String(), nil
   142  }