def _Edit( self ): do_refresh = False selected_mappings = self._mappings_list_ctrl.GetData( only_selected = True ) for selected_mapping in selected_mappings: ( description, internal_ip, internal_port, external_port, protocol, duration ) = selected_mapping with ClientGUIDialogs.DialogInputUPnPMapping( self, external_port, protocol, internal_port, description, duration ) as dlg: if dlg.exec() == QW.QDialog.Accepted: ( external_port, protocol, internal_port, description, duration ) = dlg.GetInfo() HydrusNATPunch.RemoveUPnPMapping( external_port, protocol ) internal_client = HydrusNATPunch.GetLocalIP() HydrusNATPunch.AddUPnPMapping( internal_client, internal_port, external_port, protocol, description, duration = duration ) do_refresh = True else: break if do_refresh: self._RefreshMappings()
def InitView(self): job = self.CallRepeating(60.0, 300.0, self.MaintainDB, maintenance_mode=HC.MAINTENANCE_IDLE) job.WakeOnPubSub('wake_idle_workers') job.ShouldDelayOnWakeup(True) self._daemon_jobs['maintain_db'] = job job = self.CallRepeating(10.0, 120.0, self.SleepCheck) self._daemon_jobs['sleep_check'] = job job = self.CallRepeating(10.0, 60.0, self.MaintainMemoryFast) self._daemon_jobs['maintain_memory_fast'] = job job = self.CallRepeating(10.0, 300.0, self.MaintainMemorySlow) self._daemon_jobs['maintain_memory_slow'] = job upnp_services = self._GetUPnPServices() self.services_upnp_manager = HydrusNATPunch.ServicesUPnPManager( upnp_services) job = self.CallRepeating(10.0, 43200.0, self.services_upnp_manager.RefreshUPnP) self._daemon_jobs['services_upnp'] = job
def _Add(self): do_refresh = False external_port = HC.DEFAULT_SERVICE_PORT protocol = 'TCP' internal_port = HC.DEFAULT_SERVICE_PORT description = 'hydrus service' duration = 0 with ClientGUIDialogs.DialogInputUPnPMapping(self, external_port, protocol, internal_port, description, duration) as dlg: if dlg.exec() == QW.QDialog.Accepted: (external_port, protocol, internal_port, description, duration) = dlg.GetInfo() for (existing_description, existing_internal_ip, existing_internal_port, existing_external_port, existing_protocol, existing_lease) in self._mappings: if external_port == existing_external_port and protocol == existing_protocol: QW.QMessageBox.critical( self, 'Error', 'That external port already exists!') return internal_client = HydrusNATPunch.GetLocalIP() HydrusNATPunch.AddUPnPMapping(internal_client, internal_port, external_port, protocol, description, duration=duration) do_refresh = True if do_refresh: self._RefreshMappings()
def work_callable(): if remove_existing: HydrusNATPunch.RemoveUPnPMapping( external_port, protocol) internal_client = HydrusNATPunch.GetLocalIP() HydrusNATPunch.AddUPnPMapping(internal_client, internal_port, external_port, protocol, description, duration=duration) return True
def test_upnp( self ): internal_client = HydrusNATPunch.GetLocalIP() internal_port = random.randint( 1000, 1500 ) external_port = random.randint( 1000, 1500 ) description_tcp = 'hydrus test tcp' description_udp = 'hydrus test udp' HydrusNATPunch.AddUPnPMapping( internal_client, internal_port, external_port, 'TCP', description_tcp ) HydrusNATPunch.AddUPnPMapping( internal_client, internal_port, external_port, 'UDP', description_udp ) mappings = HydrusNATPunch.GetUPnPMappings() mappings_without_lease_times = [ mapping[:-1] for mapping in mappings ] self.assertIn( ( description_tcp, internal_client, internal_port, external_port, 'TCP' ), mappings_without_lease_times ) self.assertIn( ( description_udp, internal_client, internal_port, external_port, 'UDP' ), mappings_without_lease_times ) HydrusNATPunch.RemoveUPnPMapping( external_port, 'TCP' ) HydrusNATPunch.RemoveUPnPMapping( external_port, 'UDP' ) mappings = HydrusNATPunch.GetUPnPMappings() mappings_without_lease_times = [ mapping[:-1] for mapping in mappings ] self.assertNotIn( ( description_tcp, internal_client, internal_port, external_port, 'TCP' ), mappings_without_lease_times ) self.assertNotIn( ( description_udp, internal_client, internal_port, external_port, 'UDP' ), mappings_without_lease_times )
def work_callable(): for selected_mapping in selected_mappings: (description, internal_ip, internal_port, external_port, protocol, duration) = selected_mapping HydrusNATPunch.RemoveUPnPMapping(external_port, protocol) return True
def test_upnp_parsing(self): mappings = HydrusNATPunch.GetUPnPMappingsParseResponse(GET_MAPPINGS_OK) expected_mappings = {('Hydrus Client API', '192.168.0.101', 45871, 45871, 'TCP', 0), ('xXxP2P_LiNuX_ISO_420xXx', '192.168.0.101', 44040, 44040, 'UDP', 3600), ('Quake', '192.168.0.102', 980, 1240, 'TCP', 0)} self.assertEqual(expected_mappings, set(mappings)) # HydrusNATPunch.AddUPnPMappingCheckResponse(TEST_ME_IP, 45871, 45871, 'TCP', ADD_STDOUT_OK, '') try: HydrusNATPunch.AddUPnPMappingCheckResponse( TEST_ME_IP, 4000, 45871, 'TCP', ADD_STDOUT_PORT_MAPPED_OTHER_PORT, '') except HydrusExceptions.RouterException as e: self.assertIn( 'The UPnP mapping of 192.168.0.101:4000->external:45871(TCP) could not be added because that external port is already forwarded to another port on this computer!', str(e)) try: HydrusNATPunch.AddUPnPMappingCheckResponse( TEST_ME_IP, 45871, 45871, 'TCP', ADD_STDOUT_PORT_MAPPED_OTHER_COMP, '') except HydrusExceptions.RouterException as e: self.assertIn( 'The UPnP mapping of 192.168.0.101:45871->external:45871(TCP) could not be added because that external port is already mapped to another computer on this network!', str(e))
def THREADdo_it(): try: external_ip = HydrusNATPunch.GetExternalIP() external_ip_text = 'External IP: {}'.format(external_ip) except Exception as e: external_ip_text = 'Error finding external IP: ' + str(e) QP.CallAfter(qt_code, external_ip_text)
def work_callable(): try: mappings = HydrusNATPunch.GetUPnPMappings() except Exception as e: HydrusData.ShowException(e) return e return mappings
def work_callable(): try: external_ip = HydrusNATPunch.GetExternalIP() external_ip_text = 'External IP: {}'.format(external_ip) except Exception as e: external_ip_text = 'Error finding external IP: ' + str(e) return external_ip_text
def THREADdo_it(): try: mappings = HydrusNATPunch.GetUPnPMappings() except Exception as e: HydrusData.ShowException( e ) QP.CallAfter( QW.QMessageBox.critical, self, 'Error', 'Could not load mappings:'+os.linesep*2+str(e) ) return QP.CallAfter( qt_code, mappings )
def _Remove( self ): do_refresh = False selected_mappings = self._mappings_list_ctrl.GetData( only_selected = True ) for selected_mapping in selected_mappings: ( description, internal_ip, internal_port, external_port, protocol, duration ) = selected_mapping HydrusNATPunch.RemoveUPnPMapping( external_port, protocol ) do_refresh = True if do_refresh: self._RefreshMappings()