6
6
"encoding/json"
7
7
"fmt"
8
8
"io"
9
- "io/ioutil"
10
9
"log"
11
10
"mime/multipart"
12
11
"net/http"
@@ -169,7 +168,7 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
169
168
response .WriteHeader (301 )
170
169
return
171
170
} else {
172
- // Otherwise set the appropriate CORS polciy and continue.
171
+ // Otherwise set the appropriate CORS policy and continue.
173
172
response .Header ().Add ("Access-Control-Allow-Origin" , request .Header .Get ("Origin" ))
174
173
}
175
174
@@ -247,7 +246,15 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
247
246
proxyRequest .Header .Set (k , v )
248
247
}
249
248
250
- proxyRequest .Header .Set ("User-Agent" , "Proxyscotch/1.1" )
249
+ // Add proxy headers.
250
+ proxyRequest .Header .Set ("X-Forwarded-For" , request .RemoteAddr )
251
+ proxyRequest .Header .Set ("Via" , "Proxyscotch/1.1" )
252
+
253
+ if len (strings .TrimSpace (proxyRequest .Header .Get ("User-Agent" ))) < 1 {
254
+ // If there is no valid user agent specified at all, *then* use the default.
255
+ // We'll do this for now, we could look at using the User-Agent from whatever made the request.
256
+ proxyRequest .Header .Set ("User-Agent" , "Proxyscotch/1.1" )
257
+ }
251
258
252
259
if isMultipart {
253
260
body := & bytes.Buffer {}
@@ -297,13 +304,13 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
297
304
}
298
305
contentType := fmt .Sprintf ("multipart/form-data; boundary=%v" , writer .Boundary ())
299
306
proxyRequest .Header .Set ("content-type" , contentType )
300
- proxyRequest .Body = ioutil .NopCloser (bytes .NewReader (body .Bytes ()))
307
+ proxyRequest .Body = io .NopCloser (bytes .NewReader (body .Bytes ()))
301
308
proxyRequest .ContentLength = int64 (len (body .Bytes ()))
302
- proxyRequest .Body .Close ()
309
+ _ = proxyRequest .Body .Close ()
303
310
} else if len (requestData .Data ) > 0 {
304
- proxyRequest .Body = ioutil .NopCloser (strings .NewReader (requestData .Data ))
311
+ proxyRequest .Body = io .NopCloser (strings .NewReader (requestData .Data ))
305
312
proxyRequest .ContentLength = int64 (len (requestData .Data ))
306
- proxyRequest .Body .Close ()
313
+ _ = proxyRequest .Body .Close ()
307
314
}
308
315
309
316
var client http.Client
@@ -320,7 +327,7 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
320
327
responseData .Success = true
321
328
responseData .Status = proxyResponse .StatusCode
322
329
responseData .StatusText = strings .Join (strings .Split (proxyResponse .Status , " " )[1 :], " " )
323
- responseBytes , err := ioutil .ReadAll (proxyResponse .Body )
330
+ responseBytes , err := io .ReadAll (proxyResponse .Body )
324
331
responseData .Headers = headerToArray (proxyResponse .Header )
325
332
326
333
if requestData .WantsBinary {
@@ -351,8 +358,8 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
351
358
}
352
359
}
353
360
354
- /// Converts http.Header to a map.
355
- /// Original Source: https://stackoverflow.com/a/37030039/2872279 (modified).
361
+ // / Converts http.Header to a map.
362
+ // / Original Source: https://stackoverflow.com/a/37030039/2872279 (modified).
356
363
func headerToArray (header http.Header ) (res map [string ]string ) {
357
364
res = make (map [string ]string )
358
365
0 commit comments