immortalwrt/package/network/services/omcproxy/files/omcproxy.init

167 lines
3.5 KiB
Plaintext
Raw Normal View History

2017-09-06 19:19:45 +08:00
#!/bin/sh /etc/rc.common
2020-03-28 23:37:17 +08:00
# Copyright (C) 2018 OpenWrt.org
2017-09-06 19:19:45 +08:00
START=99
USE_PROCD=1
PROG=/usr/sbin/omcproxy
2020-03-28 23:37:17 +08:00
# Uncomment to enable verbosity
#OPTIONS="-v"
2017-09-06 19:19:45 +08:00
PROXIES=""
omcproxy_add_proxy() {
2020-03-28 23:37:17 +08:00
local proxy scope uplink updevice downlinks
2017-09-06 19:19:45 +08:00
config_get uplink $1 uplink
2020-03-28 23:37:17 +08:00
[ -n "$uplink" ] || return
2017-09-06 19:19:45 +08:00
2020-03-28 23:37:17 +08:00
network_get_device updevice "$uplink" || {
procd_append_param error "$uplink is not up"
return;
}
2017-09-06 19:19:45 +08:00
2020-03-28 23:37:17 +08:00
config_get downlinks $1 downlink
for downlink in $downlinks; do
local device
2017-09-06 19:19:45 +08:00
2020-03-28 23:37:17 +08:00
network_get_device device "$downlink" || {
procd_append_param error "$downlink is not up"
continue;
}
2020-03-28 23:37:17 +08:00
proxy="$proxy,$device"
# Disable in-kernel querier while ours is active, default is 1.
[ -f /sys/class/net/$device/bridge/multicast_querier ] && \
echo 0 > /sys/class/net/$device/bridge/multicast_querier
2017-09-06 19:19:45 +08:00
done
[ -n "$proxy" ] || return 0
2020-03-28 23:37:17 +08:00
config_get scope $1 scope
[ -n "$scope" ] && proxy="$proxy,scope=$scope"
2017-09-06 19:19:45 +08:00
2020-03-28 23:37:17 +08:00
PROXIES="$PROXIES $updevice$proxy"
}
2020-03-28 23:37:17 +08:00
omcproxy_add_network_triggers() {
local uplink downlinks
2017-09-06 19:19:45 +08:00
config_get uplink $1 uplink
2020-03-28 23:37:17 +08:00
config_get downlinks $1 downlink
for link in $uplink $downlinks; do
local duplicate=0
2017-09-06 19:19:45 +08:00
2020-03-28 23:37:17 +08:00
for l in $LINKS; do
[ "$l" = "$link" ] && duplicate=1
done
[ "$duplicate" = 0 ] && {
LINKS="$LINKS $link"
procd_add_interface_trigger "interface.*" $link /etc/init.d/omcproxy restart
}
2017-09-06 19:19:45 +08:00
done
}
2020-03-28 23:37:17 +08:00
omcproxy_add_firewall_rules() {
local uplink downlinks
2017-09-06 19:19:45 +08:00
config_get uplink $1 uplink
2020-03-28 23:37:17 +08:00
config_get downlinks $1 downlink
2017-09-06 19:19:45 +08:00
2018-01-15 18:26:41 +08:00
upzone=$(fw3 -q network $uplink 2>/dev/null)
2017-09-06 19:19:45 +08:00
[ -n "$upzone" ] || return 0
json_add_object ""
json_add_string type rule
json_add_string src "$upzone"
2020-03-28 23:37:17 +08:00
json_add_string family ipv4
2017-09-06 19:19:45 +08:00
json_add_string proto igmp
json_add_string target ACCEPT
json_close_object
json_add_object ""
json_add_string type rule
json_add_string family ipv6
json_add_string src "$upzone"
json_add_string proto icmp
json_add_string src_ip fe80::/10
json_add_array icmp_type
json_add_string "" 130/0
json_add_string "" 131/0
json_add_string "" 132/0
json_add_string "" 143/0
json_close_array
json_add_string target ACCEPT
json_close_object
2020-03-28 23:37:17 +08:00
for downlink in $downlinks; do
downzone=$(fw3 -q network $downlink 2>/dev/null)
2017-09-06 19:19:45 +08:00
[ -n "$downzone" ] || continue
json_add_object ""
json_add_string type rule
json_add_string src "$upzone"
json_add_string dest "$downzone"
json_add_string family ipv4
2020-03-28 23:37:17 +08:00
json_add_string proto udp
2017-09-06 19:19:45 +08:00
json_add_string dest_ip "224.0.0.0/4"
json_add_string target ACCEPT
json_close_object
json_add_object ""
json_add_string type rule
json_add_string src "$upzone"
json_add_string dest "$downzone"
json_add_string family ipv6
2020-03-28 23:37:17 +08:00
json_add_string proto udp
2017-09-06 19:19:45 +08:00
json_add_string dest_ip "ff00::/8"
json_add_string target ACCEPT
json_close_object
done
}
service_triggers() {
2020-03-28 23:37:17 +08:00
LINKS=""
2017-09-06 19:19:45 +08:00
procd_add_reload_trigger "omcproxy"
2020-03-28 23:37:17 +08:00
config_foreach omcproxy_add_network_triggers proxy
2017-09-06 19:19:45 +08:00
}
start_service() {
2020-03-28 23:37:17 +08:00
. /lib/functions/network.sh
2017-09-06 19:19:45 +08:00
config_load omcproxy
2020-03-28 23:37:17 +08:00
config_foreach omcproxy_add_proxy proxy
2017-09-06 19:19:45 +08:00
[ -n "$PROXIES" ] || return 0
procd_open_instance
procd_set_param command $PROG
[ -n "$OPTIONS" ] && procd_append_param command $OPTIONS
procd_append_param command $PROXIES
procd_set_param respawn
procd_open_data
json_add_array firewall
2020-03-28 23:37:17 +08:00
config_foreach omcproxy_add_firewall_rules proxy
2017-09-06 19:19:45 +08:00
json_close_array
procd_close_data
procd_close_instance
2020-03-28 23:37:17 +08:00
# Increase maximum IPv4 group memberships per socket, default is 100.
2017-09-06 19:19:45 +08:00
echo 128 > /proc/sys/net/ipv4/igmp_max_memberships
}
service_started() {
procd_set_config_changed firewall
}
2020-03-28 23:37:17 +08:00
stop_service() {
procd_set_config_changed firewall
}