MCPcopy Index your code
hub / github.com/HelloZeroNet/ZeroNet / getPeerLocations

Method getPeerLocations

plugins/Sidebar/SidebarPlugin.py:653–701  ·  view source on GitHub ↗
(self, peers)

Source from the content-addressed store, hash-verified

651 return None
652
653 def getPeerLocations(self, peers):
654 import maxminddb
655
656 db_path = self.getGeoipDb()
657 if not db_path:
658 self.log.debug("Not showing peer locations: no GeoIP database")
659 return False
660
661 self.log.info("Loading GeoIP database from: %s" % db_path)
662 geodb = maxminddb.open_database(db_path)
663
664 peers = list(peers.values())
665 # Place bars
666 peer_locations = []
667 placed = {} # Already placed bars here
668 for peer in peers:
669 # Height of bar
670 if peer.connection and peer.connection.last_ping_delay:
671 ping = round(peer.connection.last_ping_delay * 1000)
672 else:
673 ping = None
674 loc = self.getLoc(geodb, peer.ip)
675
676 if not loc:
677 continue
678 # Create position array
679 lat, lon = loc["lat"], loc["lon"]
680 latlon = "%s,%s" % (lat, lon)
681 if latlon in placed and helper.getIpType(peer.ip) == "ipv4": # Dont place more than 1 bar to same place, fake repos using ip address last two part
682 lat += float(128 - int(peer.ip.split(".")[-2])) / 50
683 lon += float(128 - int(peer.ip.split(".")[-1])) / 50
684 latlon = "%s,%s" % (lat, lon)
685 placed[latlon] = True
686 peer_location = {}
687 peer_location.update(loc)
688 peer_location["lat"] = lat
689 peer_location["lon"] = lon
690 peer_location["ping"] = ping
691
692 peer_locations.append(peer_location)
693
694 # Append myself
695 for ip in self.site.connection_server.ip_external_list:
696 my_loc = self.getLoc(geodb, ip)
697 if my_loc:
698 my_loc["ping"] = 0
699 peer_locations.append(my_loc)
700
701 return peer_locations
702
703 @flag.admin
704 @flag.async_run

Callers 2

actionSidebarGetPeersMethod · 0.95

Calls 4

getGeoipDbMethod · 0.95
getLocMethod · 0.95
valuesMethod · 0.80
updateMethod · 0.45

Tested by

no test coverage detected