Skip to content

Instantly share code, notes, and snippets.

@AzimsTech
Forked from ShapeShifter499/vpn-hotspot.sh
Last active August 11, 2022 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AzimsTech/94a5c9be47a539123a4430393fe76d0c to your computer and use it in GitHub Desktop.
Save AzimsTech/94a5c9be47a539123a4430393fe76d0c to your computer and use it in GitHub Desktop.
VPN over Hotspot on Android
#!/system/bin/sh
# Use Script Runner 2 Link: https://play.google.com/store/apps/details?id=com.ajay.ssr2&hl=en_US
# This script should help forward VPN over any tethered connection on a Android device.
# Turn on tethering, then enable VPN, then run this script.
# Inital variable setup
tethering=0
# Setup iptables before forwarding VPN
iptables -t filter -F FORWARD
iptables -t nat -F POSTROUTING
iptables -t filter -I FORWARD -j ACCEPT
iptables -t nat -I POSTROUTING -j MASQUERADE
# Check if bluetooth is tethered, if so forward VPN
blue=$(/system/bin/ifconfig bt-pan 2>/dev/null | grep "UP" | wc -l)
blueIP=$(ifconfig bt-pan 2>/dev/null | grep "inet addr" | cut -d":" -f2 | cut -d' ' -f1 | cut -d'.' -f1-3)
if [[ "$blue" -gt 0 ]];
then
ip rule add from "$blueIP".0/24 lookup 61
touch /storage/emulated/0/vpn-hotspot.lock
nohup sh -c 'while [[ -f /storage/emulated/0/vpn-hotspot.lock ]]; do ip route add default dev tun0 scope link table 61; sleep 180; done;' </dev/null >/dev/null 2>&1 &
ip route add "$blueIP".0/24 dev bt-pan scope link table 61
ip route add broadcast 255.255.255.255 dev bt-pan scope link table 61
blueTethered=1
tethering=1
echo "Set up VPN on Bluetooth sucessfully"
else
blueTethered=0
echo "Not tethering on Bluetooth"
fi
# Check if USB is tethered, if so forward VPN
usb=$(/system/bin/ifconfig rndis0 2>/dev/null | grep "UP" | wc -l)
usbIP=$(ifconfig rndis0 2>/dev/null | grep "inet addr" | cut -d":" -f2 | cut -d' ' -f1 | cut -d'.' -f1-3)
if [[ "$usb" -gt 0 ]];
then
ip rule add from "$usbIP".0/24 lookup 61
touch /storage/emulated/0/vpn-hotspot.lock
nohup sh -c 'while [[ -f /storage/emulated/0/vpn-hotspot.lock ]]; do ip route add default dev tun0 scope link table 61; sleep 180; done;' </dev/null >/dev/null 2>&1 &
ip route add "$usbIP".0/24 dev rndis0 scope link table 61
ip route add broadcast 255.255.255.255 dev rndis0 scope link table 61
usbTethered=1
tethering=1
echo "Set up VPN on USB successfully"
else
usbTethered=0
echo "Not tethering on USB"
fi
# Check if WIFI is tethered, if so forward VPN
wifi=$(dumpsys wifi | grep curState=ApEnabledState | wc -l)
wifiIP=$(ifconfig wlan0 2>/dev/null | grep "inet addr" | cut -d":" -f2 | cut -d' ' -f1 | cut -d'.' -f1-3)
if [[ "$wifi" -gt 0 ]];
then
ip rule add from "$wifiIP".0/24 lookup 61
touch /storage/emulated/0/vpn-hotspot.lock
nohup sh -c 'while [[ -f /storage/emulated/0/vpn-hotspot.lock ]]; do ip route add default dev tun0 scope link table 61; sleep 180; done;' </dev/null >/dev/null 2>&1 &
echo $! > /storage/emulated/0/vpn-hotspot.pid
ip route add "$wifiIP".0/24 dev wlan0 scope link table 61
ip route add broadcast 255.255.255.255 dev wlan0 scope link table 61
wifiTethered=1
tethering=1
echo "Set up VPN on WIFI successfully"
else
wifiTethered=0
echo "Not tethering on WIFI"
fi
# Clean up if no tethering is occuring
if [[ "$tethering" -eq 0 ]];
then
if [[ "$blueTethered" -gt 0 ]];
then
ip rule delete from "$blueIP".0/24 lookup 61
rm /storage/emulated/0/vpn-hotspot.lock
ip route delete default dev tun0 scope link table 61
ip route delete "$blueIP".0/24 dev bt-pan scope link table 61
ip route delete broadcast 255.255.255.255 dev bt-pan scope link table 61
fi
if [[ "$usbTethered" -gt 0 ]];
then
ip rule delete from "$usbIP".0/24 lookup 61
rm /storage/emulated/0/vpn-hotspot.lock
ip route delete default dev tun0 scope link table 61
ip route delete "$usbIP".0/24 dev rndis0 scope link table 61
ip route delete broadcast 255.255.255.255 dev rndis0 scope link table 61
fi
if [[ "$wifiTethered" -gt 0 ]];
then
ip rule delete from "$wifiIP".0/24 lookup 61
rm /storage/emulated/0/vpn-hotspot.lock
ip route delete default dev tun0 scope link table 61
ip route delete "$wifiIP".0/24 dev wlan0 scope link table 61
ip route delete broadcast 255.255.255.255 dev wlan0 scope link table 61
fi
echo "Cleaned up iptables rules since we are not tethering"
else
echo "Sucessfully forwarding VPN over tethering"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment