From fd3bca3a3989471df734122da5065a53dd0cdf9f Mon Sep 17 00:00:00 2001 From: Tommy Mikkelsen Date: Tue, 16 Apr 2019 01:12:10 +0200 Subject: [PATCH 1/9] Fixed #8 --- Windows/ClaimIt/Form1.cs | 71 ++++++++++++++++------ Windows/ClaimIt/Properties/AssemblyInfo.cs | 4 +- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/Windows/ClaimIt/Form1.cs b/Windows/ClaimIt/Form1.cs index 0aa797c..9e279f8 100644 --- a/Windows/ClaimIt/Form1.cs +++ b/Windows/ClaimIt/Form1.cs @@ -269,32 +269,69 @@ private void UpdateStatus(string status, Color color) LWStatus.Items[LWStatus.Items.Count - 1].EnsureVisible(); } + /// + /// Checks if an IP V4 Address is in private address space + /// + /// + /// True if in private address space, else False + private bool _IsPrivate(string ipAddress) + { + int[] ipParts = ipAddress.Split(new String[] { "." }, StringSplitOptions.RemoveEmptyEntries) + .Select(s => int.Parse(s)).ToArray(); + // in private ip range + if (ipParts[0] == 10 || + (ipParts[0] == 192 && ipParts[1] == 168) || + (ipParts[0] == 172 && (ipParts[1] >= 16 && ipParts[1] <= 31))) + { + return true; + } + + // IP Address is probably public. + // This doesn't catch some VPN ranges like OpenVPN and Hamachi. + return false; + } + private void BtnClaimIt_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; PMSUsr = this.tbPlexTVName.Text; PMSPwd = this.tbPlexTvPassword.Text; PMSIPAddr = mtbIPAddress.Text.Replace(" ", ""); - // Get IP of PC running this - IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); - foreach (var IP in localIPs) + // Check if address is either loopback or Private + if (!((PMSIPAddr == "127.0.0.1") || _IsPrivate(PMSIPAddr))) { - this.LWStatus.Items.Add(LWItem("This app is running on IP: " + IP.ToString(), Color.Gray)); + UpdateStatus("The IP address entered is not in Private Address Space", Color.Red); + UpdateStatus("Either '127.0.0.1' or an address in private address space is needed to claim a server", Color.Red); + UpdateStatus("See: https://en.wikipedia.org/wiki/Private_network", Color.Red); + FatalError(); } - UpdateStatus("PMS IP: " + PMSIPAddr, Color.Gray); - UpdateStatus("Starting to work...", Color.Gray); - if (ComparePwd()) - { - if (GetPMSIdentifier()) + else + { + // Get IP of PC running this + IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); + foreach (var IP in localIPs) + { + this.LWStatus.Items.Add(LWItem("This app is running on IP: " + IP.ToString(), Color.Gray)); + } + UpdateStatus("PMS IP: " + PMSIPAddr, Color.Gray); + UpdateStatus("Starting to work...", Color.Gray); + if (ComparePwd()) { - if (GetUserToken()) + if (GetPMSIdentifier()) { - if (GetClaimToken()) + if (GetUserToken()) { - if (ClaimIt()) + if (GetClaimToken()) { - UpdateStatus("All Done", Color.Green); - UpdateStatus(string.Format("Please close your browser, reopen, and browse to http://{0}:32400/web", PMSIPAddr), Color.Green); + if (ClaimIt()) + { + UpdateStatus("All Done", Color.Green); + UpdateStatus(string.Format("Please close your browser, reopen, and browse to http://{0}:32400/web", PMSIPAddr), Color.Green); + } + else + { + FatalError(); + } } else { @@ -315,12 +352,8 @@ private void BtnClaimIt_Click(object sender, EventArgs e) { FatalError(); } + Cursor.Current = Cursors.Default; } - else - { - FatalError(); - } - Cursor.Current = Cursors.Default; } } } diff --git a/Windows/ClaimIt/Properties/AssemblyInfo.cs b/Windows/ClaimIt/Properties/AssemblyInfo.cs index c747e0f..8b129a7 100644 --- a/Windows/ClaimIt/Properties/AssemblyInfo.cs +++ b/Windows/ClaimIt/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.0.1.0")] +[assembly: AssemblyFileVersion("1.0.1.0")] From cbd0735902d5fc5a783fe8d9f03e7661ad3932e1 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 3 May 2019 13:38:48 +0200 Subject: [PATCH 2/9] Initial test branch of private IP check on Linux. --- linux/claimpms.sh | 88 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/linux/claimpms.sh b/linux/claimpms.sh index 1c28a17..f950654 100644 --- a/linux/claimpms.sh +++ b/linux/claimpms.sh @@ -10,6 +10,15 @@ # Mark Walker/ZiGGiMoN, a Plex hobbyist #************************************************************************ +#***************************************** +#* Test if /bin/sh is dash +#* Functions's don't work with dash. +#***************************************** +#if [ "$(readlink -- "/bin/sh")" = dash ]; then +# echo "/bin/sh links to dash, upgrade to bash or run: bash claimit.sh" +# exit 1 +#fi + #************************************************************************ # Functions #************************************************************************ @@ -26,6 +35,26 @@ function ComparePwd() fi } +function ValidateIP() +# Check if IP has valid format +{ + if [ "`echo $ippms | awk -F'.' 'NF==4 && $1 > 0 && $1<256 && $2<256 && $3<256 && $4<256 && !/\.\./'`" == "$ippms" ] + then + if echo "$ippms" | grep -Eq '(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)' >/dev/null; then + return 0 + else + echo "The IP address entered is not in Private Address Space" + echo "Either '127.0.0.1' or an address in private address space is needed to claim a server" + echo "See: https://en.wikipedia.org/wiki/Private_network" + exit 1 + fi + return 0 + else + echo "IP is not valid" + exit 1 # terminate and indicate error + fi +} + function GetClaimToken() #***************************************** #* Get claim token from plex.tv @@ -85,7 +114,7 @@ function GetLoginToken() function GetClientIdentifier() # Get PMS machineIdentifier { - url="$ippms:32400/identity" + url="https://$ippms:32400/identity" content=$(curl -i -k -L -s $url) local machineIdentifier=$(printf %s "$content" | awk -F= '$1=="machineIdentifier"{print $2}' RS=' '| cut -d '"' -f 2) local http_status=$(echo "$content" | grep HTTP | awk '{print $2}') @@ -141,6 +170,35 @@ function Claimit() fi } +function CheckForOnLineMail() +#***************************************** +#* Get user auth token from plex.tv +#* Needs the following params: +#* 1 Param: UserName +#* 2 Param: Password +#* 3 Param: X-Plex-Client-Identifier +#***************************************** +{ + url="https://$ippms:32400/:/prefs?X-Plex-Token=$UserToken" + local response=$(curl -i -k -L -s $url) + # Grap the MailAddr + local usermail=$(printf %s "$response" | awk -F= '/PlexOnlineMail/{print $7}'|cut -d '"' -f 2) + # grap the return code + local http_status=$(echo "$response" | grep "HTTP/" | awk '{print $2}') + if [ -z "$http_status" ]; + then + exit 1 + else + if [ $http_status -eq "200" ] + then + echo "$usermail" + exit 0 + else + exit 1 + fi + fi +} + #************************************************************************ #* Main #************************************************************************ @@ -173,6 +231,16 @@ echo "Comparing entered passwords" ComparePwd echo "Comparing entered passwords ok" +echo "Validating IP address" +if ! CheckIPValidity=$(ValidateIP); +then + echo "******** ERROR ********" + echo "The IP address entered is not in Private Address Space" + echo "Either '127.0.0.1' or an address in private address space is needed to claim a server" + echo "See: https://en.wikipedia.org/wiki/Private_network" + exit 1 +fi + echo "Getting PMS Server Identifier" if ! XPlexClientIdentifier=$(GetClientIdentifier); then @@ -205,6 +273,22 @@ then fi echo "Getting PMS Claim Token ok" +# Check if server is claimed already +echo "Checking if server is already claimed" +if ! CheckClaimedEmail=$(CheckForOnLineMail); +then + echo "******** ERROR ********" + echo "We failed to grab claim email from Plex Media Server" + echo "Who are you gonna call?" + exit 1 +fi + +if echo "$CheckClaimedEmail" | grep '^[a-zA-Z0-9._]*@[a-zA-Z0-9.]*\.[a-zA-Z0-9]*' >/dev/null; then + echo "The server is already claimed with email: $CheckClaimedEmail" + echo "Exiting" + exit 1 +fi + # Claiming server echo "Claiming server" -Claimit "$ippms" "$XPlexClaimtoken" "$XPlexClientIdentifier" "$UserToken" +echo Claimit "$ippms" "$XPlexClaimtoken" "$XPlexClientIdentifier" "$UserToken" From 07d350543fd19cd3d7934ac6ab2127e56568c1d7 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 3 May 2019 13:55:56 +0200 Subject: [PATCH 3/9] Fix ssl to be non-ssl --- linux/claimpms.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux/claimpms.sh b/linux/claimpms.sh index f950654..6cc587a 100644 --- a/linux/claimpms.sh +++ b/linux/claimpms.sh @@ -114,7 +114,7 @@ function GetLoginToken() function GetClientIdentifier() # Get PMS machineIdentifier { - url="https://$ippms:32400/identity" + url="http://$ippms:32400/identity" content=$(curl -i -k -L -s $url) local machineIdentifier=$(printf %s "$content" | awk -F= '$1=="machineIdentifier"{print $2}' RS=' '| cut -d '"' -f 2) local http_status=$(echo "$content" | grep HTTP | awk '{print $2}') From 54de49e715ff5859a427034fbe75813b3a87be94 Mon Sep 17 00:00:00 2001 From: Tommy Mikkelsen Date: Sun, 5 May 2019 21:42:41 +0200 Subject: [PATCH 4/9] Removed check for already claimed --- linux/claimpms.sh | 49 ++--------------------------------------------- 1 file changed, 2 insertions(+), 47 deletions(-) diff --git a/linux/claimpms.sh b/linux/claimpms.sh index 6cc587a..5e7332d 100644 --- a/linux/claimpms.sh +++ b/linux/claimpms.sh @@ -170,35 +170,6 @@ function Claimit() fi } -function CheckForOnLineMail() -#***************************************** -#* Get user auth token from plex.tv -#* Needs the following params: -#* 1 Param: UserName -#* 2 Param: Password -#* 3 Param: X-Plex-Client-Identifier -#***************************************** -{ - url="https://$ippms:32400/:/prefs?X-Plex-Token=$UserToken" - local response=$(curl -i -k -L -s $url) - # Grap the MailAddr - local usermail=$(printf %s "$response" | awk -F= '/PlexOnlineMail/{print $7}'|cut -d '"' -f 2) - # grap the return code - local http_status=$(echo "$response" | grep "HTTP/" | awk '{print $2}') - if [ -z "$http_status" ]; - then - exit 1 - else - if [ $http_status -eq "200" ] - then - echo "$usermail" - exit 0 - else - exit 1 - fi - fi -} - #************************************************************************ #* Main #************************************************************************ @@ -214,10 +185,10 @@ echo "*" echo "*" echo "* Made by dane22, a Plex community member" echo "* And Mark Walker/ZiGGiMoN, a Plex hobbyist" +echo "*" +echo "* To see the manual, please visit https://github.com/ukdtom/ClaimIt/wiki" echo "************************************************************************" - - read -p 'plex.tv Username: ' uservar echo '' read -sp 'plex.tv Password: ' passvar @@ -273,22 +244,6 @@ then fi echo "Getting PMS Claim Token ok" -# Check if server is claimed already -echo "Checking if server is already claimed" -if ! CheckClaimedEmail=$(CheckForOnLineMail); -then - echo "******** ERROR ********" - echo "We failed to grab claim email from Plex Media Server" - echo "Who are you gonna call?" - exit 1 -fi - -if echo "$CheckClaimedEmail" | grep '^[a-zA-Z0-9._]*@[a-zA-Z0-9.]*\.[a-zA-Z0-9]*' >/dev/null; then - echo "The server is already claimed with email: $CheckClaimedEmail" - echo "Exiting" - exit 1 -fi - # Claiming server echo "Claiming server" echo Claimit "$ippms" "$XPlexClaimtoken" "$XPlexClientIdentifier" "$UserToken" From 11e10f136d5d9e969abd763a18d9572b5b507cb7 Mon Sep 17 00:00:00 2001 From: Tommy Mikkelsen Date: Sun, 5 May 2019 22:10:11 +0200 Subject: [PATCH 5/9] Switched to use own Wiki pages instead, as well as enabled final action --- linux/claimpms.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/linux/claimpms.sh b/linux/claimpms.sh index 5e7332d..d16dd90 100644 --- a/linux/claimpms.sh +++ b/linux/claimpms.sh @@ -8,6 +8,8 @@ # # Made by dane22, a Plex community member # Mark Walker/ZiGGiMoN, a Plex hobbyist +# +# Home: https://github.com/ukdtom/ClaimIt #************************************************************************ #***************************************** @@ -208,7 +210,7 @@ then echo "******** ERROR ********" echo "The IP address entered is not in Private Address Space" echo "Either '127.0.0.1' or an address in private address space is needed to claim a server" - echo "See: https://en.wikipedia.org/wiki/Private_network" + echo "See: https://github.com/ukdtom/ClaimIt/wiki/IP-Address-requirement" exit 1 fi @@ -246,4 +248,4 @@ echo "Getting PMS Claim Token ok" # Claiming server echo "Claiming server" -echo Claimit "$ippms" "$XPlexClaimtoken" "$XPlexClientIdentifier" "$UserToken" +Claimit "$ippms" "$XPlexClaimtoken" "$XPlexClientIdentifier" "$UserToken" From be9ce8f2aab00fd08fb7f1374b47e8ce54bab4a8 Mon Sep 17 00:00:00 2001 From: Tommy Mikkelsen Date: Sun, 5 May 2019 22:22:02 +0200 Subject: [PATCH 6/9] Removed not used code to check if using bash or dash --- linux/claimpms.sh | 9 --------- 1 file changed, 9 deletions(-) diff --git a/linux/claimpms.sh b/linux/claimpms.sh index d16dd90..f263c73 100644 --- a/linux/claimpms.sh +++ b/linux/claimpms.sh @@ -12,15 +12,6 @@ # Home: https://github.com/ukdtom/ClaimIt #************************************************************************ -#***************************************** -#* Test if /bin/sh is dash -#* Functions's don't work with dash. -#***************************************** -#if [ "$(readlink -- "/bin/sh")" = dash ]; then -# echo "/bin/sh links to dash, upgrade to bash or run: bash claimit.sh" -# exit 1 -#fi - #************************************************************************ # Functions #************************************************************************ From 92d13f2e480603833661e5810f18ce03e277fa1e Mon Sep 17 00:00:00 2001 From: Tommy Mikkelsen Date: Sun, 5 May 2019 22:23:13 +0200 Subject: [PATCH 7/9] Missed one wiki direct --- linux/claimpms.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux/claimpms.sh b/linux/claimpms.sh index f263c73..ea70f18 100644 --- a/linux/claimpms.sh +++ b/linux/claimpms.sh @@ -38,7 +38,7 @@ function ValidateIP() else echo "The IP address entered is not in Private Address Space" echo "Either '127.0.0.1' or an address in private address space is needed to claim a server" - echo "See: https://en.wikipedia.org/wiki/Private_network" + echo "See: https://github.com/ukdtom/ClaimIt/wiki/IP-Address-requirement" exit 1 fi return 0 From 183202dde1fd36c5d0539cdd4e7e9be1428fc067 Mon Sep 17 00:00:00 2001 From: Tommy Mikkelsen Date: Sun, 5 May 2019 23:13:42 +0200 Subject: [PATCH 8/9] Switched to GitHub Wiki --- Windows/ClaimIt/Form1.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Windows/ClaimIt/Form1.cs b/Windows/ClaimIt/Form1.cs index 9e279f8..3c02528 100644 --- a/Windows/ClaimIt/Form1.cs +++ b/Windows/ClaimIt/Form1.cs @@ -255,8 +255,7 @@ private Boolean ClaimIt() { UpdateStatus("PROBLEM: Can't Claim PMS.....Job Aborted", Color.Red); UpdateStatus("Error was: " + ex.Message, Color.Red); - UpdateStatus("If status code is 401, then most likely the PMS was already claimed, and belongs to somebody else!", Color.Red); - UpdateStatus("If this really is your server, then see: https://support.plex.tv/articles/204281528-why-am-i-locked-out-of-server-settings-and-how-do-i-get-in/", Color.Red); + UpdateStatus("Please see: https://github.com/ukdtom/ClaimIt/wiki/Error-Codes", Color.Red); return false; } } @@ -301,8 +300,7 @@ private void BtnClaimIt_Click(object sender, EventArgs e) if (!((PMSIPAddr == "127.0.0.1") || _IsPrivate(PMSIPAddr))) { UpdateStatus("The IP address entered is not in Private Address Space", Color.Red); - UpdateStatus("Either '127.0.0.1' or an address in private address space is needed to claim a server", Color.Red); - UpdateStatus("See: https://en.wikipedia.org/wiki/Private_network", Color.Red); + UpdateStatus("Please see: https://github.com/ukdtom/ClaimIt/wiki/IP-Address-requirement", Color.Red); FatalError(); } else From 07ca073666fdc19d49b1302b6002268757d1676a Mon Sep 17 00:00:00 2001 From: Tommy Mikkelsen Date: Sun, 5 May 2019 23:36:27 +0200 Subject: [PATCH 9/9] Changed version to 1.1.0.0. --- Windows/ClaimIt/Properties/AssemblyInfo.cs | 4 ++-- linux/claimpms.sh | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Windows/ClaimIt/Properties/AssemblyInfo.cs b/Windows/ClaimIt/Properties/AssemblyInfo.cs index 8b129a7..45db835 100644 --- a/Windows/ClaimIt/Properties/AssemblyInfo.cs +++ b/Windows/ClaimIt/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.1.0")] -[assembly: AssemblyFileVersion("1.0.1.0")] +[assembly: AssemblyVersion("1.1.0.0")] +[assembly: AssemblyFileVersion("1.1.0.0")] diff --git a/linux/claimpms.sh b/linux/claimpms.sh index ea70f18..322b13d 100644 --- a/linux/claimpms.sh +++ b/linux/claimpms.sh @@ -9,6 +9,8 @@ # Made by dane22, a Plex community member # Mark Walker/ZiGGiMoN, a Plex hobbyist # +# Version: 1.1.0.0 +# # Home: https://github.com/ukdtom/ClaimIt #************************************************************************ @@ -179,6 +181,8 @@ echo "*" echo "* Made by dane22, a Plex community member" echo "* And Mark Walker/ZiGGiMoN, a Plex hobbyist" echo "*" +echo "* Version 1.1.0.0" +echo "*" echo "* To see the manual, please visit https://github.com/ukdtom/ClaimIt/wiki" echo "************************************************************************"