-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSet-PlexConfiguration.ps1
235 lines (204 loc) · 7.58 KB
/
Set-PlexConfiguration.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
function Set-PlexConfiguration
{
<#
.SYNOPSIS
Obtains an access token for your account and saves it and your server details.
.DESCRIPTION
Used to save Plex configuration to disk, which is used by all other functions.
.PARAMETER Credential
Credential object containing your Plex username and password.
.EXAMPLE
Set-PlexConfiguration -Credential (Get-Credential)
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[PSCredential]
$Credential,
[Parameter(Mandatory = $true)]
[String]
$DefaultServerName
)
#Region Get auth token:
Write-Verbose -Message "Getting authentication token"
try
{
$Base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Credential.GetNetworkCredential().UserName, $Credential.GetNetworkCredential().Password)))
$Data = Invoke-RestMethod -Uri "https://plex.tv/users/sign_in.json" -Method POST -Headers @{
'Authorization' = ("Basic {0}" -f $Base64AuthInfo);
'X-Plex-Client-Identifier' = "PowerShell-Test";
'X-Plex-Product' = 'PowerShell-Test';
'X-Plex-Version' = "V0.01";
'X-Plex-Username' = $Credential.GetNetworkCredential().UserName;
} -ErrorAction Stop
}
catch
{
throw $_
}
#EndRegion
#Region Get online servers
Write-Verbose -Message "Getting list of accessible servers"
try
{
$ResourceData = Invoke-RestMethod -Uri "https://plex.tv/api/v2/resources?includeHttps=1&X-Plex-Token=$($Data.user.authentication_token)&X-Plex-Client-Identifier=PSPlex" -Method GET -UseBasicParsing -Headers @{"Accept" = "application/json, text/plain, */*" }
if(!$ResourceData)
{
throw "Could not get resource data."
}
Write-Verbose -Message "The following servers were returned: $(($ResourceData.name | Sort-Object) -join ", ")"
# Refine to only servers that are online and owned by the user:
Write-Verbose -Message "Refining to only owned and online servers."
[Array]$OwnedAndOnline = $ResourceData | Where-Object { $_.product -eq 'Plex Media Server' -and $_.owned -eq 1 -and $_.presence -eq $True }
if(!$OwnedAndOnline)
{
throw "No owned servers online."
}
Write-Verbose -Message "The following servers are owned and online: $(($OwnedAndOnline.name | Sort-Object) -join ", ")"
# If in the owned and online servers, there's no match for $DefaultServerName, throw an error:
if($OwnedAndOnline.Name -notcontains $DefaultServerName)
{
throw "The server name '$DefaultServerName' does not match any of the owned and online servers."
}
# Loop and construct a custom object to store in our configuration file.
$ConfigurationData = [System.Collections.ArrayList]@()
foreach($Server in $OwnedAndOnline)
{
<#
When storing the configuration data for each server we need an accessible uri.
Servers can have multiple connections defined in the .connections property.
Example for a server where remote access is turned off:
protocol : https
address : 192.168.0.100
port : 32400
uri : https://192-168-0-100.someidentifier.plex.direct:32400
local : True
relay : False
IPv6 : False
Example for a server with remote access enabled. Note the public IP address in the .connections.address property
and multiple plex.direct uris:
protocol : https
address : 172.17.0.5
port : 32400
uri : https://172-17-0-5.someidentifier.plex.direct:32400
local : True
relay : False
IPv6 : False
protocol : https
address : 219.153.56.22
port : 32400
uri : https://219-153-56-22.someidentifier.plex.direct:32400
local : False
relay : False
IPv6 : False
Example for a server where remote access is turned off but we are advertising a custom uri for discovery by clients
Note the only way we can access this particular server if it's on another network, is via the custom uri (as this IP
is an internal docker IP address).
protocol : https
address : mydomain.com
port : 443
uri : https://mydomain.com:443
local : False
relay : False
IPv6 : False
protocol : https
address : 172.18.0.2
port : 32400
uri : https://172-18-0-2.someidentifier.plex.direct:32400
local : True
relay : False
IPv6 : False
There might be a more elegant way to select the connection we want but for now we'll go with this for a priority ordering:
1) A direct to the server connection (not via .plex.direct) that is secured with https via a domain name (not an IP address).
Example: mydomain.com:443
2) A secured plex.direct connection. There could be multiple. Prioritise the non-local one but fallback to the local one if necessary.
3) A direct to the server connection (not via .plex.direct) that is not secured with https.
Example: 192.168.0.100:32400
#>
$DirectToServerConnection = $Server.connections | Where-Object { $_.uri -notmatch "plex.direct" -and $_.uri -match "^https" -and $_.address -notmatch "(\d{1,3}\.){3}\d{1,3}" }
if($DirectToServerConnection)
{
Write-Verbose -Message "Found a direct to server connection that is secured with https via a domain name: $($DirectToServerConnection.uri)"
$Uri = $DirectToServerConnection.uri
$Port = $DirectToServerConnection.port
}
else
{
[Array]$PlexDirectConnection = $Server.connections | Where-Object { $_.uri -match "plex.direct" }
# If there are multiple plex.direct connections, prioritise the non-local one:
$NonLocalPlexDirectConnection = $PlexDirectConnection | Where-Object { $_.local -eq $False }
if($NonLocalPlexDirectConnection)
{
Write-Verbose -Message "Found a non-local plex.direct connection: $($NonLocalPlexDirectConnection.uri)"
$Uri = $NonLocalPlexDirectConnection.uri
$Port = $NonLocalPlexDirectConnection.port
}
else
{
$LocalPlexDirectConnection = $PlexDirectConnection | Where-Object { $_.local -eq $True }
if($LocalPlexDirectConnection)
{
Write-Verbose -Message "Found a local plex.direct connection: $($LocalPlexDirectConnection.uri)"
$Uri = $LocalPlexDirectConnection.uri
$Port = $LocalPlexDirectConnection.port
}
}
}
if(!$Uri)
{
# We didn't find a suitable connection to use so skip this server
Write-Verbose -Message "No suitable connection found for server $($Server.name). Skipping."
continue
}
# If the current server name is equal to $DefaultServerName, set the 'Default' property to $true
if($Server.name -eq $DefaultServerName)
{
$Default = $true
}
else
{
$Default = $false
}
Write-Verbose -Message "Adding server $($Server.name) to configuration data."
$ConfigurationData.Add(
[PSCustomObject]@{
PlexServer = $Server.name
Port = $Port
PublicAddress = $Server.publicAddress
ClientIdentifier = $Server.clientIdentifier
Token = $Server.accessToken
Uri = $Uri
Default = $Default
}) | Out-Null
}
}
catch
{
throw $_
}
#EndRegion
#Region Save Configuration to disk
if($ConfigurationData.Count -gt 0)
{
try
{
$ConfigFile = Get-PlexConfigFileLocation -ErrorAction Stop
# Create folder if it doesn't exist:
if(-not (Test-Path (Split-Path $ConfigFile)))
{
New-Item -ItemType Directory -Path (Split-Path $ConfigFile) | Out-Null
}
# Write the configuration data to disk:
ConvertTo-Json -InputObject @($ConfigurationData) -Depth 3 -ErrorAction Stop | Out-File -FilePath $ConfigFile -Force -ErrorAction Stop
}
catch
{
throw $_
}
}
else
{
throw "No servers found."
}
#EndRegion
}