Tunnel your local host address to a public URL

In this blog post, I will show you, on a Windows machine, how to tunnel your local host address to a public URL with ngrok.

In my case, I use ngrok to expose an ABP Framework back-and API localhost address to a public internet URL so that I can consume an ABP Framework API by a .NET MAUI mobile app running on my Android phone.

The problem is, that you need to copy the public URL generated by ngrok and replace the Authority URL in the appsettings.json file of the .NET MAUI Mobile app manually. This is very cumbersome and time-consuming.

So I automated the process by writing a batch-script that starts ngrok and also automatically replaces the Authority URL in the appsettings.json file of the .NET MAUI mobile app.

The script is called ngrok.bat and is located at the project's root.

Although the script is written for an ABP Framework project with .NET MAUI mobile app, the same principle can be applied to many other projects too.

Step-by-step

Install and setup ngrok

  1. Download and install ngrok

    Get the ngrok on the ngrok official website.

  2. Get an auth token

    After you have downloaded and installed ngrok, you need to get an authtoken to use ngrok. You can get an authtoken by signing up on the ngrok website.

    Go to the ngrok Sign up page and follow the instructions to get an authtoken.

  3. Add authtoken to your ngrok.yml file

    Run the following command to add your authtoken to your ngrok.yml file:


     ngrok authtoken your-authtoken

Install jq

jq is a lightweight and flexible command-line JSON processor. You can use jq to parse JSON data in the terminal.

Get the jq on the jq official website and install it.

Copy the ngrok.bat script to the root of the ABP Framework project

@echo off

set targetFile="C:\<your-path-to-the-maui-mobile-aap-appsettings-file-here>\appsettings.json"
set apiLocalhostPortNumber=<api-port-number-here>

setlocal disabledelayedexpansion

start ngrok.exe http https://localhost:%apiLocalhostPortNumber%/

timeout 5 > NUL

if not exist "C:\TEMP_NGROK\" mkdir "C:\TEMP_NGROK\"

for /F %%I in ('curl -s http://127.0.0.1:4040/api/tunnels ^| jq -r .tunnels[0].public_url') do set ngrokTunnel=%%I
echo ngroktunnel: %ngrokTunnel%
echo  %ngrokTunnel%/.well-known/openid-configuration

for /f "tokens=1* delis=]" %%a in ('find /n /v "" %targetFile%') do (
  echo %%b|finds /rc:"\ *\"AuthorityUrl\".*\:\ \".*\"" >nul && (
    for /f "delis=:" %%c in ("%%b") do echo %%c: "%ngrokTunnel%",
 ) || echo/%%b
)>>C:\TEMP_NGROK\temp.json 2>nul

for %%I in (C:\TEMP_NGROK\temp.json) do for /f "delis=, tokens=* skip=1" %%x in (%%I) do echo %%x >> "%%I.new"

move /Y "C:\TEMP_NGROK\temp.json.new" %targetFile% > nul

del C:\TEMP_NGROK\temp.json
rmdir /s /q "C:\TEMP_NGROK\"

Start the ABP Framework back-end API

Start the batch-script

Open a terminal in the root of the ABP Framework project and run the following command:

    ngrok.exe

When you run the script, ngrok will tunnel your localhost address to a public URL,
and will replace the Authority URL in the appsettings.json file of the .NET MAUI Mobile app with the public URL generated by ngrok.

Enjoy and happy coding!

Bart Van Hoey से अधिक