#!/bin/sh
# Copyright 2022 Stan Grishin (stangri@melmac.ca)
# shellcheck disable=SC2018,SC2019,SC2039,SC3043,SC3057,SC3060
# TechRef: https://openwrt.org/docs/techref/rpcd
# TESTS
# ubus -v list luci.pbr
# ubus -S call luci.pbr getInitList '{"name": "pbr" }'
# ubus -S call luci.pbr getInitStatus '{"name": "pbr" }'
# ubus -S call luci.pbr getPlatformSupport '{"name": "pbr" }'
# ubus -S call luci.pbr getGateways '{"name": "pbr" }'
# ubus -S call luci.pbr getInterfaces '{"name": "pbr" }'
readonly luciCompat='8'
readonly pbrFunctionsFile='/etc/init.d/pbr'
if [ -s "$pbrFunctionsFile" ]; then
# shellcheck source=../../../../../pbr/files/etc/init.d/pbr
. "$pbrFunctionsFile"
else
print_json_string 'error' "pbr init.d file ($pbrFunctionsFile) not found!"
logger -t pbr 'error' "pbr init.d file ($pbrFunctionsFile) not found!"
fi
# compatibility with old luci app versions
is_running_iptables() { iptables -t mangle -L | grep -q PBR_PREROUTING >/dev/null 2>&1; }
is_running() { is_running_iptables || is_running_nft; }
check_ipset() { { [ -n "$ipset" ] && "$ipset" help hash:net; } >/dev/null 2>&1; }
check_agh_ipset() {
check_ipset || return 1
check_agh || return 1
is_greater_or_equal "$($agh --version | sed 's|AdGuard Home, version v\(.*\)|\1|' | sed 's|-.*||')" '0.107.13'
}
check_dnsmasq_ipset() {
local o;
check_ipset || return 1
check_dnsmasq || return 1
o="$(dnsmasq -v 2>/dev/null)"
! echo "$o" | grep -q 'no-ipset' && echo "$o" | grep -q 'ipset'
}
get_init_list() {
local name
name="$(basename "$1")"
name="${name:-$packageName}"
json_init
json_add_object "$packageName"
json_add_boolean 'enabled' "$(is_enabled "$packageName")"
if is_running "$packageName"; then
json_add_boolean 'running' '1'
else
json_add_boolean 'running' '0'
fi
json_close_object
json_dump
json_cleanup
}
set_init_action() {
local name action="$2" cmd
name="$(basename "$1")"
name="${name:-$packageName}"
if [ ! -f "/etc/init.d/$packageName" ]; then
print_json_string 'error' 'Init script not found!'
return
fi
case $action in
enable)
cmd="/etc/init.d/${name} ${action}"
cmd="${cmd} && uci_set ${name} config enabled 1 && uci_commit $name"
;;
disable)
cmd="/etc/init.d/${name} ${action}"
cmd="${cmd} && uci_set ${name} config enabled 0 && uci_commit $name"
;;
start|stop|reload|restart)
cmd="/etc/init.d/${name} ${action}"
;;
esac
if [ -n "$cmd" ] && eval "$cmd" 1>/dev/null 2>&1; then
print_json_bool 'result' '1'
else
print_json_bool 'result' '0'
fi
}
get_init_status() {
local name
name="$(basename "$1")"
name="${name:-$packageName}"
local gateways warnings errors
gateways="$(ubus_get_status gateways | sed "s|\\\n|
|g;s|\(\\\033[^<]*\)|✓|g;")"
warnings="$(ubus_get_status warnings)"
errors="$(ubus_get_status errors)"
json_init
json_add_object "$packageName"
json_add_boolean 'enabled' "$(is_enabled "$packageName")"
if is_running "$packageName"; then
json_add_boolean 'running' '1'
else
json_add_boolean 'running' '0'
fi
if is_running_iptables "$packageName"; then
json_add_boolean 'running_iptables' '1'
else
json_add_boolean 'running_iptables' '0'
fi
if is_running_nft "$packageName"; then
json_add_boolean 'running_nft' '1'
else
json_add_boolean 'running_nft' '0'
fi
if is_running_nft_file "$packageName"; then
json_add_boolean 'running_nft_file' '1'
else
json_add_boolean 'running_nft_file' '0'
fi
json_add_string 'version' "$PKG_VERSION"
json_add_string 'gateways' "$gateways"
json_add_array 'errors'
if [ -n "$errors" ]; then
while read -r line; do
if str_contains "$line" ' '; then
error_id="${line% *}"
error_extra="${line#* }"
else
error_id="$line"
unset error_extra
fi
json_add_object
json_add_string 'id' "$error_id"
json_add_string 'extra' "$error_extra"
json_close_object
done <