def __init__(self, data): self.connection = None self.connections = [] self.accessToken = None self.sourceType = None if data is None: return self.accessToken = data.attrib.get('accessToken') self.httpsRequired = data.attrib.get('httpsRequired') == '1' self.type = data.attrib.get('type') self.clientIdentifier = data.attrib.get('clientIdentifier') self.product = data.attrib.get('product') self.provides = data.attrib.get('provides') self.serverClass = data.attrib.get('serverClass') self.sourceType = data.attrib.get('sourceType') self.uuid = self.clientIdentifier hasSecureConn = False for conn in data.findall('Connection'): if conn.attrib.get('protocol') == "https": hasSecureConn = True break for conn in data.findall('Connection'): connection = plexconnection.PlexConnection( plexconnection.PlexConnection.SOURCE_MYPLEX, conn.attrib.get('uri'), conn.attrib.get('local') == '1', self.accessToken, hasSecureConn and conn.attrib.get('protocol') != "https" ) # Keep the secure connection on top if connection.isSecure: self.connections.insert(0, connection) else: self.connections.append(connection) # If the connection is one of our plex.direct secure connections, add # the nonsecure variant as well, unless https is required. # if self.httpsRequired and conn.attrib.get('protocol') == "https" and conn.attrib.get('address') not in conn.attrib.get('uri'): self.connections.append( plexconnection.PlexConnection( plexconnection.PlexConnection.SOURCE_MYPLEX, "http://{0}:{1}".format(conn.attrib.get('address'), conn.attrib.get('port')), conn.attrib.get('local') == '1', self.accessToken, True ) )
def onManualConnectionsResponse(self, request, response, context): if not response.isSuccess(): return data = response.getBodyXml() if data is not None: serverAddress = context.serverAddress util.DEBUG_LOG( "Received manual connection response for {0}".format( serverAddress)) machineID = data.attrib.get('machineIdentifier') name = context.address if not name or not machineID: return # TODO(rob): Do we NOT want to consider manual connections local? conn = plexconnection.PlexConnection( plexresource.ResourceConnection.SOURCE_MANUAL, serverAddress, True, None) server = plexserver.createPlexServerForConnection(conn) server.uuid = machineID server.name = name server.sourceType = plexresource.ResourceConnection.SOURCE_MANUAL self.updateFromConnectionType( [server], plexresource.ResourceConnection.SOURCE_MANUAL)
def __init__(self): plexserver.PlexServer.__init__(self) self.uuid = 'myplex' self.name = 'plex.tv' conn = plexconnection.PlexConnection( plexresource.ResourceConnection.SOURCE_MYPLEX, "https://plex.tv", False, None) self.connections.append(conn) self.activeConnection = conn
def onSocketEvent(self, message, addr): util.DEBUG_LOG('Received GDM message:\n' + str(message)) hostname = addr[0] # socket.gethostbyaddr(addr[0])[0] name = parseFieldValue(message, "Name: ") port = parseFieldValue(message, "Port: ") or "32400" machineID = parseFieldValue(message, "Resource-Identifier: ") secureHost = parseFieldValue(message, "Host: ") util.DEBUG_LOG("Received GDM response for " + repr(name) + " at http://" + hostname + ":" + port) if not name or not machineID: return import plexserver conn = plexconnection.PlexConnection( plexconnection.PlexConnection.SOURCE_DISCOVERED, "http://" + hostname + ":" + port, True, None, bool(secureHost)) server = plexserver.createPlexServerForConnection(conn) server.uuid = machineID server.name = name server.sameNetwork = True # If the server advertised a secure hostname, add a secure connection as well, and # set the http connection as a fallback. # if secureHost: server.connections.insert( 0, plexconnection.PlexConnection( plexconnection.PlexConnection.SOURCE_DISCOVERED, "https://" + hostname.replace(".", "-") + "." + secureHost + ":" + port, True, None)) self.servers.append(server)
def loadState(self): jstring = plexapp.INTERFACE.getRegistry("PlexServerManager") if not jstring: return try: obj = json.loads(jstring) except: util.ERROR() obj = None if not obj: util.ERROR_LOG("Failed to parse PlexServerManager JSON") return for serverObj in obj['servers']: server = plexserver.createPlexServerForName( serverObj['uuid'], serverObj['name']) server.owned = bool(serverObj.get('owned')) server.sameNetwork = serverObj.get('sameNetwork') hasSecureConn = False for i in range(len(serverObj.get('connections', []))): conn = serverObj['connections'][i] if conn['address'][:5] == "https": hasSecureConn = True break for i in range(len(serverObj.get('connections', []))): conn = serverObj['connections'][i] isFallback = hasSecureConn and conn['address'][:5] != "https" sources = plexconnection.PlexConnection.SOURCE_BY_VAL[ conn['sources']] connection = plexconnection.PlexConnection( sources, conn['address'], conn['isLocal'], conn['token'], isFallback) # Keep the secure connection on top if connection.isSecure: server.connections.insert(0, connection) else: server.connections.append(connection) self.serversByUuid[server.uuid] = server util.LOG("Loaded {0} servers from registry".format(len( obj['servers']))) self.updateReachability(False, True)
def deSerialize(cls, jstring): try: serverObj = json.loads(jstring) except: util.ERROR() util.ERROR_LOG("Failed to deserialize PlexServer JSON") return import plexconnection server = createPlexServerForName(serverObj['uuid'], serverObj['name']) server.owned = bool(serverObj.get('owned')) server.sameNetwork = serverObj.get('sameNetwork') hasSecureConn = False for i in range(len(serverObj.get('connections', []))): conn = serverObj['connections'][i] if conn['address'][:5] == "https": hasSecureConn = True break for i in range(len(serverObj.get('connections', []))): conn = serverObj['connections'][i] isFallback = hasSecureConn and conn['address'][:5] != "https" sources = plexconnection.PlexConnection.SOURCE_BY_VAL[ conn['sources']] connection = plexconnection.PlexConnection(sources, conn['address'], conn['isLocal'], conn['token'], isFallback) # Keep the secure connection on top if connection.isSecure: server.connections.insert(0, connection) else: server.connections.append(connection) if conn.get('active'): server.activeConnection = connection return server