-
Notifications
You must be signed in to change notification settings - Fork 16
/
build_request.go
200 lines (177 loc) · 4.62 KB
/
build_request.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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: AGPL
package daemon
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/google/uuid"
"github.com/pb33f/wiretap/config"
"github.com/pb33f/wiretap/shared"
"github.com/pterm/pterm"
)
type HttpTransactionConfig struct {
OriginalRequest *http.Request
NewRequest *http.Request
ID *uuid.UUID
TransactionConfig *shared.WiretapConfiguration
}
func BuildHttpTransaction(build HttpTransactionConfig) *HttpTransaction {
var dropHeaders []string
var injectHeaders map[string]string
cf := build.TransactionConfig
// add global headers with injection.
if cf.Headers != nil {
dropHeaders = cf.Headers.DropHeaders
injectHeaders = cf.Headers.InjectHeaders
}
// now add path specific headers.
matchedPaths := config.FindPaths(build.OriginalRequest.URL.Path, cf)
auth := ""
if len(matchedPaths) > 0 {
for _, path := range matchedPaths {
auth = path.Auth
if path.Headers != nil {
dropHeaders = append(dropHeaders, path.Headers.DropHeaders...)
newInjectHeaders := path.Headers.InjectHeaders
for key := range injectHeaders {
newInjectHeaders[key] = injectHeaders[key]
}
injectHeaders = newInjectHeaders
}
break
}
}
newReq := CloneExistingRequest(CloneRequest{
Request: build.NewRequest,
Protocol: cf.RedirectProtocol,
Host: cf.RedirectHost,
Port: cf.RedirectPort,
DropHeaders: dropHeaders,
Auth: auth,
InjectHeaders: injectHeaders,
})
var requestBody []byte
headers := make(map[string]any)
for k, v := range newReq.Header {
headers[k] = v[0]
}
cookies := make(map[string]*HttpCookie)
for _, c := range newReq.Cookies() {
cookies[c.Name] = &HttpCookie{
Value: c.Value,
Path: c.Path,
Domain: c.Domain,
Expires: c.RawExpires,
MaxAge: c.MaxAge,
Secure: c.Secure,
HttpOnly: c.HttpOnly,
}
}
// check if request is a multipart form
if ct, ok := headers["Content-Type"].(string); ok {
if strings.Contains(ct, "multipart/form-data") {
err := newReq.ParseMultipartForm(32 << 2)
if err != nil {
pterm.Error.Println(err.Error())
}
}
}
if newReq.MultipartForm != nil {
var parts []FormPart
for i := range newReq.MultipartForm.Value {
parts = append(parts, FormPart{
Name: i,
Value: newReq.MultipartForm.Value[i],
})
}
for k, fHeaders := range newReq.MultipartForm.File {
var formFiles []*FormFile
for z := range fHeaders {
ff := &FormFile{
Name: fHeaders[z].Filename,
Headers: fHeaders[z].Header,
}
formFiles = append(formFiles, ff)
}
parts = append(parts, FormPart{
Name: k,
Files: formFiles,
})
}
requestBody, _ = json.Marshal(parts)
} else {
requestBody, _ = io.ReadAll(newReq.Body)
}
replaced := config.RewritePath(build.NewRequest.URL.Path, cf)
var newUrl = build.NewRequest.URL
if replaced != "" {
var e error
newUrl, e = url.Parse(replaced)
if e != nil {
newUrl = build.NewRequest.URL
pterm.Error.Printf("major configuration problem: cannot parse URL: `%s`: %s", replaced, e.Error())
}
if build.NewRequest.URL.RawQuery != "" {
newUrl.RawQuery = build.NewRequest.URL.RawQuery
}
}
return &HttpTransaction{
Id: build.ID.String(),
Request: &HttpRequest{
URL: newUrl.String(),
Method: build.NewRequest.Method,
Path: newUrl.Path,
Host: newUrl.Host,
Query: newUrl.RawQuery,
DroppedHeaders: dropHeaders,
InjectedHeaders: injectHeaders,
OriginalPath: build.NewRequest.URL.Path,
Cookies: cookies,
Headers: headers,
Body: string(requestBody),
Timestamp: time.Now().UnixMilli(),
},
}
}
func ReconstructURL(r *http.Request, protocol, host, basepath string, port string) string {
if host == "" {
host = r.Host
}
if protocol == "" {
protocol = "http"
}
url := fmt.Sprintf("%s://%s", protocol, host)
if port != "" {
url += fmt.Sprintf(":%s", port)
}
if basepath != "" {
url += basepath
}
if r.URL.Path != "" {
url += r.URL.Path
}
if r.URL.RawQuery != "" {
url += fmt.Sprintf("?%s", r.URL.RawQuery)
}
return url
}
func ExtractHeaders(resp *http.Response) map[string]any {
headers := make(map[string]any)
for k, v := range resp.Header {
headers[k] = v[0]
}
return headers
}
func ReplaceWithVariables(variables map[string]*shared.CompiledVariable, input string) string {
for x := range variables {
if variables[x] != nil {
input = variables[x].CompiledVariable.ReplaceAllString(input, variables[x].VariableValue)
}
}
return input
}