github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/common/contextargs/metainput.go (about)

     1  package contextargs
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  
     8  	jsoniter "github.com/json-iterator/go"
     9  )
    10  
    11  // MetaInput represents a target with metadata (TODO: replace with https://github.com/projectdiscovery/metainput)
    12  type MetaInput struct {
    13  	// Input represent the target
    14  	Input string `json:"input,omitempty"`
    15  	// CustomIP to use for connection
    16  	CustomIP string `json:"customIP,omitempty"`
    17  }
    18  
    19  func (metaInput *MetaInput) marshalToBuffer() (bytes.Buffer, error) {
    20  	var b bytes.Buffer
    21  	err := jsoniter.NewEncoder(&b).Encode(metaInput)
    22  	return b, err
    23  }
    24  
    25  // ID returns a unique id/hash for metainput
    26  func (metaInput *MetaInput) ID() string {
    27  	if metaInput.CustomIP != "" {
    28  		return fmt.Sprintf("%s-%s", metaInput.Input, metaInput.CustomIP)
    29  	}
    30  	return metaInput.Input
    31  }
    32  
    33  func (metaInput *MetaInput) MarshalString() (string, error) {
    34  	b, err := metaInput.marshalToBuffer()
    35  	return b.String(), err
    36  }
    37  
    38  func (metaInput *MetaInput) MustMarshalString() string {
    39  	marshaled, _ := metaInput.MarshalString()
    40  	return marshaled
    41  }
    42  
    43  func (metaInput *MetaInput) MarshalBytes() ([]byte, error) {
    44  	b, err := metaInput.marshalToBuffer()
    45  	return b.Bytes(), err
    46  }
    47  
    48  func (metaInput *MetaInput) MustMarshalBytes() []byte {
    49  	marshaled, _ := metaInput.MarshalBytes()
    50  	return marshaled
    51  }
    52  
    53  func (metaInput *MetaInput) Unmarshal(data string) error {
    54  	return jsoniter.NewDecoder(strings.NewReader(data)).Decode(metaInput)
    55  }
    56  
    57  func (metaInput *MetaInput) Clone() *MetaInput {
    58  	return &MetaInput{
    59  		Input:    metaInput.Input,
    60  		CustomIP: metaInput.CustomIP,
    61  	}
    62  }
    63  
    64  func (metaInput *MetaInput) PrettyPrint() string {
    65  	if metaInput.CustomIP != "" {
    66  		return fmt.Sprintf("%s [%s]", metaInput.Input, metaInput.CustomIP)
    67  	}
    68  	return metaInput.Input
    69  }