-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi_sncf.sh
executable file
·306 lines (275 loc) · 10.2 KB
/
wifi_sncf.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
# set -x
# Function to display usage
usage() {
echo "Usage: $0 [OPTION]"
echo "Options:"
echo " (no option) Show current DNS configuration and ping google.com"
echo " -h Show this help message"
echo " -sncf Use SNCF Wifi gateway as DNS server and get some details about the trip"
echo " -normandie Use Normandie Wifi gateway as DNS server and get some details about the trip"
echo " -google Use Google DNS servers as DNS servers"
echo " -nextdns Go through NextDNS only"
exit 1
}
# Function to set DNS servers
set_dns() {
local servers=$1
if [[ $(uname) == "Linux" ]]; then
nmcli device modify wlan0 ipv4.dns "$servers" # TBC
elif [[ $(uname) == "Darwin" ]]; then
networksetup -setdnsservers Wi-Fi "$servers"
fi
}
# Function to get current DNS servers
get_dns() {
echo "DNS config:"
if [[ $(uname) == "Linux" ]]; then
nmcli device show wlan0 | grep IP4.DNS # TBC
elif [[ $(uname) == "Darwin" ]]; then
networksetup -getdnsservers Wi-Fi
fi
# Try to ping google.com
if ping -c 3 google.com >/dev/null 2>&1; then
echo -e "\nPinging google.com: OK"
else
echo -e "\nPinging google.com: NOK"
fi
}
# Function to get default gateway
get_gateway() {
if [[ $(uname) == "Linux" ]]; then
ip route | awk '/default/ { print $3 }' # TBC
elif [[ $(uname) == "Darwin" ]]; then
netstat -rn | grep default | grep -E 'en[0-9]' | awk '{ print $2 }' | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
fi
}
# Function to accept the sncf portal conditions
accept_connection() {
local type=$1
# Check the connection status
if connection_status "$type"; then
echo -e "Connection already activated.\n"
else
response=$(curl -s "$type/router/api/connection/activate/auto" \
-H 'Accept-Language: en-US,en;q=0.9' \
-H 'Connection: keep-alive' \
-H "Origin: $type" \
-H "Referer: $type/en/internet/login" \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36' \
-H 'accept: application/json' \
-H 'content-type: application/json' \
-H 'sec-ch-ua: "Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "macOS"' \
--data-raw '{"without21NetConnection":false}')
# Check if response is not empty
if [ -n "$response" ]; then
status=$(echo "$response" | jq -r '.travel.status.active')
status_description=$(echo "$response" | jq -r '.travel.status.status_description')
echo -e "Connection activated. Status: $status ($status_description)\n"
else
echo -e "Error: No response received.\n"
fi
fi
}
# Function to get WiFi connection status
connection_status() {
local type=$1
response=$(curl -s "$type/router/api/connection/status")
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch data from the endpoint"
return 1
fi
# Check if response is not empty
if [ -n "$response" ]; then
# Parse and convert JSON fields
status=$(echo "$response" | jq -r '.active')
status_description=$(echo "$response" | jq -r '.status_description')
granted_bandwidth=$(echo "$response" | jq -r '.granted_bandwidth')
remaining_data=$(echo "$response" | jq -r '.remaining_data')
consumed_data=$(echo "$response" | jq -r '.consumed_data')
# Convert kilobytes to megabytes
granted_bandwidth_MB=$(echo "scale=2; $granted_bandwidth / 1024 / 10" | bc)
remaining_data_MB=$(echo "scale=2; $remaining_data / 1024" | bc)
consumed_data_MB=$(echo "scale=2; $consumed_data / 1024" | bc)
# Calculate percentage of data remaining
data_remaining_percentage=$(echo "scale=2; ($remaining_data / ($remaining_data + $consumed_data)) * 100" | bc)
# Convert next_reset timestamp to human-readable format
next_reset=$(echo "$response" | jq -r '.next_reset' | awk '{print int($1/1000)}' | xargs -I{} date -r {} +"%Y-%m-%d %H:%M:%S")
# Display values
echo -e "\nStatus: $status ($status_description)"
echo -e "\nGranted Bandwidth: $granted_bandwidth_MB MB/s"
echo "Remaining Data: $remaining_data_MB MB"
echo "Consumed Data: $consumed_data_MB MB"
echo "Data Remaining: $data_remaining_percentage%"
echo -e "Next Reset: $next_reset\n"
# echo -e "\nRaw:\n$(echo $response | jq .)"
# Check if status is true or false and return accordingly
if [ "$status" == "true" ]; then
return 0 # Success status
else
return 1 # Failure status
fi
else
echo "Error: No response received."
return 1 # Error status
fi
}
# Function to get WiFi connection statistics
get_wifi_statistics() {
local type=$1
response=$(curl -sL "$type/router/api/connection/statistics")
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch data from the endpoint"
return
fi
quality=$(echo "$response" | jq -r '.quality')
devices=$(echo "$response" | jq -r '.devices')
echo "WiFi Quality: $quality/5"
echo "Number of Devices Connected: $devices"
}
# Function to get bar attendance
get_bar_attendance() {
local type=$1
response=$(curl -s "$type/router/api/bar/attendance")
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch data from the endpoint"
return
fi
is_empty=$(echo "$response" | jq -r '.isBarQueueEmpty')
if [ "$is_empty" = true ]; then
echo "Bar queue is empty"
else
echo "Bar queue is not empty"
fi
}
# Function to get train speed and altitude
get_train_gps() {
local type=$1
response=$(curl -s "$type/router/api/train/gps")
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch data from the endpoint"
return
fi
speed=$(echo "$response" | jq -r '.speed')
altitude=$(echo "$response" | jq -r '.altitude')
# Convert speed from m/s to km/h
speed_kmh=$(echo "scale=2; $speed * 3.6" | bc 2>/dev/null)
echo "Train speed: $speed_kmh km/h"
echo "Train altitude: $altitude meters"
}
# Function to get percentage of the trip
get_trip_percentage() {
local type=$1
response=$(curl -sL "$type/router/api/train/details")
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch data from the endpoint"
return
fi
percentage=$(echo "$response" | jq -Mc '.stops | map(select (.progress != null)) | [ (map(.progress.traveledDistance) | add), (map(.progress.remainingDistance) | add)] | if (.[0] + .[1]) then .[0] / (.[0] + .[1]) * 100 else "EEE" end')
stops=$(echo "$response" | jq '.stops | length')
echo "Trip percentage: ${percentage%%.*}%"
echo "Number of Stops: $(echo $stops - 2 | bc)"
}
# Function to check for delayed stops and display their duration
check_delayed_stops() {
local type=$1
response=$(curl -sL "$type/router/api/train/details")
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch data from the endpoint"
return
fi
delayed_stops=$(echo "$response" | jq -r '.stops[] | select(.isDelayed == true) | "\(.label) is delayed by \(.delay) minutes"')
# Get the theoretical and real arrival dates for the last stop
if [ "$type" == "https://wifi.sncf" ]; then
last_stop=$(echo "$response" | jq -r '.stops | last')
name_stop=$(echo "$last_stop" | jq -r '.name')
theoric_date=$(echo "$last_stop" | jq -r '.theoricDate')
real_date=$(echo "$last_stop" | jq -r '.realDate')
elif [ "$type" == "https://wifi.normandie.fr" ]; then
last_stop=$(echo "$response" | jq -r '.stops | last')
name_stop=$(echo "$last_stop" | jq -r '.location.name')
theoric_date=$(echo "$last_stop" | jq -r '.arrival.date')
real_date=$(echo "$last_stop" | jq -r '.arrival.realDate')
else
echo "Wifi Router unknown"
fi
# Remove milliseconds and Z from the dates
theoric_date=$(echo "${theoric_date}" | sed 's/\(.*\)Z/\1/')
real_date=$(echo "${real_date}" | sed 's/\(.*\)Z/\1/')
if [[ $theoric_date == *"+02:00"* || $theoric_date == *"+01:00"* ]]; then
ds="+0H"
elif [ $(date -jf "%Y-%m-%dT%H:%M:%S" "${theoric_date}" +"%Z" 2>/dev/null) = "CEST" ]; then
# Central European Summer Time
ds="+2H"
else
# Central European Time
ds="+1H"
fi
# Convert UTC to Paris time
theoric_date_local=$(date -jf "%Y-%m-%dT%H:%M:%S" -v $ds "${theoric_date}" +"%H:%M" 2>/dev/null)
real_date_local=$(date -jf "%Y-%m-%dT%H:%M:%S" -v $ds "${real_date}" +"%H:%M" 2>/dev/null)
echo "Final destination: ${name_stop}"
echo "Theoretical Arrival: ${theoric_date_local}"
echo "Real Arrival: ${real_date_local}"
if [ -n "$delayed_stops" ]; then
echo "$delayed_stops"
else
echo "No delayed stops"
fi
}
# Main script
case "$1" in
-h)
usage
;;
-sncf)
type="https://wifi.sncf"
gw=$(get_gateway)
set_dns "$gw"
sleep 5
get_dns
accept_connection "$type"
get_wifi_statistics "$type"
get_train_gps "$type"
echo
get_trip_percentage "$type"
check_delayed_stops "$type"
echo
get_bar_attendance "$type"
;;
-normandie)
type="https://wifi.normandie.fr"
gw=$(get_gateway)
set_dns "$gw"
sleep 3
get_dns
accept_connection "$type"
get_wifi_statistics "$type"
get_train_gps "$type"
echo
get_trip_percentage "$type"
check_delayed_stops "$type"
echo
get_bar_attendance "$type"
;;
-google)
set_dns 8.8.8.8
sleep 3
get_dns
;;
-nextdns)
set_dns "127.0.0.1"
sleep 3
get_dns
;;
*)
# Display current DNS configuration
get_dns
;;
esac
exit 0