#!/system/bin/sh
SCRIPT SOURCE: https://android.stackexchange.com/a/215514
SEE ALSO: https://android.stackexchange.com/a/202335
set -e
#set -x
this scripts creates a hotspot network
[ "$(id -u)" = 0 ] || { echo 'Not running as root!' >&2; exit 1; }
Add /vendor/bin/hw to path. That’s where hostapd is in LineageOS 19.0
export PATH=$PATH:/vendor/bin/hw
check required binaries are on PATH
for bin in iw ip iptables hostapd dnsmasq
do
! which $bin >/dev/null || continue
echo "$bin not found." >&2
exit 1
done
####################
define variables
####################
source /data/data/com.termux/files/home/customAP/config.sh
##########################
start / stop tethering
##########################
STOP()
(
echo 'Cleaning up. Feel free to ignore any errors in this stage..'
# don't print error messages
#exec >/dev/null 2>&1
# hope there are no other instances of same daemons
pkill -15 hostapd
# Do not kill wpa_supplicant to avoid WifiSelfRecovery from disabling wifi
#pkill -15 wpa_supplicant
pkill -15 dnsmasq
# remove RPDB rule and iptables rule
ip rule del lookup main
iptables -D INPUT -i $AP_INTERFACE -p udp -m udp --dport 67 -j ACCEPT
# delete AP interface
#ip link show
iw dev $AP_INTERFACE del
#ip link show
rm -rf $DIR
echo "Cleanup done."
)
if [ "$1" = stop ]
then
STOP || true
exit
elif [ "$1" != start ]
then
echo 'Usage:' >&2
printf '\t%s\n' "$(basename "$0") start|stop" >&2
exit 1
fi
################
basic checks
################
if ! iw phy | grep -A10 'Supported interface modes:' | grep -q '*[ ]*AP'
then
echo 'AP mode not supported.' >&2
exit 1
fi
if ! iw dev $WIFI_INTERFACE link | grep -q '^Not connected'
then
echo 'First disconnect form Wi-Fi.' >&2
exit 1
fi
##########################
stop running instances
##########################
STOP || true
#####################################
create virtual wireless interface
#####################################
if ! iw dev $WIFI_INTERFACE interface add $AP_INTERFACE type __ap
then
echo "Couldn't create AP interface." >&2
exit 1
fi
#####################################
configure newly created interface
#####################################
echo 'Configuring network...'
activate the interface and add IP
ip link set up dev $AP_INTERFACE
ip addr add ${IP}/24 broadcast ${SUBNET}.255 dev $AP_INTERFACE
Android doesn't look up into main table by default
ip rule add lookup main
#######################
access point daemon
#######################
create configuration file
mkdir -p "$DIR"
cat <<-EOF >$DIR/hostapd.conf
logger_syslog=1
logger_syslog_level=0
logger_stdout=-1
logger_stdout_level=0
country_code=AR
network name
ssid=$SSID
passphrase to use for protected access
wpa_passphrase=$PASSCODE
network interface to listen on
interface=$AP_INTERFACE
wi-fi driver
driver=nl80211
set operation mode, 'g' for 2.4GHz band
hw_mode=g
WLAN frequency channel to use
channel=9
#Win10 compatibility stuff
ieee8021x=0
eap_server=0
ignore_broadcast_ssid=0
Security
wpa=2
key management protocol; use pre-share key
wpa_key_mgmt=WPA-PSK
#Win10 compatibility stuff
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOF
#Show condiguration
#echo '========================================='
#echo 'Using this hostapd.conf configuration file:'
#cat $DIR/hostapd.conf
#echo '========================================='
echo 'Starting hostapd...'
hostapd -B $DIR/hostapd.conf
################################################
run a dhcp server to assign IP's dynamically
################################################
create configuration file
cat <<-EOF >$DIR/dnsmasq.conf
# we dont want DNS server, only DHCP
port=0
# nameservers to be sent to clients
dhcp-option=6,1.1.1.1,1.0.0.1
# range of IPs to make available to wlan devices and when to renew IP
dhcp-range=$IP,${SUBNET}.254,24h
# where to save leases
dhcp-leasefile=$DIR/dnsmasq.leases
# respond to requests from a different IP broadcast subnet
dhcp-authoritative
# don't look for any hosts file and resolv file
no-hosts
no-resolv
EOF
open listening port
iptables -I INPUT -i $AP_INTERFACE -p udp -m udp --dport 67 -j ACCEPT
#echo "dnsmasq config:"
#cat $DIR/dnsmasq.conf
echo 'Starting DHCP server...'
dnsmasq --pid-file -C $DIR/dnsmasq.conf </dev/null
echo "All Done!"