summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-vpnbypass/root/usr/libexec/rpcd/luci.vpnbypass
blob: e5eb7c2ca25710c4bd711d3c518ff01f81103d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/sh
# Copyright 2021 Stan Grishin (stangri@melmac.net)
# shellcheck disable=SC1091,SC2039

# TechRef: https://openwrt.org/docs/techref/rpcd

. /lib/functions.sh
. /usr/share/libubox/jshn.sh

pkgName="vpnbypass"

is_enabled() { uci -q get "${1}.config.enabled"; }
is_running() { iptables -t mangle -L | grep -q VPNBYPASS && echo '1' || echo '0'; }
get_version() { grep -A2 -w "Package: $1$" /usr/lib/opkg/status | sed -n 's/Version: //p'; }
print_json_bool() { json_init; json_add_boolean "$1" "$2"; json_dump; json_cleanup; }
print_json_string() { json_init; json_add_string "$1" "$2"; json_dump; json_cleanup; }
logger() { /usr/bin/logger -t "$pkgName" "$@"; }

get_init_list() {
	local name="$1"
	json_init
	json_add_object "$name"
	json_add_boolean 'enabled' "$(is_enabled "$name")"
	json_add_boolean 'running' "$(is_running "$name")"
	json_close_object
	json_dump
	json_cleanup
}

set_init_action() {
	local name="$1" action="$2" cmd
	if [ ! -f "/etc/init.d/$name" ]; then
		print_json_string 'error' 'Init script not found!'
		return
	fi
	case $action in
		enable)
			cmd="uci -q set ${name}.config.enabled=1 && uci commit $name";;
		disable)
			cmd="uci -q 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="$1"
	json_init
	json_add_object "$name"
	json_add_boolean 'enabled' "$(is_enabled "$name")"
	json_add_boolean 'running' "$(is_running "$name")"
	json_add_string 'version' "$(get_version "$name")"
	json_close_object
	json_dump
	json_cleanup
}

case "$1" in
	list)
		json_init
		json_add_object "getInitList"
			json_add_string 'name' 'name'
		json_close_object
		json_add_object "setInitAction"
			json_add_string 'name' 'name'
			json_add_string 'action' 'action'
		json_close_object
		json_add_object "getInitStatus"
			json_add_string 'name' 'name'
		json_close_object
		json_dump
		json_cleanup
		;;
	call)
		case "$2" in
			getInitList)
				read -r input
				json_load "$input"
				json_get_var name 'name'
				json_cleanup
				get_init_list "$name"
				;;
			getInitStatus)
				read -r input
				json_load "$input"
				json_get_var name 'name'
				json_cleanup
				get_init_status "$name"
				;;
			setInitAction)
				read -r input
				json_load "$input"
				json_get_var name 'name'
				json_get_var action 'action'
				json_cleanup
				set_init_action "$name" "$action"
				;;
		esac
	;;
esac