-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
125 lines (115 loc) · 2.89 KB
/
config.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
package main
import (
"io/ioutil"
"log"
"reflect"
"regexp"
"strconv"
"strings"
gcfg "gopkg.in/gcfg.v1"
)
type Config struct {
Search struct {
CommandGrep string
SessionKeyGrep string
SessionCookieName string
}
Version struct {
ConfigVersion string
}
Command map[string]*struct {
ReqUrl string
ReqContentType string
ReqType string
ReqBody string
ReqUpload string
EncryptStartByte string
EncryptNumBytes string
EncryptKey string
EncryptIv string
DoCall string
MsecDelay string
MsecRepeat string
Md5Input string
PKSInput string
Base64Input string
MustCapture string
ResponseCode string
ReqHeaders []string
SessionVar []string
FuncVar []string
//GrepVar []string
}
CommandSequence struct {
Sequence string
SessionLog string
}
UserFunctions struct {
Import []string
}
FileName string
}
func NewConfig(configFile string) *Config {
toReturn := &Config{FileName: configFile}
bytes, err1 := ioutil.ReadFile(configFile)
equalLine := regexp.MustCompile("=")
openQuote := regexp.MustCompile("=[\t ]+")
closeQuote := regexp.MustCompile("$") // (?m) is a flag that says to treat as multi-line strings (so that $ will match EOL instead of just EOF)
output := ""
str := strings.Split(string(bytes), "\n")
for _, line := range str {
if equalLine.MatchString(line) {
line = strings.Replace(line, "\"", "\\\"", -1)
line = openQuote.ReplaceAllString(line, "= \"")
line = closeQuote.ReplaceAllString(line, "\"")
}
output += line + "\n"
}
if err1 != nil {
log.Fatalf("Failed to open %s file. %s", configFile, err1)
}
err2 := gcfg.ReadStringInto(toReturn, output)
if err2 != nil {
log.Fatalf("Failed to parse gcfg data: %s", err2)
}
return toReturn
}
func (config *Config) FieldString(field string, command string) string {
data := ""
r := reflect.ValueOf(config.Command[command])
if !reflect.Indirect(r).IsValid() {
r = reflect.ValueOf(config.Command["default"])
}
if reflect.Indirect(r).IsValid() {
f := reflect.Indirect(r).FieldByName(field)
if f.String() != "" {
data = f.String()
} else {
r := reflect.ValueOf(config.Command["default"])
if reflect.Indirect(r).IsValid() {
f := reflect.Indirect(r).FieldByName(field)
data = f.String()
}
}
}
return data
}
func (config *Config) FieldInteger(field string, command string) int {
str := config.FieldString(field, command)
toReturn, err := strconv.Atoi(str)
if err != nil {
return 0
}
return toReturn
}
func (config *Config) MustCaptureElement(element string, command string) bool {
var mustCapture bool = false
str := config.FieldString("MustCapture", command)
for _, elem := range strings.Fields(strings.Replace(str, ",", " ", -1)) {
if elem == element {
mustCapture = true
break
}
}
return mustCapture
}