-
Notifications
You must be signed in to change notification settings - Fork 3
/
reboot-fbx.sh
executable file
·163 lines (128 loc) · 3.42 KB
/
reboot-fbx.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
#!/bin/bash
APP_ID="reboot-fbx.sh"
APP_NAME="Reboot"
APP_VERSION="0.0.2"
DEVICE_NAME="$(hostname -s)"
FREEBOX_BASE_URL=${FREEBOX_BASE_URL:-'http://mafreebox.freebox.fr'}
[ -z $DEBUG ] && exec 2>/dev/null || set -x
echo "***DEBUG***" >&2
config=${CONFIG:-"$HOME/.reboot-fbx.conf"}
freebox_tls_cert=${FREEBOX_TLS_CERT:-"$HOME/.reboot-fbx.cert"}
cacert_params=''
# set -eu
set -e
set -o pipefail
function get_tls_cert() {
if ! echo "$FREEBOX_BASE_URL" | grep '^https://' > /dev/null; then
return
fi
cacert_params="--cacert $freebox_tls_cert"
fbx_hostname=$(echo $FREEBOX_BASE_URL | awk -F/ '{print $3}')
if [ ! -f $freebox_tls_cert ]; then
echo 'quit' | openssl s_client \
-showcerts \
-servername $fbx_hostname \
-connect $fbx_hostname:443 > $freebox_tls_cert 2> /dev/null
fi
}
function read_config() {
set -a
. $config
set +a
}
function set_param() {
param=$1
value=$2
grep "$param" $config > /dev/null || echo "$param=$value" >> $config
read_config
}
function post() {
url=$1
data=$2
session_token=$3
echo "POST $url" >&2
echo "$data" >&2
if [ "$session_token" != "" ]; then
session_token_header="X-Fbx-App-Auth: $session_token"
fi
result=$(curl -s \
$cacert_params \
-X POST \
-H "Content-Type: application/json" \
-H "$session_token_header" \
-d "$data" \
${FREEBOX_BASE_URL}${url} | jq .)
echo "RESULT:" >&2
echo "$result" >&2
echo $result
}
function get() {
url=$1
echo "GET $url" >&2
result=$(curl -s \
$cacert_params \
${FREEBOX_BASE_URL}${url} \
| jq .)
echo "RESULT:" >&2
echo "$result" >&2
echo $result
}
function hmac_sha1() {
app_token=$1
challenge=$2
echo -n "$challenge" | openssl sha1 -hmac "$app_token" | awk '{print $2}'
}
echo "$APP_ID" >&2
echo "- config file: $config" >&2
[ -f $config ] || touch $config
read_config
get_tls_cert
api_version=$(get '/api_version' | jq -r '.api_version')
echo "api_version: $api_version"
if [ "$app_token" == "" ]; then
data="{
\"app_id\": \"$APP_ID\",
\"app_name\": \"$APP_NAME\",
\"app_version\": \"$APP_VERSION\",
\"device_name\": \"$DEVICE_NAME\"
}"
result=$(post "/api/v4/login/authorize" "$data")
app_token=$(echo $result| jq -r '.result.app_token')
set_param 'app_token' "'$app_token'"
track_id=$(echo $result| jq -r '.result.track_id')
set_param 'track_id' "$track_id"
fi
echo "app_token $app_token" >&2
echo "track_id: $track_id"
status='pending'
echo -n 'waiting'
while [ $status == 'pending' ]
do
sleep 1
result=$(get "/api/v4/login/authorize/$track_id")
status=$(echo $result | jq -r '.result.status')
challenge=$(echo $result | jq -r '.result.challenge')
password_salt=$(echo $result | jq -r '.result.password_salt')
echo -n '.'
done
echo ""
if [ "$status" != "granted" ]; then
echo "Error: status $status"
exit 1
fi
password=$(hmac_sha1 $app_token $challenge)
echo "password: $password" >&2
data="{
\"app_id\": \"$APP_ID\",
\"password\": \"$password\"
}"
result=$(post "/api/v4/login/session" "$data")
session_token=$(echo $result | jq -r '.result.session_token')
echo "session_token: $session_token" >&2
result=$(post "/api/v4/system/reboot" "$data" "$session_token")
success=$(echo $result | jq -r '.success')
if [ "$success" != "true" ]; then
echo 'Error: You must grant reboot permission'
exit 1
fi
echo 'Reboot initiated'