1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/hotspot_ynh.git synced 2024-09-03 19:25:53 +02:00
hotspot_ynh/conf/ynh-hotspot
2014-11-05 23:41:51 +01:00

227 lines
5.1 KiB
Bash

#!/bin/bash
### BEGIN INIT INFO
# Provides: ynh-hotspot
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Set prerequisites for wifi hotspot.
# Description: Set prerequisites for wifi hotspot.
### END INIT INFO
is_ndproxy_set() {
proxy=$(ip -6 neigh show proxy)
[ ! -z "${proxy}" ]
}
is_nat_set() {
iptables -nt nat -L POSTROUTING | grep -q MASQUERADE
}
is_ip4nataddr_set() {
ip a s dev <TPL:WIFI_DEVICE> | grep -q <TPL:IP4_NAT_PREFIX>.1/24
}
is_ip6addr_set() {
ip a s dev <TPL:WIFI_DEVICE> | grep -q <TPL:IP6_ADDR>/64
}
is_forwarding_set() {
ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
[ ${ip6} -eq 1 -a ${ip4} -eq 1 ]
}
is_hostapd_running() {
service hostapd status &> /dev/null
}
is_radvd_running() {
service radvd status &> /dev/null
}
is_dhcpd_running() {
service isc-dhcp-server status &> /dev/null
}
is_running() {
is_ndproxy_set && is_nat_set && is_forwarding_set\
&& is_hostapd_running && is_radvd_running && is_dhcpd_running
}
internet_device=$(ip r | awk '/default via/ { print $NF; }')
internet_device_vpn=tun0
internet_device_default=${internet_device}
# Apply NAT on tun0 if IPv6 address if VPN is used
ip l sh dev tun0 &> /dev/null
if [ "$?" -eq 0 ]; then
internet_device_default=${internet_device_vpn}
fi
case "$1" in
start)
if is_running; then
echo "Already correctly set"
else
if ! is_ndproxy_set; then
echo "Set NDP proxy"
ip -6 neigh add proxy <TPL:IP6_ADDR> dev <TPL:WIFI_DEVICE>
fi
if ! is_nat_set; then
echo "Set NAT"
iptables -t nat -A POSTROUTING -o ${internet_device_default} -j MASQUERADE
fi
if ! is_ip4nataddr_set; then
echo "Set IPv4 NAT address"
ip a a <TPL:IP4_NAT_PREFIX>.1/24 dev <TPL:WIFI_DEVICE>
fi
if ! is_ip6addr_set; then
echo "Set IPv6 address"
ip a a <TPL:IP6_ADDR>/64 dev <TPL:WIFI_DEVICE>
fi
if ! is_forwarding_set; then
echo "Set forwarding"
sysctl -w net.ipv6.conf.all.forwarding=1 &> /dev/null
sysctl -w net.ipv4.conf.all.forwarding=1 &> /dev/null
fi
if ! is_hostapd_running; then
echo "Run hostapd"
service hostapd start
fi
# must be running after hostapd
if ! is_radvd_running; then
echo "Run radvd"
sleep 1
service radvd start
fi
# "options routers" addr (is_ip6addr_set) must be set before
if ! is_dhcpd_running; then
echo "Run dhcpd"
service isc-dhcp-server start
fi
fi
;;
stop)
if is_ndproxy_set; then
echo "Unset NDP proxy"
ip -6 neigh del proxy <TPL:IP6_ADDR> dev <TPL:WIFI_DEVICE>
fi
if is_nat_set; then
echo "Unset NAT"
# Depending on the presence or not of a VPN at the last startup, one of these 2 lines is unnecessary
iptables -t nat -D POSTROUTING -o ${internet_device} -j MASQUERADE 2> /dev/null
iptables -t nat -D POSTROUTING -o ${internet_device_vpn} -j MASQUERADE 2> /dev/null
fi
if is_ip4nataddr_set; then
echo "Unset IPv4 NAT address"
ip a d <TPL:IP4_NAT_PREFIX>.1/24 dev <TPL:WIFI_DEVICE>
fi
if is_ip6addr_set; then
echo "Unset IPv6 address"
ip a d <TPL:IP6_ADDR>/64 dev <TPL:WIFI_DEVICE>
fi
if is_forwarding_set; then
echo "Unset forwarding"
sysctl -w net.ipv6.conf.all.forwarding=0 &> /dev/null
sysctl -w net.ipv4.conf.all.forwarding=0 &> /dev/null
fi
if is_hostapd_running; then
echo "Stop hostapd"
service hostapd stop
fi
if is_radvd_running; then
echo "Stop radvd"
service radvd stop
fi
if is_dhcpd_running; then
echo "Stop dhcpd"
service isc-dhcp-server stop
fi
;;
restart)
$0 stop
$0 start
;;
status)
exitcode=0
if is_ndproxy_set; then
echo "NDP proxy is correctly set"
else
echo "NDP proxy is NOT set"
exitcode=1
fi
if is_nat_set; then
echo "NAT is correctly set"
else
echo "NAT is NOT set"
exitcode=1
fi
if is_ip4nataddr_set; then
echo "IPv4 NAT address is correctly set"
else
echo "IPv4 NAT address is NOT set"
exitcode=1
fi
if is_ip6addr_set; then
echo "IPv6 address is correctly set"
else
echo "IPv6 address is NOT set"
exitcode=1
fi
if is_forwarding_set; then
echo "Forwarding is correctly set"
else
echo "Forwarding is NOT set"
exitcode=1
fi
if is_hostapd_running; then
echo "Hostapd is running"
else
echo "Hostapd is NOT running"
exitcode=1
fi
if is_radvd_running; then
echo "Radvd is running"
else
echo "Radvd is NOT running"
exitcode=1
fi
if is_dhcpd_running; then
echo "Dhcpd is running"
else
echo "Dhcpd is NOT running"
exitcode=1
fi
exit ${exitcode}
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0