forked from pagpeter/TrackMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
executable file
·202 lines (174 loc) · 3.98 KB
/
utils.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"crypto/md5"
"encoding/hex"
"io/ioutil"
"log"
"net/url"
"os"
"sort"
"strings"
"golang.org/x/net/http2"
)
var dataFrameFlags = map[http2.Flags]string{
http2.FlagDataEndStream: "EndStream (0x1)",
http2.FlagDataPadded: "Padded (0x8)",
}
var pushFrameFlags = map[http2.Flags]string{
http2.FlagPushPromiseEndHeaders: "EndHeaders (0x4)",
http2.FlagPushPromisePadded: "Padded (0x8)",
}
var headersFrameFlags = map[http2.Flags]string{
http2.FlagHeadersEndStream: "EndStream (0x1)",
http2.FlagHeadersEndHeaders: "EndHeaders (0x4)",
http2.FlagHeadersPadded: "Padded (0x8)",
http2.FlagHeadersPriority: "Priority (0x20)",
}
func GetAllFlags(frame http2.Frame) []string {
var flagsArray []string
flags := frame.Header().Flags
switch frame.(type) {
case *http2.SettingsFrame:
if flags.Has(http2.FlagSettingsAck) {
flagsArray = append(flagsArray, "Ack (0x1)")
}
case *http2.HeadersFrame:
for _, key := range getKeysInOrder(headersFrameFlags) {
if flags.Has(key) {
flagsArray = append(flagsArray, headersFrameFlags[key])
}
}
case *http2.DataFrame:
for _, key := range getKeysInOrder(dataFrameFlags) {
if flags.Has(key) {
flagsArray = append(flagsArray, dataFrameFlags[key])
}
}
case *http2.PingFrame:
if flags.Has(http2.FlagPingAck) {
flagsArray = append(flagsArray, "Ack (0x1)")
}
case *http2.ContinuationFrame:
if flags.Has(http2.FlagContinuationEndHeaders) {
flagsArray = append(flagsArray, "EndHeaders (0x4)")
}
case *http2.PushPromiseFrame:
for _, key := range getKeysInOrder(pushFrameFlags) {
if flags.Has(key) {
flagsArray = append(flagsArray, pushFrameFlags[key])
}
}
}
return flagsArray
}
func GetUserAgent(res Response) string {
var headers []string
var ua string
if res.HTTPVersion == "h2" {
return res.UserAgent
} else {
if res.Http1 == nil {
return ""
}
headers = res.Http1.Headers
}
for _, header := range headers {
lower := strings.ToLower(header)
if strings.HasPrefix(lower, "user-agent: ") {
ua = strings.Split(header, ": ")[1]
}
}
return ua
}
func GetMD5Hash(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}
func ReadFile(filename string) ([]byte, error) {
body, err := ioutil.ReadFile(filename)
if err != nil {
log.Println("unable to read file: %v", err)
return nil, err
}
return body, nil
}
func WriteToFile(filename string, data []byte) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return err
}
return nil
}
func IsIPBlocked(ip string) bool {
rawIPs, err := ReadFile("blockedIPs")
if err != nil {
WriteToFile("blockedIPs", []byte(""))
return false
}
ips := strings.Split(string(rawIPs), "\n")
for _, i := range ips {
if ip == i {
return true
}
}
return false
}
func getKeysInOrder(m map[http2.Flags]string) []http2.Flags {
keys := make([]http2.Flags, 0)
for k, _ := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
return keys
}
func splitBytesIntoChunks(buf []byte, lim int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/lim+1)
for len(buf) >= lim {
chunk, buf = buf[:lim], buf[lim:]
chunks = append(chunks, chunk)
}
if len(buf) > 0 {
chunks = append(chunks, buf[:len(buf)])
}
return chunks
}
type kv struct {
Key string
Value int
}
func sortByVal(m map[string]int, x int) map[string]int {
// Turning the map into this structure
var ss []kv
for k, v := range m {
ss = append(ss, kv{k, v})
}
// Then sorting the slice by value, higher first.
sort.Slice(ss, func(i, j int) bool {
return ss[i].Value > ss[j].Value
})
res := map[string]int{}
tmp := []kv{}
if len(ss) > x {
tmp = ss[:x]
} else {
tmp = ss
}
for _, obj := range tmp {
res[obj.Key] = obj.Value
}
return res
}
func getParam(key string, m url.Values) string {
if val, ok := m["by"]; ok {
if len(val) != 0 {
return val[0]
}
}
return ""
}