From 253310b8f85b0123d60866a9945bf5276c0c12ff Mon Sep 17 00:00:00 2001 From: opi Date: Mon, 20 Jun 2016 23:43:51 +0200 Subject: [PATCH] Initial commit. --- README.md | 25 +++++++++++++ conf/nginx-proxy.conf | 13 +++++++ conf/nginx-visible-301.conf | 3 ++ conf/nginx-visible-302.conf | 3 ++ manifest.json | 74 +++++++++++++++++++++++++++++++++++++ scripts/backup | 26 +++++++++++++ scripts/install | 71 +++++++++++++++++++++++++++++++++++ scripts/remove | 26 +++++++++++++ scripts/restore | 57 ++++++++++++++++++++++++++++ scripts/upgrade | 66 +++++++++++++++++++++++++++++++++ 10 files changed, 364 insertions(+) create mode 100644 README.md create mode 100644 conf/nginx-proxy.conf create mode 100644 conf/nginx-visible-301.conf create mode 100644 conf/nginx-visible-302.conf create mode 100644 manifest.json create mode 100644 scripts/backup create mode 100644 scripts/install create mode 100644 scripts/remove create mode 100644 scripts/restore create mode 100644 scripts/upgrade diff --git a/README.md b/README.md new file mode 100644 index 0000000..52ed14a --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# Redirect App + +Create redirect and add a link on user panel. Add a simple Nginx configuration +file with `redirect` or `proxy_pass` rule. Nothing more. + +## Redirect type + +### Visible redirect + +Visitor's address bar will change. Helpfull to add a user link to another +website + + you-domain.com -> another-domain.net + you-domain.com/foo -> another-domain.net/bar + +## Invisible (proxy) redirect + +Visitor's address bar will remain the same. Mostly use to serve local webserver +for a personnal application. + + you-domain.com/foo -> http://172.0.0.1:8080/app + +## Credits + +Insprired by [scith](https://github.com/scith) work. diff --git a/conf/nginx-proxy.conf b/conf/nginx-proxy.conf new file mode 100644 index 0000000..254209f --- /dev/null +++ b/conf/nginx-proxy.conf @@ -0,0 +1,13 @@ +location YNH_LOCATION { + proxy_pass YNH_REDIRECT_PATH; + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; + + # Include SSOWAT user panel. + include conf.d/yunohost_panel.conf.inc; + more_clear_input_headers 'Accept-Encoding'; +} \ No newline at end of file diff --git a/conf/nginx-visible-301.conf b/conf/nginx-visible-301.conf new file mode 100644 index 0000000..99802a0 --- /dev/null +++ b/conf/nginx-visible-301.conf @@ -0,0 +1,3 @@ +location YNH_LOCATION { + return 301 YNH_REDIRECT_PATH; +} \ No newline at end of file diff --git a/conf/nginx-visible-302.conf b/conf/nginx-visible-302.conf new file mode 100644 index 0000000..1f3c40c --- /dev/null +++ b/conf/nginx-visible-302.conf @@ -0,0 +1,3 @@ +location YNH_LOCATION { + return 302 YNH_REDIRECT_PATH; +} \ No newline at end of file diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..03ea4f4 --- /dev/null +++ b/manifest.json @@ -0,0 +1,74 @@ +{ + "name": "Redirect", + "id": "redirect", + "packaging_format": 1, + "requirements": { + "yunohost": ">= 2.4" + }, + "description": { + "en": "Create a redirection or a proxy to another path.", + "fr": "Créer une redirection ou un proxy vers un autre emplacement" + }, + "maintainer": { + "name": "opi", + "email": "opi@zeropi.net" + }, + "multi_instance": "true", + "services": [ + "nginx" + ], + "arguments": { + "install" : [ + { + "name": "domain", + "type": "domain", + "ask": { + "en": "Choose a domain for your redirect", + "fr": "Choisissez un domaine pour votre redirection" + }, + "example": "domain.org" + }, + { + "name": "path", + "type": "path", + "ask": { + "en": "Choose a path for your redirect", + "fr": "Choisissez un chemin pour votre redirection" + }, + "example": "/redirect", + "default": "/redirect" + }, + { + "name": "redirect_path", + "ask": { + "en": "Redirect destination path", + "fr": "Emplacement de destination" + }, + "example": "http://127.0.0.1:8080/app/", + "default": "http://127.0.0.1" + }, + { + "name": "is_public", + "type": "boolean", + "ask": { + "en": "Is it a public redirect ?", + "fr": "Est-ce une redirection publique ?" + }, + "default": false + }, + { + "name": "redirect_type", + "ask": { + "en": "Redirect type", + "fr": "Type de redirection" + }, + "choices": { + "visible_302" : "Visible (302 redirect)", + "visible_301" : "Visible (301 redirect)", + "proxy": "Proxy, invisible (Nginx proxy_pass)" + }, + "default": "visible_302" + } + ] + } +} diff --git a/scripts/backup b/scripts/backup new file mode 100644 index 0000000..9e3ada6 --- /dev/null +++ b/scripts/backup @@ -0,0 +1,26 @@ +#!/bin/bash + +# causes the shell to exit if any subcommand or pipeline returns a non-zero status +set -e + +# Source YNH helpers +. /usr/share/yunohost/helpers + +# This is a multi-instance app, meaning it can be installed several times independently +# The id of the app as stated in the manifest is available as $YNH_APP_ID +# The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...) +# The app instance name is available as $YNH_APP_INSTANCE_NAME +# - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample +# - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2 +# - ynhexample__{N} for the subsequent installations, with N=3,4, ... +# The app instance name is probably what you are interested the most, since this is +# guaranteed to be unique. This is a good unique identifier to define installation path, +# db names, ... +app=$YNH_APP_INSTANCE_NAME + +# Retrieve arguments +domain=$(sudo yunohost app setting $app domain) + +# Copy the conf files +sudo mkdir -p ./conf +sudo cp -a "/etc/nginx/conf.d/${domain}.d/${app}.conf" ./conf/nginx.conf \ No newline at end of file diff --git a/scripts/install b/scripts/install new file mode 100644 index 0000000..869afc4 --- /dev/null +++ b/scripts/install @@ -0,0 +1,71 @@ +#!/bin/bash + +# causes the shell to exit if any subcommand or pipeline returns a non-zero status +set -ue + +# Source app helpers +. /usr/share/yunohost/helpers + +# This is a multi-instance app, meaning it can be installed several times independently +# The id of the app as stated in the manifest is available as $YNH_APP_ID +# The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...) +# The app instance name is available as $YNH_APP_INSTANCE_NAME +# - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample +# - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2 +# - ynhexample__{N} for the subsequent installations, with N=3,4, ... +# The app instance name is probably what you are interested the most, since this is +# guaranteed to be unique. This is a good unique identifier to define installation path, +# db names, ... +app=$YNH_APP_INSTANCE_NAME + +# Retrieve arguments +domain=$YNH_APP_ARG_DOMAIN +path=$YNH_APP_ARG_PATH +is_public=$YNH_APP_ARG_IS_PUBLIC +redirect_type=$YNH_APP_ARG_REDIRECT_TYPE +redirect_path=$YNH_APP_ARG_REDIRECT_PATH + +# Remove trailing slash to path +path=${path%/} +#force location to be / or /foo +location=${path:-/} + +# Check domain/path availability +sudo yunohost app checkurl $domain$path -a $app \ + || (echo "Path not available: $domain$path" && exit 1) + +# Validate redirect path +url_regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' +[[ ! $redirect_path =~ $url_regex ]] && ynh_die \ +"Invalid destination: $redirect_path" 1 + +# Save extra settings +sudo yunohost app setting $app is_public -v "$is_public" +sudo yunohost app setting $app redirect_type -v "$redirect_type" +sudo yunohost app setting $app redirect_path -v "$redirect_path" + +# Nginx configuration +sed -i "s@YNH_LOCATION@$location@g" ../conf/nginx-*.conf +if [ "$redirect_type" = "visible_302" ]; +then + sed -i "s@YNH_REDIRECT_PATH@$redirect_path@g" ../conf/nginx-visible-302.conf + sudo cp ../conf/nginx-visible-302.conf /etc/nginx/conf.d/$domain.d/$app.conf +elif [ "$redirect_type" = "visible_301" ]; +then + sed -i "s@YNH_REDIRECT_PATH@$redirect_path@g" ../conf/nginx-visible-301.conf + sudo cp ../conf/nginx-visible-301.conf /etc/nginx/conf.d/$domain.d/$app.conf +elif [ "$redirect_type" = "proxy" ]; +then + sed -i "s@YNH_REDIRECT_PATH@$redirect_path@g" ../conf/nginx-proxy.conf + sudo cp ../conf/nginx-proxy.conf /etc/nginx/conf.d/$domain.d/$app.conf +fi + +# Make app public if necessary +if [[ "$is_public" -ne 0 ]]; +then + sudo yunohost app setting $app unprotected_uris -v "/" +fi + +# Reload Nginx and regenerate SSOwat conf +sudo service nginx reload +sudo yunohost app ssowatconf diff --git a/scripts/remove b/scripts/remove new file mode 100644 index 0000000..c05f24c --- /dev/null +++ b/scripts/remove @@ -0,0 +1,26 @@ +#!/bin/bash + +# causes the shell to exit if any subcommand or pipeline returns a non-zero status +set -e + +# This is a multi-instance app, meaning it can be installed several times independently +# The id of the app as stated in the manifest is available as $YNH_APP_ID +# The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...) +# The app instance name is available as $YNH_APP_INSTANCE_NAME +# - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample +# - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2 +# - ynhexample__{N} for the subsequent installations, with N=3,4, ... +# The app instance name is probably what you are interested the most, since this is +# guaranteed to be unique. This is a good unique identifier to define installation path, +# db names, ... +app=$YNH_APP_INSTANCE_NAME + +# Retrieve arguments +domain=$(sudo yunohost app setting $app domain) + +# Remove configuration files +sudo rm -f /etc/nginx/conf.d/$domain.d/$app.conf + +# Restart services +sudo service nginx reload +sudo yunohost app ssowatconf diff --git a/scripts/restore b/scripts/restore new file mode 100644 index 0000000..a3590bb --- /dev/null +++ b/scripts/restore @@ -0,0 +1,57 @@ +#!/bin/bash + +# causes the shell to exit if any subcommand or pipeline returns a non-zero status +set -e + +# Source YNH helpers +. /usr/share/yunohost/helpers + +# This is a multi-instance app, meaning it can be installed several times independently +# The id of the app as stated in the manifest is available as $YNH_APP_ID +# The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...) +# The app instance name is available as $YNH_APP_INSTANCE_NAME +# - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample +# - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2 +# - ynhexample__{N} for the subsequent installations, with N=3,4, ... +# The app instance name is probably what you are interested the most, since this is +# guaranteed to be unique. This is a good unique identifier to define installation path, +# db names, ... +app=$YNH_APP_INSTANCE_NAME + +# Retrieve arguments +domain=$(ynh_app_setting_get "$app" domain) +path=$(ynh_app_setting_get "$app" path) +is_public=$(ynh_app_setting_get "$app" is_public) +redirect_type=$(ynh_app_setting_get "$app" redirect_type) +redirect_path=$(ynh_app_setting_get "$app" redirect_path) + +# Remove trailing slash to path +path=${path%/} + +# Check domain/path availability +sudo yunohost app checkurl $domain$path -a $app \ + || die "The path ${domain}${path} is not available for app installation." + +# Validate redirect path +url_regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' +[[ ! $redirect_path =~ $url_regex ]] && ynh_die \ +"Invalid destination: $redirect_path" 1 + +# Check configuration files +NGINX_CONF="/etc/nginx/conf.d/${domain}.d/${app}.conf" +[[ -f $NGINX_CONF ]] && die \ +"The NGINX configuration already exists at '${NGINX_CONF}'. + You should safely delete it before restoring this app." + +# Restore configuration files +sudo cp -a ./conf/nginx.conf "$NGINX_CONF" + +# Make app public if necessary +if [[ "$is_public" -ne 0 ]]; +then + sudo yunohost app setting $app unprotected_uris -v "/" +fi + +# Reload Nginx and regenerate SSOwat conf +sudo service nginx reload +sudo yunohost app ssowatconf diff --git a/scripts/upgrade b/scripts/upgrade new file mode 100644 index 0000000..ee4b589 --- /dev/null +++ b/scripts/upgrade @@ -0,0 +1,66 @@ +#!/bin/bash + +# causes the shell to exit if any subcommand or pipeline returns a non-zero status +set -e + +# This is a multi-instance app, meaning it can be installed several times independently +# The id of the app as stated in the manifest is available as $YNH_APP_ID +# The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...) +# The app instance name is available as $YNH_APP_INSTANCE_NAME +# - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample +# - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2 +# - ynhexample__{N} for the subsequent installations, with N=3,4, ... +# The app instance name is probably what you are interested the most, since this is +# guaranteed to be unique. This is a good unique identifier to define installation path, +# db names, ... +app=$YNH_APP_INSTANCE_NAME + +# Source app helpers +. /usr/share/yunohost/helpers + +# Retrieve arguments +domain=$(ynh_app_setting_get "$app" domain) +path=$(ynh_app_setting_get "$app" path) +is_public=$(ynh_app_setting_get "$app" is_public) +redirect_type=$(ynh_app_setting_get "$app" redirect_type) +redirect_path=$(ynh_app_setting_get "$app" redirect_path) + +# Remove trailing slash to path +path=${path%/} +#force location to be / or /foo +location=${path:-/} + +# Check domain/path availability +sudo yunohost app checkurl $domain$path -a $app \ + || (echo "Path not available: $domain$path" && exit 1) + +# Validate redirect path +url_regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' +[[ ! $redirect_path =~ $url_regex ]] && ynh_die \ +"Invalid destination: $redirect_path" 1 + +# Nginx configuration +sed -i "s@YNH_LOCATION@$location@g" ../conf/nginx-*.conf +if [ "$redirect_type" = "visible_302" ]; +then + sed -i "s@YNH_REDIRECT_PATH@$redirect_path@g" ../conf/nginx-visible-302.conf + sudo cp ../conf/nginx-visible-302.conf /etc/nginx/conf.d/$domain.d/$app.conf +elif [ "$redirect_type" = "visible_301" ]; +then + sed -i "s@YNH_REDIRECT_PATH@$redirect_path@g" ../conf/nginx-visible-301.conf + sudo cp ../conf/nginx-visible-301.conf /etc/nginx/conf.d/$domain.d/$app.conf +elif [ "$redirect_type" = "proxy" ]; +then + sed -i "s@YNH_REDIRECT_PATH@$redirect_path@g" ../conf/nginx-proxy.conf + sudo cp ../conf/nginx-proxy.conf /etc/nginx/conf.d/$domain.d/$app.conf +fi + +# Make app public if necessary +if [[ "$is_public" -ne 0 ]]; +then + sudo yunohost app setting $app unprotected_uris -v "/" +fi + +# Reload Nginx and regenerate SSOwat conf +sudo service nginx reload +sudo yunohost app ssowatconf