Skip to content

Commit c273ac9

Browse files
committed
Fix unexpected header modification (closes #46)
1 parent 9946103 commit c273ac9

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717

1818
require (
1919
github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e // indirect
20+
github.com/cratonica/2goarray v0.0.0-20190331194516-514510793eaa // indirect
2021
github.com/davecgh/go-spew v1.1.1 // indirect
2122
github.com/getlantern/context v0.0.0-20220418194847-3d5e7a086201 // indirect
2223
github.com/getlantern/errors v1.0.1 // indirect
@@ -37,6 +38,8 @@ require (
3738
go.uber.org/atomic v1.9.0 // indirect
3839
go.uber.org/multierr v1.8.0 // indirect
3940
go.uber.org/zap v1.21.0 // indirect
41+
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
4042
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
43+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
4144
gopkg.in/yaml.v3 v3.0.1 // indirect
4245
)

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLj
44
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
55
github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e h1:KCjb01YiNoRaJ5c+SbnPLWjVzU9vqRYHg3e5JcN50nM=
66
github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e/go.mod h1:f7vw6ObmmNcyFQLhZX9eUGBJGpnwTJFDvVjqZxIxHWY=
7+
github.com/cratonica/2goarray v0.0.0-20190331194516-514510793eaa h1:Wg+722vs7a2zQH5lR9QWYsVbplKeffaQFIs5FTdfNNo=
8+
github.com/cratonica/2goarray v0.0.0-20190331194516-514510793eaa/go.mod h1:6Arca19mRx58CA7OWEd7Wu1NpC1rd3uDnNs6s1pj/DI=
79
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
810
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
911
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -129,6 +131,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
129131
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
130132
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
131133
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
134+
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
135+
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
132136
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
133137
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
134138
golang.org/x/net v0.0.0-20180406214816-61147c48b25b/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -150,6 +154,7 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
150154
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
151155
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
152156
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
157+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
153158
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
154159
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
155160
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

libproxy/crypto.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"crypto/x509"
1010
"crypto/x509/pkix"
1111
"encoding/pem"
12-
"io/ioutil"
1312
"log"
1413
"math/big"
1514
"net"
@@ -83,15 +82,15 @@ func EnsurePrivateKeyInstalled() error {
8382
// and then return no error (unless one was thrown in the process of creating the key.)
8483
if os.IsNotExist(err) {
8584
encodedPEM := CreateKeyPair()
86-
err = ioutil.WriteFile(GetOrCreateDataPath()+"/cert.pem", encodedPEM[0].Bytes(), 0600)
85+
err = os.WriteFile(GetOrCreateDataPath()+"/cert.pem", encodedPEM[0].Bytes(), 0600)
8786
if err != nil {
88-
err = ioutil.WriteFile(GetOrCreateDataPath()+"/key.pem", encodedPEM[1].Bytes(), 0600)
87+
err = os.WriteFile(GetOrCreateDataPath()+"/key.pem", encodedPEM[1].Bytes(), 0600)
8988
}
9089

9190
if runtime.GOOS == "windows" {
9291
// Windows doesn't recognize .pem as certificates, but we can simply write the PEM data
9392
// into a .cer file and it works just fine!
94-
err = ioutil.WriteFile(GetOrCreateDataPath()+"/cert.cer", encodedPEM[0].Bytes(), 0600)
93+
err = os.WriteFile(GetOrCreateDataPath()+"/cert.cer", encodedPEM[0].Bytes(), 0600)
9594
}
9695

9796
if err == nil {

libproxy/proxy.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"log"
1110
"mime/multipart"
1211
"net/http"
@@ -169,7 +168,7 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
169168
response.WriteHeader(301)
170169
return
171170
} else {
172-
// Otherwise set the appropriate CORS polciy and continue.
171+
// Otherwise set the appropriate CORS policy and continue.
173172
response.Header().Add("Access-Control-Allow-Origin", request.Header.Get("Origin"))
174173
}
175174

@@ -247,7 +246,15 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
247246
proxyRequest.Header.Set(k, v)
248247
}
249248

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+
}
251258

252259
if isMultipart {
253260
body := &bytes.Buffer{}
@@ -297,13 +304,13 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
297304
}
298305
contentType := fmt.Sprintf("multipart/form-data; boundary=%v", writer.Boundary())
299306
proxyRequest.Header.Set("content-type", contentType)
300-
proxyRequest.Body = ioutil.NopCloser(bytes.NewReader(body.Bytes()))
307+
proxyRequest.Body = io.NopCloser(bytes.NewReader(body.Bytes()))
301308
proxyRequest.ContentLength = int64(len(body.Bytes()))
302-
proxyRequest.Body.Close()
309+
_ = proxyRequest.Body.Close()
303310
} else if len(requestData.Data) > 0 {
304-
proxyRequest.Body = ioutil.NopCloser(strings.NewReader(requestData.Data))
311+
proxyRequest.Body = io.NopCloser(strings.NewReader(requestData.Data))
305312
proxyRequest.ContentLength = int64(len(requestData.Data))
306-
proxyRequest.Body.Close()
313+
_ = proxyRequest.Body.Close()
307314
}
308315

309316
var client http.Client
@@ -320,7 +327,7 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
320327
responseData.Success = true
321328
responseData.Status = proxyResponse.StatusCode
322329
responseData.StatusText = strings.Join(strings.Split(proxyResponse.Status, " ")[1:], " ")
323-
responseBytes, err := ioutil.ReadAll(proxyResponse.Body)
330+
responseBytes, err := io.ReadAll(proxyResponse.Body)
324331
responseData.Headers = headerToArray(proxyResponse.Header)
325332

326333
if requestData.WantsBinary {
@@ -351,8 +358,8 @@ func proxyHandler(response http.ResponseWriter, request *http.Request) {
351358
}
352359
}
353360

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).
356363
func headerToArray(header http.Header) (res map[string]string) {
357364
res = make(map[string]string)
358365

version.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Version Information
2-
VERSION_NAME=0.1.0
3-
VERSION_CODE=10
2+
VERSION_NAME=0.1.1
3+
VERSION_CODE=11

0 commit comments

Comments
 (0)