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

How to connect behind a proxy #395

Closed
LCozzoli opened this issue Jun 7, 2023 · 9 comments
Closed

How to connect behind a proxy #395

LCozzoli opened this issue Jun 7, 2023 · 9 comments

Comments

@LCozzoli
Copy link

LCozzoli commented Jun 7, 2023

Hey,
I'm currently using gorilla and wanted to migrate to this more recent library, unfortunately I can't seems to find any info about a proxy support. Is there a way to connect to a websocket server using an http proxy like gorilla does ?

@nhooyr
Copy link
Owner

nhooyr commented Jun 7, 2023

Yup, pass in an http.Client configured for your proxy. See DialOptions and net/http docs on proxy configuration for http.Client.

@nhooyr nhooyr closed this as completed Jun 7, 2023
@LCozzoli
Copy link
Author

LCozzoli commented Jun 8, 2023

Perfect, thanks!

@LCozzoli
Copy link
Author

LCozzoli commented Jun 9, 2023

I'm running into Connection header "keep-alive" does not contain Upgrade issues when connecting

wsURL := "ws://example.com"
proxyURL := "http://proxy_host:proxy_port"
proxyURLParsed, err := url.Parse(proxyURL)
if err != nil {
	log.Fatalf("Failed to parse proxy URL: %v", err)
}
dialer := &websocket.DialOptions{
	HTTPClient: &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyURL(proxyURLParsed),
		},
	},
}

conn, _, err := websocket.Dial(context.Background(), wsURL, dialer)
if err != nil {
	log.Fatalf("Failed to connect: %v", err)
}

@nhooyr
Copy link
Owner

nhooyr commented Jun 10, 2023

Not sure what you mean by header "keep-alive"? What's the full error message and on what line are you getting it?

@LCozzoli
Copy link
Author

Hello,
I think that my request is getting blocked by this line:
https://github.com/nhooyr/websocket/blob/14fb98eba64eeb5e9d06a88b98c47ae924ac82b4/accept.go#LL165C56-L165C56
I get
Failed to connect: WebSocket protocol violation: Connection header "keep-alive" does not contain Upgrade

@nhooyr
Copy link
Owner

nhooyr commented Jun 10, 2023

What version of Go are you using?

@nhooyr nhooyr reopened this Jun 10, 2023
@LCozzoli
Copy link
Author

LCozzoli commented Jun 10, 2023

go version go1.20.2 windows/amd64
The proxy I used to try works with Gorilla, I tried the same code (the closest syntax).
If you have the time and want to reproduce the issue I can give you access to a proxy, add me discord SegFault#0001

test
73185f1b44da57ed748fa9c2b89f2907

@nhooyr
Copy link
Owner

nhooyr commented Sep 28, 2023

All good, I'll setup a full go test case as I'll want to ensure this works going forward.

@nhooyr nhooyr added the bug label Sep 28, 2023
@nhooyr nhooyr added this to the v1.8.8 milestone Sep 28, 2023
nhooyr added a commit that referenced this issue Oct 19, 2023
For #395

Somehow currently reproduces #391...

Debugging still.
nhooyr added a commit that referenced this issue Oct 19, 2023
Closes #395

Confirmed library works correctly with a working forward proxy.
@nhooyr
Copy link
Owner

nhooyr commented Oct 19, 2023

Yup, test passes so not sure what you were dealing with. Are you sure your forward proxy is/was working correctly? Was it working with Gorilla?

Closing for now, feel free to comment if you have further information.

@nhooyr nhooyr closed this as completed Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants