Description

A registered user can redirect an URL under their domain (identified by an alias) in the form http://<path to redirections>/<user>/<alias> to the desired target URL.

Repository

     https://github.com/samuelmh/django-smh_redirections

Use cases

Shorten/remember URLs

The simplest way to use the service is as a mean to shorten URLs like https://goo.gl/ or https://bit.ly//. The user asigns the target URL to an alias (this binding won't be modified).

For example: http://redirections/my_user/school_function could redirect to some youtube video. And the former link is easier to remember.

Web pages locator

This is a case where the destination URL can vary over the time while the alias is inmutable. It is useful as a locator for dynamic IP devices (i.e. locate a home web server).


Functionality

Redirection anatomy

These are the properties of a redirection:

Web page interface

Redirections are fully manageable through the web interface.

Add/modify a redirection

The Add/modify redirection widget allows the user to create or modify a redirection. The alias and URL fields are mandatory. It is possible to go from the modify mode to the add one by pressing the 'Cancel' button.

List redirections

The List section shows the user's redirections.

Actions:

Other

This actions can be performed by any user, even if they are not registered in the system.

Visit a redirection

When a user visits http://<path to redirections>/<user>/<alias>, if the redirection is public, they will be sent to the URL the user <user> has given to the alias <alias>. The 'Visits' field will be increased by 1.

Refresh a redirection

If a redirection has a password, it is possible to modify the 'URL' field with a POST call to http://<path to redirections>/<user>/<alias> sending the params password=<configured password> and url=<new url>.

The redirection fields 'Visits' will be set to 0 and 'Last refresh' to the time the redirection was modified.

If no POST param url is provided, the 'URL' field will be set to the IP address of the request.


F.A.Q.

  1. Where can I find the code?

    This is free software and the original code can be found at https://github.com/samuelmh/django-smh_redirections

  2. How can I made a machine to update a redirection easily?

    The easiest way is to force the machine to send a HTTP POST call to http://<path to redirections>/<user>/<alias>. With the password as a param.

    In GNU/Linux this can be automated with the curl command (for the HTTP call) and the cron daemon (calls curl over the time).

    EXAMPLE:

    Let say your username is 'john', you have a web server installed in your Raspberry Pi at your home but you can't access from outside your home network because you never remember (or even know) the router IP address. Let say you also want to update the redirection every 10 minutes so in case the router resets and change its IP you can access your Raspberry Pi in no more than 10 minutes.
    Here the goal is to access your machine just by visiting http://<path to redirections>/john/raspberry These are the steps to follow:

    • Create a redirection in the web interface (i.e. alias=raspberry, url=http://dummy.test), remember to set a password (i.e. password=MyP455W0rD).
    • From a computer, execute the following line (change <path to redirections> for a valid webpage).
      $ curl --data "password=MyP455W0rD" http://<path to redirections>/john/raspberry
    • Refresh the table in the web interface. If everything is correct, you will see the URL of the 'raspberry' redirection has changed. The curl command is correct.
    • In the raspberry Pi, execute $ sudo crontab -e and add the followig line at the end of the file.
      0-59/10 * * * * curl --data "password=MyP455W0rD" http://<path to redirections>/john/raspberry
    • Wait 10 minutes and refresh the table in the web interface. If everything is correct, you will see the URL for the 'raspberry' redirection has changed. You have finished the configuration.

    In this example, the POST param 'url' is not provided because the raspberry doesn't know which is the public IP of the network, so it is better to delegate the guess to the redirections platform. If we wanted to know our public IP whe should ask some external service like http://www.whatsmyip.org/

  3. I have completed the previous tutorial successfully but I cannot access my machine.

    Do you have any HTTP service running in your machine? Did you open the needed ports in your router? Are you behind a proxy? Do you even have an idea of what you are doing?


Technical issues

REST API

The base URL for the REST API is http://<path to redirections>/api/v1/

List redirections

Path: /<user>

Method: GET

Auth: YES

IN: NO params

OUT: JSON {redirecions: [{alias:str, url:str, password:str, prints:int, public:bool, last_refresh: int(UTC millis) }] }

Description:

List all the <user> redirections with all the fields.

List public redirections

Path: /<user>

Method: GET

Auth: NO

IN: NO params

OUT: JSON {redirecions: [{alias:str, url:str}] }

Description:

List only the public <user> redirections with the user and alias fields.

Delete a redirection

Path: /<user>/<alias>

Method: DELETE

Auth: YES

IN: NO params

OUT: JSON {msg: str}

Description:

Delete a redirection. The output is a message with the result of the operation.

Add/modify a redirection

Path: /<user>/<alias>

Method: PUT

Auth: YES

IN:JSON

{url:str, public:bool, password:str}

OUT: JSON {msg: str}

Description:

Add or modify (if exists) a redirection. The output is a message with the result of the operation.

Authentication

The authentication functionality is provided by the django-smh_jwt app. Which decorates the required views (REST API) and expects a JWT token in the Authentication header.

Requirements

Web page interface

This Django app provides a sample template with a rich web application written in Angular. This webapp uses the REST API to perform the actions.