mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
dynamicdns: Replace ez-ipupdate
Add Python implementation of GnuDIP client. Tests: - In testing container, configure Dynamic DNS with a (previously offlined) freedombox.rocks account. FreedomBox interface shows that the address has been updated. GnuDIP server also shows the correct IP address. - Running "gnudip update" and "dynamicdns update" actions produce the expected results.
This commit is contained in:
1 parent
bfbb5ac62b
commit
84a7323b42
4 files changed
+130
-31
No files matched your search
+14
-27
@@ -31,7 +31,7 @@ UPDATEMINUTES=5
|
||||
# a "blind" update
|
||||
UPDATEMINUTESUNKNOWN=3600
|
||||
TOOLNAME=ez-ipupdate
|
||||
UPDATE_TOOL=$(which ${TOOLNAME})
|
||||
GNUDIP_ACTION=/usr/share/plinth/actions/gnudip
|
||||
DISABLED_STRING='disabled'
|
||||
ENABLED_STRING='enabled'
|
||||
|
||||
@@ -157,13 +157,6 @@ doWriteCFG()
|
||||
else
|
||||
# we are directly connected
|
||||
echo "NAT no" >> ${HELPERCFG}
|
||||
# if this file is added ez-ipupdate will take ip form this interface
|
||||
{
|
||||
echo "interface=${default_interface}"
|
||||
# if this line is added to config file, ez-ipupdate will be launched on startup via init.d
|
||||
echo "daemon"
|
||||
echo "execute=${0} success"
|
||||
} >> ${out_file}
|
||||
fi
|
||||
fi
|
||||
}
|
||||
@@ -296,7 +289,7 @@ doGetWANIP()
|
||||
fi
|
||||
}
|
||||
|
||||
# actualy do the update (using wget or ez-ipupdate or even both)
|
||||
# actualy do the update (using wget or gnudip action or even both)
|
||||
# this function is called via cronjob
|
||||
doUpdate()
|
||||
{
|
||||
@@ -309,7 +302,11 @@ doUpdate()
|
||||
return
|
||||
fi
|
||||
if [ ! -z "${server}" ];then
|
||||
start-stop-daemon -S -x "${UPDATE_TOOL}" -m -p "${PIDFILE}" -- -c "${cfgfile}"
|
||||
if "${GNUDIP_ACTION}" update; then
|
||||
${0} success ${wanip}
|
||||
else
|
||||
${0} failed
|
||||
fi
|
||||
fi
|
||||
if [ ! -z "${updateurl}" ];then
|
||||
doReplaceVars
|
||||
@@ -351,24 +348,14 @@ case ${cmd} in
|
||||
;;
|
||||
start)
|
||||
doGetWANIP
|
||||
if [ "$(grep ^NAT ${HELPERCFG} | awk '{print $2}')" = "no" ];then
|
||||
#if we are not behind a NAT device and we use gnudip, start the daemon tool
|
||||
gnudipServer=$(grep ^server= ${cfgfile} 2> /dev/null | cut -d = -f 2- |grep -v ^\'\')
|
||||
if [ ! -f ${CFG} -a ! -z "${gnudipServer}" ];then
|
||||
mv ${CFG_disabled} ${CFG}
|
||||
/etc/init.d/${TOOLNAME} start
|
||||
fi
|
||||
# if we are not behind a NAT device and we use update-URL, add a cronjob
|
||||
# (daemon tool does not support update-URL feature)
|
||||
if [ ! -z "$(grep ^POSTURL $HELPERCFG | awk '{print $2}')" ];then
|
||||
echo "*/${UPDATEMINUTES} * * * * root ${0} update" > ${CRONJOB}
|
||||
$0 update
|
||||
fi
|
||||
else
|
||||
# if we are behind a NAT device, add a cronjob (daemon tool cannot monitor WAN IP changes)
|
||||
echo "*/${UPDATEMINUTES} * * * * root ${0} update" > $CRONJOB
|
||||
$0 update
|
||||
# if we use gnudip, enable gnudip.
|
||||
gnudipServer=$(grep ^server= ${cfgfile} 2> /dev/null | cut -d = -f 2- |grep -v ^\'\')
|
||||
if [ ! -f ${CFG} -a ! -z "${gnudipServer}" ];then
|
||||
mv ${CFG_disabled} ${CFG}
|
||||
fi
|
||||
# add a cronjob
|
||||
echo "*/${UPDATEMINUTES} * * * * root ${0} update" > $CRONJOB
|
||||
$0 update
|
||||
;;
|
||||
get-nat)
|
||||
NAT=$(grep ^NAT $HELPERCFG 2> /dev/null | awk '{print $2}')
|
||||
|
||||
Executable
+77
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/python3
|
||||
# -*- mode: python -*-
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""
|
||||
GnuDIP client for updating Dynamic DNS records.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
from plinth.modules.dynamicdns import gnudip
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CONFIG_FILE = pathlib.Path('/etc/ez-ipupdate/ez-ipupdate.conf')
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
"""Return parsed command line arguments as dictionary."""
|
||||
parser = argparse.ArgumentParser()
|
||||
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
|
||||
|
||||
subparsers.add_parser('update', help='Update Dynamic DNS record')
|
||||
|
||||
subparsers.required = True
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def subcommand_update(_):
|
||||
"""Update Dynamic DNS record.
|
||||
|
||||
Uses settings from ez-ipupdate config file.
|
||||
"""
|
||||
if not CONFIG_FILE.is_file():
|
||||
logger.info('GnuDIP configuration not found.')
|
||||
return
|
||||
|
||||
config = {}
|
||||
with CONFIG_FILE.open() as cf:
|
||||
lines = cf.readlines()
|
||||
for line in lines:
|
||||
if '=' in line:
|
||||
items = line.split('=')
|
||||
key = items[0].strip()
|
||||
value = items[1].strip()
|
||||
else:
|
||||
key = line.strip()
|
||||
value = True
|
||||
|
||||
config[key] = value
|
||||
|
||||
if not all(['host' in config, 'server' in config, 'user' in config]):
|
||||
logger.warn('GnuDIP configuration is not complete.')
|
||||
return
|
||||
|
||||
username, password = config['user'].split(':')
|
||||
result, new_ip = gnudip.update(config['server'], config['host'], username,
|
||||
password)
|
||||
if result == 0 and new_ip:
|
||||
print(new_ip)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
"""Parse arguments and perform all duties."""
|
||||
arguments = parse_arguments()
|
||||
|
||||
subcommand = arguments.subcommand.replace('-', '_')
|
||||
subcommand_method = globals()['subcommand_' + subcommand]
|
||||
sys.exit(subcommand_method(arguments))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user