From cc006c23379fb2ee8a0cd05896a841a68083b0eb Mon Sep 17 00:00:00 2001 From: bolvan Date: Thu, 16 May 2019 08:06:32 +0300 Subject: [PATCH] sysv init : more code dedup --- init.d/sysv/functions | 83 ++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/init.d/sysv/functions b/init.d/sysv/functions index b86da15..438e775 100644 --- a/init.d/sysv/functions +++ b/init.d/sysv/functions @@ -37,6 +37,20 @@ exists() which $1 >/dev/null 2>/dev/null } +on_off_function() +{ + # $1 : function name on + # $2 : function name off + # $3 : 0 - off, 1 - on + local F="$1" + [ "$3" = "1" ] || F="$2" + shift + shift + shift + "$F" $@ +} + + ipt() { iptables -C $@ 2>/dev/null || iptables -I $@ @@ -47,13 +61,7 @@ ipt_del() } ipt_add_del() { - if [ "$1" = "1" ]; then - shift - ipt $@ - else - shift - ipt_del $@ - fi + on_off_function ipt ipt_del $@ } ipt6() { @@ -65,13 +73,7 @@ ipt6_del() } ipt6_add_del() { - if [ "$1" = "1" ]; then - shift - ipt6 $@ - else - shift - ipt6_del $@ - fi + on_off_function ipt6 ipt6_del $@ } # there's no route_localnet for ipv6 @@ -254,6 +256,12 @@ stop_daemon() fi fi } +do_daemon() +{ + # $1 - 1 - run, 0 - stop + on_off_function run_daemon stop_daemon $@ +} + prepare_tpws() { @@ -283,6 +291,11 @@ stop_tpws() [ -n "$IFACE_LAN" ] && stop_daemon $((660+$1)) $TPWS } } +do_tpws() +{ + # $1 - 1 - run, 0 - stop + on_off_function run_tpws stop_tpws $@ +} create_ipset() @@ -360,52 +373,42 @@ zapret_unapply_firewall() zapret_do_firewall 0 $@ } -zapret_run_daemons() +zapret_do_daemons() { + # $1 - 1 - run, 0 - stop + case "${MODE}" in tpws_hostlist) - run_tpws 1 "$TPWS_OPT_BASE_HTTP $TPWS_OPT_HTTP --hostlist=$TPWS_HOSTLIST" + do_tpws $1 1 "$TPWS_OPT_BASE_HTTP $TPWS_OPT_HTTP --hostlist=$TPWS_HOSTLIST" ;; tpws_ipset) - run_tpws 1 "$TPWS_OPT_BASE_HTTP $TPWS_OPT_HTTP" + do_tpws $1 1 "$TPWS_OPT_BASE_HTTP $TPWS_OPT_HTTP" ;; tpws_ipset_https|tpws_all_https) - run_tpws 1 "$TPWS_OPT_BASE_HTTP $TPWS_OPT_HTTP" - run_tpws 2 "$TPWS_OPT_BASE_HTTPS $TPWS_OPT_HTTPS" + do_tpws $1 1 "$TPWS_OPT_BASE_HTTP $TPWS_OPT_HTTP" + do_tpws $1 2 "$TPWS_OPT_BASE_HTTPS $TPWS_OPT_HTTPS" ;; tpws_all) - run_tpws 1 "$TPWS_OPT_BASE_HTTP $TPWS_OPT_HTTP" + do_tpws $1 1 "$TPWS_OPT_BASE_HTTP $TPWS_OPT_HTTP" ;; nfqws_ipset|nfqws_ipset_https|nfqws_all|nfqws_all_https) - run_daemon 1 $NFQWS "$NFQWS_OPT_BASE $NFQWS_OPT" + do_daemon $1 1 $NFQWS "$NFQWS_OPT_BASE $NFQWS_OPT" ;; custom) # PLACEHOLDER echo !!! NEED ATTENTION !!! echo Start daemon\(s\) echo Study how other sections work - run_daemon 1 /bin/sleep 20 + do_daemon $1 1 /bin/sleep 20 ;; esac } + +zapret_run_daemons() +{ + zapret_do_daemons 1 $@ +} zapret_stop_daemons() { - case "${MODE}" in - tpws_hostlist|tpws_all|tpws_ipset) - stop_tpws 1 - ;; - tpws_ipset_https|tpws_all_https) - stop_tpws 1 - stop_tpws 2 - ;; - nfqws_ipset|nfqws_ipset_https|nfqws_all|nfqws_all_https) - stop_daemon 1 $NFQWS - ;; - custom) - # PLACEHOLDER - echo !!! NEED ATTENTION !!! - echo Stop daemon\(s\) previously started. - echo Study how other sections work. - ;; - esac + zapret_do_daemons 0 $@ }