go-http-tunnel icon indicating copy to clipboard operation
go-http-tunnel copied to clipboard

On reload redirects to the local address

Open applegrew opened this issue 7 years ago • 3 comments

I am facing a weird issue. My tunnel.yaml file is as below :-

server_addr: example.com:5223
tunnels:
      now:
        proto: http
        addr: http://127.0.0.1:8080
        host: me.example.com

On server side -httpAddr is set to :8000. When I access the site from the browsers as http://me.example.com:8000 it works fine. However, when I reload the site I get a 302 from the site and I am redirected to http://127.0.0.1:8080. The actual site does not seem to issue the 302. It seems like go-http-tunnel is issuing that for some reason. If I goto some other url and come back to the same url I do not get the 302; it is only when I hit the same url twice consecutively I get the 302. If I change the addr: to say https://google.com then I do not see this issue.

applegrew avatar Jul 29 '18 11:07 applegrew

Hi applegrew, I am facing a similar issue as redirected to localdomain of the client. Do you manage to find a solution?

Harvey-Kaer avatar Apr 23 '19 17:04 Harvey-Kaer

Nopes.

On Tue, 23 Apr, 2019, 10:36 PM Harvey-Kaer, [email protected] wrote:

Hi applegrew, I am facing a similar issue as redirected to localdomain of the client. Do you manage to find a solution?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mmatczuk/go-http-tunnel/issues/84#issuecomment-485891154, or mute the thread https://github.com/notifications/unsubscribe-auth/AADKDDRR6V5DCMUQGOW4ZMDPR46YZANCNFSM4FMVRTSA .

applegrew avatar Apr 23 '19 20:04 applegrew

After some search and trial, I found a solution works for me. I use nginx as proxy for go-http-tunnel and rewrite the HTTP header using proxy_redirect.

`

server {

# HTTPS is used
listen 443;
server_name *.kaertest.harveyliu.me;

ssl on;
ssl_certificate			/path/to/cert;
ssl_certificate_key		/path/to/private_key;

location / {
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Real-IP $remote_addr;

	#replace [tunneld_httpsAddr] with a new port for your tunneld
	proxy_set_header Host $host:[tunneld_httpsAddr];
	proxy_pass https://127.0.0.1:[tunneld_httpsAddr];

	#proxy_redirect will rewrite the local hostname in the header
	proxy_redirect ~^http://[^/]+(/.*)$ https://$http_host$1;
}

}

`

Then run tunneld on your server with additional -httpsAddr flag (or -httpAddr if you only need HTTP): tunneld -httpsAddr [tunneld_httpsAddr] -tlsCrt /path/to/cert -tlsKey /path/to/private_key

=============================================================== References: As of hint of OP, I looked into the packages around 302 Redirect and found the "LOCATION" header was responded as "localhost" or local domain in some packages.

https://stackoverflow.com/questions/46947548/ngrok-not-passing-my-post-request-on-to-localhost ngrok has a -host-header=rewrite flag so I'm looking for a similar one.

Then I decided to use nginx to redirect package: https://serverfault.com/questions/428793/using-nginxs-proxy-redirect-when-the-response-locations-domain-varies (for the regex in proxy_redirect, I used ~^http://[^/]+(/.*)$ instead of ~^http://[^/]+(/.+)$ so that empty path string (like http://localhost/) will also be redirected.

Harvey-Kaer avatar Apr 25 '19 01:04 Harvey-Kaer