2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-15 22:12:52 +11:00
|
|
|
"""
|
|
|
|
PlexGDM.py - Version 0.2
|
|
|
|
|
|
|
|
This class implements the Plex GDM (G'Day Mate) protocol to discover
|
|
|
|
local Plex Media Servers. Also allow client registration into all local
|
|
|
|
media servers.
|
|
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
|
|
|
"""
|
2018-07-13 02:46:02 +10:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2016-09-03 01:20:19 +10:00
|
|
|
import logging
|
2016-01-15 22:12:52 +11:00
|
|
|
import socket
|
|
|
|
import threading
|
|
|
|
import time
|
2016-04-05 18:57:30 +10:00
|
|
|
from xbmc import sleep
|
2016-03-24 02:07:09 +11:00
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
from ..downloadutils import DownloadUtils as DU
|
|
|
|
from .. import utils
|
|
|
|
from .. import variables as v
|
2016-09-03 01:20:19 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2018-06-22 03:24:37 +10:00
|
|
|
log = logging.getLogger('PLEX.plexgdm')
|
2016-09-03 01:20:19 +10:00
|
|
|
|
|
|
|
###############################################################################
|
2016-03-09 03:41:07 +11:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
|
|
|
|
class plexgdm:
|
|
|
|
|
2016-04-05 18:57:30 +10:00
|
|
|
def __init__(self):
|
2016-01-15 22:12:52 +11:00
|
|
|
self.discover_message = 'M-SEARCH * HTTP/1.0'
|
|
|
|
self.client_header = '* HTTP/1.0'
|
|
|
|
self.client_data = None
|
2016-04-05 18:57:30 +10:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
self._multicast_address = '239.0.0.250'
|
|
|
|
self.discover_group = (self._multicast_address, 32414)
|
|
|
|
self.client_register_group = (self._multicast_address, 32413)
|
2018-06-22 03:24:37 +10:00
|
|
|
self.client_update_port = int(utils.settings('companionUpdatePort'))
|
2016-01-15 22:12:52 +11:00
|
|
|
|
|
|
|
self.server_list = []
|
|
|
|
self.discovery_interval = 120
|
2016-04-05 18:57:30 +10:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
self._discovery_is_running = False
|
|
|
|
self._registration_is_running = False
|
|
|
|
|
|
|
|
self.client_registered = False
|
2018-06-22 03:24:37 +10:00
|
|
|
self.download = DU().downloadUrl
|
2016-01-15 22:12:52 +11:00
|
|
|
|
2017-12-10 02:30:52 +11:00
|
|
|
def clientDetails(self):
|
2016-04-05 18:57:30 +10:00
|
|
|
self.client_data = (
|
2017-02-20 03:07:42 +11:00
|
|
|
"Content-Type: plex/media-player\n"
|
|
|
|
"Resource-Identifier: %s\n"
|
|
|
|
"Name: %s\n"
|
|
|
|
"Port: %s\n"
|
|
|
|
"Product: %s\n"
|
|
|
|
"Version: %s\n"
|
|
|
|
"Protocol: plex\n"
|
|
|
|
"Protocol-Version: 1\n"
|
2016-04-05 18:57:30 +10:00
|
|
|
"Protocol-Capabilities: timeline,playback,navigation,"
|
2017-02-20 03:07:42 +11:00
|
|
|
"playqueues\n"
|
|
|
|
"Device-Class: HTPC\n"
|
2016-04-05 18:57:30 +10:00
|
|
|
) % (
|
2017-12-10 02:30:52 +11:00
|
|
|
v.PKC_MACHINE_IDENTIFIER,
|
|
|
|
v.DEVICENAME,
|
|
|
|
v.COMPANION_PORT,
|
|
|
|
v.ADDON_NAME,
|
|
|
|
v.ADDON_VERSION
|
2016-04-05 18:57:30 +10:00
|
|
|
)
|
2016-01-15 22:12:52 +11:00
|
|
|
|
|
|
|
def getClientDetails(self):
|
|
|
|
return self.client_data
|
|
|
|
|
2017-02-20 03:07:42 +11:00
|
|
|
def register_as_client(self):
|
|
|
|
"""
|
|
|
|
Registers PKC's Plex Companion to the PMS
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
log.debug("Sending registration data: HELLO %s\n%s"
|
|
|
|
% (self.client_header, self.client_data))
|
|
|
|
self.update_sock.sendto("HELLO %s\n%s"
|
|
|
|
% (self.client_header, self.client_data),
|
|
|
|
self.client_register_group)
|
|
|
|
log.debug('(Re-)registering PKC Plex Companion successful')
|
|
|
|
except:
|
|
|
|
log.error("Unable to send registration message")
|
|
|
|
|
2016-04-05 18:57:30 +10:00
|
|
|
def client_update(self):
|
2017-02-20 03:07:42 +11:00
|
|
|
self.update_sock = socket.socket(socket.AF_INET,
|
|
|
|
socket.SOCK_DGRAM,
|
|
|
|
socket.IPPROTO_UDP)
|
|
|
|
update_sock = self.update_sock
|
2016-04-05 18:57:30 +10:00
|
|
|
|
|
|
|
# Set socket reuse, may not work on all OSs.
|
2016-01-15 22:12:52 +11:00
|
|
|
try:
|
2016-04-05 18:57:30 +10:00
|
|
|
update_sock.setsockopt(socket.SOL_SOCKET,
|
|
|
|
socket.SO_REUSEADDR,
|
|
|
|
1)
|
2016-01-15 22:12:52 +11:00
|
|
|
except:
|
|
|
|
pass
|
2016-04-05 18:57:30 +10:00
|
|
|
|
|
|
|
# Attempt to bind to the socket to recieve and send data. If we cant
|
|
|
|
# do this, then we cannot send registration
|
2016-01-15 22:12:52 +11:00
|
|
|
try:
|
2016-04-05 18:57:30 +10:00
|
|
|
update_sock.bind(('0.0.0.0', self.client_update_port))
|
2016-01-15 22:12:52 +11:00
|
|
|
except:
|
2017-01-24 20:16:32 +11:00
|
|
|
log.error("Unable to bind to port [%s] - Plex Companion will not "
|
|
|
|
"be registered. Change the Plex Companion update port!"
|
|
|
|
% self.client_update_port)
|
2018-06-22 03:24:37 +10:00
|
|
|
if utils.settings('companion_show_gdm_port_warning') == 'true':
|
2018-09-19 00:26:40 +10:00
|
|
|
from .windows import optionsdialog
|
|
|
|
# Plex Companion could not open the GDM port. Please change it
|
|
|
|
# in the PKC settings.
|
|
|
|
if optionsdialog.show(utils.lang(29999),
|
|
|
|
'Port %s\n%s' % (self.client_update_port,
|
|
|
|
utils.lang(39079)),
|
|
|
|
utils.lang(30013), # Never show again
|
|
|
|
utils.lang(186)) == 0:
|
2018-06-22 03:24:37 +10:00
|
|
|
utils.settings('companion_show_gdm_port_warning',
|
|
|
|
value='false')
|
2017-01-24 21:53:19 +11:00
|
|
|
from xbmc import executebuiltin
|
|
|
|
executebuiltin(
|
|
|
|
'Addon.OpenSettings(plugin.video.plexkodiconnect)')
|
2016-04-05 18:57:30 +10:00
|
|
|
return
|
|
|
|
|
|
|
|
update_sock.setsockopt(socket.IPPROTO_IP,
|
|
|
|
socket.IP_MULTICAST_TTL,
|
|
|
|
255)
|
|
|
|
update_sock.setsockopt(socket.IPPROTO_IP,
|
|
|
|
socket.IP_ADD_MEMBERSHIP,
|
|
|
|
socket.inet_aton(
|
|
|
|
self._multicast_address) +
|
|
|
|
socket.inet_aton('0.0.0.0'))
|
2016-01-15 22:12:52 +11:00
|
|
|
update_sock.setblocking(0)
|
2016-04-05 18:57:30 +10:00
|
|
|
|
|
|
|
# Send initial client registration
|
2017-02-20 03:07:42 +11:00
|
|
|
self.register_as_client()
|
2016-04-05 18:57:30 +10:00
|
|
|
|
2016-09-03 01:20:19 +10:00
|
|
|
# Now, listen format client discovery reguests and respond.
|
2016-01-15 22:12:52 +11:00
|
|
|
while self._registration_is_running:
|
|
|
|
try:
|
|
|
|
data, addr = update_sock.recvfrom(1024)
|
2016-09-03 01:20:19 +10:00
|
|
|
log.debug("Recieved UDP packet from [%s] containing [%s]"
|
|
|
|
% (addr, data.strip()))
|
2016-04-05 18:57:30 +10:00
|
|
|
except socket.error:
|
2016-01-15 22:12:52 +11:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if "M-SEARCH * HTTP/1." in data:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.debug("Detected client discovery request from %s. "
|
|
|
|
" Replying" % str(addr))
|
2016-01-15 22:12:52 +11:00
|
|
|
try:
|
2017-02-20 03:07:42 +11:00
|
|
|
update_sock.sendto("HTTP/1.0 200 OK\n%s"
|
2016-04-05 18:57:30 +10:00
|
|
|
% self.client_data,
|
|
|
|
addr)
|
2016-01-15 22:12:52 +11:00
|
|
|
except:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.error("Unable to send client update message")
|
2016-04-05 18:57:30 +10:00
|
|
|
|
2016-09-03 01:20:19 +10:00
|
|
|
log.debug("Sending registration data HTTP/1.0 200 OK")
|
2016-01-15 22:12:52 +11:00
|
|
|
self.client_registered = True
|
2016-04-05 18:57:30 +10:00
|
|
|
sleep(500)
|
2016-09-03 01:20:19 +10:00
|
|
|
log.info("Client Update loop stopped")
|
2016-04-05 18:57:30 +10:00
|
|
|
# When we are finished, then send a final goodbye message to
|
|
|
|
# deregister cleanly.
|
2017-02-20 03:07:42 +11:00
|
|
|
log.debug("Sending registration data: BYE %s\n%s"
|
2016-09-03 01:20:19 +10:00
|
|
|
% (self.client_header, self.client_data))
|
2016-01-15 22:12:52 +11:00
|
|
|
try:
|
2017-02-20 03:07:42 +11:00
|
|
|
update_sock.sendto("BYE %s\n%s"
|
2016-04-05 18:57:30 +10:00
|
|
|
% (self.client_header, self.client_data),
|
|
|
|
self.client_register_group)
|
2016-01-15 22:12:52 +11:00
|
|
|
except:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.error("Unable to send client update message")
|
2016-01-15 22:12:52 +11:00
|
|
|
self.client_registered = False
|
2016-04-05 18:57:30 +10:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def check_client_registration(self):
|
2017-02-20 03:07:42 +11:00
|
|
|
if not self.client_registered:
|
|
|
|
log.debug('Client has not been marked as registered')
|
|
|
|
return False
|
|
|
|
if not self.server_list:
|
|
|
|
log.info("Server list is empty. Unable to check")
|
|
|
|
return False
|
|
|
|
for server in self.server_list:
|
2018-06-22 03:24:37 +10:00
|
|
|
if server['uuid'] == utils.window('plex_machineIdentifier'):
|
2017-02-20 03:07:42 +11:00
|
|
|
media_server = server['server']
|
|
|
|
media_port = server['port']
|
|
|
|
scheme = server['protocol']
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
log.info("Did not find our server!")
|
|
|
|
return False
|
|
|
|
|
|
|
|
log.debug("Checking server [%s] on port [%s]"
|
|
|
|
% (media_server, media_port))
|
|
|
|
xml = self.download(
|
|
|
|
'%s://%s:%s/clients' % (scheme, media_server, media_port))
|
|
|
|
try:
|
|
|
|
xml[0].attrib
|
|
|
|
except (TypeError, IndexError, AttributeError):
|
|
|
|
log.error('Could not download clients for %s' % media_server)
|
|
|
|
return False
|
|
|
|
registered = False
|
|
|
|
for client in xml:
|
|
|
|
if (client.attrib.get('machineIdentifier') ==
|
2017-12-10 02:30:52 +11:00
|
|
|
v.PKC_MACHINE_IDENTIFIER):
|
2017-02-20 03:07:42 +11:00
|
|
|
registered = True
|
|
|
|
if registered:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
log.info("Client registration not found. "
|
|
|
|
"Client data is: %s" % xml)
|
2016-01-15 22:12:52 +11:00
|
|
|
return False
|
2016-04-05 18:57:30 +10:00
|
|
|
|
|
|
|
def getServerList(self):
|
2016-01-15 22:12:52 +11:00
|
|
|
return self.server_list
|
2016-04-05 18:57:30 +10:00
|
|
|
|
2016-01-15 22:12:52 +11:00
|
|
|
def discover(self):
|
2018-06-22 03:24:37 +10:00
|
|
|
currServer = utils.window('pms_server')
|
2016-06-03 05:12:56 +10:00
|
|
|
if not currServer:
|
|
|
|
return
|
|
|
|
currServerProt, currServerIP, currServerPort = \
|
|
|
|
currServer.split(':')
|
|
|
|
currServerIP = currServerIP.replace('/', '')
|
|
|
|
# Currently active server was not discovered via GDM; ADD
|
|
|
|
self.server_list = [{
|
|
|
|
'port': currServerPort,
|
|
|
|
'protocol': currServerProt,
|
|
|
|
'class': None,
|
|
|
|
'content-type': 'plex/media-server',
|
|
|
|
'discovery': 'auto',
|
|
|
|
'master': 1,
|
|
|
|
'owned': '1',
|
|
|
|
'role': 'master',
|
|
|
|
'server': currServerIP,
|
2018-06-22 03:24:37 +10:00
|
|
|
'serverName': utils.window('plex_servername'),
|
2016-06-03 05:12:56 +10:00
|
|
|
'updated': int(time.time()),
|
2018-06-22 03:24:37 +10:00
|
|
|
'uuid': utils.window('plex_machineIdentifier'),
|
2016-06-03 05:12:56 +10:00
|
|
|
'version': 'irrelevant'
|
|
|
|
}]
|
2016-01-15 22:12:52 +11:00
|
|
|
|
|
|
|
def setInterval(self, interval):
|
|
|
|
self.discovery_interval = interval
|
|
|
|
|
|
|
|
def stop_all(self):
|
|
|
|
self.stop_discovery()
|
|
|
|
self.stop_registration()
|
|
|
|
|
|
|
|
def stop_discovery(self):
|
|
|
|
if self._discovery_is_running:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.info("Discovery shutting down")
|
2016-01-15 22:12:52 +11:00
|
|
|
self._discovery_is_running = False
|
|
|
|
self.discover_t.join()
|
|
|
|
del self.discover_t
|
|
|
|
else:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.info("Discovery not running")
|
2016-01-15 22:12:52 +11:00
|
|
|
|
|
|
|
def stop_registration(self):
|
|
|
|
if self._registration_is_running:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.info("Registration shutting down")
|
2016-01-15 22:12:52 +11:00
|
|
|
self._registration_is_running = False
|
|
|
|
self.register_t.join()
|
|
|
|
del self.register_t
|
|
|
|
else:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.info("Registration not running")
|
2016-01-15 22:12:52 +11:00
|
|
|
|
|
|
|
def run_discovery_loop(self):
|
2016-04-05 18:57:30 +10:00
|
|
|
# Run initial discovery
|
2016-01-15 22:12:52 +11:00
|
|
|
self.discover()
|
|
|
|
|
2016-04-05 18:57:30 +10:00
|
|
|
discovery_count = 0
|
2016-01-15 22:12:52 +11:00
|
|
|
while self._discovery_is_running:
|
2016-04-05 18:57:30 +10:00
|
|
|
discovery_count += 1
|
2016-01-15 22:12:52 +11:00
|
|
|
if discovery_count > self.discovery_interval:
|
|
|
|
self.discover()
|
2016-04-05 18:57:30 +10:00
|
|
|
discovery_count = 0
|
|
|
|
sleep(500)
|
2016-01-15 22:12:52 +11:00
|
|
|
|
2016-04-05 18:57:30 +10:00
|
|
|
def start_discovery(self, daemon=False):
|
2016-01-15 22:12:52 +11:00
|
|
|
if not self._discovery_is_running:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.info("Discovery starting up")
|
2016-01-15 22:12:52 +11:00
|
|
|
self._discovery_is_running = True
|
|
|
|
self.discover_t = threading.Thread(target=self.run_discovery_loop)
|
|
|
|
self.discover_t.setDaemon(daemon)
|
|
|
|
self.discover_t.start()
|
|
|
|
else:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.info("Discovery already running")
|
2016-04-05 18:57:30 +10:00
|
|
|
|
|
|
|
def start_registration(self, daemon=False):
|
2016-01-15 22:12:52 +11:00
|
|
|
if not self._registration_is_running:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.info("Registration starting up")
|
2016-01-15 22:12:52 +11:00
|
|
|
self._registration_is_running = True
|
|
|
|
self.register_t = threading.Thread(target=self.client_update)
|
|
|
|
self.register_t.setDaemon(daemon)
|
|
|
|
self.register_t.start()
|
|
|
|
else:
|
2016-09-03 01:20:19 +10:00
|
|
|
log.info("Registration already running")
|
2016-04-05 18:57:30 +10:00
|
|
|
|
|
|
|
def start_all(self, daemon=False):
|
2016-01-15 22:12:52 +11:00
|
|
|
self.start_discovery(daemon)
|
2018-06-22 03:24:37 +10:00
|
|
|
if utils.settings('plexCompanion') == 'true':
|
2016-04-27 01:15:05 +10:00
|
|
|
self.start_registration(daemon)
|