Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: fix SetProxyURL functionality #3109

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
urlpkg "net/url"
"os"
"path/filepath"
"sync"
Expand All @@ -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
Expand Down Expand Up @@ -58,9 +54,6 @@ type Client struct {
userAgent string
referer string

// proxy
proxyURL string

// user defined request hooks
userRequestHooks []RequestHook

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -582,7 +566,6 @@ func (c *Client) Reset() {
c.timeout = 0
c.userAgent = ""
c.referer = ""
c.proxyURL = ""
c.retryConfig = nil
c.debug = false

Expand Down
67 changes: 53 additions & 14 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably creating a server in a port already used

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a InmemoryListener for tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@efectn can you check this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a InmemoryListener for tests?

i dont't think we can, since FasthttpHTTPDialer only accepts a valid proxy url at ip:port format

However, i'll randomize de port instead of using fixed port


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())
})
Expand All @@ -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")
client := New()
err := client.SetProxyURL(<-addrChan)

require.NoError(t, err)
})

t.Run("wrong url", func(t *testing.T) {
t.Parallel()
client := New()

err := client.SetProxyURL(":this is not a url")
resp, err := client.Get("http://localhost:3000")
require.NoError(t, err)

require.Error(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)
})
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
Loading