-
Notifications
You must be signed in to change notification settings - Fork 46
/
former.go
74 lines (62 loc) · 1.38 KB
/
former.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"math/rand"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
type htmlForm struct {
Action string
Method string
Values url.Values
}
func parseForms(node *html.Node) (forms []htmlForm) {
if node == nil {
return nil
}
doc := goquery.NewDocumentFromNode(node)
doc.Find("form").Each(func(_ int, s *goquery.Selection) {
form := htmlForm{Values: url.Values{}}
form.Action, _ = s.Attr("action")
form.Method, _ = s.Attr("method")
s.Find("input").Each(func(_ int, s *goquery.Selection) {
name, _ := s.Attr("name")
if name == "" {
return
}
typ, _ := s.Attr("type")
typ = strings.ToLower(typ)
_, checked := s.Attr("checked")
if (typ == "radio" || typ == "checkbox") && !checked {
return
}
value, _ := s.Attr("value")
form.Values.Add(name, value)
})
s.Find("textarea").Each(func(_ int, s *goquery.Selection) {
name, _ := s.Attr("name")
if name == "" {
return
}
value := s.Text()
form.Values.Add(name, value)
})
forms = append(forms, form)
})
return forms
}
func randStringGen(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = BYTES[rand.Intn(len(BYTES))]
}
return string(b)
}
func setValues(form *url.Values) map[string]string {
dcombo := make(map[string]string)
for key := range *form {
dcombo[key] = randStringGen(baseFormLen)
}
return dcombo
}