qdm12 / gluetun

VPN client in a thin Docker container for multiple VPN providers, written in Go, and using OpenVPN or Wireguard, DNS over TLS, with a few proxy servers built-in.
https://hub.docker.com/r/qmcgaw/gluetun
MIT License
8k stars 368 forks source link

Documentation: advanced port forwarding setup #464

Closed ddelange closed 2 years ago

ddelange commented 3 years ago

Is this urgent?: No

Host OS (approximate answer is fine too): Ubuntu Desktop 20.10

CPU arch or device name: aarch64

What VPN provider are you using: private internet access

What is the version of the program:

Running version latest built on 2021-05-19T17:58:32Z (commit a3751a7)

What's the problem 🤔

I've been struggling to host a docker container on the forwarded port:

any help reaching my owncloud container through the forwarded port would be greatly appreciated!

What are you using to run your container?: Docker Compose

version: "3.7"
services:
  gluetun:
    image: qmcgaw/gluetun:latest
    container_name: gluetun
    cap_add:
      - NET_ADMIN
    # ports:
    #   - 8888:8888/tcp # HTTP proxy
    #   - 8388:8388/tcp # Shadowsocks
    #   - 8388:8388/udp # Shadowsocks
    #   - 8000:8000/tcp # Built-in HTTP control server
    volumes:
      - ./storage/gluetun:/gluetun
      - ./storage/forwarded_port_dir:/tmp/forwarded_port_dir
    environment:
      - VPNSP=private internet access
      - PORT_FORWARDING=on
      - PORT_FORWARDING_STATUS_FILE=/tmp/forwarded_port_dir/forwarded_port
      - OPENVPN_USER=${OPENVPN_USER}
      - OPENVPN_PASSWORD=${OPENVPN_PASSWORD}
      - REGION=${REGION:-DE Berlin}
      # https://github.com/qdm12/gluetun/wiki/Environment-variables
      - DOT=off
    restart: always

  owncloud:
    image: owncloud/server:${OWNCLOUD_RELEASE_DOCKER_TAG:-10.6}
    network_mode: "service:gluetun"
    restart: always
    # could extract forwarded port from gluetun bind mount, export env var in host, and then another docker-compose up
    # but ports statement doesn't work with network_mode: "service:gluetun"
    # ports:
    #   - ${OWNCLOUD_HTTP_PORT}:8080
qdm12 commented 3 years ago

Host ports mapping

More of an FYI than a useful point;

You need to publish the ports on gluetun, as there is only one 'network host' which is gluetun. Containers you connect to gluetun use the network stack of gluetun. So technically your owncloud container has the same container IP address as gluetun. Therefore you need to have the ports in the gluetun service definition. However this seems to be irrelevant in your situation since you care about the VPN server side port forwarded, so no need to worry about that ports section.

Reverse proxy

I would recommend you to try caddy v2 instead of nginx or traefik, it's really better having tried them all. You can use qmcgaw/caddy-scratch if you want. You should use a Caddyfile to define your proxy rules, it should be easier-than-nginx to setup. And make Caddy connected to gluetun as you did for owncloud.

Listen on the forwarded port

Adapt your Caddyfile such that Caddy listens on the forwarded port, e.g. :5858, and proxies to 127.0.0.1:8080 (your LAMP stack). The only problem is that you would need to update Caddy whenever that port forwarded port changes (see the Extras)

HTTPS

That way you should be able to access https://yourdomain.com:forwarded_port 😉

Note that these may miss small things as I'm pulling this off from the top of my head, but feel free to add missing steps, I'll most likely add a page in the Wiki.

Extras

Auto-update the forwarded port in Caddy

Caddy has an HTTP API listening on port 2019 that you can enable (see the Caddyfile config options). You could have program running in gluetun periodically fetching the forwarded port with the gluetun control http server and the patching the Caddy configuration through the Caddy API. If you managed to each all the steps, I can help code that for you if you want.

Use subpaths to split to different services

You can make Caddy to use subpaths such as /owncloud and proxy to owncloud. That way you could split it to more services running in gluetun.

ddelange commented 3 years ago

Hi @qdm12, thanks a million for your thoughtful and structured response.

I will dive into Caddy ASAP and report back!

Also thanks for elaborating on HTTPS (and keeping me from giving up on security), sounds indeed like Caddy is the way to go for me 💪

ddelange commented 3 years ago

Hi again!

My progress:

So making my way down the TODO list, I've reached a bit of a catch 22:

Trying to keep compose as basic as possible for testing purposes:

services:
  # gluetun unchanged

  # ddns added

  # forwarded_port_bind_mount_listener added, updates/reloads caddy when needed

  caddy:
    image: qmcgaw/caddy-scratch
    container_name: caddy
    volumes:
     - ./caddydir:/caddydir
    network_mode: "service:gluetun"
    restart: always

  ocis:
    image: "owncloud/ocis:${OCIS_DOCKER_TAG:-latest}"
    container_name: ocis
    network_mode: "service:gluetun"
    environment:
      - OCIS_RUNTIME_PORT=9250

My search for similar issues left me stranded, this post is the closest I got. Any more wisdom you can spare? :)

ddelange commented 3 years ago

Next (failed) try which confirms caddy occupying 9250:

ddelange commented 3 years ago

And probably a separate issue with PIA PF, there seems no open connection over the port into the docker network although gluetun reports PF success on startup:

qdm12 commented 3 years ago

Sorry I'm still reading / processing all of that, thanks for sharing !

From within same ocis pod, to gluetun's reported vpn_ip/ddns_ip, connection won't establish as suggested in docs linked below

PIA has been notoriously impossible to access its forwarded port. We and other users did a lot of experiments on another issue, and it's not gluetun's fault but rather PIA, as it doesn't even work with plain openvpn with their scripts. HOWEVER it works with the yougetsignal port checker website and for torrenting port forwarding. The only explanation I have so far is they do deep packet inspection on the port forwarded traffic and drop traffic they don't like. Their support wasn't helpful but you can try ask them.

Alternatively go with another provider like Mullvad/VyprVPN that also offers port forwarding.

EDIT: See this comment : https://github.com/qdm12/gluetun/issues/53#issuecomment-634329721

ddelange commented 2 years ago

PIA replied that their service does not support incoming connections over a forwarded port. With that, I'll close this issue. Thanks again for all the help!

qdm12 commented 2 years ago

Oh what that's so strange... what's the point then 😄 I'll document this in the wiki

ddelange commented 2 years ago

I also don't understand the answer (I was asking specifically about hosting a webserver on the forwarded port), because incoming connections on the forwarded port seem to work fine e.g. for P2P protocols

qdm12 commented 2 years ago

Added your comments and linked to this issue at https://github.com/qdm12/gluetun/wiki/Private-internet-access#warning

I had the same experience back when I was a PIA subscriber as well.

prabbit237 commented 1 year ago

I haven't actually tried it with gluetun yet but port-forwarding with PIA using https://github.com/pia-foss/manual-connections and the openvpn protocol works fine. I found this thread when troubleshooting some issues with the port forwarding

What seems to not work properly (at least with pia-foss/manual-connections) is port-forwarding with wireguard. I can get that to work in Windows with the native PIA GUI using wireguard and running an Apache server on the forwarded port. But trying to use wireguard and manual-connections on a Raspberry Pi failed. Switching to openvpn was fine.

sudo VPN_PROTOCOL=wireguard DISABLE_IPV6=yes DIP_TOKEN=no AUTOCONNECT=true PIA_PF=true PIA_DNS=true PIA_USER=p1234567 PIA_PASS=XXXXXX MAX_LATENCY=0.1 ./run_setup.sh

fails.

sudo VPN_PROTOCOL=openvpn DISABLE_IPV6=yes DIP_TOKEN=no AUTOCONNECT=true PIA_PF=true PIA_DNS=true PIA_USER=p1234567 PIA_PASS=XXXXXX MAX_LATENCY=0.1 ./run_setup.sh

works fine. I got an IP of 1.2.3.4 and a port 12345 and was able to then run:

ssh 1.2.3.4 -p 12345

with no issue (changing sshd to listen on the port and using the real IP & port, of course.)

ddelange commented 1 year ago

That's great news, time to try again:) Thanks for reporting!

Seros commented 10 months ago

Hey @ddelange, just out of curiosity, were you able to confirm that port forwarding with gluetun is now working as expected?

ddelange commented 10 months ago

hi :wave: didn't have the time yet