forked from dzonerzy/goWAPT
/
utils.go
111 lines (97 loc) · 2.33 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
package main
import (
"io/ioutil"
"net/http"
"net/http/httputil"
"os"
"strconv"
"strings"
"github.com/nsf/termbox-go"
)
func checkSize() (int, int, bool) {
width, height := termbox.Size()
if (width < 105) || (height < 23) {
return width, height, false
}
return width, height, true
}
func cleanAndExit() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
termbox.Close()
os.Exit(0)
}
type callbackMethod func(int) interface{}
type Callback struct {
cmd int
action callbackMethod
}
var callbacks = map[string][]Callback{}
func addCallbackMenu(menuName string, charcmd int, callback callbackMethod) {
callbacks[menuName] = append(callbacks[menuName], Callback{cmd: charcmd, action: callback})
}
func haveCallbackDefined(menuName string, charcmd int) bool {
for _, callback := range callbacks[menuName] {
if callback.cmd == charcmd || callback.cmd == CALLBACK_EVERY_KEY {
return true
}
}
return false
}
func checkCallback(menuName string, cmd int) interface{} {
for _, callback := range callbacks[menuName] {
if callback.cmd == cmd || callback.cmd == CALLBACK_EVERY_KEY {
return callback.action(cmd)
}
}
return nil
}
func formatConfigOption(s string) string {
if s == "" {
return "<None>"
} else {
return s
}
}
func resetCallbacks(menuName string) {
callbacks[menuName] = []Callback{}
}
func splitToFill(s string, n int) []string {
var pieces []string
var tmp string
var count int = 0
for _, element := range s {
count++
tmp += string(element)
if strings.Contains(tmp, "\n") {
count = 0
pieces = append(pieces, tmp)
tmp = ""
}
if count == n {
count = 0
pieces = append(pieces, tmp)
tmp = ""
}
}
pieces = append(pieces, tmp)
return pieces
}
func response2String(response *http.Response) []byte {
var reqBodyBytes []byte
respBodyBytes, _ := ioutil.ReadAll(response.Body)
cl, _ := strconv.Atoi(response.Request.Header.Get("Content-Length"))
if cl > 0 {
reqBodyBytes, _ = ioutil.ReadAll(response.Request.Body)
} else {
reqBodyBytes = []byte{}
}
s_req, _ := httputil.DumpRequest(response.Request, true)
s_res, _ := httputil.DumpResponse(response, false)
var full []byte
full = append(full, s_req...)
full = append(full, reqBodyBytes...)
full = append(full, []byte{0x0a, 0x0a, 0x0a}...)
full = append(full, s_res...)
full = append(full, respBodyBytes...)
return full
}