diff --git a/common.py b/common.py index a95cf6b..fbecffc 100644 --- a/common.py +++ b/common.py @@ -26,16 +26,19 @@ if not config.has_section('AUTOBACKUP'): logging.info('Creating configuration file %s', CONFIG_FILE) config.add_section('AUTOBACKUP') + if not config.has_option('AUTOBACKUP', 'backup_dir'): config.set('AUTOBACKUP', 'backup_dir', os.path.expanduser('~/PCAutoBackup')) + if not config.has_option('AUTOBACKUP', 'default_interface'): try: config.set('AUTOBACKUP', 'default_interface', socket.gethostbyname(socket.gethostname())) except socket.error: logging.error('Unable to determine IP address. Please set manually!') config.set('AUTOBACKUP', 'default_interface', '127.0.0.1') - config.set('AUTOBACKUP', 'server_name', - '[%s]AutoBackup' % socket.gethostname().split('.')[0]) + if not config.has_option('AUTOBACKUP', 'server_name'): + config.set('AUTOBACKUP', 'server_name', '[PC]AutoBackup') + if not config.has_option('AUTOBACKUP', 'server_name'): config.set('AUTOBACKUP', 'uuid', uuid.uuid4()) with open(CONFIG_FILE, 'wb') as config_file: config.write(config_file) diff --git a/ssdp.py b/ssdp.py index c18edc8..ff10927 100644 --- a/ssdp.py +++ b/ssdp.py @@ -14,8 +14,8 @@ import common -pattern = r'^M-SEARCH.*HOST: (.*):(\d+).*urn:schemas-upnp-org:device:(\w+):1.*' -MSEARCH = re.compile(pattern, re.DOTALL) +MSEARCH = re.compile(r'^M-SEARCH \* HTTP/1.1', re.DOTALL) +MSEARCH_DATA = re.compile(r'^([^:]+):\s+(.*)') SSDP_RESPONSE = ('HTTP/1.1 200 OK\r\n' 'CACHE-CONTROL: max-age = 1800\r\n' @@ -41,12 +41,34 @@ if m: # TODO(jrebeiro): Verify that MediaServer is the only discovery request # PCAutoBackup responds to. - self.logger.debug('Received SSDP M-SEARCH for %s from %s', m.group(3), - address[0]) - if m.group(3) == 'MediaServer': + msearch_data = self.ParseSSDPDiscovery(datagram) + if msearch_data.get('discovery_type'): + self.logger.debug('Received SSDP M-SEARCH for %s from %s', + msearch_data.get('discovery_type'), address[0]) + else: + self.logger.debug('Received SSDP M-SEARCH from %s', address[0]) + + if msearch_data.get('discovery_type') == 'MediaServer': self.logger.info('Sending SSDP response to %s', address[0]) self.SendSSDPResponse(address) + def ParseSSDPDiscovery(self, datagram): + parsed_data = {} + + for line in datagram.splitlines(): + if line.startswith('M-SEARCH'): + continue + + m = MSEARCH_DATA.match(line) + if m: + parsed_data[m.group(1)] = m.group(2) + + # ST: urn:schemas-upnp-org:device:MediaServer:1 + if m.group(1) == 'ST': + parsed_data['discovery_type'] = m.group(2).split(':')[3] + + return parsed_data + def SendSSDPResponse(self, address): """Send a response to an SSDP MediaServer discovery request.