From 615aa5ce1c4097e6e9569b5b3cc3aeed1abf05ce Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sat, 3 Dec 2022 11:28:12 +0100 Subject: [PATCH 1/2] Fix redirect to https with default http port Fixes #528 --- runner/runner.go | 7 ++++++- runner/runner_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 runner/runner_test.go diff --git a/runner/runner.go b/runner/runner.go index 1bf2b7f..8b9642f 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "strconv" + "strings" "time" "github.com/gotify/server/v2/config" @@ -75,7 +76,11 @@ func redirectToHTTPS(port string) http.HandlerFunc { func changePort(hostPort, port string) string { host, _, err := net.SplitHostPort(hostPort) if err != nil { - return hostPort + // There is no exported error. + if !strings.Contains(err.Error(), "missing port") { + return hostPort + } + host = hostPort } return net.JoinHostPort(host, port) } diff --git a/runner/runner_test.go b/runner/runner_test.go new file mode 100644 index 0000000..fa22255 --- /dev/null +++ b/runner/runner_test.go @@ -0,0 +1,35 @@ +package runner + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRedirect(t *testing.T) { + cases := []struct { + Request string + TLS int + Expect string + }{ + {Request: "http://gotify.net/meow", TLS: 443, Expect: "https://gotify.net:443/meow"}, + {Request: "http://gotify.net:8080/meow", TLS: 443, Expect: "https://gotify.net:443/meow"}, + {Request: "http://gotify.net:8080/meow", TLS: 8443, Expect: "https://gotify.net:8443/meow"}, + } + + for _, testCase := range cases { + name := fmt.Sprintf("%s -- %d -> %s", testCase.Request, testCase.TLS, testCase.Expect) + t.Run(name, func(t *testing.T) { + req := httptest.NewRequest("GET", testCase.Request, nil) + rec := httptest.NewRecorder() + + redirectToHTTPS(fmt.Sprint(testCase.TLS)).ServeHTTP(rec, req) + + assert.Equal(t, http.StatusFound, rec.Result().StatusCode) + assert.Equal(t, testCase.Expect, rec.Header().Get("location")) + }) + } +} From 522d7fbf4cfc0352086022d6880d3955ffd2f676 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sat, 3 Dec 2022 11:37:58 +0100 Subject: [PATCH 2/2] Update golang ci action --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a5dc31b..bee0591 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -24,10 +24,11 @@ jobs: key: ${{ runner.os }}-node_modules-${{ hashFiles('**/yarn.lock') }} restore-keys: ${{ runner.os }}-node_modules- - uses: actions/checkout@v2 - - uses: golangci/golangci-lint-action@v2 + - uses: golangci/golangci-lint-action@v3 with: version: v1.45 args: --timeout=5m + skip-cache: true - run: go mod download - run: make download-tools - run: (cd ui && yarn)