1
0
mirror of https://github.com/mail-in-a-box/mailinabox.git synced 2026-03-12 17:07:23 +01:00

Fix url redirection when a remote nextcloud is used so that .well-known/caldav and carddav work properly, as well as the redirecting /cloud to the remote Nextcloud. Since the nginx config is replaced by the management daemon whenever a new domain is added, this change adds a hooking mechanism for setup mods.Fix url redirection when a remote nextcloud is in use. This corrects redirection for /.well-known/caldav, /.well-known/carddav and /cloud to send the client to the remote nextcloud. This requires an nginx configuration change, and since the nginx config is replaced by the management daemon whenever a new domain is added, this change adds a hooking mechanism for setup mods allowing them to intercept and modify the resultant nginx config.

This commit is contained in:
downtownallday
2022-09-21 15:52:47 -04:00
parent dae697e6af
commit 53cbabac75
13 changed files with 282 additions and 5 deletions

View File

@@ -0,0 +1,63 @@
#
# This is a web_update management hook for the remote-nextcloud setup
# mod.
#
# When management/web_update.py creates a new nginx configuration file
# "local.conf", this mod will ensure that .well-known/caldav and
# .well-known/carddav urls are redirected to the remote nextcloud.
#
# The hook is enabled by placing the file in directory
# LOCAL_MODS_DIR/managment_hooks_d.
#
import os
import logging
log = logging.getLogger(__name__)
def do_hook(hook_name, hook_data, mods_env):
if hook_name != 'web_update':
# we only care about hooking web_update
log.debug('hook - ignoring %s' % hook_name)
return False
if 'NC_HOST' not in mods_env or mods_env['NC_HOST'].strip() == '':
# not configured for a remote nextcloud
log.debug('hook - not configured for a remote nextcloud')
return False
# get the remote nextcloud url and ensure no tailing /
nc_url = "%s://%s:%s%s" % (
mods_env['NC_PROTO'],
mods_env['NC_HOST'],
mods_env['NC_PORT'],
mods_env['NC_PREFIX'][0:-1] if mods_env['NC_PREFIX'].endswith('/') else mods_env['NC_PREFIX']
)
#
# modify nginx_conf
#
def do_replace(find_str, replace_with):
if hook_data['nginx_conf'].find(find_str) == -1:
log.warning('remote-nextcloud hook: string "%s" not found in proposed nginx_conf' % (find_str))
return False
hook_data['nginx_conf'] = hook_data['nginx_conf'].replace(
find_str,
replace_with
)
return True
# 1. change the .well-known/(caldav|carddav) redirects
do_replace(
'/cloud/remote.php/dav/',
'%s/remote.php/dav/' % nc_url
)
# 2. redirect /cloud to the remote nextcloud
do_replace(
'rewrite ^/cloud/$ /cloud/index.php;',
'rewrite ^/cloud/(.*)$ %s/$1 redirect;' % nc_url
)