r/pivpn•
techjp

Forwarding external (public) port through pivpn to remote client over Wireguard

Looking to do something a little odd and my Google-fu has failed me.

I'd like to enable external access to a notebook that gets its Internet connectivity through an iPhone hotspot, but iPhone hotspots do not allow any sort of port forwarding, all inbound connections are blocked and there is no way to change this.

So I would like to have the notebook connect to my pivpn server (on my home network) using Wireguard. Then I want the person I'm granting access to, to be able to access the notebook through through the VPN tunnel. Assuming it's for SSH, it would work something like this:

  1. My router connected to the Internet, has my public (fixed) IP address.

  2. Person in a faraway place needs to access my notebook via SSH. This person would connect to my home Internet public IP, port 50222

  3. Router port-forwards port 50222 to the raspberry pi running pivpn, also targeting 50222. (pivpn does not have it's own public IP address.)

  4. Remote notebook is connected by wireguard to the pivpn server. IP assigned by wireguard to each user is static, and known.

  5. The SSH 50222 connection request is port-forwarded by the pivpn server back through the tunnel, targeting port 50222 on the remote notebook.

  6. Remote notebook is running an SSH server on port 50222. SSH prompt appears, login, etc.

The router port I can do of course but I have never tried to port forward back through a pivpn setup like this.

Is it possible? Too many port forwards? Will it be reliable?

Any tips on how to make it work, or a link to a tutorial on making it work, would be very much appreciated.

Posted Dec 17, 2022, 10:41 AM UTC · edited

10
Hende101•

Did you ever get this to work? I also want a similar setup

1
techjpOP•

I've been trying periodically to get this to work but haven't made a big effort. Every time I find a need for it, the need passes before I have enough time to really try to make it work.

I have found a few more resources, such as this one:
https://superuser.com/questions/1776851/routing-wireguard-peers-traffic-via-another-peer

Googling things like wireguard forward between peers seems to turn up some helpful results.

I would say it is possible to make this work, just annoying.

1
Hende101•

I think I have got it to work now.

Using these rules:

-A PREROUTING -p tcp -m tcp --dport <PORT> -j DNAT --to-destination <CLIENT WIREGUARD IP>:<PORT>
-A POSTROUTING -p tcp -m tcp --dport <PORT> -j SNAT --to-source <PUBLIC IP>:<PORT>

Obviously replacing <PORT>, <CLIENT WIREGUARD IP> and <PUBLIC IP> with your own.

1
techjpOP•

Thanks, I'll give it another try!

1