I’m renting a VPS and wanted to use it to send my mail. But the only VPN connection I’ve got to it is via Tailscale. So I thought, why not use it as an exit node and send my mail that way?

Well, there are a few problems to this idea, most are self inflicted. The first mayor one is that I do not want to use the exit node for anything other than email, so no package updates or anything else. The second one is that it needs to be accessible through my Tailnet. Either directly or through a subnet router that I have in the same network.

My first attempt was to simply install Tailscale the normal way, enable the exit node and add some routing rules using ip rule to direct only traffic targeting port 25 into the Tailnet and leave everything else to the normal routes. This worked, at least for sending mail and accessing the Internet, but connectivity from the Tailnet did not work at all. So I had to figure something else out.

The solution that I found in the end was to use Tailscales userspace-networking feature, originally intended for containers that do not have access to /dev/tun. This works by creating a SOCKS5 or HTTP Proxy that the application can connect to to access the Tailnet. So far so good, the only problem was that my MTA, Postfix, does not support either to send mail.

So to fix that I needed another piece of software called Redsocks. It allows you to route arbitrary connections through a SOCKS5 Proxy, with the help of a firewall. Since Tailscale has a dependency on NFTables (at least on Debian) I chose to use it for this setup.

So, this is how I configured everything in the end: Tailscaled is started with the flags --tun=userspace-networking --socks5-server=localhost:20000 easily configured in /etc/default/tailscaled. I connected it to my Tailnet and enabled the exit node, making sure that the ACLs allowed a connection to the internet on port 25.

Redsocks is configured to route the requests to Tailscale by setting the following in its /etc/redsocks.conf

base {                                                                                                                                                                                       
        // my common settings, not relevant here
}
redsocks {
        local_ip = 127.0.0.1;
        local_port = 20001;
        ip = 127.0.0.1;
        port = 20000;
        type = socks5;
}

Now I can route any request to my exit node using NFTables. This is done by adding a new NAT table with a chain called REDSOCKS. This chain redirects all requests that it handles to Redsocks on the correct port. I configured this using the legacy iptables interface since I know how to use it. First creating the chain with iptables -t nat -N REDSOCKS then add the redirect with iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 20001 and finally the rule to direct any traffic from Postifx there iptables -t nat -A OUTPUT -p tcp -m owner --uid-owner postfix -m tcp --dport 25 -j REDSOCKS.

The end result was a working setup sending my mail, and only my mail, through my VPS. But we aren’t quite done yet. If the system reboots then the firewall rules are lost. Using nft list ruleset to get the rules that NFTables generated and saving it to /etc/nftables.conf persists the firewall changes as well. Here is the section that I needed to add:

table ip nat {
        chain REDSOCKS {
                ip protocol tcp counter packets 1 bytes 60 redirect to :20001
        }

        chain OUTPUT {
                type nat hook output priority dstnat; policy accept;
                ip protocol tcp skuid 103 tcp dport 25 counter packets 1 bytes 60 jump REDSOCKS
        }
}

This was quite the adventure using Tailscale in a way that is apparently wasn’t intended to be used. But I’m quite happy with the end result. I hope that this small guide can be of use for some of you.

  • frongt@lemmy.zip
    link
    fedilink
    English
    arrow-up
    3
    ·
    4 hours ago

    A couple other good options:

    • run the mail server directly on the VPS, either as the primary server or a relay

    • run the mail server locally and bind it to the VPN interface

    • run the mail server locally in a docker container and the VPN in gluetun and use docker networking

    • Gagootron@feddit.orgOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      4 hours ago

      These are good options, but didn’t work for me because of one reason or another.

      • The VPS has too few resources to run another instance of Postfix
      • That works for normal VPNs but Tailscale tries to force all internet traffic through it if you enable an exit node, and doesn’t allow any if you disable it
      • I’m not using docker anywhere only full Vms

      But I’m sure for someone without the restrictions that I put on myself might have an easier time with those methods.

      • MatSeFi@lemmy.liebeleu.de
        link
        fedilink
        English
        arrow-up
        3
        ·
        3 hours ago

        The VPS has too few resources to run another instance of Postfix

        Ok there are only just a few cases where even the smallest vps you can pay for not capable of handling this.

        • you have a ton of client sending mail via the same instance. In this case you should probably invest in a rigid solution anyways.
        • you want to sent out GB of spam mails. Thus use vps to conveniently switch ips once they are burned/listed at spamhouse or similar.