-
Notifications
You must be signed in to change notification settings - Fork 43
/
hijack.go
150 lines (134 loc) · 3.85 KB
/
hijack.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
package simple
import (
"github.com/go-rod/rod"
"github.com/yaklang/yaklang/common/utils"
"regexp"
"strings"
)
type ModifyTarget string
var HeadersModifyTarget ModifyTarget = "headers"
var BodyModifyTarget ModifyTarget = "body"
var BodyReplaceTarget ModifyTarget = "bodyReplace"
var HostModifyTarget ModifyTarget = "host"
type baseModify struct {
modifyUrl string
modifyReg *regexp.Regexp
modifyTarget ModifyTarget
modifyResult interface{}
}
type ResponseModification struct {
baseModify
}
func (modification *baseModify) Generate() error {
reg, err := regexp.Compile(modification.modifyUrl)
if err != nil {
return utils.Errorf("regexp compile %s error: %s", modification.modifyUrl, err)
}
modification.modifyReg = reg
return nil
}
func (modification *baseModify) GetReg() *regexp.Regexp {
if modification.modifyReg == nil {
modification.Generate()
}
return modification.modifyReg
}
func (modification *ResponseModification) Modify(response *rod.HijackResponse) error {
switch modification.modifyTarget {
case BodyModifyTarget:
return responseBodyModify(response, modification.modifyResult)
case HeadersModifyTarget:
return responseHeadersModify(response, modification.modifyResult)
case BodyReplaceTarget:
return responseBodyReplace(response, modification.modifyResult)
default:
return utils.Errorf("error response modify target.")
}
}
func responseBodyModify(response *rod.HijackResponse, result interface{}) error {
switch result.(type) {
case string:
bodyStr := result.(string)
response.SetBody(bodyStr)
return nil
default:
return utils.Errorf("body type error.")
}
}
func responseBodyReplace(response *rod.HijackResponse, result interface{}) error {
originBody := response.Body()
switch result.(type) {
case []string:
bodyList := result.([]string)
for count := 0; count < len(bodyList)-1; count += 2 {
originBody = strings.Replace(originBody, bodyList[count], bodyList[count+1], -1)
}
response.SetBody(originBody)
return nil
default:
return utils.Errorf("body replace data type error.")
}
}
func responseHeadersModify(response *rod.HijackResponse, result interface{}) error {
switch result.(type) {
case map[string]string:
strMap := result.(map[string]string)
strList := make([]string, len(strMap)*2)
for k, v := range strMap {
strList = append(strList, k, v)
}
response.SetHeader(strList...)
case []string:
strList := result.([]string)
response.SetHeader(strList...)
default:
return utils.Errorf("headers type error.")
}
return nil
}
type RequestModification struct {
baseModify
}
func (modification *RequestModification) Modify(request *rod.HijackRequest) error {
switch modification.modifyTarget {
case HeadersModifyTarget:
return requestHeadersModify(request, modification.modifyResult)
case HostModifyTarget:
return requestHostModify(request, modification.modifyResult)
case BodyModifyTarget:
return requestBodyModify(request, modification.modifyResult)
default:
return utils.Errorf("error request modify target.")
}
}
func requestHeadersModify(request *rod.HijackRequest, result interface{}) error {
switch result.(type) {
case []string:
headersList := result.([]string)
for count := 0; count < len(headersList)-1; count += 2 {
request.Req().Header.Add(headersList[count], headersList[count+1])
}
case map[string]string:
headersMap := result.(map[string]string)
for k, v := range headersMap {
request.Req().Header.Set(k, v)
}
default:
return utils.Errorf("request headers data type error")
}
return nil
}
func requestHostModify(request *rod.HijackRequest, result interface{}) error {
switch result.(type) {
case string:
host := result.(string)
request.Req().Host = host
default:
return utils.Errorf("request host data type error")
}
return nil
}
func requestBodyModify(request *rod.HijackRequest, result interface{}) error {
request.SetBody(result)
return nil
}