Rework V4 heartbeat, fixing connection bug and improving connection speed if Internet connection is available and device restricted to No VPN
This commit is contained in:
parent
bff0c04c33
commit
d3052e0489
1 changed files with 42 additions and 33 deletions
|
@ -42,6 +42,8 @@ led5g(){
|
||||||
. /etc/vpnsecret # Source the server authentication secret
|
. /etc/vpnsecret # Source the server authentication secret
|
||||||
|
|
||||||
# Prepare for default VPN-WiFi bridge
|
# Prepare for default VPN-WiFi bridge
|
||||||
|
/sbin/uci set network.VPN360.ifname="eth1 tap0"
|
||||||
|
/sbin/uci set network.DHCP.ifname="lo"
|
||||||
/sbin/uci set wireless.@wifi-iface[0].network="VPN360"
|
/sbin/uci set wireless.@wifi-iface[0].network="VPN360"
|
||||||
/sbin/uci commit
|
/sbin/uci commit
|
||||||
|
|
||||||
|
@ -62,49 +64,56 @@ counter=0
|
||||||
while [ $counter -lt 60 ]
|
while [ $counter -lt 60 ]
|
||||||
do
|
do
|
||||||
# Retrieve hosts file from server
|
# Retrieve hosts file from server
|
||||||
if /usr/bin/wget -O/etc/hosts https://$HOSTNAME/hosts --timeout=2 --post-data "secret=$SECRET" --no-check-certificate >/var/log/wget 2>&1
|
if /usr/bin/wget -O/etc/hosts https://$HOSTNAME/hosts --timeout=2 --post-data "secret=$SECRET" --no-check-certificate >/var/log/wget 2>&1
|
||||||
then
|
then
|
||||||
|
|
||||||
if pgrep "openvpn" >/dev/null
|
if grep -Fq "No VPN" /etc/hosts;
|
||||||
then
|
then
|
||||||
if [ $(ipaddr) ] # = If connection to the server is working
|
break
|
||||||
then
|
fi
|
||||||
# Turn on LEDs indicating boot completion and connection success
|
|
||||||
powerled 1
|
|
||||||
led5g 1
|
|
||||||
|
|
||||||
# Enable WiFi as the VPN bridge is now functional
|
if pgrep "openvpn" >/dev/null
|
||||||
startwifi
|
then
|
||||||
|
if [ $(ipaddr) ] # = If connection to the server is working
|
||||||
|
then
|
||||||
|
# Turn on LEDs indicating boot completion and connection success
|
||||||
|
powerled 1
|
||||||
|
led5g 1
|
||||||
|
|
||||||
# Send a heartbeat to the server every 10 seconds
|
# Enable WiFi as the VPN bridge is now functional
|
||||||
# This is also used to transfer commands from the server to the device
|
startwifi
|
||||||
while [ True ]
|
|
||||||
do
|
|
||||||
/bin/sleep 10
|
|
||||||
|
|
||||||
# Let's hope there is an IP address on the VPN interface
|
# Send a heartbeat to the server every 10 seconds
|
||||||
# If not, this might be a temporary issue (lost network connection or lease expiration)
|
# This is also used to transfer commands from the server to the device
|
||||||
# We assume that users will reboot the device if it doesn't work for extended periods of time
|
while [ True ]
|
||||||
if [ $(ipaddr) ]
|
do
|
||||||
then
|
/bin/sleep 10
|
||||||
/usr/bin/wget -O- https://$HOSTNAME/heartbeat --post-data "secret=$SECRET&ip=$(ipaddr)" --no-check-certificate 2>/var/log/wget | /bin/ash
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
fi
|
# Let's hope there is an IP address on the VPN interface
|
||||||
else
|
# If not, this might be a temporary issue (lost network connection or lease expiration)
|
||||||
# Launch VPN client if not running
|
# We assume that users will reboot the device if it doesn't work for extended periods of time
|
||||||
/usr/sbin/openvpn /etc/openvpn/client.conf >/var/log/openvpn &
|
if [ $(ipaddr) ]
|
||||||
fi
|
then
|
||||||
fi
|
/usr/bin/wget -O- https://$HOSTNAME/heartbeat --post-data "secret=$SECRET&ip=$(ipaddr)" --no-check-certificate 2>/var/log/wget | /bin/ash
|
||||||
counter=$(( counter + 1 ))
|
fi
|
||||||
powerled $(( counter % 2 ))
|
done
|
||||||
/bin/sleep 1 # Wait for a second before re-trying
|
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Launch VPN client if not running
|
||||||
|
/usr/sbin/openvpn /etc/openvpn/client.conf >/var/log/openvpn &
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
counter=$(( counter + 1 ))
|
||||||
|
powerled $(( counter % 2 ))
|
||||||
|
/bin/sleep 1 # Wait for a second before re-trying
|
||||||
done
|
done
|
||||||
|
|
||||||
# We should only ever get to this point if no VPN connection was established within a minute
|
# We should only ever get to this point if no VPN connection was established within a minute
|
||||||
|
|
||||||
# Switch WiFi device to the DHCP bridge
|
# Switch WiFi device to the DHCP bridge
|
||||||
|
/sbin/uci set network.VPN360.ifname="tap0"
|
||||||
|
/sbin/uci set network.DHCP.ifname="eth1"
|
||||||
/sbin/uci set wireless.@wifi-iface[0].network="DHCP"
|
/sbin/uci set wireless.@wifi-iface[0].network="DHCP"
|
||||||
/sbin/uci commit
|
/sbin/uci commit
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue