Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

Webterminal: Added SSL support with existing LE certificates. #130

Merged
merged 9 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/webterminal.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,17 @@ panel_iframe:
icon: mdi:console
url: 'http://192.168.1.2:4200'
```

#### Notes for SSL
If you enable the use of existing Let's Encrypt certificates you need to open ports in your firewall to use them.

If SSL is used the panel_iframe has to use the same domain name as the one issued with your certificate.
```yaml
panel_iframe:
web_terminal:
title: 'Web terminal'
icon: mdi:console
url: 'https://yourdomain.duckdns.org:4200'
```
***
This script was originally contributed by [@Ludeeus](https://github.com/ludeeus).
16 changes: 16 additions & 0 deletions package/opt/hassbian/suites/files/webterminalsslhelper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
# Helper script for using LE certificates with Webterminal (shellinabox)
if [ -d "/etc/letsencrypt/live" ]; then
CERTDIR="/etc/letsencrypt/live/"
elif [ -d "/home/homeassistant/dehydrated/certs" ]; then
CERTDIR="/home/homeassistant/dehydrated/certs/"
else
CERTDIR=""
fi
FULLCHAIN=$(find "$CERTDIR" -type f | grep fullchain)
PRIVKEY=$(find "$CERTDIR" -type f | grep privkey)
DOMAIN=$(ls "$CERTDIR")
cat "$FULLCHAIN" "$PRIVKEY" > /var/lib/shellinabox/certificate-"$DOMAIN".pem
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing it this way would not link the cert but only "copy" it. Preferable would be to create a symlink to the certificate so that it's linked to the used certificate.

Copy link
Member Author

@ludeeus ludeeus Mar 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In every guide I have found, and my own testing. the cert file used for shellinabox has to include both certificate and privkey, is it possible to merge with linking?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that. That would require some strange magic.
Most likely this will stop working as soon as the certificate expires. If your ok with that then we can merge this.
Could you add to the update function to update the certificate if it's there?

Copy link
Member Author

@ludeeus ludeeus Mar 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope not :( that defeats the purpose of the separate webterminalhelper.sh script..
Should't this update the cert for shellinabox 1AM every night?
looking at the timestamp after running the file it looks like that would work.
image
I can add a note about running that file in the docs?

"Could you add to the update function to update the certificate if it's there?"
Do you mean the update for dehydrated with the DuckDNS script, then no. that will result in certbot certs not being updated.
If this was not what you ment, enlighten me :D

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that part of the webterminalhepler.sh script.

This look great now!

chown shellinabox:shellinabox -R /var/lib/shellinabox/
service shellinabox restart
exit 0
52 changes: 47 additions & 5 deletions package/opt/hassbian/suites/webterminal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,67 @@ function webterminal-show-copyright-info {
}

function webterminal-install-package {
if [ "$ACCEPT" == "true" ]; then # True if `-y` flag is used.
if [ -d "/etc/letsencrypt/live" ] || [ -d "/home/homeassistant/dehydrated/certs" ]; then
SSL="Y"
else
SSL="N"
fi
else
echo ""
echo -n "Do you use SSL (https) with Home Assistant? [N/y] : "
read -r SSL
if [ ! "$SSL" ]; then
SSL="N"
fi
fi

echo "Installing packages."
sudo apt-get install -y openssl shellinabox

echo "Changing config."
sudo sed -i 's/--no-beep/--no-beep --disable-ssl/g' /etc/default/shellinabox
if [ "$SSL" == "y" ] || [ "$SSL" == "Y" ]; then
echo "No need to change default configuration, skipping this step..."
echo "Checking cert directory..."
if [ -d "/etc/letsencrypt/live" ]; then
CERTDIR="/etc/letsencrypt/live/"
elif [ -d "/home/homeassistant/dehydrated/certs" ]; then
CERTDIR="/home/homeassistant/dehydrated/certs/"
else
CERTDIR=""
fi
echo "Setting cert fullchain location..."
FULLCHAIN=$(find "$CERTDIR" -type f | grep fullchain)
echo "Setting cert privkey location..."
PRIVKEY=$(find "$CERTDIR" -type f | grep privkey)
DOMAIN=$(ls "$CERTDIR")
echo "Merging files and adding to correct dir..."
cat "$FULLCHAIN" "$PRIVKEY" > /var/lib/shellinabox/certificate-"$DOMAIN".pem
chown shellinabox:shellinabox -R /var/lib/shellinabox/
echo "Adding crong job to copy certs..."
(crontab -l ; echo "0 1 1 * * bash /opt/hassbian/suites/files/webterminalsslhelper.sh >/dev/null 2>&1")| crontab -
else
sed -i 's/--no-beep/--no-beep --disable-ssl/g' /etc/default/shellinabox
fi

echo "Reloading and starting the service."
sudo service shellinabox reload
sudo service shellinabox restart
service shellinabox reload
service shellinabox restart

ip_address=$(ifconfig | grep "inet.*broadcast" | grep -v 0.0.0.0 | awk '{print $2}')

if [ "$SSL" == "y" ] || [ "$SSL" == "Y" ]; then
PROTOCOL="https"
else
PROTOCOL="http"
fi

echo "Checking the installation..."
validation=$(pgrep -f shellinaboxd)
if [ ! -z "${validation}" ]; then
echo
echo -e "\\e[32mInstallation done..\\e[0m"
echo
echo "You can now access the web terminal here: http://$ip_address:4200"
echo "You can now access the web terminal here: $PROTOCOL://$ip_address:4200"
echo "You can also add this to your Home-Assistant config in an 'panel_iframe'"
echo
else
Expand Down