docker-qBittorrentvpn/qbittorrent/iptables.sh

171 lines
5.2 KiB
Bash
Raw Normal View History

2018-02-03 02:21:12 +00:00
#!/bin/bash
# Kanged from binhex's OpenVPN dockers
# ip route
###
2018-02-03 07:24:52 +00:00
while true; do
2018-02-03 07:34:00 +00:00
if [ -c /dev/net/tun ]; then
2018-02-03 07:24:52 +00:00
break
else
sleep 1
fi
done
2018-02-03 02:21:12 +00:00
# split comma seperated string into list from LAN_NETWORK env variable
IFS=',' read -ra lan_network_list <<< "${LAN_NETWORK}"
# process lan networks in the list
for lan_network_item in "${lan_network_list[@]}"; do
# strip whitespace from start and end of lan_network_item
lan_network_item=$(echo "${lan_network_item}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
echo "[info] Adding ${lan_network_item} as route via docker eth0"
ip route add "${lan_network_item}" via "${DEFAULT_GATEWAY}" dev eth0
done
2018-02-03 07:32:25 +00:00
echo "[info] ip route defined as follows..." | ts '%Y-%m-%d %H:%M:%.S'
2018-02-03 02:21:12 +00:00
echo "--------------------"
ip route
echo "--------------------"
# setup iptables marks to allow routing of defined ports via eth0
###
if [[ "${DEBUG}" == "true" ]]; then
2018-02-03 03:45:09 +00:00
echo "[debug] Modules currently loaded for kernel" ; lsmod
2018-02-03 02:21:12 +00:00
fi
# check we have iptable_mangle, if so setup fwmark
lsmod | grep iptable_mangle
iptable_mangle_exit_code=$?
if [[ $iptable_mangle_exit_code == 0 ]]; then
2018-02-03 07:32:25 +00:00
echo "[info] iptable_mangle support detected, adding fwmark for tables" | ts '%Y-%m-%d %H:%M:%.S'
2018-02-03 02:21:12 +00:00
# setup route for deluge webui using set-mark to route traffic for port 8112 to eth0
2018-02-03 04:11:15 +00:00
echo "8080 webui" >> /etc/iproute2/rt_tables
2018-02-03 02:21:12 +00:00
ip rule add fwmark 1 table webui
ip route add default via $DEFAULT_GATEWAY table webui
fi
# identify docker bridge interface name (probably eth0)
docker_interface=$(netstat -ie | grep -vE "lo|tun|tap" | sed -n '1!p' | grep -P -o -m 1 '^[^:]+')
if [[ "${DEBUG}" == "true" ]]; then
2018-02-03 03:45:09 +00:00
echo "[debug] Docker interface defined as ${docker_interface}"
2018-02-03 02:21:12 +00:00
fi
# identify ip for docker bridge interface
docker_ip=$(ifconfig "${docker_interface}" | grep -P -o -m 1 '(?<=inet\s)[^\s]+')
if [[ "${DEBUG}" == "true" ]]; then
2018-02-03 03:45:09 +00:00
echo "[debug] Docker IP defined as ${docker_ip}"
2018-02-03 02:21:12 +00:00
fi
# identify netmask for docker bridge interface
docker_mask=$(ifconfig "${docker_interface}" | grep -P -o -m 1 '(?<=netmask\s)[^\s]+')
if [[ "${DEBUG}" == "true" ]]; then
2018-02-03 03:45:09 +00:00
echo "[debug] Docker netmask defined as ${docker_mask}"
2018-02-03 02:21:12 +00:00
fi
# convert netmask into cidr format
docker_network_cidr=$(ipcalc "${docker_ip}" "${docker_mask}" | grep -P -o -m 1 "(?<=Network:)\s+[^\s]+")
2018-02-03 07:32:25 +00:00
echo "[info] Docker network defined as ${docker_network_cidr}" | ts '%Y-%m-%d %H:%M:%.S'
2018-02-03 02:21:12 +00:00
# input iptable rules
###
# set policy to drop ipv4 for input
iptables -P INPUT DROP
# set policy to drop ipv6 for input
ip6tables -P INPUT DROP 1>&- 2>&-
# accept input to tunnel adapter
iptables -A INPUT -i "${VPN_DEVICE_TYPE}" -j ACCEPT
# accept input to/from docker containers (172.x range is internal dhcp)
iptables -A INPUT -s "${docker_network_cidr}" -d "${docker_network_cidr}" -j ACCEPT
# accept input to vpn gateway
iptables -A INPUT -i eth0 -p $VPN_PROTOCOL --sport $VPN_PORT -j ACCEPT
# accept input to deluge webui port 8112
2018-02-03 04:06:45 +00:00
iptables -A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 8080 -j ACCEPT
2018-02-03 02:21:12 +00:00
# process lan networks in the list
for lan_network_item in "${lan_network_list[@]}"; do
# strip whitespace from start and end of lan_network_item
lan_network_item=$(echo "${lan_network_item}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
# accept input to deluge daemon port - used for lan access
2018-02-03 04:06:45 +00:00
iptables -A INPUT -i eth0 -s "${lan_network_item}" -p tcp --dport 8999 -j ACCEPT
2018-02-03 02:21:12 +00:00
done
# accept input icmp (ping)
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
# accept input to local loopback
iptables -A INPUT -i lo -j ACCEPT
# output iptable rules
###
# set policy to drop ipv4 for output
iptables -P OUTPUT DROP
# set policy to drop ipv6 for output
ip6tables -P OUTPUT DROP 1>&- 2>&-
# accept output from tunnel adapter
iptables -A OUTPUT -o "${VPN_DEVICE_TYPE}" -j ACCEPT
# accept output to/from docker containers (172.x range is internal dhcp)
iptables -A OUTPUT -s "${docker_network_cidr}" -d "${docker_network_cidr}" -j ACCEPT
# accept output from vpn gateway
iptables -A OUTPUT -o eth0 -p $VPN_PROTOCOL --dport $VPN_PORT -j ACCEPT
# if iptable mangle is available (kernel module) then use mark
if [[ $iptable_mangle_exit_code == 0 ]]; then
# accept output from deluge webui port 8112 - used for external access
iptables -t mangle -A OUTPUT -p tcp --dport 8112 -j MARK --set-mark 1
iptables -t mangle -A OUTPUT -p tcp --sport 8112 -j MARK --set-mark 1
fi
# accept output from deluge webui port 8112 - used for lan access
2018-02-03 04:06:45 +00:00
iptables -A OUTPUT -o eth0 -p tcp --dport 8080 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 8080 -j ACCEPT
2018-02-03 02:21:12 +00:00
# process lan networks in the list
for lan_network_item in "${lan_network_list[@]}"; do
# strip whitespace from start and end of lan_network_item
lan_network_item=$(echo "${lan_network_item}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~')
# accept output to deluge daemon port - used for lan access
2018-02-03 04:06:45 +00:00
iptables -A OUTPUT -o eth0 -d "${lan_network_item}" -p tcp --sport 8999 -j ACCEPT
2018-02-03 02:21:12 +00:00
done
# accept output for icmp (ping)
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
# accept output from local loopback adapter
iptables -A OUTPUT -o lo -j ACCEPT
2018-02-03 07:32:25 +00:00
echo "[info] iptables defined as follows..." | ts '%Y-%m-%d %H:%M:%.S'
2018-02-03 02:21:12 +00:00
echo "--------------------"
iptables -S
echo "--------------------"
2018-02-03 07:25:24 +00:00
exec /bin/bash /etc/qbittorrent/startup.sh