This page looks best with JavaScript enabled

Remote control a Linux-Computer via mobile device - call the webradio

 ·  ☕ 6 min read  ·  🤖 SWU

Energy, electricity suddenly costs a lot more

Energy prices are rising and awareness of energy consumption is increasing, as is the desire to save energy, because you are affected by it yourself.
That’s why I’m rethinking the subject and trying to reconsider what can be achieved in my household.

Power off

It would be best not to use energy in the first place and that is a point I would like to realize more often for my main computer. I have a standard PC with Linux, which I use every day for surfing and such, as well as one with Windows to play around with and a mini PC, i. i. an Intel NUC, the data heap for my LAN, it’s always running for me, just like the fridge, but it’s a low-consumption device that I’d like to afford. The NUC is headless, i.e. without a monitor and keyboard, and can only be accessed via the network - I normally only access it when I have a standard PC in operation. When I’m not playing, Windows is off, but when I’m at home, the Linux computer is running - I’ve chosen it to be off more often, even when I’m there.

Design issues

So if I can migrate tasks that I normally do on the main computer to the NUC because they are not or only slightly interactive processes, I still need a way to start and stop them. A smartphone and an old tablet are available for this purpose. Of course, I can also SSH from a mobile device, but the non-haptic keyboard is annoying because the keys are so small and there is no mechanical feedback. I would like big buttons for this, which can also be easily operated on the small smartphone display with a fat thumb.

Which GUI to choose

A native Android app is out of the question because I can’t shake it out of my head. A browser would be the easier solution. However, I haven’t followed the topic of web development in its details for a long time. I knew what I wanted, a remote control for tasks that I used to do from computer to computer via SSH, in the form of a button on a smartphone. Since I don’t usually write any software for mobile devices, it should be like this to use a web browser. The mini-PC gets a web server that offers the buttons in the LAN and an evaluation of the information that flows back.

You need a website with responsive design (RWD) so that you can use it on a web browser in large format and also on a smartphone screen in a reasonable way. This is probably standard by now, but I explicitly paid attention to it when choosing a hugo theme. And of course there have to be big buttons - I chose “Bootstrap-BP hugo startpage” by Sebastian Pech - I’m already using Hugo for my website’s blog anyway. It was clear that this was my starting point.

PHP presumably with javascript to achieve my goal seem appropriate. I’ve heard and seen a lot about it, but I’ve never really looked into it - so I should have learned that first. What I’ve done before is to use CGI-BIN to call scripts on the web server. I hear it’s old technology. But I guess even if I’m programming this with PHP and JavaScript, if it then crosses the limit of the computer’s file system, that’s just as much a security issue as using CGI-BIN and active scripts.

Current security technology of the web server

The web server user is fairly unprivileged, so much so that e.g. is not allowed to use the audio device at all - he is not allowed to play web radio. In contrast to the past, the web server user is now normally not even allowed to create a file in /tmp or /var/tmp. He can, but that happens there in a security container controlled by sysmtemd. If you gain access to this (root permission required) and monitor the creation and deletion of a specific file, you can turn music on and off as a system user, because the system user is allowed to use the audio device.

First step is taken

I’m that far. I replace a call to a Startpage config target with http://IP address NUC/cgi-bin/webradio.sh

The server sided file webradio.sh has the following content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/sh
echo "Content-type: text/html"
echo
echo
echo "<head>"
echo '<meta http-equiv="refresh" content="0; URL=http://<IP-Adresse NUC>/nucbian/">'
echo "</head>"
if [ -f /tmp/webradio.on ]; then
        echo "<h1>Stopping Webradio<h1>"
        rm /tmp/webradio.on
else

        echo "<h1>Starting Webradio<h1>"
        touch /tmp/webradio.on
fi
echo '<input value="Go Back" type="button" onclick="window.history.back()" class="back-button">'

The call produces a very sober web page that toggles web radio status on/off and even provides information on what to do to go back immediately. If the automatic return does not work, there is also a back button. As you can see here in a few lines, the script ensures on the one hand that proper HTML code is produced with simple Javascript that can be output by the web server and on the other hand code is executed that is already beyond the capabilities of HTML - the creation of a file in /tmp.
The file is actually placed by systemd in a security container named “systemd-private-?????????????????????????????????- apache2.service-??????”
At the file level, this directory is owned by user and group root, permissions are only configured for the root user. As a result, an unprivileged system user has no access to it.
In order for us to be able to evaluate this, root must do this or an unprivileged user with sudo.

The user file web.watch.sh:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/sh
sudo inotifywait -m -e create,delete \
        /proc/`pidof -s /usr/sbin/apache2`/root/tmp/ \
        | while read dir action; do
                #echo "File $file changed via $action in dir $dir"
                if [ "$action" = "CREATE webradio.on" ]; then
                        #echo on
                        mpv "http://somafm.com/sonicuniverse.pls" 2>&1 1>/dev/null &
                fi
                if [  "$action" = "DELETE webradio.on" ]; then
                        #echo off
                        killall mpv
                fi
        done

This script checks whether a specific file is created or deleted in a specific path and processes are started or terminated depending on this. This construction already works. I just call the script manually with nohup, then it runs. This can be improved even further by letting systemd manage it and also the possibility of connecting mpv to a socket for information retrieval and control options goals.

However, proof of feasibility has been provided. I can now switch the web radio on and off with my smartphone.

Smartphone Screenshot:

Smartphone PC-RemoteControl Webradio, more pictures at Gallery Photos
Smartphone Screenshot: Smartphone PC-RemoteControl Webradio, more pictures at Gallery Photos

wüsti
WRITTEN BY
SWU
human