Skip to content

Commit 7870ff8

Browse files
committed
Add support for connecting to resource using IPv6
1 parent d32d8fc commit 7870ff8

1 file changed

Lines changed: 49 additions & 8 deletions

File tree

plexapi/myplex.py

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,11 +1453,12 @@ class MyPlexResource(PlexObject):
14531453
synced (bool): Unknown (possibly True if the resource has synced content?)
14541454
"""
14551455
TAG = 'resource'
1456-
key = 'https://plex.tv/api/v2/resources?includeHttps=1&includeRelay=1'
1456+
key = 'https://plex.tv/api/v2/resources?includeHttps=1&includeRelay=1&includeIPv6=1'
14571457

14581458
# Default order to prioritize available resource connections
14591459
DEFAULT_LOCATION_ORDER = ['local', 'remote', 'relay']
14601460
DEFAULT_SCHEME_ORDER = ['https', 'http']
1461+
DEFAULT_IP_ORDER = ['ipv4', 'ipv6']
14611462

14621463
def _loadData(self, data):
14631464
""" Load attribute values from Plex XML response. """
@@ -1491,8 +1492,10 @@ def connections(self):
14911492
def preferred_connections(
14921493
self,
14931494
ssl=None,
1495+
ipv6=None,
14941496
locations=None,
14951497
schemes=None,
1498+
ipvs=None,
14961499
):
14971500
""" Returns a sorted list of the available connection addresses for this resource.
14981501
Often times there is more than one address specified for a server or client.
@@ -1502,37 +1505,70 @@ def preferred_connections(
15021505
ssl (bool, optional): Set True to only connect to HTTPS connections. Set False to
15031506
only connect to HTTP connections. Set None (default) to connect to any
15041507
HTTP or HTTPS connection.
1508+
ipv6 (bool, optional): Set True to only connect to IPv6 connections. Set False to
1509+
only connect to IPv4 connections. Set None (default) to connect to any
1510+
IPv4 or IPv6 connection.
15051511
"""
15061512
if locations is None:
15071513
locations = self.DEFAULT_LOCATION_ORDER[:]
15081514
if schemes is None:
15091515
schemes = self.DEFAULT_SCHEME_ORDER[:]
1516+
if ipvs is None:
1517+
ipvs = self.DEFAULT_IP_ORDER[:]
1518+
1519+
# Create a copy to avoid mutating args
1520+
schemes = schemes[:]
1521+
ipvs = ipvs[:]
1522+
1523+
if ssl is True: schemes.remove('http')
1524+
elif ssl is False: schemes.remove('https')
1525+
1526+
if ipv6 is True: ipvs.remove('ipv4')
1527+
elif ipv6 is False: ipvs.remove('ipv6')
1528+
1529+
connections_dict = {
1530+
location: {
1531+
scheme: {
1532+
ip: []
1533+
for ip in ipvs
1534+
}
1535+
for scheme in schemes
1536+
}
1537+
for location in locations
1538+
}
15101539

1511-
connections_dict = {location: {scheme: [] for scheme in schemes} for location in locations}
15121540
for connection in self.connections:
15131541
# Only check non-local connections unless we own the resource
15141542
if self.owned or (not self.owned and not connection.local):
1543+
15151544
location = 'relay' if connection.relay else ('local' if connection.local else 'remote')
15161545
if location not in locations:
15171546
continue
1547+
1548+
ipv = 'ipv6' if connection.ipv6 else 'ipv4'
1549+
if ipv not in ipvs:
1550+
continue
1551+
15181552
if 'http' in schemes:
1519-
connections_dict[location]['http'].append(connection.httpuri)
1553+
connections_dict[location]['http'][ipv].append(connection.httpuri)
15201554
if 'https' in schemes:
1521-
connections_dict[location]['https'].append(connection.uri)
1522-
if ssl is True: schemes.remove('http')
1523-
elif ssl is False: schemes.remove('https')
1555+
connections_dict[location]['https'][ipv].append(connection.uri)
1556+
15241557
connections = []
15251558
for location in locations:
15261559
for scheme in schemes:
1527-
connections.extend(connections_dict[location][scheme])
1560+
for ipv in ipvs:
1561+
connections.extend(connections_dict[location][scheme][ipv])
15281562
return connections
15291563

15301564
def connect(
15311565
self,
15321566
ssl=None,
1567+
ipv6=None,
15331568
timeout=None,
15341569
locations=None,
15351570
schemes=None,
1571+
ipvs=None,
15361572
):
15371573
""" Returns a new :class:`~plexapi.server.PlexServer` or :class:`~plexapi.client.PlexClient` object.
15381574
Uses `MyPlexResource.preferred_connections()` to generate the priority order of connection addresses.
@@ -1543,6 +1579,9 @@ def connect(
15431579
ssl (bool, optional): Set True to only connect to HTTPS connections. Set False to
15441580
only connect to HTTP connections. Set None (default) to connect to any
15451581
HTTP or HTTPS connection.
1582+
ipv6 (bool, optional): Set True to only connect to IPv6 connections. Set False to
1583+
only connect to IPv4 connections. Set None (default) to connect to any
1584+
IPv4 or IPv6 connection.
15461585
timeout (int, optional): The timeout in seconds to attempt each connection.
15471586
15481587
Raises:
@@ -1552,8 +1591,10 @@ def connect(
15521591
locations = self.DEFAULT_LOCATION_ORDER[:]
15531592
if schemes is None:
15541593
schemes = self.DEFAULT_SCHEME_ORDER[:]
1594+
if ipvs is None:
1595+
ipvs = self.DEFAULT_IP_ORDER[:]
15551596

1556-
connections = self.preferred_connections(ssl, locations, schemes)
1597+
connections = self.preferred_connections(ssl, ipv6, locations, schemes, ipvs)
15571598
# Try connecting to all known resource connections in parallel, but
15581599
# only return the first server (in order) that provides a response.
15591600
cls = PlexServer if 'server' in self.provides else PlexClient

0 commit comments

Comments
 (0)