Skip to content

Commit

Permalink
fix: return "Bad Gateway" when there's no destination and add some un…
Browse files Browse the repository at this point in the history
…it tests
  • Loading branch information
ryanbekhen committed Sep 24, 2023
1 parent 898ca97 commit b7f03c2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 2 additions & 4 deletions webproxy/webproxy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package webproxy

import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
"io"
Expand Down Expand Up @@ -49,7 +48,7 @@ func (s *WebProxy) handleHTTP(c *fiber.Ctx) error {
// send request and receive response
var resp fiber.Response
if err := agent.DoTimeout(req, &resp, s.TunnelTimeout); err != nil {
return err
return c.SendStatus(fiber.StatusBadGateway)
}

// copy response headers
Expand All @@ -63,8 +62,7 @@ func (s *WebProxy) handleHTTP(c *fiber.Ctx) error {
func (s *WebProxy) handleTunneling(c *fiber.Ctx) error {
destConn, err := fasthttp.DialTimeout(c.OriginalURL(), s.TunnelTimeout)
if err != nil {
fmt.Println(err)
return c.Status(fiber.StatusServiceUnavailable).SendString(err.Error())
return c.SendStatus(fiber.StatusBadGateway)
}

// hijack the client connection from the HTTP server
Expand Down
9 changes: 8 additions & 1 deletion webproxy/webproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Test_Middleware_WebProxy_HTTPS(t *testing.T) {
appProxy := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
proxy := New(time.Second * 5)
proxy := New(3 * time.Second)
appProxy.All("*", proxy.Handler)

tlsconf, _, err := getTLSConfigs()
Expand Down Expand Up @@ -122,6 +122,13 @@ func Test_Middleware_WebProxy_HTTPS(t *testing.T) {
body, err := io.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "Hello, World!", string(body))

client2 := &http.Client{
Transport: transport,
}

_, err = client2.Get("https://wronghost")
utils.AssertEqual(t, "Get \"https://wronghost\": Bad Gateway", err.Error())
}

// getTLSConfigs returns a server and client TLS config
Expand Down

0 comments on commit b7f03c2

Please sign in to comment.