Skip to content

Instantly share code, notes, and snippets.

@tommeier
Last active January 11, 2020 04:10
Show Gist options
  • Save tommeier/6255771 to your computer and use it in GitHub Desktop.
Save tommeier/6255771 to your computer and use it in GitHub Desktop.
Plexconnect for Qnap (Intel - tested on 439 with Apple TV 3). Streaming media to Apple TV 3 from Qnap 439.
#!/bin/sh
# /share/MD0_DATA/.qpkg/autorun/autorun-plexconnect.sh
# chmod +x /share/MD0_DATA/.qpkg/autorun/autorun-plexconnect.sh
curl -L https://gist.github.com/tommeier/6255771/raw/update_plex_connect.sh | bash &
#!/bin/sh
# http://wiki.qnap.com/wiki/Running_Your_Own_Application_at_Startup
# Update and run plexconnect (with logging)
[[ -f /share/MD0_DATA/.qpkg/autorun/autorun-plexconnect.sh ]] && /share/MD0_DATA/.qpkg/autorun/autorun-plexconnect.sh &
#!/bin/bash
set -e
# ======================================================================
#
# Install / Update Plexconnect on Qnap
#
# ======================================================================
# Context:
# This is my build + update script for plexconnect on a Qnap 439. I run this
# script from my QNAP while SSHed in from a Mac.
# You can fork this code here: https://gist.github.com/tommeier/6255771
# Running this raw script is as easy as running this one line on the QNAP:
# curl -L https://gist.github.com/tommeier/6255771/raw/update_plex_connect.sh | bash
#
# Requirements:
# - Latest Plex QPkg is installed and running
# - Knowledge on how to SSH on to your QNAP
# - ipkg install bash
# - Ensure your webserver is disabled or change the port from 80 to something else
# - Set autorun.sh to load PlexConnect on boot. See http://wiki.qnap.com/wiki/Running_Your_Own_Application_at_Startup
# Ensure the following is installed on the QNAP already via QPKGs
# - Optware (http://wiki.qnap.com/wiki/Install_Optware_IPKG)
# - Python
#
# Example usage:
# > ssh on to qnap (ssh [email protected])
# > Simply run this next line from the command line on the QNAP
# curl -L https://gist.github.com/tommeier/6255771/raw/update_plex_connect.sh | bash
#
# Useful commands in debugging:
# # Copy all plex logs:
# > scp -r "[email protected]:/share/MD0_DATA/.qpkg/PlexMediaServer/Library/Plex\ Media\ Server/Logs" .
# # Copy certificate files (to install on AppleTV):
# > scp -r "[email protected]:/share/MD0_DATA/.plexconnect/assets/certificates" .
#
# Global variables
PLEX_GIT="git://github.com/iBaa/PlexConnect.git"
PLEX_GIT_DIR_ROOT="/share/MD0_DATA"
PLEX_FOLDER_NAME='.plexconnect'
PLEX_GIT_DIR="${PLEX_GIT_DIR_ROOT}/${PLEX_FOLDER_NAME}"
PLEX_CERT_DIR="${PLEX_GIT_DIR}/assets/certificates"
PLEX_CERT_PATH="${PLEX_CERT_DIR}/trailers.pem" # trailers.cer also required
PYTHON_BIN="/share/MD0_DATA/.qpkg/Python/bin/python"
#PLEX_BIN="${PLEX_GIT_DIR}/PlexConnect.py"
PLEX_DAEMON_SH="PlexConnect_daemon.bash"
PLEX_DAEMON_SH_PATH="${PLEX_GIT_DIR}/${PLEX_DAEMON_SH}"
SPACER="--------------------------------------------"
# /Global variables
# Qnap logging with echo
write_to_log() {
local message="$1"
local log_type="0"
#0 = information, 1 = warning, 2 = error
if [[ "$2" != '' ]]; then
local log_type="$2"
fi;
echo "[plexconnect-script] ${message}"
/sbin/log_tool --append "[plexconnect-script] ${message}" --type $log_type
}
check_daemon_exists(){
if [ ! -e $PLEX_DAEMON_SH_PATH ]; then
echo $SPACER
write_to_log ">!! Error: Expected to find '$PLEX_DAEMON_SH_PATH' to control PlexConnect. Change script to match updates."
echo $SPACER
exit 1
fi;
}
echo $SPACER
write_to_log ">>> Start of Plexconnect Install Script <<<<"
echo $SPACER
write_to_log ">>> Checking dependencies..."
echo $SPACER
if [ ! -e $PYTHON_BIN ]; then
echo $SPACER
write_to_log ">!! Error: Please install Python as a QPKG."
echo $SPACER
exit 1
fi;
if [ ! -d "/share/MD0_DATA/.qpkg/PlexMediaServer" ]; then
echo $SPACER
write_to_log ">!! Error: Please install Plex Media Server as a QPKG."
echo $SPACER
exit 1
fi;
echo $SPACER
write_to_log ">>> Installing required iPkg dependencies"
echo $SPACER
ipkg update
ipkg install bash
ipkg install git
if [ ! -d $PLEX_GIT_DIR ]; then
echo $SPACER
write_to_log ">>> Cloning latest plexconnect to '$PLEX_GIT_DIR'"
echo $SPACER
cd $PLEX_GIT_DIR_ROOT
git clone $PLEX_GIT $PLEX_FOLDER_NAME
else
echo $SPACER
write_to_log ">>> Stopping any existing PlexConnect process"
echo $SPACER
check_daemon_exists;
cd $PLEX_GIT_DIR
./$PLEX_DAEMON_SH stop
echo $SPACER
write_to_log ">>> Updating existing plexconnect"
echo $SPACER
cd $PLEX_GIT_DIR
git pull
fi;
if [ ! -e $PLEX_CERT_PATH ]; then
echo $SPACER
write_to_log ">>> Plex certificate missing."
echo $SPACER
cd $PLEX_CERT_DIR
write_to_log ">>> -> Generating trailers.pem certificate."
openssl req -new -nodes -newkey rsa:2048 -out trailers.pem -keyout trailers.key -x509 -days 7300 -subj "/C=US/CN=trailers.apple.com"
write_to_log ">>> -> Applying key and permissions to certificate."
openssl x509 -in trailers.pem -outform der -out trailers.cer && cat trailers.key >> trailers.pem
write_to_log ">>> -> Done. (optionally - add to apple tv)."
cd $PLEX_GIT_DIR
else
echo $SPACER
write_to_log ">>> Plex certificate present: '${PLEX_CERT_PATH}'"
echo $SPACER
fi;
check_daemon_exists;
echo $SPACER
write_to_log ">>> Starting PlexConnect Daemon"
echo $SPACER
cd $PLEX_GIT_DIR
./$PLEX_DAEMON_SH start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment