def EditMappings(self): do_refresh = False for index in self._mappings_list_ctrl.GetAllSelected(): (description, internal_ip, internal_port, external_ip, external_port, protocol, duration) = self._mappings_list_ctrl.GetClientData(index) with ClientGUIDialogs.DialogInputUPnPMapping( self, external_port, protocol, internal_port, description, duration) as dlg: if dlg.ShowModal() == wx.ID_OK: (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 if do_refresh: self._RefreshMappings()
def DAEMONUPnP(controller): try: local_ip = HydrusNATPunch.GetLocalIP() current_mappings = HydrusNATPunch.GetUPnPMappings() our_mappings = {(internal_client, internal_port): external_port for (description, internal_client, internal_port, external_ip_address, external_port, protocol, enabled) in current_mappings} except: return # This IGD probably doesn't support UPnP, so don't spam the user with errors they can't fix! services = HG.server_controller.GetServices() for service in services: internal_port = service.GetPort() upnp_port = service.GetUPnPPort() if (local_ip, internal_port) in our_mappings: current_external_port = our_mappings[(local_ip, internal_port)] if upnp_port is None or upnp_port != current_external_port: HydrusNATPunch.RemoveUPnPMapping(current_external_port, 'TCP') for service in services: internal_port = service.GetPort() upnp_port = service.GetUPnPPort() if upnp_port is not None and (local_ip, internal_port) not in our_mappings: external_port = upnp_port protocol = 'TCP' service_type = service.GetServiceType() description = HC.service_string_lookup[ service_type] + ' at ' + local_ip + ':' + str(internal_port) duration = 3600 HydrusNATPunch.AddUPnPMapping(local_ip, internal_port, external_port, protocol, description, duration=duration)
def DAEMONUPnP(controller): try: local_ip = HydrusNATPunch.GetLocalIP() current_mappings = HydrusNATPunch.GetUPnPMappings() our_mappings = {(internal_client, internal_port): external_port for (description, internal_client, internal_port, external_ip_address, external_port, protocol, enabled) in current_mappings} except: return # This IGD probably doesn't support UPnP, so don't spam the user with errors they can't fix! services_info = controller.Read('services_info') for (service_key, service_type, options) in services_info: internal_port = options['port'] upnp = options['upnp'] if (local_ip, internal_port) in our_mappings: current_external_port = our_mappings[(local_ip, internal_port)] if current_external_port != upnp: HydrusNATPunch.RemoveUPnPMapping(current_external_port, 'TCP') for (service_key, service_type, options) in services_info: internal_port = options['port'] upnp = options['upnp'] if upnp is not None and (local_ip, internal_port) not in our_mappings: external_port = upnp protocol = 'TCP' description = HC.service_string_lookup[ service_type] + ' at ' + local_ip + ':' + str(internal_port) duration = 3600 HydrusNATPunch.AddUPnPMapping(local_ip, internal_port, external_port, protocol, description, duration=duration)
def THREADdo_it(): def wx_code(mappings): if not self: return self._mappings = mappings for mapping in self._mappings: self._mappings_list_ctrl.Append(mapping, mapping) self._status_st.SetLabelText('') try: mappings = HydrusNATPunch.GetUPnPMappings() except Exception as e: HydrusData.ShowException(e) wx.CallAfter( wx.MessageBox, 'Could not load mappings:' + os.linesep * 2 + str(e)) return wx.CallAfter(wx_code, mappings)
def EventAddCustomMapping(self, event): 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.ShowModal() == wx.ID_OK: (external_port, protocol, internal_port, description, duration) = dlg.GetInfo() for (existing_description, existing_internal_ip, existing_internal_port, existing_external_ip, existing_external_port, existing_protocol, existing_lease) in self._mappings: if external_port == existing_external_port and protocol == existing_protocol: wx.MessageBox('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 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() external_ip_address = mappings[0][3] mappings_without_lease_times = [ mapping[:-1] for mapping in mappings ] self.assertIn( ( description_tcp, internal_client, internal_port, external_ip_address, external_port, 'TCP' ), mappings_without_lease_times ) self.assertIn( ( description_udp, internal_client, internal_port, external_ip_address, 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_ip_address, external_port, 'TCP' ), mappings_without_lease_times ) self.assertNotIn( ( description_udp, internal_client, internal_port, external_ip_address, external_port, 'UDP' ), mappings_without_lease_times )
def RemoveMappings(self): do_refresh = False for index in self._mappings_list_ctrl.GetAllSelected(): (description, internal_ip, internal_port, external_ip, external_port, protocol, duration) = self._mappings_list_ctrl.GetClientData(index) HydrusNATPunch.RemoveUPnPMapping(external_port, protocol) do_refresh = True if do_refresh: self._RefreshMappings()
def EventCopyExternalShareURL(self, event): shares = self._booru_shares.GetSelectedClientData() if len(shares) > 0: (name, text, timeout, (num_hashes, hashes, share_key)) = shares[0] info = self._service.GetInfo() external_ip = HydrusNATPunch.GetExternalIP( ) # eventually check for optional host replacement here external_port = info['upnp'] if external_port is None: external_port = info['port'] url = 'http://' + external_ip + ':' + HydrusData.ToUnicode( external_port) + '/gallery?share_key=' + share_key.encode( 'hex') self._controller.pub('clipboard', 'text', url)
def DAEMONUPnP(controller): try: local_ip = HydrusNATPunch.GetLocalIP() current_mappings = HydrusNATPunch.GetUPnPMappings() our_mappings = {(internal_client, internal_port): external_port for (description, internal_client, internal_port, external_ip_address, external_port, protocol, enabled) in current_mappings} except: return # This IGD probably doesn't support UPnP, so don't spam the user with errors they can't fix! services = controller.services_manager.GetServices((HC.LOCAL_BOORU, )) for service in services: internal_port = service.GetPort() if (local_ip, internal_port) in our_mappings: current_external_port = our_mappings[(local_ip, internal_port)] upnp_port = service.GetUPnPPort() if upnp_port is None or current_external_port != upnp_port: HydrusNATPunch.RemoveUPnPMapping(current_external_port, 'TCP') for service in services: internal_port = service.GetPort() upnp_port = service.GetUPnPPort() if upnp_port is not None: if (local_ip, internal_port) not in our_mappings: service_type = service.GetServiceType() protocol = 'TCP' description = HC.service_string_lookup[ service_type] + ' at ' + local_ip + ':' + str( internal_port) duration = 3600 try: HydrusNATPunch.AddUPnPMapping(local_ip, internal_port, upnp_port, protocol, description, duration=duration) except HydrusExceptions.FirewallException: HydrusData.Print( 'The UPnP Daemon tried to add ' + local_ip + ':' + internal_port + '->external:' + upnp_port + ' but it failed due to router error. Please try it manually to get a full log of what happened.' ) return except: raise
def DAEMONUPnP(controller): try: local_ip = HydrusNATPunch.GetLocalIP() current_mappings = HydrusNATPunch.GetUPnPMappings() our_mappings = {(internal_client, internal_port): external_port for (description, internal_client, internal_port, external_ip_address, external_port, protocol, enabled) in current_mappings} except: return # This IGD probably doesn't support UPnP, so don't spam the user with errors they can't fix! services = controller.GetServicesManager().GetServices((HC.LOCAL_BOORU, )) for service in services: info = service.GetInfo() internal_port = info['port'] upnp = info['upnp'] if (local_ip, internal_port) in our_mappings: current_external_port = our_mappings[(local_ip, internal_port)] if upnp is None or current_external_port != upnp: HydrusNATPunch.RemoveUPnPMapping(current_external_port, 'TCP') for service in services: info = service.GetInfo() internal_port = info['port'] upnp = info['upnp'] if upnp is not None: if (local_ip, internal_port) not in our_mappings: service_type = service.GetServiceType() external_port = upnp protocol = 'TCP' description = HC.service_string_lookup[ service_type] + ' at ' + local_ip + ':' + str( internal_port) duration = 3600 HydrusNATPunch.AddUPnPMapping(local_ip, internal_port, external_port, protocol, description, duration=duration)