I wanted a way to combine the two and I think I have figured something out. Below is the image of how it works.
- Python 3
-
- Getting your DuckDNS domain name
- Register for a free DuckDNS account.
- Register a domain name.
- Replace the
duckdns_domain
andduckdns_token
intunnel/config.conf
with your own DuckDNS domain name and token, respectively.
-
- Setting up ngrok
- Register for a free ngrok account.
- Copy your auth token and replace the value of
ngrok_auth_token
with your auth token.
-
-
Setting up your redirection server (Using PythonAnywhere)
-
Register for a free PythonAnywhere account.
-
Add a new web app.
- Choose Flask as the Python Web Framework.
- Select the latest supported Python version (as of this writing, it is Python 3.10)
- Set the path of the Flask project. (I will set it to
/home/<username>/redirector/redirector.py
)
-
Create a new directory
/home/<username>/redirector/
. -
Upload the contents of
redirector/
in your machine into the new web app's directory. -
Edit the WSGI Python file. (Usually located in
/var/www/<username>_pythonanywhere_com_wsgi.py
)-
Before the line with
from redirector import app as application # noqa
, add the following code:from redirector import main main("<PATH_TO_CONFIG_FILE>")
-
-
Save the file and reload the server.
-
Open the server logs (located in
/var/log/<username>.pythonanywhere.com.server.log
) and get your redirector API key. Look for something like this:[i] New API key has been set. You'll only see this once: 6f024c51ca5d0b6568919e134353aaf1398ff090c92f6173f5ce0315fa266b93
-
-
- Setting up the tunnel client.
- Fill up the
config.conf
file of thetunnel.py
script.- server_port: The port of the local server you want to expose.
- protocol: The protocol to pass to ngrok (i.e.,
http
ortcp
) - update_url: The URL of the redirection server.
- update_port: The port of the redirection server.
- update_url_https: Set this to true to use https instead of http.
- api_key: The API key you saw from the redirection server's server logs from step 3.7.
- duckdns_domain: The name of your DuckDNS subdomain.
- duckdns_token: The token of your DuckDNS account.
- tunnel_name: The name of your ngrok tunnel. (What you will see in your ngrok dashboard)
- ngrok_auth_token: Your ngrok authentication token.
-
- It should be ready to use now! When you visit
<domain>.duckdns.org
, you will be redirected to your ngrok instance.
- It should be ready to use now! When you visit
If you are getting a 403 Forbidden
error, make sure that you have set the correct API key in the config.conf
file.
If you are getting a 502 Bad Gateway
error, make sure that you have set the correct path of the Flask project in the PythonAnywhere web app settings.
If you are getting a 503 Service Unavailable
error, make sure that tunnel.py
is running and has successfully updated the redirection server for the ngrok instance address.
If you use PythonAnywhere as your redirection server, try to change the port of the web app to 80
. If you are using a different redirection server, make sure that the redirection server is running and that you have set the correct port in the config.conf
file. Otherwise, check the output of tunnel.py
for any errors.
The API key is shown on the logs only once. If you want to see the API key again, you can open the config.conf
file. It will be the value of api_key
. If you want to reset the API key, remove the key/value pair in the configuration file. You can also set your own API key, but it is not advisable.