Allow server claiming at any time

This commit is contained in:
Joe Groocock 2018-10-04 12:48:52 +01:00
parent e36ec1723e
commit dac8a21edc
Signed by: frebib
GPG Key ID: E0B16BEACFBB6A86
5 changed files with 94 additions and 36 deletions

View File

@ -25,7 +25,7 @@ _*last we checked_
Navigate to [plex.tv/claim](https://www.plex.tv/claim) and obtain a token in the form `claim-xxxx...`
Start the container, as demonstrated below, passing the claim token via the `PLEX_CLAIM` environment variable. This only has to be present on the first run (when the configuration is generated) and can be removed for subsequent runs. _The Plex claim token is optional however it will make the server available to your account immediately._
Start the container, as demonstrated below, passing the claim token via the `PLEX_CLAIM` environment variable. This only has to be present on the first run (when the configuration is generated/if you need to re-claim the server at any time) and can be removed for subsequent runs. _The Plex claim token is optional however it will make the server available to your account immediately._
Setting the container hostname on first boot will set the Plex server name.
@ -80,3 +80,8 @@ See also: https://support.plex.tv/articles/201543147-what-network-ports-do-i-nee
At the very least, you should expose `32400/tcp` to your network, and _port forward_ it through your router if you would like Plex access outside your home network.
If you wish, you can map the Plex port to any other port outside your network, just be sure to update the port in _Settings > Server > Remote Access_ (Show Advanced) under _Manually specify public port_.
## Troubleshooting
* **Help, I accidentally logged my server out and I can no longer access it**
Just get another claim token from [plex.tv/claim](https://www.plex.tv/claim) and restart the container with it in the environment variable PLEX_CLAIM. This should re-claim your server and it'll appear in your server list once again. You can remove the claim token as soon as the server has been claimed- _they expire after 5 minutes anyway_

61
claim-server.sh Executable file
View File

@ -0,0 +1,61 @@
#!/bin/sh
set -e
# Contains getPref/setPref and PREF_FILE vars
source plex-util.sh
opts=`getopt -n "$0" -l save -l token: -l client-id: -l load-client-id -- st:c:l "$@"` || exit 1
eval set -- "$opts"
while true; do
case "$1" in
-s|--save) save=true; shift;;
-t|--token) claimToken="$2"; shift 2;;
-c|--client-id) clientId="$2"; shift 2;;
-l|--load-client-id) clientId="$(getPref "ProcessedMachineIdentifier")"; shift;;
--) shift; break;;
*) echo 'Error: getopt'; exit 1;;
esac
done
claimToken="${PLEX_CLAIM:-$claimToken}"
clientId="${PLEX_CLIENT_ID:-$clientId}"
if [ -z "${claimToken}" ]; then
>&2 echo "Error: \$PLEX_CLAIM or --token required to claim a server"
>&2 echo " Obtain one from https://plex.tv/claim"
exit 2
fi
if [ -z "${clientId}" ]; then
>&2 echo "Error: \$PLEX_CLIENT_ID or --client-id required to claim a server"
>&2 echo " This is found as 'ProcessedMachineIdentifier' in Preferences.xml"
>&2 echo " Calling this script with --load-client-id will attempt to populate this for you"
exit 3
fi
>&2 echo "Attempting to obtain server token from claim token"
loginInfo="$(curl -X POST \
-H 'X-Plex-Client-Identifier: '${clientId} \
-H 'X-Plex-Product: Plex Media Server'\
-H 'X-Plex-Version: 1.1' \
-H 'X-Plex-Provides: server' \
-H 'X-Plex-Platform: Linux' \
-H 'X-Plex-Platform-Version: 1.0' \
-H 'X-Plex-Device-Name: PlexMediaServer' \
-H 'X-Plex-Device: Linux' \
"https://plex.tv/api/claim/exchange?token=${claimToken}")"
authtoken="$(echo "$loginInfo" | sed -n 's/.*<authentication-token>\(.*\)<\/authentication-token>.*/\1/p')"
if [ -z "$authtoken" ]; then
>&2 echo "Error: Unable to obtain authentication token from Plex"
exit 10
else
>&2 echo "Token obtained successfully"
fi
if [ -n "$save" ]; then
setPref "PlexOnlineToken" "${authtoken}"
else
printf "%s" "$authtoken"
fi

View File

@ -44,11 +44,19 @@ export PREF_FILE="$CONFIG_DIR/Preferences.xml"
exec su-exec -e sh <<EOF
source plex-util.sh
# Generate a default configuration on first run, including some user-specified values
if [ ! -e "\$PREF_FILE" ]; then
source gen-config.sh
fi
# Always attempt to claim an unclaimed server in case it was signed out
token="\$(getPref "PlexOnlineToken")"
if [ ! -z "\${PLEX_CLAIM}" ] && [ -z "\${token}" ]; then
claim-server.sh --load-client-id --save
fi
# Duplicate the Plex logs to stdout (docker logs)
tail -Fn 0 "\$CONFIG_DIR/Logs/Plex Media Server.log" 2>/dev/null &

View File

@ -1,20 +1,8 @@
#!/bin/sh
set -e
CONFIG_DIR="${CONFIG_DIR:-/config}"
PREF_FILE="${PREF_FILE:-"$CONFIG_DIR/Preferences.xml"}"
getPref() {
xmlstarlet sel -T -t -m "/Preferences" -v "@$1" -n "${PREF_FILE}"
}
setPref() {
count="$(xmlstarlet sel -t -v "count(/Preferences/@$1)" "${PREF_FILE}")"
if [ $(($count + 0)) -gt 0 ]; then
xmlstarlet ed --inplace --update "/Preferences/@$1" -v "$2" "${PREF_FILE}" 2>/dev/null
else
xmlstarlet ed --inplace --insert "/Preferences" --type attr -n "$1" -v "$2" "${PREF_FILE}" 2>/dev/null
fi
}
# Contains getPref/setPref and PREF_FILE vars
source plex-util.sh
# Create a default config file allowing external access
echo -e $'<?xml version="1.0" encoding="utf-8"?>\n<Preferences />' > "${PREF_FILE}"
@ -38,27 +26,9 @@ if [ -z "${clientId}" ]; then
setPref "ProcessedMachineIdentifier" "${clientId}"
fi
# Get server token and only turn claim token into server token if we have former but not latter.
token="$(getPref "PlexOnlineToken")"
if [ ! -z "${PLEX_CLAIM}" ] && [ -z "${token}" ]; then
echo "Attempting to obtain server token from claim token"
loginInfo="$(curl -X POST \
-H 'X-Plex-Client-Identifier: '${clientId} \
-H 'X-Plex-Product: Plex Media Server'\
-H 'X-Plex-Version: 1.1' \
-H 'X-Plex-Provides: server' \
-H 'X-Plex-Platform: Linux' \
-H 'X-Plex-Platform-Version: 1.0' \
-H 'X-Plex-Device-Name: PlexMediaServer' \
-H 'X-Plex-Device: Linux' \
"https://plex.tv/api/claim/exchange?token=${PLEX_CLAIM}")"
token="$(echo "$loginInfo" | sed -n 's/.*<authentication-token>\(.*\)<\/authentication-token>.*/\1/p')"
if [ "$token" ]; then
echo "Token obtained successfully"
setPref "PlexOnlineToken" "${token}"
fi
fi
# Claiming the PlexOnlineToken is now done in entrypoint on every boot
# It can also be triggered manually at any time by running
# $ claim-server.sh --load-client-id --save
test -n "${ADVERTISE_IP}" && setPref "customConnections" "${ADVERTISE_IP}"
test -n "${ALLOWED_NETWORKS}" && setPref "allowedNetworks" "${ALLOWED_NETWORKS}"

14
plex-util.sh Normal file
View File

@ -0,0 +1,14 @@
CONFIG_DIR="${CONFIG_DIR:-/config}"
PREF_FILE="${PREF_FILE:-"$CONFIG_DIR/Preferences.xml"}"
getPref() {
xmlstarlet sel -T -t -m "/Preferences" -v "@$1" -n "${PREF_FILE}"
}
setPref() {
count="$(xmlstarlet sel -t -v "count(/Preferences/@$1)" "${PREF_FILE}")"
if [ $(($count + 0)) -gt 0 ]; then
xmlstarlet ed --inplace --update "/Preferences/@$1" -v "$2" "${PREF_FILE}" 2>/dev/null
else
xmlstarlet ed --inplace --insert "/Preferences" --type attr -n "$1" -v "$2" "${PREF_FILE}" 2>/dev/null
fi
}