forked from fabianishere/udm-iptv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udm-iptvd
executable file
·236 lines (198 loc) · 6.87 KB
/
udm-iptvd
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/sh
# Script for managing the udm-iptv service
#
# Copyright (C) 2021 Fabian Mastenbroek.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# Default options
IPTV_WAN_VLAN_INTERFACE="${IPTV_WAN_VLAN_INTERFACE:-iptv}"
IPTV_WAN_DHCP_OPTIONS="${IPTV_WAN_DHCP_OPTIONS:-"-O staticroutes -V IPTV_RG"}"
IPTV_LAN_RANGES="${IPTV_LAN_RANGES:-""}"
# Use busybox if udhcpc is not installed directly
if ! command -v udhcpc > /dev/null 2>&1; then
alias udhcpc="busybox udhcpc"
fi
# Setup the network, creating the IPTV VLAN interface if necessary
# and obtaining an IP address for the interface.
_network_setup() {
local target
target="$IPTV_WAN_INTERFACE"
# Make sure we obtain IP address for VLAN interface
if [ "$IPTV_WAN_VLAN" -gt 0 ]; then
target="$IPTV_WAN_VLAN_INTERFACE"
if ! ip link show "$IPTV_WAN_INTERFACE" >/dev/null; then
echo "Device $IPTV_WAN_INTERFACE for $IPTV_WAN_VLAN_INTERFACE does not exist"
exit 1
fi
# Delete existing VLAN interface
if [ -e /sys/class/net/"$IPTV_WAN_VLAN_INTERFACE" ]; then
echo "Device $IPTV_WAN_VLAN_INTERFACE already exists.. Deleting device"
ip link delete dev "$IPTV_WAN_VLAN_INTERFACE"
fi
ip link add link "$IPTV_WAN_INTERFACE" name "$IPTV_WAN_VLAN_INTERFACE" type vlan id "$IPTV_WAN_VLAN"
# Specify custom MAC address for VLAN interface
if [ -n "$IPTV_WAN_VLAN_MAC" ]; then
ip link set dev "$IPTV_WAN_VLAN_INTERFACE" address "$IPTV_WAN_VLAN_MAC"
fi
# Bring VLAN interface up
ip link set dev "$IPTV_WAN_VLAN_INTERFACE" up
if [ "$IPTV_WAN_DHCP" != "false" ]; then
echo "Obtaining IP address for VLAN interface"
# Obtain IP address for VLAN interface
udhcpc -b -R -p /var/run/udhcpc."$IPTV_WAN_VLAN_INTERFACE".pid -s "$(dirname "$0")"/udhcpc.hook -i "$IPTV_WAN_VLAN_INTERFACE" $IPTV_WAN_DHCP_OPTIONS
fi
fi
if [ "$IPTV_WAN_DHCP" = "false" ] && [ -n "$IPTV_WAN_STATIC_IP" ]; then
ip -4 addr change "$IPTV_WAN_STATIC_IP" dev "$target"
fi
echo "NATing IPTV network ranges (if necessary)"
# NAT the IPTV ranges
for range in $IPTV_WAN_RANGES; do
# Only add the NAT rule if it does not yet exist
if ! iptables -C POSTROUTING -t nat -d "$range" -j MASQUERADE -o "$target" >/dev/null 2>&1; then
iptables -A POSTROUTING -t nat -d "$range" -j MASQUERADE -o "$target"
fi
done
echo "Creating static routes (if necessary)"
for range in $IPTV_STATIC_ROUTES; do
ip route add "$range" dev "$target"
done
}
# Watch for network changes and re-apply the configuration
_network_watch() {
if [ "$IPTV_WAN_VLAN" -gt 0 ]; then
return
fi
echo "Monitoring interface $IPTV_WAN_INTERFACE for changes"
# Monitor for changes on the WAN interface
ip monitor address dev "$IPTV_WAN_INTERFACE" | while read line; do
if echo "$line" | grep -q "Deleted"; then
_network_setup
fi
done & # Monitoring happens in background
}
# Tear down the network configuration
_network_teardown() {
# Delete the IPTables rules
for range in $IPTV_WAN_RANGES; do
iptables -D POSTROUTING -t nat -d "$range" -j MASQUERADE -o "$target" >/dev/null 2>&1
done
}
# Build the configuration needed by IGMP Proxy
_igmpproxy_build_config() {
echo "# igmpproxy configuration for udm-iptv"
if [ -z "$IPTV_IGMPPROXY_DISABLE_QUICKLEAVE" ] || [ "$IPTV_IGMPPROXY_DISABLE_QUICKLEAVE" = "false" ]; then
echo "quickleave"
fi
local target
if [ "$IPTV_WAN_VLAN" -gt 0 ]; then
target="$IPTV_WAN_VLAN_INTERFACE"
else
target="$IPTV_WAN_INTERFACE"
fi
echo "phyint $target upstream ratelimit 0 threshold 1"
for range in $IPTV_LAN_RANGES $IPTV_WAN_RANGES; do
echo " altnet $range"
done
# Configure the igmpproxy interfaces
for path in /sys/class/net/*; do
local interface
interface=$(basename "$path")
if echo "$IPTV_LAN_INTERFACES" | grep -w -q "$interface"; then
echo "phyint $interface downstream ratelimit 0 threshold 1"
elif [ "$interface" != "lo" ] && [ "$interface" != "$target" ]; then
echo "phyint $interface disabled"
fi
done
}
# Build the configuration needed by IMProxy
_igmpproxy_build_config_improxy() {
echo "# improxy configuration for udm-iptv"
if [ "$IPTV_IGMPPROXY_IGMP_VERSION" = 3 ]; then
echo "igmp enable version 3"
else
echo "igmp enable version 2"
fi
echo "mld disable"
local target
if [ "$IPTV_WAN_VLAN" -gt 0 ]; then
target="$IPTV_WAN_VLAN_INTERFACE"
else
target="$IPTV_WAN_INTERFACE"
fi
echo "upstream $target"
# Configure the upstream interfaces
for path in /sys/class/net/*; do
local interface
interface=$(basename "$path")
if echo "$IPTV_LAN_INTERFACES" | grep -w -q "$interface"; then
echo "downstream $interface"
fi
done
}
# Configure IGMP Proxy to bridge multicast traffic
_igmpproxy_setup() {
echo "Setting up IGMP Proxy"
if [ "$IPTV_IGMPPROXY_PROGRAM" = "improxy" ]; then
echo "Using improxy..."
_igmpproxy_build_config_improxy > /var/run/igmpproxy.iptv.conf
else
echo "Using igmpproxy..."
_igmpproxy_build_config > /var/run/igmpproxy.iptv.conf
fi
}
# Tear down the IGMP Proxy configuration
_igmpproxy_teardown() {
rm -f /var/run/igmpproxy.iptv.conf
}
# Start the igmpproxy service
_igmpproxy_start() {
echo "Starting IGMP Proxy"
args=""
if [ -n "$IPTV_IGMPPROXY_DEBUG" ] && [ "$IPTV_IGMPPROXY_DEBUG" != "false" ]; then
if [ "$IPTV_IGMPPROXY_PROGRAM" = "improxy" ]; then
args="-d 5"
else
args="-d -v"
fi
fi
if [ "$IPTV_IGMPPROXY_PROGRAM" = "improxy" ]; then
if ! command -v improxy > /dev/null 2>&1; then
echo "error: improxy not installed..."
exit 1
fi
# shellcheck disable=SC2086
exec improxy $args "$@" -c /var/run/igmpproxy.iptv.conf
else
if ! command -v igmpproxy > /dev/null 2>&1; then
echo "error: igmpproxy not installed..."
exit 1
fi
# shellcheck disable=SC2086
exec igmpproxy -n $args "$@" /var/run/igmpproxy.iptv.conf
fi
}
# Check if the user passed any argument
if [ $# -eq 0 ]; then
exit 1
fi
case $1 in
start)
shift
_network_setup
_network_watch
_igmpproxy_setup
_igmpproxy_start "$@"
;;
stop)
shift
_network_teardown
_igmpproxy_teardown
;;
*)
exit 1
;;
esac