-
Notifications
You must be signed in to change notification settings - Fork 16
/
tool.go
88 lines (80 loc) · 1.89 KB
/
tool.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gospider
import (
"crypto/md5"
"github.com/zhshch2002/goreq"
"io/ioutil"
slog "log"
"net/url"
"os"
"runtime"
"sort"
"strings"
)
var log *slog.Logger
func init() {
log = slog.New(os.Stdout, "GoSpider|", slog.Lmicroseconds|slog.Lshortfile)
}
func SprintStack() string {
var buf [4096]byte
n := runtime.Stack(buf[:], false)
return string(buf[:n])
}
// GetRequestHash return a hash of url,header,cookie and body data from a request
func GetRequestHash(r *goreq.Request) [md5.Size]byte {
u := r.URL
UrtStr := u.Scheme + "://"
if u.User != nil {
UrtStr += u.User.String() + "@"
}
UrtStr += strings.ToLower(u.Host)
path := u.EscapedPath()
if path != "" && path[0] != '/' {
UrtStr += "/"
}
UrtStr += path
if u.RawQuery != "" {
QueryParam := u.Query()
var QueryK []string
for k := range QueryParam {
QueryK = append(QueryK, k)
}
sort.Strings(QueryK)
var QueryStrList []string
for _, k := range QueryK {
val := QueryParam[k]
sort.Strings(val)
for _, v := range val {
QueryStrList = append(QueryStrList, url.QueryEscape(k)+"="+url.QueryEscape(v))
}
}
UrtStr += "?" + strings.Join(QueryStrList, "&")
}
Header := r.Header
var HeaderK []string
for k := range Header {
HeaderK = append(HeaderK, k)
}
sort.Strings(HeaderK)
var HeaderStrList []string
for _, k := range HeaderK {
val := Header[k]
sort.Strings(val)
for _, v := range val {
HeaderStrList = append(HeaderStrList, url.QueryEscape(k)+"="+url.QueryEscape(v))
}
}
HeaderStr := strings.Join(HeaderStrList, "&")
var Cookie []string
for _, i := range r.Cookies() {
Cookie = append(Cookie, i.Name+"="+i.Value)
}
CookieStr := strings.Join(Cookie, "&")
data := []byte(strings.Join([]string{UrtStr, HeaderStr, CookieStr}, "@#@"))
if br, err := r.GetBody(); err == nil {
if b, err := ioutil.ReadAll(br); err == nil {
data = append(data, b...)
}
}
has := md5.Sum(data)
return has
}