From dac8a21edc5854ba887e93b883163d9e0a73fb51 Mon Sep 17 00:00:00 2001 From: Joe Groocock Date: Thu, 4 Oct 2018 12:48:52 +0100 Subject: [PATCH] Allow server claiming at any time --- README.md | 7 +++++- claim-server.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ entrypoint | 8 +++++++ gen-config.sh | 40 ++++---------------------------- plex-util.sh | 14 ++++++++++++ 5 files changed, 94 insertions(+), 36 deletions(-) create mode 100755 claim-server.sh create mode 100644 plex-util.sh diff --git a/README.md b/README.md index d8c6d95..8d56b26 100644 --- a/README.md +++ b/README.md @@ -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_ diff --git a/claim-server.sh b/claim-server.sh new file mode 100755 index 0000000..d9cf853 --- /dev/null +++ b/claim-server.sh @@ -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>.*/\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 + diff --git a/entrypoint b/entrypoint index 7f83c77..8b9ae67 100755 --- a/entrypoint +++ b/entrypoint @@ -44,11 +44,19 @@ export PREF_FILE="$CONFIG_DIR/Preferences.xml" exec su-exec -e sh </dev/null & diff --git a/gen-config.sh b/gen-config.sh index 86adfd8..3480ee3 100755 --- a/gen-config.sh +++ b/gen-config.sh @@ -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 $'\n' > "${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>.*/\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}" diff --git a/plex-util.sh b/plex-util.sh new file mode 100644 index 0000000..3d560af --- /dev/null +++ b/plex-util.sh @@ -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 +}