-
Notifications
You must be signed in to change notification settings - Fork 97
/
proxy.go
373 lines (324 loc) · 11 KB
/
proxy.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package libproxy
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/google/uuid"
)
type statusChangeFunction func(status string, isListening bool)
var (
accessToken string
sessionFingerprint string
allowedOrigins []string
bannedOutputs []string
bannedDests []string
)
type Request struct {
AccessToken string
WantsBinary bool
Method string
Url string
Auth struct {
Username string
Password string
}
Headers map[string]string
Data string
Params map[string]string
}
type Response struct {
Success bool `json:"success"`
IsBinary bool `json:"isBinary"`
Status int `json:"status"`
Data string `json:"data"`
StatusText string `json:"statusText"`
Headers map[string]string `json:"headers"`
}
func isAllowedDest(dest string) bool {
for _, b := range bannedDests {
if b == dest {
return false
}
}
return true
}
func isAllowedOrigin(origin string) bool {
if allowedOrigins[0] == "*" {
return true
}
for _, b := range allowedOrigins {
if b == origin {
return true
}
}
return false
}
func Initialize(
initialAccessToken string,
proxyURL string,
initialAllowedOrigins string,
initialBannedOutputs string,
initialBannedDests string,
onStatusChange statusChangeFunction,
withSSL bool,
finished chan bool,
) {
if initialBannedOutputs != "" {
bannedOutputs = strings.Split(initialBannedOutputs, ",")
}
if initialBannedDests != "" {
bannedDests = strings.Split(initialBannedDests, ",")
} else {
bannedDests = []string{}
}
allowedOrigins = strings.Split(initialAllowedOrigins, ",")
accessToken = initialAccessToken
sessionFingerprint = uuid.New().String()
log.Println("Starting proxy server...")
http.HandleFunc("/", proxyHandler)
if !withSSL {
go func() {
httpServerError := http.ListenAndServe(proxyURL, nil)
if httpServerError != nil {
onStatusChange("An error occurred: "+httpServerError.Error(), false)
}
finished <- true
}()
onStatusChange("Listening on http://"+proxyURL+"/", true)
} else {
onStatusChange("Checking SSL certificate...", false)
err := EnsurePrivateKeyInstalled()
if err != nil {
log.Println(err.Error())
onStatusChange("An error occurred.", false)
}
go func() {
httpServerError := http.ListenAndServeTLS(proxyURL, GetOrCreateDataPath()+"/cert.pem", GetOrCreateDataPath()+"/key.pem", nil)
if httpServerError != nil {
onStatusChange("An error occurred.", false)
}
}()
onStatusChange("Listening on https://"+proxyURL+"/", true)
log.Println("Proxy server listening on https://" + proxyURL + "/")
}
}
func GetAccessToken() string {
return accessToken
}
func SetAccessToken(newAccessToken string) {
accessToken = newAccessToken
}
const ErrorBodyInvalidRequest = "{\"success\": false, \"data\":{\"message\":\"(Proxy Error) Invalid request.\"}}"
const ErrorBodyProxyRequestFailed = "{\"success\": false, \"data\":{\"message\":\"(Proxy Error) Request failed.\"}}"
const maxMemory = int64(32 << 20) // multipartRequestDataKey currently its 32 MB
func proxyHandler(response http.ResponseWriter, request *http.Request) {
// We want to allow all types of requests to the proxy, though we only want to allow certain
// origins.
response.Header().Add("Access-Control-Allow-Headers", "*")
if request.Method == "OPTIONS" {
response.Header().Add("Access-Control-Allow-Origin", "*")
response.WriteHeader(200)
return
}
if request.Header.Get("Origin") == "" || !isAllowedOrigin(request.Header.Get("Origin")) {
if strings.HasPrefix(request.Header.Get("Content-Type"), "application/json") {
response.Header().Add("Access-Control-Allow-Headers", "*")
response.Header().Add("Access-Control-Allow-Origin", "*")
response.WriteHeader(200)
_, _ = fmt.Fprintln(response, ErrorBodyProxyRequestFailed)
return
}
// If it is not an allowed origin, redirect back to hoppscotch.io.
response.Header().Add("Location", "https://hoppscotch.io/")
response.WriteHeader(301)
return
} else {
// Otherwise set the appropriate CORS policy and continue.
response.Header().Add("Access-Control-Allow-Origin", request.Header.Get("Origin"))
}
// For anything other than an POST request, we'll return an empty JSON object.
response.Header().Add("Content-Type", "application/json; charset=utf-8")
if request.Method != "POST" {
_, _ = fmt.Fprintln(response, "{\"success\": true, \"data\":{\"sessionFingerprint\":\""+sessionFingerprint+"\", \"isProtected\":"+strconv.FormatBool(len(accessToken) > 0)+"}}")
return
}
// Attempt to parse request body.
var requestData Request
isMultipart := strings.HasPrefix(request.Header.Get("content-type"), "multipart/form-data")
var multipartRequestDataKey = request.Header.Get("multipart-part-key")
if multipartRequestDataKey == "" {
multipartRequestDataKey = "proxyRequestData"
}
if isMultipart {
var err = request.ParseMultipartForm(maxMemory)
if err != nil {
log.Printf("Failed to parse request body: %v", err)
_, _ = fmt.Fprintln(response, ErrorBodyInvalidRequest)
return
}
r := request.MultipartForm.Value[multipartRequestDataKey]
err = json.Unmarshal([]byte(r[0]), &requestData)
if err != nil || len(requestData.Url) == 0 || len(requestData.Method) == 0 {
// If the logged err is nil here, it means either the URL or method were not supplied
// in the request data.
log.Printf("Failed to parse request body: %v", err)
_, _ = fmt.Fprintln(response, ErrorBodyInvalidRequest)
return
}
} else {
var err = json.NewDecoder(request.Body).Decode(&requestData)
if err != nil || len(requestData.Url) == 0 || len(requestData.Method) == 0 {
// If the logged err is nil here, it means either the URL or method were not supplied
// in the request data.
log.Printf("Failed to parse request body: %v", err)
_, _ = fmt.Fprintln(response, ErrorBodyInvalidRequest)
return
}
}
if len(accessToken) > 0 && requestData.AccessToken != accessToken {
log.Print("An unauthorized request was made.")
_, _ = fmt.Fprintln(response, "{\"success\": false, \"data\":{\"message\":\"(Proxy Error) Unauthorized request; you may need to set your access token in Settings.\"}}")
return
}
// Make the request
var proxyRequest http.Request
proxyRequest.Header = make(http.Header)
proxyRequest.Method = requestData.Method
proxyRequest.URL, _ = url.Parse(requestData.Url)
// Block requests to illegal destinations
if !isAllowedDest(proxyRequest.URL.Hostname()) {
log.Print("A request to a banned destination was made.")
_, _ = fmt.Fprintln(response, "{\"success\": false, \"data\":{\"message\":\"(Proxy Error) Request cannot be to this destination.\"}}")
return
}
var params = proxyRequest.URL.Query()
for k, v := range requestData.Params {
params.Set(k, v)
}
proxyRequest.URL.RawQuery = params.Encode()
if len(requestData.Auth.Username) > 0 && len(requestData.Auth.Password) > 0 {
proxyRequest.SetBasicAuth(requestData.Auth.Username, requestData.Auth.Password)
}
for k, v := range requestData.Headers {
proxyRequest.Header.Set(k, v)
}
// Add proxy headers.
proxyRequest.Header.Set("X-Forwarded-For", request.RemoteAddr)
proxyRequest.Header.Set("Via", "Proxyscotch/1.1")
if len(strings.TrimSpace(proxyRequest.Header.Get("User-Agent"))) < 1 {
// If there is no valid user agent specified at all, *then* use the default.
// We'll do this for now, we could look at using the User-Agent from whatever made the request.
proxyRequest.Header.Set("User-Agent", "Proxyscotch/1.1")
}
if isMultipart {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for key := range request.MultipartForm.Value {
if key == multipartRequestDataKey {
continue
}
for _, val := range request.MultipartForm.Value[key] {
// This usually never happens, mostly memory issue
err := writer.WriteField(key, val)
if err != nil {
log.Printf("Failed to write multipart field key: %s error: %v", key, err)
return
}
}
}
for fileKey := range request.MultipartForm.File {
for _, val := range request.MultipartForm.File[fileKey] {
f, err := val.Open()
if err != nil {
log.Printf("Failed to write multipart field: %s err: %v", fileKey, err)
continue
}
field, _ := writer.CreatePart(val.Header)
_, err = io.Copy(field, f)
if err != nil {
log.Printf("Failed to write multipart field: %s err: %v", fileKey, err)
}
// Close need not be handled, as go will clear temp file
defer func(f multipart.File) {
err := f.Close()
if err != nil {
log.Printf("Failed to close file")
}
}(f)
}
}
err := writer.Close()
if err != nil {
log.Printf("Failed to write multipart content: %v", err)
_, _ = fmt.Fprintf(response, ErrorBodyProxyRequestFailed)
if err != nil {
return
}
return
}
contentType := fmt.Sprintf("multipart/form-data; boundary=%v", writer.Boundary())
proxyRequest.Header.Set("content-type", contentType)
proxyRequest.Body = io.NopCloser(bytes.NewReader(body.Bytes()))
proxyRequest.ContentLength = int64(len(body.Bytes()))
_ = proxyRequest.Body.Close()
} else if len(requestData.Data) > 0 {
proxyRequest.Body = io.NopCloser(strings.NewReader(requestData.Data))
proxyRequest.ContentLength = int64(len(requestData.Data))
_ = proxyRequest.Body.Close()
}
var client http.Client
var proxyResponse *http.Response
proxyResponse, err := client.Do(&proxyRequest)
if err != nil {
log.Print("Failed to write response body: ", err.Error())
_, _ = fmt.Fprintln(response, ErrorBodyProxyRequestFailed)
return
}
var responseData Response
responseData.Success = true
responseData.Status = proxyResponse.StatusCode
responseData.StatusText = strings.Join(strings.Split(proxyResponse.Status, " ")[1:], " ")
responseBytes, _ := io.ReadAll(proxyResponse.Body)
responseData.Headers = headerToArray(proxyResponse.Header)
if requestData.WantsBinary {
for _, bannedOutput := range bannedOutputs {
responseBytes = bytes.ReplaceAll(responseBytes, []byte(bannedOutput), []byte("[redacted]"))
}
// If using the new binary format, encode the response body.
responseData.Data = base64.RawStdEncoding.EncodeToString(responseBytes)
responseData.IsBinary = true
} else {
// Otherwise, simply return the old format.
responseData.Data = string(responseBytes)
for _, bannedOutput := range bannedOutputs {
responseData.Data = strings.Replace(responseData.Data, bannedOutput, "[redacted]", -1)
}
}
// Write the request body to the response.
err = json.NewEncoder(response).Encode(responseData)
// Return the response.
if err != nil {
log.Print("Failed to write response body: ", err.Error())
_, _ = fmt.Fprintln(response, ErrorBodyProxyRequestFailed)
return
}
}
// / Converts http.Header to a map.
// / Original Source: https://stackoverflow.com/a/37030039/2872279 (modified).
func headerToArray(header http.Header) (res map[string]string) {
res = make(map[string]string)
for name, values := range header {
for _, value := range values {
res[strings.ToLower(name)] = value
}
}
return res
}