| 842 | |
| 843 | # Return: Probably peers verified to be connectable recently |
| 844 | def getConnectablePeers(self, need_num=5, ignore=[], allow_private=True): |
| 845 | peers = list(self.peers.values()) |
| 846 | found = [] |
| 847 | for peer in peers: |
| 848 | if peer.key.endswith(":0"): |
| 849 | continue # Not connectable |
| 850 | if not peer.connection: |
| 851 | continue # No connection |
| 852 | if peer.ip.endswith(".onion") and not self.connection_server.tor_manager.enabled: |
| 853 | continue # Onion not supported |
| 854 | if peer.key in ignore: |
| 855 | continue # The requester has this peer |
| 856 | if time.time() - peer.connection.last_recv_time > 60 * 60 * 2: # Last message more than 2 hours ago |
| 857 | peer.connection = None # Cleanup: Dead connection |
| 858 | continue |
| 859 | if not allow_private and helper.isPrivateIp(peer.ip): |
| 860 | continue |
| 861 | found.append(peer) |
| 862 | if len(found) >= need_num: |
| 863 | break # Found requested number of peers |
| 864 | |
| 865 | if len(found) < need_num: # Return not that good peers |
| 866 | found += [ |
| 867 | peer for peer in peers |
| 868 | if not peer.key.endswith(":0") and |
| 869 | peer.key not in ignore and |
| 870 | (allow_private or not helper.isPrivateIp(peer.ip)) |
| 871 | ][0:need_num - len(found)] |
| 872 | |
| 873 | return found |
| 874 | |
| 875 | # Return: Recently found peers |
| 876 | def getRecentPeers(self, need_num): |