class SimplePairingManager(AbstractPairingManager): def __init__(self, crypto_provider): self.crypto_provider = crypto_provider self.config_helper = RequiredFeature('config-helper').request() self.logger = RequiredFeature('logger').request() def pair(self, nvhttp, server_info, dialog): self.logger.info('[MoonlightHelper] - Attempting to pair host: ' + self.config_helper.host_ip) pairing_proc = subprocess.Popen( ['stdbuf', '-oL', self.config_helper.get_binary(), 'pair', self.config_helper.host_ip], stdout=subprocess.PIPE) lines_iterator = iter(pairing_proc.stdout.readline, b"") pairing_thread = threading.Thread(target=self.loop_lines, args=(self.logger, lines_iterator, dialog)) pairing_thread.start() while True: xbmc.sleep(1000) if not pairing_thread.isAlive(): break new_server_info = nvhttp.get_server_info() if self.get_pair_state(nvhttp, new_server_info) == self.STATE_PAIRED: return self.STATE_PAIRED else: return self.STATE_FAILED def loop_lines(self, logger, iterator, dialog): pin_regex = r'^Please enter the following PIN on the target PC: (\d{4})' for line in iterator: match = re.match(pin_regex, line) if match: self.update_dialog(match.group(1), dialog) logger.info(line)
class SimplePairingManager(AbstractPairingManager): def __init__(self, crypto_provider): self.crypto_provider = crypto_provider self.config_helper = RequiredFeature('config-helper').request() self.logger = RequiredFeature('logger').request() def pair(self, nvhttp, server_info, dialog): self.logger.info('[MoonlightHelper] - Attempting to pair host: ' + self.config_helper.host_ip) pairing_proc = subprocess.Popen( ["moonlight", "pair"], cwd="/storage/moonlight", env={'LD_LIBRARY_PATH': '/storage/moonlight'}, shell=False, stdout=subprocess.PIPE, preexec_fn=os.setsid) lines_iterator = iter(pairing_proc.stdout.readline, b"") pairing_thread = threading.Thread(target=self.loop_lines, args=(self.logger, lines_iterator, dialog)) pairing_thread.start() while True: xbmc.sleep(1000) sys.stdout.flush() if not pairing_thread.isAlive(): break new_server_info = nvhttp.get_server_info() if self.get_pair_state(nvhttp, new_server_info) == self.STATE_PAIRED: return self.STATE_PAIRED else: return self.STATE_FAILED main = "pkill -x moonlight" print(os.system(main)) def loop_lines(self, logger, iterator, dialog): pin_regex = r'^Please enter the following PIN on the target PC: (\d{4})' for line in iterator: match = re.match(pin_regex, line) if match: self.update_dialog(match.group(1), dialog) logger.info(line)
def re_encode_string(self, xml_string): logger = RequiredFeature('logger').request() regex = re.compile('UTF-\d{1,2}') specified_encoding = regex.search(xml_string) logger.info("Trying to decode as: %s" % 'ASCII') try: xml_string = xml_string.decode(encoding='ascii') except UnicodeDecodeError as e: logger.info("Decoding as %s failed, trying as %s" % ('ASCII', 'UTF-8')) try: xml_string = xml_string.decode(encoding='UTF-8') except UnicodeDecodeError as e: logger.info("Decoding as %s failed, trying as %s" % ('UTF-8', 'UTF-16')) try: xml_string = xml_string.decode(encoding='UTF-16') except UnicodeDecodeError as e: logger.error( "Decoding as UTF-16 failed, this was the last attempt. Offending string follows ..." ) logger.error(xml_string) raise ValueError("String Decode Failed") if specified_encoding is not None: try: logger.info("Trying to encode as specified in XML: %s" % specified_encoding.group(0)) xml_string = xml_string.encode( encoding=specified_encoding.group(0)) except UnicodeEncodeError as e: new_encode_setting = 'UTF-16' if specified_encoding.group( 0) == 'UTF-8' else 'UTF-8' logger.info("Encoding as %s failed, trying as %s" % (specified_encoding.group(0), new_encode_setting)) try: xml_string = xml_string.encode(encoding=new_encode_setting) except UnicodeEncodeError as e: logger.error( "Encoding as %s failed, this was the last attempt. Offending string follows ..." % new_encode_setting) logger.error(xml_string) raise ValueError("String Encode Failed") return xml_string else: logger.info("RegExp couldn't find a match in the XML string ...") try: logger.info("Trying to encode as: UTF-8") xml_string = xml_string.encode(encoding='UTF-8') except UnicodeEncodeError as e: logger.info("Encoding as UTF-8 failed, trying as UTF-16") try: xml_string = xml_string.encode(encoding='UTF-16') except UnicodeEncodeError as e: logger.error( "Encoding as UTF-16 failed, this was the last attempt. Offending string follows ..." ) logger.error(xml_string) raise ValueError("String Encode Failed") return xml_string
def re_encode_string(self, xml_string): logger = RequiredFeature('logger').request() regex = re.compile('UTF-\d{1,2}') specified_encoding = regex.search(xml_string) logger.info("Trying to decode as: %s" % 'ASCII') try: xml_string = xml_string.decode(encoding='ascii') except UnicodeDecodeError as e: logger.info("Decoding as %s failed, trying as %s" % ('ASCII', 'UTF-8')) try: xml_string = xml_string.decode(encoding='UTF-8') except UnicodeDecodeError as e: logger.info("Decoding as %s failed, trying as %s" % ('UTF-8', 'UTF-16')) try: xml_string = xml_string.decode(encoding='UTF-16') except UnicodeDecodeError as e: logger.error("Decoding as UTF-16 failed, this was the last attempt. Offending string follows ...") logger.error(xml_string) raise ValueError("String Decode Failed") if specified_encoding is not None: try: logger.info("Trying to encode as specified in XML: %s" % specified_encoding.group(0)) xml_string = xml_string.encode(encoding=specified_encoding.group(0)) except UnicodeEncodeError as e: new_encode_setting = 'UTF-16' if specified_encoding.group(0) == 'UTF-8' else 'UTF-8' logger.info("Encoding as %s failed, trying as %s" % (specified_encoding.group(0), new_encode_setting)) try: xml_string = xml_string.encode(encoding=new_encode_setting) except UnicodeEncodeError as e: logger.error( "Encoding as %s failed, this was the last attempt. Offending string follows ..." % new_encode_setting) logger.error(xml_string) raise ValueError("String Encode Failed") return xml_string else: logger.info("RegExp couldn't find a match in the XML string ...") try: logger.info("Trying to encode as: UTF-8") xml_string = xml_string.encode(encoding='UTF-8') except UnicodeEncodeError as e: logger.info("Encoding as UTF-8 failed, trying as UTF-16") try: xml_string = xml_string.encode(encoding='UTF-16') except UnicodeEncodeError as e: logger.error("Encoding as UTF-16 failed, this was the last attempt. Offending string follows ...") logger.error(xml_string) raise ValueError("String Encode Failed") return xml_string