Debian Codegraph @

pkg/scraper/scraper.go

raw symbols SCIP
1 package scraperscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/
2
3 import (
4 "encoding/jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/"
5 "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/"
6 "osscip-go gomod github.com/golang/go/src go1.18 os/"
7 "path/filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/"
8 "regexpscip-go gomod github.com/golang/go/src go1.18 regexp/"
9 "stringsscip-go gomod github.com/golang/go/src go1.18 strings/"
10
11 "github.com/ffuf/ffuf/v2/pkg/ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/"
12
13 "github.com/PuerkitoBio/goqueryscip-go gomod github.com/PuerkitoBio/goquery v1.8.0 `github.com/PuerkitoBio/goquery`/"
14 )
15
16 type ScraperRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule# struct {
17 Namescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Name. string `json:"name"`
18 Rulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Rule. string `json:"rule"`
19 Targetscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Target. string `json:"target"`
20 compiledRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#compiledRule. *regexpscip-go gomod github.com/golang/go/src go1.18 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.18 regexp/Regexp#
21 Typescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Type. string `json:"type"`
22 OnlyMatchedscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#OnlyMatched. bool `json:"onlymatched"`
23 Actionscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Action. []string `json:"action"`
24 }
25
26 type ScraperGroupscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup# struct {
27 Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#Rules. []*ScraperRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule# `json:"rules"`
28 Namescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#Name. string `json:"groupname"`
29 Activescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#Active. bool `json:"active"`
30 }
31
32 type Scraperscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper# struct {
33 Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#Rules. []*ScraperRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#
34 }
35
36 func readGroupFromFilescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/readGroupFromFile().(filename string) (ScraperGroupscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#, error) {
37 data, err := osscip-go gomod github.com/golang/go/src go1.18 os/.ReadFilescip-go gomod github.com/golang/go/src go1.18 os/ReadFile().(filename)
38 if err != nil {
39 return ScraperGroupscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#{Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#Rules.: make([]*ScraperRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#, 0)}, err
40 }
41 sc := ScraperGroupscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#{}
42 err = jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Unmarshal().([]byte(data), &sc)
43 return sc, err
44 }
45
46 func FromDirscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/FromDir().(dirname string, activestr string) (ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.Scraperscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Scraper#, ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.Multierrorscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Multierror#) {
47 scr := Scraperscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#{Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#Rules.: make([]*ScraperRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#, 0)}
48 errs := ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.NewMultierrorscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/NewMultierror().()
49 activegrps := parseActiveGroupsscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/parseActiveGroups().(activestr)
50 all_files, err := osscip-go gomod github.com/golang/go/src go1.18 os/.ReadDirscip-go gomod github.com/golang/go/src go1.18 os/ReadDir().(ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.SCRAPERDIRscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/SCRAPERDIR.)
51 if err != nil {
52 errs.Addscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Multierror#Add().(err)
53 return &scr, errs
54 }
55 for _, filename := range all_files {
56 if filename.Typescip-go gomod github.com/golang/go/src go1.18 `io/fs`/DirEntry#Type().().IsRegularscip-go gomod github.com/golang/go/src go1.18 `io/fs`/FileMode#IsRegular().() && stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.18 strings/HasSuffix().(filename.Namescip-go gomod github.com/golang/go/src go1.18 `io/fs`/DirEntry#Name().(), ".json") {
57 sg, err := readGroupFromFilescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/readGroupFromFile().(filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(dirname, filename.Namescip-go gomod github.com/golang/go/src go1.18 `io/fs`/DirEntry#Name().()))
58 if err != nil {
59 cerr := fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("%s : %s", filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(dirname, filename.Namescip-go gomod github.com/golang/go/src go1.18 `io/fs`/DirEntry#Name().()), err)
60 errs.Addscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Multierror#Add().(cerr)
61 continue
62 }
63 if (sg.Activescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#Active. && isActivescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/isActive().("all", activegrps)) || isActivescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/isActive().(sg.Namescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#Name., activegrps) {
64 for _, r := range sg.Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#Rules. {
65 err = r.initscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#init().()
66 if err != nil {
67 cerr := fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("%s : %s", filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(dirname, filename.Namescip-go gomod github.com/golang/go/src go1.18 `io/fs`/DirEntry#Name().()), err)
68 errs.Addscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Multierror#Add().(cerr)
69 continue
70 }
71 scr.Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#Rules. = append(scr.Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#Rules., r)
72 }
73 }
74 }
75 }
76 return &scr, errs
77 }
78
79 // FromFile initializes a scraper instance and reads rules from a file
80 func (s *Scraperscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#) AppendFromFilescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#AppendFromFile().(path string) error {
81 sg, err := readGroupFromFilescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/readGroupFromFile().(path)
82 if err != nil {
83 return err
84 }
85
86 for _, r := range sg.Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperGroup#Rules. {
87 err = r.initscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#init().()
88 if err != nil {
89 continue
90 }
91 s.Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#Rules. = append(s.Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#Rules., r)
92 }
93
94 return err
95 }
96
97 func (s *Scraperscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#) Executescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#Execute().(resp *ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.Responsescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#, matched bool) []ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.ScraperResultscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/ScraperResult# {
98 res := make([]ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.ScraperResultscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/ScraperResult#, 0)
99 for _, rule := range s.Rulesscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/Scraper#Rules. {
100 if !matched && rule.OnlyMatchedscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#OnlyMatched. {
101 // pass this rule as there was no match
102 continue
103 }
104 sourceData := ""
105 if rule.Targetscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Target. == "body" {
106 sourceData = string(resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data.)
107 } else if rule.Targetscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Target. == "headers" {
108 sourceData = headerStringscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/headerString().(resp.Headersscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Headers.)
109 } else {
110 sourceData = headerStringscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/headerString().(resp.Headersscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Headers.) + string(resp.Datascip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/Response#Data.)
111 }
112 val := rule.Checkscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Check().(sourceData)
113 if len(val) > 0 {
114 res = append(res, ffufscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/.ScraperResultscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/ScraperResult#{
115 Namescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/ScraperResult#Name.: rule.Namescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Name.,
116 Typescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/ScraperResult#Type.: rule.Typescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Type.,
117 Actionscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/ScraperResult#Action.: rule.Actionscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Action.,
118 Resultsscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/ffuf`/ScraperResult#Results.: val,
119 })
120 }
121 }
122 return res
123 }
124
125 // init initializes the scraper rule, and returns an error in case there's an error in the syntax
126 func (r *ScraperRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#) initscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#init().() error {
127 var err error
128 if r.Typescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Type. == "regexp" {
129 r.compiledRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#compiledRule., err = regexpscip-go gomod github.com/golang/go/src go1.18 regexp/.Compilescip-go gomod github.com/golang/go/src go1.18 regexp/Compile().(r.Rulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Rule.)
130 if err != nil {
131 return err
132 }
133 }
134 return err
135 }
136
137 func (r *ScraperRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#) Checkscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Check().(data string) []string {
138 if r.Typescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Type. == "regexp" {
139 return r.checkRegexpscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#checkRegexp().(data)
140 } else if r.Typescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Type. == "query" {
141 return r.checkQueryscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#checkQuery().(data)
142 }
143 return []string{}
144 }
145
146 func (r *ScraperRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#) checkQueryscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#checkQuery().(data string) []string {
147 val := make([]string, 0)
148 doc, err := goqueryscip-go gomod github.com/PuerkitoBio/goquery v1.8.0 `github.com/PuerkitoBio/goquery`/.NewDocumentFromReaderscip-go gomod github.com/PuerkitoBio/goquery v1.8.0 `github.com/PuerkitoBio/goquery`/NewDocumentFromReader().(stringsscip-go gomod github.com/golang/go/src go1.18 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.18 strings/NewReader().(data))
149 if err != nil {
150 return []string{}
151 }
152 doc.Findscip-go gomod github.com/PuerkitoBio/goquery v1.8.0 `github.com/PuerkitoBio/goquery`/Selection#Find().(r.Rulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#Rule.).Eachscip-go gomod github.com/PuerkitoBio/goquery v1.8.0 `github.com/PuerkitoBio/goquery`/Selection#Each().(func(i int, sel *goqueryscip-go gomod github.com/PuerkitoBio/goquery v1.8.0 `github.com/PuerkitoBio/goquery`/.Selectionscip-go gomod github.com/PuerkitoBio/goquery v1.8.0 `github.com/PuerkitoBio/goquery`/Selection#) {
153 val = append(val, sel.Textscip-go gomod github.com/PuerkitoBio/goquery v1.8.0 `github.com/PuerkitoBio/goquery`/Selection#Text().())
154 })
155 return val
156 }
157
158 func (r *ScraperRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#) checkRegexpscip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#checkRegexp().(data string) []string {
159 val := make([]string, 0)
160 if r.compiledRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#compiledRule. != nil {
161 res := r.compiledRulescip-go gomod github.com/ffuf/ffuf/v2 . `github.com/ffuf/ffuf/v2/pkg/scraper`/ScraperRule#compiledRule..FindAllStringSubmatchscip-go gomod github.com/golang/go/src go1.18 regexp/Regexp#FindAllStringSubmatch().(data, -1)
162 for _, grp := range res {
163 val = append(val, grp...)
164 }
165 return val
166 }
167 return []string{}
168 }