Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
V0.930 2baf32b Jul 5, 2019
2 contributors

Users who have contributed to this file

@rudiratlos @jsn0327
executable file 851 lines (785 sloc) 25.2 KB
#!/bin/bash
set +e
hotspotver="V0.930"
opt1="$1"
opt2="$2"
opt3="$3"
opt4="$4"
#
autostart="yes"
wipeiptables="yes"
torstart="no"
ovpnstart="no"
ovpnrefreshbeforestart="yes"
nodogsplashstart="no"
#
snam="hotspot"
eth_dev="eth0"
# freq_filter: 2|5 GHz band
freq_filter="2"
if [ "$freq_filter" != "5" ]; then freq_filter="2"; fi
# if defined, automatic discovery disabled
ch_autofind="1"
ch_dflt="11"
# valid channels and selection prio
ch_2GHz_ovrlap="2 3 4 5 7 8 9 10 12 13"
ch_2GHz_022MHz="1 6 11"
ch_5GHz_020MHz="32 36 40 44 48 52 56 60 64 100 104 108 112 116 120 124 128 132 136 140 144 149 153 157 161 165 169 173"
ch_5GHz_040MHz="34 38 46 54 62 102 110 118 126 134 142 151 159"
ch_5GHz_080MHz="42 58 106 122 138 155"
ch_5GHz_160MHz="50 114"
ch_prio=${ch_2GHz_ovrlap}" "${ch_2GHz_022MHz}" "${ch_5GHz_020MHz}" "${ch_5GHz_040MHz}" "${ch_5GHz_080MHz}" "${ch_5GHz_160MHz}
# wl0_dev: on board wlan (e.g. wlan0)
# wl1_dev: external wlan adaptor (optional)
wl0_dev=""
wl1_dev=""
ap_dev="ap0"
ap_devmac="b8:27:eb:00:00:00"
ap_net="10.3.141"
ap_devext="$eth_dev"
ns_dom="local"
#
ap_share="no"
ap_ip="${ap_net}.1"
ap_subn="${ap_net}.255"
ap_ipanet="${ap_net}.1/24"
ap_iptsubn="${ap_net}.0/24"
ap_revip=`echo "$ap_ip" | awk -F "." '{ print $4"."$3"."$2"."$1 }'`
ap_revnet=`echo "$ap_ip" | awk -F "." '{ print $3"."$2"."$1 }'`
#
tor_subn="10.192.0.0/10"
#
if [ "$wl0_dev" == "" ]; then
# internal wlan adapter
wl0_devs=`ls -l --full-time /sys/class/net/ 2>/dev/null | grep -iE "mmc.*wlan" | cut -d" " -f9`
wl0_dev=`echo "$wl0_devs" | awk -F " " '{ print $1 }'`
if [ "$wl0_dev" == "" ]; then wl0_dev="wlan0"; fi
fi
if [ "$wl1_dev" == "" ]; then
# external wlan adapter via usb
wl1_devs=`ls -l --full-time /sys/class/net/ 2>/dev/null | grep -iE "usb.*wlan" | cut -d" " -f9`
wl1_dev=`echo "$wl1_devs" | awk -F " " '{ print $1 }'`
fi
#
wpactrl="/var/run/wpa_supplicant"
wpasupg="/etc/wpa_supplicant/wpa_supplicant.conf"
hostapdf="/etc/hostapd/hostapd.conf"
dnsmasqf="/etc/dnsmasq.conf"
crdaf="/etc/default/crda"
tordir="/etc/tor"
torcfg="$tordir/torrc"
hostn="`hostname`"
# spot_ssid="${hostn}-hotspot" # <hostname>-hotspot
spot_ssid="`echo "$hostn" | sed 's/./\U&/g'`wlan-`cat /sys/class/net/$wl0_dev/address | awk -F ":" '{print $4$5$6}'`" # <HOSTNAME>wlan-<lastMAC3byte>
spot_psk="hallohallo"
spot_cntry2="`locale | awk -F"[_.]" '/LANG=/{print $2}'`"
if [ "$spot_cntry2" == "" ]; then spot_cntry2="DE"; fi
#
ovpnsel="AT CH DE ES FR GB JP KR SC TW US"
ovpndir="/etc/openvpn/client"
vpngate="${ovpndir}/vpngate.csv"
vpngatefileage="900"
ovpncfg="vpngate_bestscore.ovpn"
ovpnpwd="vpngate.pwd"
ovpncfgpath="${ovpndir}/${ovpncfg}"
ovpnpwdpath="${ovpndir}/${ovpnpwd}"
ovpn_dev="`cat $ovpncfgpath 2>/dev/null | grep -E "^.{0}dev " | awk -F " " '{print $2}'`"
if [ "$ovpn_dev" == "" ] || [ "$ovpn_dev" == "tun" ]; then ovpn_dev="tun0"; fi
if [ "$ovpnpwd" == "" ]; then
ovpn_opt="--config $ovpncfgpath"
else
ovpn_opt="--config $ovpncfgpath --auth-user-pass $ovpnpwdpath"
fi
function Show_Defines {
echo `date +"%Y-%m-%d %T"` "eth_dev:$eth_dev eth_subn:$eth_subn ap_ipanet:$ap_ipanet"
echo `date +"%Y-%m-%d %T"` "wl0_dev:$wl0_dev wl0_subn:$wl0_subn wl0_ip:$wl0_ip"
echo `date +"%Y-%m-%d %T"` "wl1_dev:$wl1_dev wl1_subn:$wl1_subn wl1_ip:$wl1_ip"
echo `date +"%Y-%m-%d %T"` "ap_shar:$ap_share hs_try:$hs_try $wl1_dev avail: $wl1_avail"
echo `date +"%Y-%m-%d %T"` "wl0_dev:$wl0_dev wl0_devs: $wl0_devs"
echo `date +"%Y-%m-%d %T"` "wl1_dev:$wl1_dev wl1_devs: $wl1_devs"
echo `date +"%Y-%m-%d %T"` "apdvext:$ap_devext"
}
function Hotspot_Defines {
eth_ip=`ip addr show dev "$eth_dev" 2>/dev/null | awk '/inet / { print $2 }'`
wl0_ip=`ip addr show dev "$wl0_dev" 2>/dev/null | awk '/inet / { print $2 }'`
eth_subn=`ip addr show dev "$eth_dev" 2>/dev/null | awk '/inet / { print $4 }'`
wl0_subn=`ip addr show dev "$wl0_dev" 2>/dev/null | awk '/inet / { print $4 }'`
if [ "$wl1_dev" != "" ]; then
wl1_avail="yes"
wl1_ip=`ip addr show dev "$wl1_dev" 2>/dev/null | awk '/inet / { print $2 }'`
wl1_subn=`ip addr show dev "$wl1_dev" 2>/dev/null | awk '/inet / { print $4 }'`
if [ "$eth_ip" == "" ]; then ap_devext="$wl1_dev"; fi
else
if [ "$eth_ip" == "" ]; then ap_devext="$wl0_dev"; fi
fi
hs_try="no"
if [ "$eth_subn" != "$ap_subn" ]; then
if [ "$wl1_avail" == "yes" ] || [ "$eth_subn" == "$wl0_subn" ] || [ "$wl0_subn" == "" ]; then hs_try="yes"; fi
fi
# Show_Defines
}
function fil_bck {
if [ "$1" != "" ] && [ -f "$1" ]
then
if [ "$2" != "" ]; then fn="$1.$2"; else fn="$1.old"; fi
cp "$1" "$fn"
fi
}
function Hotspot_Status {
# echo `date +"%Y-%m-%d %T"` "Status Hotspot ..."
ps ax | grep wpa_ ; ip a ; hostname -I
}
function Mod_Para {
cfil="file_not_valid"
if [ "$1" == "hostapd" ]; then cfil="$hostapdf"; fi
if [ "$1" == "dnsmasq" ]; then cfil="$dnsmasqf"; fi
if [ "$1" == "crda" ]; then cfil="$crdaf"; fi
if [ "$1" == "ovpncfg" ]; then cfil="$ovpncfgpath"; fi
if [ "$1" == "ovpnpwd" ]; then cfil="$ovpnpwdpath"; fi
if [ "$1" == "torcfg" ]; then cfil="$torcfg"; fi
if [ "$1" == "self" ]; then cfil="$0"; fi
if [ "$cfil" != "file_not_valid" ]; then
logger -s -t $snam " change $1 $2 $3 ($cfil)"
case "$1" in
ovpnpwd) echo -e "$2\n$3" > $cfil ;;
ovpncfg|torcfg) sed -i -r "s/$2[ ]* .*/$2 $3/g" "$cfil" ;;
self) sed -i -r "s/$2[ ]*=.*/$2=\"$3\"/g" "$cfil" ;;
*) sed -i -r "s/$2[ ]*=.*/$2=$3/g" "$cfil" ;;
esac
else
echo " Mod_Para invalid file selector $1"
fi
}
function ipt_cln {
logger -s -t $snam " ipt $1 $2"
iptables -t nat -F
iptables -t mangle -F
iptables -t nat -X
iptables -t mangle -X
iptables -t raw -F
iptables -t raw -X
iptables -F
iptables -X
}
function ipt_chains {
logger -s -t $snam " ipt chains"
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
}
function ipt_ap {
logger -s -t $snam " ipt ap $1 $ap_devext $ap_dev $ap_iptsubn"
iptables -t nat $1 POSTROUTING -s $ap_iptsubn ! -d $ap_iptsubn -j MASQUERADE
iptables $1 FORWARD -i $ap_devext -o $ap_dev -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables $1 FORWARD -i $ap_dev -o $ap_devext -j ACCEPT
}
function ipt_ovpn {
logger -s -t $snam " ipt ovpn $1 $ovpn_dev $ap_dev"
iptables -t nat $1 POSTROUTING -o $ovpn_dev -j MASQUERADE
iptables $1 FORWARD -i $ovpn_dev -o $ap_dev -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables $1 FORWARD -i $ap_dev -o $ovpn_dev -j ACCEPT
}
function ipt_tor {
logger -s -t $snam " ipt tor $1 $ap_dev (using ports 22 53 9040)"
iptables -t nat $1 PREROUTING -i $ap_dev -p tcp --dport 22 -j REDIRECT --to-ports 22
iptables -t nat $1 PREROUTING -i $ap_dev -p udp --dport 53 -j REDIRECT --to-ports 53
iptables -t nat $1 PREROUTING -i $ap_dev -p tcp --syn -j REDIRECT --to-ports 9040
}
function ipt_do {
if [ "$1" == "add" ] || [ "$1" == "del" ]; then
logger -s -t $snam " ipt $1 $2"
if [ "$1" == "add" ]; then opt="-A"; else opt="-D"; fi
if [ "$2" == "tor" ]; then ipt_tor "$opt"; fi
if [ "$2" == "ovpn" ]; then ipt_ovpn "$opt"; fi
if [ "$2" == "ap" ]; then ipt_ap "$opt"; fi
else
logger -s -t $snam " ipt invalid option $1"
fi
}
function ipt_wipe {
if [ "$wipeiptables" == "yes" ]; then
ipt_cln "wipe" "$2"
ipt_chains
fi
}
function ovpn_cfg_dwn {
# download and create ovpn config files, source: www.vpngate.net
#HostName,IP,Score,Ping,Speed,CountryLong,CountryShort,NumVpnSessions,Uptime,TotalUsers,TotalTraffic,LogType,Operator,Message,OpenVPN_ConfigData_Base64
vpnurl="https://www.vpngate.net/api/iphone/"
logger -s -t $snam " dwnload $vpnurl"
wget -q -O $vpngate $vpnurl
if [ -f "$vpngate" ]; then
vg_filn2="${ovpndir}/vpngate_scores.txt"
vg_filn3="${ovpndir}/vpngate_bestscore.ovpn"
echo -e "vpn\nvpn" > ${ovpndir}/vpngate.pwd
rm $vg_filn2 >/dev/null 2>&1
nr=0
vpnsel_arr=($ovpnsel)
while read line
do
vg_hname=`echo "$line" | awk -F "," '{print $1}'`
vg_ipadr=`echo "$line" | awk -F "," '{print $2}'`
vg_score=`echo "$line" | awk -F "," '{print $3}'`
vg_cntlg=`echo "$line" | awk -F "," '{print $6}'`
vg_cntsh=`echo "$line" | awk -F "," '{print $7}'`
vg_confg=`echo "$line" | awk -F "," '{print $15}'`
vg_filn1="${ovpndir}/vpngate_${vg_cntsh}_${vg_hname}.ovpn"
idx=0
while [ "${vpnsel_arr[idx]}" != "" ]
do
if [ "$vg_cntsh" == "${vpnsel_arr[idx]}" ]; then
nr=$(($nr+1))
logger -s -t $snam " create $vg_filn1"
echo "$vg_score $vg_cntsh $vg_hname $vg_ipadr" >>$vg_filn2
echo $vg_confg | base64 --decode 2>/dev/null | tr -d "\r" >$vg_filn1
if [ "$nr" -eq "1" ]; then cp $vg_filn1 $vg_filn3; fi
fi
idx=$(($idx+1))
done
done <<< $(cat $vpngate)
chmod 600 ${ovpndir}/vpngate*
fi
}
function ovpn_refresh {
dodwn="yes"
if [ -f "$vpngate" ]; then
# test file is recent (not older than xx mins)
if [ `stat --format=%Y $vpngate` -ge $((`date +%s` - $vpngatefileage)) ]; then
dodwn="no"
fi
fi
if [ "$dodwn" == "yes" ]; then
ovpn_cfg_dwn
else
logger -s -t $snam " file is recent ($vpngate)"
fi
# rm $vpngate >/dev/null 2>&1
}
function ovpn_check_running {
vpnpid=`ps -ef | grep openvpn | grep -v grep | awk '{print $2}'`
if [ ! -z $vpnpid ]; then
# OpenVPN is currently running
echo $vpnpid | xargs kill
fi
}
function stop_ap_dev {
if [ "$1" != "" ]; then
ip -force link set dev "$1" down
ip addr flush dev "$1"
fi
}
function inst_ap_dev {
wdev="$1"
if [ "$wdev" != "$ap_dev" ] && [ "$wdev" != "" ]; then
logger -s -t $snam " install & check dev $ap_dev"
apnet255=`ip addr show dev "$ap_dev" 2>/dev/null | awk '/inet / { print $4 }'`
if [ "$ap_subn" != "$apnet255" ]; then
logger -s -t $snam " assign dev $ap_dev $ap_ipanet on $wdev"
iw dev $wdev interface add $ap_dev type __ap
stop_ap_dev "$ap_dev"
# ip link set $ap_dev address $ap_devmac
ip a add "$ap_ipanet" brd + dev "$ap_dev"
ip link set dev "$ap_dev" up
fi
apipn=`ip addr show dev "$ap_dev" 2>/dev/null | awk '/inet / { print $2 }'`
if [ "$apipn" == "$ap_ipanet" ]; then
logger -s -t $snam " install dev $ap_dev on $wdev done"
else
logger -s -t $snam " ERROR: install dev $ap_dev"
fi
fi
}
function wlan {
if [ "$wl1_avail" != "yes" ]; then wl_dev="$wl0_dev"; else wl_dev="$wl1_dev"; fi
if [ "$2" != "" ]; then wl_dev="$2"; fi
case "$1" in
start)
logger -s -t $snam " start wlan $wl_dev"
wpa_supplicant -B -i $wl_dev -c $wpasupg >/dev/null 2>&1
;;
stop)
logger -s -t $snam " stop wlan $wl_dev"
wpa_cli -p $wpactrl -i $wl_dev terminate >/dev/null 2>&1
;;
clean)
logger -s -t $snam " clean wlan $wl_dev"
wpa_cli terminate >/dev/null 2>&1
ip addr flush $wl_dev
ip link set dev $wl_dev down
rm -r /var/run/wpa_supplicant >/dev/null 2>&1
;;
check)
if ! wpa_cli status | grep 'ip_address' >/dev/null 2>&1
then
logger -s -t $snam " ERROR wlan $wl_dev"
else
logger -s -t $snam " OK wlan $wl_dev"
fi
;;
*)
echo "Usage: $snam wlan start|stop|check|clean [$wl0_dev|$wl1_dev]"
exit 1
esac
}
function do_iptab {
case "$1" in
wipe)
ipt_cln "flush"
ipt_chains
;;
*)
echo "Usage: $snam ipt wipe"
exit 1
esac
}
function do_ovpn {
case "$1" in
start)
logger -s -t $snam " start openvpn (experimental)"
ipt_do "del" "ap"
ovpn_check_running
ipt_do "add" "ovpn"
openvpn $ovpn_opt &
;;
stop)
logger -s -t $snam " stop openvpn (experimental)"
ovpn_check_running
ipt_do "del" "ovpn"
;;
refresh)
logger -s -t $snam " refresh openvpn config files (experimental)"
ovpn_cfg_dwn
;;
*)
echo "Usage: $snam ovpn start|stop"
exit 1
esac
}
function do_tor {
case "$1" in
start)
logger -s -t $snam " start tor (experimental)"
ipt_do "del" "ap"
ipt_do "add" "tor"
service tor restart
;;
stop)
logger -s -t $snam " stop tor (experimental)"
service tor stop
ipt_do "del" "tor"
;;
*)
echo "Usage: $snam tor start|stop"
exit 1
esac
}
function do_syslog {
if [ "$1" != "" ]; then lins="-A $1"; else lins=""; fi
tail -500 /var/log/syslog | grep --color -a $lins "${snam}:"
}
function Find_Chan {
# find best (least used) channel
# Find_Chan "wlan1" "11" "104" "5"
chanw=""
chflg=""
chvtc=""
chhtc=""
if [ "$ch_autofind" -gt "0" ]; then
if [ "$1" != "" ]; then
fil="/tmp/$1_$4_scan.txt"
iwlist $1 chan | grep -i " : $4" | sed 's/ //g' | sed 's/Channel 0//g' | sed 's/Channel //g' | awk -F " " '{ print $1 }' > $fil
iwlist $1 scan | grep -i "Frequency:$4" | sort | uniq -c | sort -n | sed 's/ //g' | sed 's/)//g' | awk -F " " '{ print $5 }' >> $fil
linr=0
ch_prio_arr=($ch_prio)
while read line
do
linr=$(($linr+1))
ch_use=`echo "$line" | awk '{print $1}'`
ch_num=`echo "$line" | awk '{print $2}'`
# echo "ch: $ch_num use: $ch_use"
if [ "$linr" -le "1" ]; then chanw="$ch_num"; fi
# echo "linr: $linr ch: $chanw"
idx=0
while [ "${ch_prio_arr[idx]}" != "" ]
do
if [ "$ch_num" == "${ch_prio_arr[idx]}" ] && [ "$ch_use" -le "1" ]; then
chanw="$ch_num"
# echo "found idx: $idx ch: $chanw use: $ch_use"
fi
idx=$(($idx+1))
done
done <<< $(cat $fil | sort | uniq -c | sort -n)
fi
else
chanw="$3"
fi
if [ "$chanw" == "" ]; then chanw="$2"; fi
if [ "$chanw" == "" ]; then chanw="$ch_dflt"; fi
}
function Mdfy_Chan {
logger -s -t $snam " modify channel strategy: $1 $2 $3"
ch="$2"
if [ "$ch" == "" ]; then
ch="$ch_dflt"
logger -s -t $snam " using default chan $ch"
fi
if [ "$ch" -le "14" ]; then hm="g"; else hm="a"; fi
if [ "$ch" -ge "32" ]; then att="$ch (can cause problem)"; else att="$ch"; fi
logger -s -t $snam " select hw_mode $hm chan $att"
Mod_Para "hostapd" "channel" "$ch"
Mod_Para "hostapd" "hw_mode" "$hm"
}
function Hotspot_Start {
Hotspot_Defines
logger -s -t $snam "starting hotspot $hotspotver"
logger -s -t $snam " ETHER $eth_dev $eth_ip"
logger -s -t $snam " WLANint $wl0_dev $wl0_ip"
logger -s -t $snam " WLANext $wl1_dev $wl1_ip"
chanh=`cat "$hostapdf" | awk '/channel=/ { print $1 }' | sed 's/channel=//g'`
if [ "$chanh" != "0" ]; then
if [ "$ap_share" != "yes" ] && { [ "$wl1_avail" == "yes" ] || [ "$eth_ip" != "" ]; }; then
Find_Chan "$wl0_dev" "$ch_dflt" "$chanh" "$freq_filter"
logger -s -t $snam " FndChan suggest channel $chanw"
msg="select channel for ap mode"
else
chanw=`iwlist $wl0_dev channel | awk '/urrent / { print $5 }' | sed 's/)//g'`
msg="select same channel for ap & station mode on $wl0_dev"
logger -s -t $snam " UseChan channel $chanw"
fi
if [ "$chanw" != "" ]; then
Mdfy_Chan "$msg" "$chanw" "$chanh"
fi
else
logger -t $snam " UseAuto channel selection"
fi
if [ "$1" == "nowlan" ]; then wlan stop $wl0_dev; fi
logger -t $snam " stop hostapd"
systemctl stop hostapd
logger -t $snam " stop dnsmasq"
systemctl stop dnsmasq
echo 1 > /proc/sys/net/ipv4/ip_forward
inst_ap_dev $wl0_dev
sleep 1
ipt_wipe "cln" "ap"
ipt_do "add" "ap"
logger -s -t $snam " start hostapd restart"
systemctl restart hostapd
logger -s -t $snam " start dnsmasq"
systemctl restart dnsmasq
logger -s -t $snam "started hotspot"
if [ "$ovpnstart" == "yes" ]; then
if [ "$ovpnrefreshbeforestart" == "yes" ]; then
ovpn_refresh
fi
do_ovpn start
else
if [ "$torstart" == "yes" ]; then do_tor start; fi
fi
if [ "$nodogsplashstart" == "yes" ]; then
logger -s -t $snam "start captive portal (nodogsplash)"
nodogsplash
fi
msg="`service hostapd status | grep -i \"Active:\" | awk -F " " '{ print $2" "$3 }'`"
logger -s -t $snam "hostapd $msg"
msg="`service dnsmasq status | grep -i \"Active:\" | awk -F " " '{ print $2" "$3 }'`"
logger -s -t $snam "dnsmasq $msg"
}
function Hotspot_Try {
logger -s -t $snam "trying hotspot"
sleep 5 # let wlan settle
Hotspot_Defines
# Show_Defines
if [ "$hs_try" == "yes" ] && [ "$ap_share" != "yes" ]; then
logger -s -t $snam " try hotspot"
Hotspot_Start nowlan
else
if [ "$ap_share" == "yes" ]; then
if [ "$wl0_subn" != "" ] && [ "$wl0_subn" != "$ap_subn" ]; then
logger -s -t $snam " ap&sta on $wl0_dev start hotspot in parallel to station mode"
logger -s -t $snam " (not supported, use eth0 or additional usb wlan adaptor wlan1)"
# logger -s -t $snam " starting hotspot, maybe this will fail"
Hotspot_Start wlan
else
logger -s -t $snam " ERROR subnet conflict $eth_dev $eth_subn $wl0_dev $wl0_subn $ap_dev $ap_subn"
fi
else
logger -s -t $snam " PARAM ap_share not set to yes"
fi
fi
}
function Hotspot_Stop {
logger -s -t $snam "stopping all ovpn instances"
ovpn_check_running
logger -s -t $snam "stopping hotspot"
logger -s -t $snam " stop hostapd"
systemctl stop hostapd
sleep 2
logger -s -t $snam " stop dnsmasq"
systemctl stop dnsmasq
echo 0 > /proc/sys/net/ipv4/ip_forward
ipt_do "del" "ap"
sleep 2
if [ "$1" != "nowlan" ]; then
wlan start $wl0_dev
sleep 10
fi
logger -s -t $snam " link down dev $ap_dev"
stop_ap_dev "$ap_dev"
iw dev $ap_dev del
logger -s -t $snam "stopped hotspot"
}
function Check_installed_PKG {
# Check that what we need is installed
for command in iw iptables hostapd dnsmasq; do
which $command 2>&1 >/dev/null
if (( $? != 0 )); then
logger -s -t $snam " ERROR: $command is not installed (use $snam setup)"
exit -1
fi
done
}
function Hotspot_Setup {
echo `date +"%Y-%m-%d %T"` "Setup Hotspot $1 $2 ..."
if [ "$1" != "noswpkg" ]; then
echo `date +"%Y-%m-%d %T"` "install sw packages"
apt-get -qy install iw iptables hostapd dnsmasq tor openvpn
ovpn_refresh
fi
# disable/stop services. starting by script if needed
systemctl disable dnsmasq.service >/dev/null 2>&1
systemctl stop dnsmasq.service >/dev/null 2>&1
systemctl disable openvpn.service >/dev/null 2>&1
systemctl stop openvpn.service >/dev/null 2>&1
systemctl unmask hostapd.service >/dev/null 2>&1
systemctl disable hostapd.service >/dev/null 2>&1
systemctl stop hostapd.service >/dev/null 2>&1
update-rc.d tor disable >/dev/null 2>&1
service tor stop >/dev/null 2>&1
bckext=`date +"%Y%m%d%H%M%S"`
fil="/etc/rc.local"
fil2="/etc/issue"
fil_bck "$fil" "$bckext"
fil_bck "$fil2" "$bckext"
echo `date +"%Y-%m-%d %T"` "create $fil"
cat <<EOF > "$fil"
#!/bin/sh -e
#file autocreated, do not edit
#
hsm=\$(hotspot startmode 2>/dev/null)
if [ "\$hsm" = "yes" ]; then
# hotspot dbg >>/tmp/hotspot.log 2>&1;
hotspot try >>/tmp/hotspot.log 2>&1;
fi
# Print the IP address
_IP=\$(hostname -I) || true
if [ "\$_IP" ]; then
logger -s -t hotspot "My IP address is \$_IP"
printf "IP %s\nRaspbian GNU/Linux\n" "\$_IP" > /etc/issue
else
printf "Raspbian GNU/Linux\n" > /etc/issue
fi
exit 0
EOF
chmod 750 "$fil"
fil="/etc/sysctl.conf"
fil_bck "$fil" "$bckext"
# net.ipv4.ip_forward=1
grep -q -F "net.ipv4.ip_forward=" $fil || echo "net.ipv4.ip_forward=" >> $fil
sed -r -i 's/net\.ipv4\.ip_forward[ ]*=.*/net\.ipv4\.ip_forward=1/g' $fil
sed -r -i 's/#net\.ipv4\.ip_forward=1/net\.ipv4\.ip_forward=1/g' $fil
fil="/etc/dhcpcd.conf"
fil_bck "$fil" "$bckext"
echo `date +"%Y-%m-%d %T"` "create $fil"
cat <<EOF > "$fil"
hostname
clientid
persistent
option rapid_commit
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
option ntp_servers
option interface_mtu
require dhcp_server_identifier
slaac private
timeout 25
denyinterfaces ap0
interface ap0
nohook wpa_supplicant
#static ip_address=10.3.141.1/24
EOF
chmod 664 "$fil"
chown root:netdev "$fil"
fil="/etc/dnsmasq.conf"
fil_bck "$fil" "$bckext"
echo `date +"%Y-%m-%d %T"` "create $fil"
cat <<EOF > "$fil"
dhcp-mac=set:client_is_a_pi,B8:27:EB:*:*:*
dhcp-reply-delay=tag:client_is_a_pi,2
# Never forward plain names (without a dot or domain part)
domain-needed
no-dhcp-interface=eth0,wlan0
listen-address=$ap_ip
listen-address=127.0.0.1
bind-interfaces
#==========[ NAMESERVER ]==========#
no-hosts # Do not read /etc/hosts
# Read additional hosts-file (not only /etc/hosts) to add entries into DNS
#addn-hosts=/etc/dnsmasq_static_hosts.conf
expand-hosts
domain=$ns_dom
local=/$ns_dom/$ap_ip
# Host & PTR & Reverse
host-record=$hostn.$ns_dom,$ap_ip
server=/$ap_revnet.in-addr.arpa/$ap_ip
ptr-record=$ap_revip.in-addr.arpa,$hostn.$ns_dom
#==========[ DHCP ]==========#
dhcp-range=$ap_net.50,$ap_net.199,255.255.255.0,12h
dhcp-option=option:router,$ap_ip
#dhcp-option=option:ntp-server,$ap_ip
dhcp-option=vendor:MSFT,2,1i # Tell MS Windows to release a lease on shutdown
dhcp-option=44,$ap_ip # set netbios-over-TCP/IP aka WINS
dhcp-option=45,$ap_ip # netbios datagram distribution server
dhcp-option=46,8 # netbios node type
dhcp-option=252,"\n" # REQUIRED to get win7 to behave
# DHCPv4 Should be set when dnsmasq is definitely the only DHCP server on a network
#dhcp-authoritative
# DHCPv4 Server will attempt to ensure that an address is not in use before allocating it to a host
#no-ping
#==========[ etc ]==========#
log-facility=/var/log/syslog
#log-queries
#log-dhcp
log-async
local-ttl=2
local-service
localise-queries
EOF
chmod 644 "$fil"
fil="/etc/default/hostapd"
fil_bck "$fil" "$bckext"
echo `date +"%Y-%m-%d %T"` "create $fil"
cat <<EOF > "$fil"
#RUN_DAEMON=yes
DAEMON_CONF="/etc/hostapd/hostapd.conf"
EOF
chmod 600 "$fil"
fil="/etc/hostapd/hostapd.conf"
fil_bck "$fil" "$bckext"
echo `date +"%Y-%m-%d %T"` "create $fil"
cat <<EOF > "$fil"
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
interface=$ap_dev
driver=nl80211
ignore_broadcast_ssid=0
ap_isolate=0
channel=6
hw_mode=g
country_code=$spot_cntry2
ieee80211d=1
ssid=$spot_ssid
wpa_passphrase=$spot_psk
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
ieee80211n=1
ieee80211ac=1
wmm_enabled=1
macaddr_acl=0
ignore_broadcast_ssid=0
auth_algs=1
logger_syslog=-1
logger_syslog_level=4
logger_stdout=-1
logger_stdout_level=4
EOF
echo `date +"%Y-%m-%d %T"` "$snam: setting default (pls. adjust ssid, wpa_passphrase, country_code and REGDOMAIN)"
Mdfy_Chan "select default channel"
Mod_Para "hostapd" "ssid" "$spot_ssid"
Mod_Para "hostapd" "wpa_passphrase" "$spot_psk"
Mod_Para "hostapd" "country_code" "$spot_cntry2"
Mod_Para "crda" "REGDOMAIN" "$spot_cntry2"
Mod_Para "self" "autostart" "yes"
chmod 640 "$fil"
if [ -d "$tordir" ]; then
fil="$torcfg"
fil_bck "$fil" "$bckext"
echo `date +"%Y-%m-%d %T"` "create $fil"
cat <<EOF > "$fil"
Log notice file /var/log/tor-notices.log
VirtualAddrNetwork $tor_subn
AutomapHostsSuffixes .onion,.exit
AutomapHostsOnResolve 1
TransPort ${ap_ip}:9040
DNSPort ${ap_ip}:53
EOF
chmod 644 "$fil"
touch /var/log/tor-notices.log >/dev/null 2>&1
chown debian-tor /var/log/tor-notices.log >/dev/null 2>&1
chmod 644 /var/log/tor-notices.log >/dev/null 2>&1
fi
}
Check_installed_PKG
Hotspot_Defines
case "$opt1" in
try)
Hotspot_Try "$opt2"
;;
start)
Hotspot_Start "$opt2"
;;
stop)
Hotspot_Stop "$opt2"
;;
restart)
Hotspot_Stop nowlan
logger -s -t $snam " 20sec settle time"
sleep 20
Hotspot_Start "$opt2"
;;
retry)
Hotspot_Stop nowlan
logger -s -t $snam " 20sec settle time"
sleep 20
Hotspot_Try "$opt2"
;;
wlan)
wlan "$opt2" "$opt3"
;;
modpar)
Mod_Para "$opt2" "$opt3" "$opt4"
;;
setchan)
Mdfy_Chan "given_by_cli" "$opt2" "given_by_cli"
;;
setup)
Hotspot_Setup "$opt2" "$opt3"
;;
syslog)
do_syslog "$opt2"
;;
tor)
do_tor "$opt2"
;;
ovpn)
do_ovpn "$opt2"
;;
ipt)
do_iptab "$opt2"
;;
version)
echo "$hotspotver"
;;
startmode)
echo "$autostart"
;;
status)
Hotspot_Status
;;
dbg)
Show_Defines "$opt2" "$opt3" "$opt4"
;;
*)
echo "Usage: $snam try | start [nowlan] | stop [nowlan] | retry | restart [nowlan] | status | setup | setchan [channel] | syslog [lines] | ipt [wipe] | tor [start|stop] | ovpn [start|stop|refresh] | modpar <crda|dnsmasq|hostapd|self> <name> [value] | version | dbg | wlan start|stop|check|clean [$wl0_dev|$wl1_dev]"
exit 1
esac
You can’t perform that action at this time.