soylent / weblink

Use your web browser as a proxy server.
31 stars 2 forks source link

Running my own server #13

Closed gentaz closed 9 months ago

gentaz commented 9 months ago

Hi!

I was reading through the code and a bit confused as to what it is doing. I have never worked with Ruby before so pardon my misunderstandings.

Based on my understanding

  1. You set up a small http server to display the webpage (usually on port 8080). This is run on a "relay" device such as a phone.
  2. The webpage has embedded JS code to run a relay messenger between a server and the client. In this case, the "server" is some different machine which is presumably running weblink as a server. The "client" is the one which wants to proxy through the "relay" device and the "server" machine.

The relay device simply is responsible in shuttling the websocket packets to and from the client and server. Is my understanding correct?

I see that the "server" by default is some machine that is hosted at weblinkapp.herokuapp.com. Is that yours? What if I wanted to replace that with my own server running in some remote machine? In that case, would weblink --server --port xyz be enough? Which ports on the remote machine should be opened up (only port number xyz?)?

Thanks!

soylent commented 9 months ago

Hi gentaz,

Your understanding is correct! The default server is just a Heroku app running weblink --server. Its deployment configuration is in a separate repo: https://github.com/soylent/deploy-weblink

You can certainly run your own server instance using that command. The server listens on port 8000 by default, and that is the only inbound port that you need to open. To change the default server URL, you will need to add an extra parameter when connecting from a relay device to the "client" device: http://172.20.10.2:8080/?server=wss://your-server.com:8000. Keep in mind that websockets require long-running connections, which some cloud providers might not support and may terminate after a certain time (e.g., 30 seconds).

gentaz commented 9 months ago

Thanks! I am trying to run it on my own server. Will update once I get it working.

gentaz commented 9 months ago

Hi! So I have my own server where I run weblink. I have deployed weblink on the server as a docker container behind a Traefik reverse-proxy. I also have TLS set up correctly with it.

I tested if its reachable with wscat -c wss://proxy.my.domain from my laptop when it is connected to the internet. I have Traefik configured to forward everything on proxy.my.domain to the weblink server. I have exposed port 80 and 443 in the docker container. (Question: should I be running --server 80 or --server 443? For some reason with --server 80 it works, even though wss:// seems to suggest an https upgrade - which means it should only respond on port 443 right?) The command shows the following:

(base) ➜  ~ wscat -c wss://proxy.my.domain
Connected (press CTRL+C to quit)

Which suggests that the server is reachable. The docker container running weblink also provides this output when I use wscat: weblink-docker | Unexpected request: "/"

Suggesting that it received the connection request from wscat. I guess that unexpected request is due to wscat not really sending anything and causing that error.

However, when I run weblink on my laptop, with my phone connected to it, the proxy simply doesn't work. On my phone I use this URL http://172.20.10.5:8080/?server=wss://proxy.my.domain and proceed to perform a CURL on google.com using this command curl -px http://127.0.0.1:3128 https://www.google.com/. I see weblink tries sending it to my server

(base) ➜  ~ ~/.gem/ruby/2.6.0/bin/weblink 
Open http://172.20.10.5:8080/ on your other device.
Ready
2023-12-19T19:50:41.278-05:00 client - 127.0.0.1 53051 172.20.10.1 61336 sent 119

But it just hangs there. Nothing is received. Not sure where I am going wrong with my entire set up. Any ideas?

soylent commented 9 months ago

weblink server listens on port 8000 by default, so you need to map it to a port on the Docker host to the outside world, e.g. --publish 80:8000. And Traefik needs to terminate TLS first.

gentaz commented 9 months ago

Thank you for your help! I had set up Traefik to terminate the TLS. A lot of trial and error later, I got it to work. For anyone who sees this later, this is what I did in Traefik:

  1. Traefik manages the certificates. Set up a http router in Traefik so that it detects the hostname - proxy.my.domain
  2. Traefik acts as a TLS endpoint (wss://proxy.my.domain from a web-browser requires HTTPS support)
  3. Add a load balancer to run the http router to direct traffic to the service (the docker container running weblink) to port 8000. Therefore, I do not publish port 8000, just let Traefik handle decryption of the HTTPS connection coming in on 443, and forwards it to port 8000 of the docker container.

I set up a docker container for weblink. Used the Gemfile and Gemfile.lock that soylent mentioned above. It didn't work for me since for some reason I can't connect to gem.fury.io for downloading the new proxxy gem. This is where I was stuck for the longest time. I had earlier installed proxxxy and weblink using gem install and both of those are outdated and keep crashing. Struggled with this for quite some time. soylent thanks for the comment regarding your heroku-deploy image. That gave me all that I needed.

I modified the Gemfile and Gemfile.lock to a) use ruby version 3.1.2 (I use the ruby:3.1.2-alpine docker image as my base), and replaced the references for proxxy to use soylent's proxxy git repo directly. Bundle installed and a simple bundle exec weblink --server is all I need for my purposes.

In my phone browser all I need to do is navigate to <my-laptop's-local-ip>:8080/?server=wss://proxy.my.domain once I connect up my phone to my laptop.

It works now!

soylent commented 8 months ago

Thank you for sharing your setup! For now, proxxy is hosted on a private gem server (gem.fury.io), but I plan to make a public release once its code is finalized.