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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 [Bug]: Proxy middleware and Vite dev server on localhost, tcp4 127.0.0.1:5173: connect: connection refused #2890

Closed
3 tasks done
sixcolors opened this issue Mar 3, 2024 · 6 comments 路 Fixed by #2900

Comments

@sixcolors
Copy link
Member

sixcolors commented Mar 3, 2024

Bug Description

When using the proxy middleware to proxy.BalanceForward([]string{"http://localhost:5173"})) to a Vite/react dev server running on port 5173 the connection is refused with the following output Error: The HTTP request failed with error dial tcp4 127.0.0.1:5173: connect: connection refused

How to Reproduce

Clone https://github.com/sixcolors/FiberReactTest

cd frontend
npm install --include=dev && npm run dev

The proxy demonstrates this behaviour as can be seen in the main.go:

if DevMode {
		app.Get("*", proxy.BalancerForward([]string{"http://localhost:5173"}))

However the issue has been isolated to the fasthttp client.

To verify run the following:

package main

import (
	"io"
	"log"
	"net/http"

	"github.com/valyala/fasthttp"
)

func main() {
	// test connection to http://localhost:5173
	resp, err := http.Get("http://localhost:5173")
	if err != nil {
		log.Fatalf("Error: The HTTP request failed with error %s", err)
	} else {
		defer resp.Body.Close()
		data, err := io.ReadAll(resp.Body)
		if err != nil {
			log.Fatalf("Error reading body: %v", err)
		}
		log.Printf("HTTP Response Status: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
		log.Printf("Response Body: %s", string(data))
	}

	statusCode, body, err := fasthttp.Get(nil, "http://localhost:5173")
	if err != nil {
		log.Fatalf("Error: The HTTP request failed with error %s", err)
	} else {
		log.Printf("HTTP Response Status: %d", statusCode)
		log.Printf("Response Body: %s", body)
	}
}

Expected Behavior

fasthttp client should connect to http://localhost:5173 and the proxy should work.

Actual Behavior

fasthttp client will not try dialing IPv6 by default and only IPv4 is tried, resulting in a connection refused error.

Fiber Version

v2.52.2

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.
@sixcolors
Copy link
Member Author

sixcolors commented Mar 3, 2024

bug filed with upstream package valyala/fasthttp#1730

@sixcolors
Copy link
Member Author

Erik Dubbelboer from upstream issue:

This is because vite only listens on IPv6 and fasthttp doesn't try dialing IPv6 by default. If you change the code to this it works:

	client := fasthttp.Client{
		DialDualStack: true,
	}
	statusCode, body, err := client.Get(nil, "http://localhost:5173")

@sixcolors
Copy link
Member Author

sixcolors commented Mar 3, 2024

@sixcolors
Copy link
Member Author

sixcolors commented Mar 3, 2024

Should we support dual stack be default in the client for proxy middleware?

@sixcolors sixcolors changed the title 馃悰 [Bug]: Proxy middleware and Vote dev server on localhost, tcp4 127.0.0.1:5173: connect: connection refused 馃悰 [Bug]: Proxy middleware and Vite dev server on localhost, tcp4 127.0.0.1:5173: connect: connection refused Mar 3, 2024
@negrel
Copy link
Contributor

negrel commented Mar 3, 2024

I believe adding an option for DialDualStack won't hurt. As a fiber user, I'm forced to fork proxy middleware to enable it as my cloud provider only support IPv6 for internal networking.

I can contribute if needed.

@gaby
Copy link
Member

gaby commented Mar 3, 2024

@negrel Agree we should add an option to allow either ipv4, ipv6 or DialDual to the middleware. Sounds reasonable to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants