client: fix SetProxyURL functionality (#3109)

* client: fix SetProxyURL functionality

* Update client_test.go

* fix lint error (gofumpt)

---------

Co-authored-by: RW <rene@gofiber.io>
bind^2
M. Efe Çetin 2024-08-29 17:57:01 +03:00 committed by GitHub
parent 08d9fda631
commit 4b1ee0b48d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 63 additions and 35 deletions

View File

@ -7,9 +7,7 @@ import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
urlpkg "net/url"
"os"
"path/filepath"
"sync"
@ -20,12 +18,10 @@ import (
"github.com/gofiber/utils/v2"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
)
var (
ErrInvalidProxyURL = errors.New("invalid proxy url scheme")
ErrFailedToAppendCert = errors.New("failed to append certificate")
)
var ErrFailedToAppendCert = errors.New("failed to append certificate")
// The Client is used to create a Fiber Client with
// client-level settings that apply to all requests
@ -58,9 +54,6 @@ type Client struct {
userAgent string
referer string
// proxy
proxyURL string
// user defined request hooks
userRequestHooks []RequestHook
@ -229,16 +222,7 @@ func (c *Client) SetRootCertificateFromString(pem string) *Client {
// SetProxyURL sets proxy url in client. It will apply via core to hostclient.
func (c *Client) SetProxyURL(proxyURL string) error {
pURL, err := urlpkg.Parse(proxyURL)
if err != nil {
return fmt.Errorf("client: %w", err)
}
if pURL.Scheme != "http" && pURL.Scheme != "https" {
return fmt.Errorf("client: %w", ErrInvalidProxyURL)
}
c.proxyURL = pURL.String()
c.fasthttp.Dial = fasthttpproxy.FasthttpHTTPDialer(proxyURL)
return nil
}
@ -582,7 +566,6 @@ func (c *Client) Reset() {
c.timeout = 0
c.userAgent = ""
c.referer = ""
c.proxyURL = ""
c.retryConfig = nil
c.debug = false

View File

@ -19,6 +19,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
)
func startTestServerWithPort(t *testing.T, beforeStarting func(app *fiber.App)) (*fiber.App, string) {
@ -1539,11 +1540,53 @@ func Test_Client_SetProxyURL(t *testing.T) {
app, dial, start := createHelperServer(t)
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("hello world")
return c.SendString(c.Get("isProxy"))
})
go start()
fasthttpClient := &fasthttp.Client{
Dial: dial,
NoDefaultUserAgentHeader: true,
DisablePathNormalizing: true,
}
// Create a simple proxy sever
proxyServer := fiber.New()
proxyServer.Use("*", func(c fiber.Ctx) error {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
req.SetRequestURI(c.BaseURL())
req.Header.SetMethod(fasthttp.MethodGet)
c.Request().Header.VisitAll(func(key, value []byte) {
req.Header.AddBytesKV(key, value)
})
req.Header.Set("isProxy", "true")
if err := fasthttpClient.Do(req, resp); err != nil {
return err
}
c.Status(resp.StatusCode())
c.Context().SetBody(resp.Body())
return nil
})
addrChan := make(chan string)
go func() {
assert.NoError(t, proxyServer.Listen(":0", fiber.ListenConfig{
DisableStartupMessage: true,
ListenerAddrFunc: func(addr net.Addr) {
addrChan <- addr.String()
},
}))
}()
t.Cleanup(func() {
require.NoError(t, app.Shutdown())
})
@ -1552,31 +1595,27 @@ func Test_Client_SetProxyURL(t *testing.T) {
t.Run("success", func(t *testing.T) {
t.Parallel()
client := New().SetDial(dial)
err := client.SetProxyURL("http://test.com")
require.NoError(t, err)
_, err = client.Get("http://localhost:3000")
require.NoError(t, err)
})
t.Run("wrong url", func(t *testing.T) {
t.Parallel()
client := New()
err := client.SetProxyURL(<-addrChan)
err := client.SetProxyURL(":this is not a url")
require.NoError(t, err)
require.Error(t, err)
resp, err := client.Get("http://localhost:3000")
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode())
require.Equal(t, "true", string(resp.Body()))
})
t.Run("error", func(t *testing.T) {
t.Parallel()
client := New()
err := client.SetProxyURL("htgdftp://test.com")
err := client.SetProxyURL(":this is not a proxy")
require.NoError(t, err)
_, err = client.Get("http://localhost:3000")
require.Error(t, err)
})
}

2
go.mod
View File

@ -20,6 +20,8 @@ require (
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

4
go.sum
View File

@ -36,6 +36,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -56,6 +58,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=