Skip to content

Commit 88f7642

Browse files
rmfakecloud-proxy:  Add status subcommand (#513)
* Add `rmfakecloudctl status` subcommand This subcommand reports whether rmfakecloud-proxy is enabled, currently active, and whether the upstream server has been set. If the upstream server is set, it also checks whether it is reachable and a valid rmfakecloud server. * Keep config on uninstall
1 parent 4f8dd4e commit 88f7642

File tree

2 files changed

+83
-23
lines changed

2 files changed

+83
-23
lines changed

package/rmfakecloud-proxy/package

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pkgdesc="Connect Xochitl to a rmfakecloud server"
77
_url=https://github.com/ddvk/rmfakecloud-proxy
88
url="$_url"
99
_upver=0.0.3
10-
pkgver="$_upver-1"
10+
pkgver="$_upver-2"
1111
timestamp=2021-09-26T20:38:44Z
1212
section="utils"
1313
maintainer="Mattéo Delabre <[email protected]>"
@@ -59,18 +59,7 @@ configure() {
5959
uninstall-hosts
6060
fi
6161

62-
if ! is-enabled; then
63-
cat << MSG
64-
65-
Run the following commands to use rmfakecloud-proxy:
66-
67-
$ rmfakecloudctl set-upstream https://your-server.example.org
68-
$ rmfakecloudctl enable
69-
70-
Replace your-server.example.org with the address of the server you want to use.
71-
72-
MSG
73-
fi
62+
rmfakecloudctl status
7463
}
7564

7665
preremove() {

package/rmfakecloud-proxy/rmfakecloudctl

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ xochitl_conf_path=/home/root/.config/remarkable/xochitl.conf
2626
# Path to Xochitl data
2727
xochitl_data_dir=/home/root/.local/share/remarkable/xochitl
2828

29+
# ANSI escapes for colors
30+
red="\033[31m"
31+
green="\033[32m"
32+
yellow="\033[33m"
33+
reset="\033[m"
34+
2935
# Disconnect Xochitl from the cloud
3036
#
3137
# This is a necessary step when switching cloud servers to prevent the client
@@ -140,6 +146,43 @@ addr: $proxy_listen:443
140146
YAML
141147
}
142148

149+
# Check if a given server is an rmfakecloud host
150+
#
151+
# Output: Details on the given host status
152+
# Exit code:
153+
#
154+
# 0 - If the server seems to be a valid rmfakecloud host
155+
# 1 - If it’s definitely not
156+
check-upstream() {
157+
local url="$upstream/service/json/1/test"
158+
local contents
159+
local wget_exit
160+
contents="$(wget --output-document - --quiet "$url" 2>&1)" \
161+
&& wget_exit=$? || wget_exit=$?
162+
163+
if [[ $wget_exit = 8 ]]; then
164+
# HTTP error, the page probably doesn't exist
165+
echo "invalid"
166+
return 1
167+
elif [[ $wget_exit = 4 ]]; then
168+
# Network error, the server probably doesn't exist or is offline
169+
echo "unreachable"
170+
return 1
171+
elif [[ $wget_exit != 0 ]]; then
172+
# Other error
173+
echo "error"
174+
return 1
175+
fi
176+
177+
if [[ $contents != '{"Host":"local.appspot.com","Status":"OK"}' ]]; then
178+
echo "invalid"
179+
return 1
180+
fi
181+
182+
echo "working"
183+
return 0
184+
}
185+
143186
# Generate a local certificate authority, add it to the system trust,
144187
# and create a self-signed proxy certificate
145188
install-certificates() {
@@ -280,7 +323,6 @@ uninstall() {
280323
disconnect-cloud
281324
mark-disabled
282325
systemctl disable --now rmfakecloud-proxy
283-
rm -f "$conf_dir/config"
284326
uninstall-hosts
285327
uninstall-certificates
286328
set -e
@@ -291,8 +333,9 @@ help() {
291333
Manage rmfakecloud-proxy. Available commands:
292334
293335
help Show this help message.
336+
status Check the current status of rmfakecloud-proxy.
294337
enable Enable rmfakecloud-proxy.
295-
set-upstream Define the rmfakecloud server.
338+
set-upstream Set the upstream rmfakecloud server.
296339
disable Disable rmfakecloud-proxy."
297340
}
298341

@@ -306,6 +349,42 @@ if [[ $0 = "${BASH_SOURCE[0]}" ]]; then
306349
shift
307350

308351
case $action in
352+
status)
353+
is-enabled && state="${green}enabled" || state="${red}disabled"
354+
service="$(systemctl is-active rmfakecloud-proxy.service)" || true
355+
356+
if [[ $service = active ]]; then
357+
service="$green$service"
358+
else
359+
service="$yellow$service"
360+
fi
361+
362+
if ! upstream="$(get-upstream)"; then
363+
upstream="(${yellow}not set$reset)"
364+
else
365+
if ! upstream_status="$(check-upstream "$upstream")"; then
366+
upstream="$upstream ($red$upstream_status$reset)"
367+
else
368+
upstream="$upstream ($green$upstream_status$reset)"
369+
fi
370+
fi
371+
372+
echo -e "Status: $state$reset ($service$reset)"
373+
echo -e "Upstream server: $upstream"
374+
echo
375+
376+
if is-enabled; then
377+
echo "Run \`rmfakecloudctl disable\` to disable \
378+
rmfakecloud-proxy."
379+
else
380+
echo "Run \`rmfakecloudctl enable\` to enable \
381+
rmfakecloud-proxy."
382+
fi
383+
384+
echo "Run \`rmfakecloudctl set-upstream https://<server>\` to \
385+
set the upstream server."
386+
;;
387+
309388
enable)
310389
if is-enabled; then
311390
echo "rmfakecloud-proxy is already enabled."
@@ -337,14 +416,6 @@ if [[ $0 = "${BASH_SOURCE[0]}" ]]; then
337416
systemctl restart rmfakecloud-proxy
338417
fi
339418
fi
340-
341-
if ! is-enabled; then
342-
cat << MSG
343-
Run the following command to enable rmfakecloud-proxy:
344-
345-
$ rmfakecloudctl enable
346-
MSG
347-
fi
348419
;;
349420

350421
help | -h | --help)

0 commit comments

Comments
 (0)