github.com/projectdiscovery/nuclei/v2@v2.9.15/internal/runner/nucleicloud/utils.go (about)

     1  package nucleicloud
     2  
     3  import (
     4  	"bufio"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/projectdiscovery/nuclei/v2/pkg/catalog/config"
    11  )
    12  
    13  const DDMMYYYYhhmmss = "2006-01-02 15:04:05"
    14  
    15  // ReadCatalogChecksum reads catalog checksum from nuclei-templates repository
    16  func ReadCatalogChecksum() map[string]string {
    17  	config := config.DefaultConfig
    18  
    19  	checksumFile := filepath.Join(config.TemplatesDirectory, "templates-checksum.txt")
    20  	file, err := os.Open(checksumFile)
    21  	if err != nil {
    22  		return nil
    23  	}
    24  	defer file.Close()
    25  
    26  	checksums := make(map[string]string)
    27  	scanner := bufio.NewScanner(file)
    28  	for scanner.Scan() {
    29  		text := strings.SplitN(scanner.Text(), ":", 2)
    30  		if len(text) < 2 {
    31  			continue
    32  		}
    33  		path := strings.TrimPrefix(text[0], "nuclei-templates/")
    34  		if strings.HasPrefix(path, ".") {
    35  			continue
    36  		}
    37  		checksums[path] = text[1]
    38  	}
    39  	return checksums
    40  }
    41  
    42  func PrepareScanListOutput(v GetScanRequest) ListScanOutput {
    43  	output := ListScanOutput{}
    44  	loc, _ := time.LoadLocation("Local")
    45  	status := "finished"
    46  
    47  	t := v.FinishedAt
    48  	duration := t.Sub(v.CreatedAt)
    49  
    50  	if !v.Finished {
    51  		status = "running"
    52  		t = time.Now().UTC()
    53  		duration = t.Sub(v.CreatedAt).Round(60 * time.Second)
    54  	}
    55  
    56  	val := v.CreatedAt.In(loc).Format(DDMMYYYYhhmmss)
    57  
    58  	output.Timestamp = val
    59  	output.ScanID = v.Id
    60  	output.ScanTime = duration.String()
    61  	output.ScanResult = int(v.Matches)
    62  	output.ScanStatus = status
    63  	output.Target = int(v.Targets)
    64  	output.Template = int(v.Templates)
    65  	return output
    66  }