forked from ADSBexchange/adsbx-webconfig-old
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebconfig.sh
executable file
·181 lines (147 loc) · 5.25 KB
/
webconfig.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
# this is a privileged folder ... only root can access
mkdir -p /tmp/webconfig_priv
chmod -R go-rwx /tmp/webconfig_priv
mkdir -p /tmp/webconfig
echo $USER > /tmp/webconfig/name
if ! echo "$LATITUDE $LONGITUDE" | grep -E -qs -e '[1-9]+'; then
echo "Location not set." > /tmp/webconfig/location
location_set=0
else
location_set=1
fi
function services-handle {
for SERVICE in $2; do
echo "$1 $SERVICE"
if [[ $1 == disable ]]; then
if systemctl is-enabled $SERVICE &>/dev/null; then
systemctl disable --now $SERVICE
fi
if systemctl is-active $SERVICE &>/dev/null; then
systemctl stop $SERVICE
fi
fi
if [[ $1 == enable ]]; then
if ! systemctl is-enabled $SERVICE &>/dev/null; then
systemctl enable --now $SERVICE
fi
if ! systemctl is-active $SERVICE &>/dev/null; then
systemctl start $SERVICE
fi
fi
done
}
if [[ $CUSTOMLEDS == "yes" ]];
then
services-handle enable leds
systemctl restart leds
else
services-handle disable leds
fi
chmod -R a+rwX /tmp/webconfig
# reset password when reset_password file is set
if [[ -e /boot/reset_password ]] || [[ -e /boot/reset_password.txt ]]; then
echo "pi:adsb123" | chpasswd
rm -rf /boot/reset_password /boot/reset_password.txt
fi
# Runs a script that may be manually placed on /boot for batch setup. By default, nothing there.
if [[ -f /boot/firstboot.sh ]]; then
bash /boot/firstboot.sh &
fi
lsusb -d 0bda: -v 2> /dev/null | grep iSerial | tr -s ' ' | cut -d " " -f 4 > /tmp/webconfig/sdr_serials
# make sure we wait at least 5 seconds before doing the wifi scan
sleep 5 &
internet=0
# wait until we have internet connectivity OR a maximum of 15 seconds
for i in {1..13}; do
if ping -c 1 -w 1 8.8.8.8 &>/dev/null; then
# we have internet!
internet=1
break;
fi
done
for i in {1..13}; do
if ping -c 1 -w 1 1.1.1.1 &>/dev/null; then
# we have internet!
internet=1
break;
fi
done
if [[ $internet == 1 ]]; then
echo > /dev/tty1
echo ------------- > /dev/tty1
echo "Use the webinterface at http://adsbfi.local OR http://$(ip route get 1.2.3.4 | grep -m1 -o -P 'src \K[0-9,.]*')" > /dev/tty1
echo ------------- > /dev/tty1
if [[ $location_set == 1 ]] ; then
timeout 3 wget https://api.bigdatacloud.net/data/reverse-geocode-client?latitude=$LATITUDE\&longitude=$LONGITUDE\&localityLanguage=en -q -T 3 -O /tmp/webconfig/geocode
cat /tmp/webconfig/geocode | jq -r .'locality' > /tmp/webconfig/location
cat /tmp/webconfig/geocode | jq -r .'principalSubdivisionCode' >> /tmp/webconfig/location
cat /tmp/webconfig/geocode | jq -r .'countryName' >> /tmp/webconfig/location
chmod -R a+rwX /tmp/webconfig
fi
fi
# make sure we wait at least 5 seconds before doing the wifi scan
wait
iw wlan0 scan | grep SSID: | sort | uniq | cut -c 8- | grep '\S' | grep -v '\x00' > /tmp/webconfig/wifi_scan
if [ $? -ne 0 ]
then
sleep 3
iw wlan0 scan | grep SSID: | sort | uniq | cut -c 8- | grep '\S' | grep -v '\x00' > /tmp/webconfig/wifi_scan
fi
if [[ $internet == 1 ]]; then
echo "1.1.1.1 or 8.8.8.8 pingable, exiting"
exit 0
fi
echo "ip connectivity failed, enabling ADSBfi-config network"
echo > /dev/tty1
echo ------------- > /dev/tty1
echo "Internet can't be reached with current WiFi settings, enabling ADSBfi-config WiFi Network!" > /dev/tty1
echo "Use your smartphone / laptop to connect to the WiFi network called: ADSBfi-config" > /dev/tty1
echo "On that device visit the URL http://adsbfi.local in your browser" > /dev/tty1
echo "Select a WiFi network / country / password for the Raspberry Pi to join" > /dev/tty1
echo ------------- > /dev/tty1
netnum=$(wpa_cli list_networks | grep ADSBfi-config | cut -f 1)
wpa_cli enable_network $netnum
dnsmasq
totalwait=0
touch /tmp/webconfig_priv/unlock
fatal="no"
until [ $totalwait -gt 900 ]
do
ssid=$(wpa_cli status | grep ssid | grep -v bssid | cut -d "=" -f 2)
if [ "$ssid" = "ADSBfi-config" ]; then
ipset=$(ip address show dev wlan0 | grep "172.23.45.1")
if [ -z "$ipset" ]; then
ip address replace 172.23.45.1/24 dev wlan0; echo "setting wlan0 ip to 172.23.45.1/24"
fi
clientip=$(cat /tmp/webconfig/dnsmasq.leases | head -n 1 | cut -d " " -f3)
if [[ ! -z "$clientip" ]]; then
echo "Client lease detected at $clientip"
fi
fi
if (( $totalwait > 15 )) && [[ "$ssid" != "ADSBfi-config" ]]; then
fatal="yes"
break;
fi
((totalwait++))
sleep 1
done
if [[ "$ssid" == "ADSBfi-config" ]] && [[ "$fatal" != "yes" ]]; then
ping $clientip -I wlan0 -f -w 1; hostup=$?
if [ $hostup -eq 0 ]; then
echo "timeout tripped but client connected, disabling ADSBfi-config in 900 sec"
sleep 900
wpa_cli disable $netnum
fi
fi
kill $(cat /var/run/dnsmasq.pid)
sleep 1
killall dnsmasq #Make sure dnsmasq is off
sleep 2
pkill -9 dnsmasq # Make extra sure dnsmasq is off
ip address del 172.23.45.1/32 dev wlan0
rm -rf /tmp/webconfig_priv/unlock
wpa_cli disable $netnum
# in case any subtasks started by firstboot.sh, wait for them to complete
wait
exit 0;