simple port forwarding

For whatever reason, ngrok decided to suspend my account some time ago. Because of that I've decided to setup a port forwarding service on my own. Here's a quick walkthrough of what I did for anyone else's future reference :)

You will need:

  • A domain
  • A server of some sorts (With a dedicated IP address)

First, install frps onto your server by extracting and placing the frps binary into /usr/bin. Then create a config file at /etc/frp/frps.toml with the following config.

TOML
frps.toml
bindPort = 7000
vhostHTTPPort = 1337 
subDomainHost = "tun.<your-domain>"
auth.token = "<password>"

Next, setup the caddy route to point at vhostHTTPPort.

TEXT
Caddyfile
https://*.tun.voxal.dev {
    tls {
        // you'll need to configure caddy to solve the dns challenge
        // https://caddyserver.com/docs/automatic-https#dns-challenge
    }

    reverse_proxy localhost:1337
}

Make sure to also setup the DNS records to point to your IP address!

TEXT
A        *.tun.<your-domain>        <server-ip-address>

Finally on your client machine, install frpc and then set up a command to let you forward a port.

FISH
tun.fish
function random_flower
    set flowers rose tulip sunflower daisy lily orchid daffodil \
        iris peony lavender dahlia marigold poppy violet jasmine \
        magnolia cherry-blossom azalea begonia camellia crocus \
        freesia gardenia hibiscus pansy zinnia aster
    echo $flowers[(random 1 (count $flowers))]
end

function tun
    argparse 'd/subdomain=' -- $argv
    or return

    set subdomain $(random_flower)
    set -ql _flag_subdomain && set subdomain $_flag_subdomain

    echo "Forwarding port $argv[1] to https://$subdomain.tun.<your-domain>"
    frpc http \
        --proxy-name tun \
        --server-addr <server-ip-address> \
        --local-port $argv[1] \
        --sd $subdomain \
        --token "<password>"
end

I use fish so its a fish script, but feel free to translate it into bash. Now you can use tun 5000 or any other port number to port forward an HTTP service to a random subdomain at *.tun.<your-domain>. Implementing tcp port forwarding is left as an exercise for the reader.

That's all! Happy Hacking!