private void updatePeer( final Peer peer, final InetSocketAddress isa, final Type type, final LanternTrafficCounter trafficCounter) { // We can get multiple notifications for the same peer, in which case // they'll already have a counter. if (peer.getTrafficCounter() == null) { log.debug("Setting traffic counter..."); peer.setTrafficCounter(trafficCounter); } else { log.debug("Peer already has traffic counter..."); } final String address = isa.getAddress().getHostAddress(); if (StringUtils.isBlank(peer.getIp())) { peer.setIp(address); } if (peer.getPort() == 0) { peer.setPort(isa.getPort()); } if (peer.getRosterEntry() == null) { log.debug("Setting roster entry"); final URI uri = LanternUtils.newURI(peer.getPeerid()); peer.setRosterEntry(rosterEntry(uri)); } peer.setType(type.toString()); updateGeoData(peer, isa.getAddress()); // Note we don't sync peers with the frontend here because the timer // will do it for us }
private void updateGeoData(final Peer peer, final String address) { if (peer.hasGeoData()) { log.debug("Peer already had geo data: {}", peer); return; } final GeoData geo = geoIpLookupService.getGeoData(address); peer.setCountry(geo.getCountrycode()); peer.setLat(geo.getLatitude()); peer.setLon(geo.getLongitude()); }
@Subscribe public void onIncomingPeerEvent(final IncomingPeerEvent event) { // First we have to figure out which peer this is an incoming socket // for base on the certificate. final X509Certificate cert = event.getCert(); final Channel channel = event.getChannel(); final LanternTrafficCounter counter = event.getTrafficCounter(); try { final String base64Cert = Base64.encodeBase64String(cert.getEncoded()); final Peer peer = certsToPeers.get(base64Cert); if (peer == null) { log.error("No matching peer for cert: {} in {}", base64Cert, certsToPeers); return; } log.debug("Found peer by certificate!!!"); peer.setMode(Mode.get); updatePeer(peer, (InetSocketAddress) channel.getRemoteAddress(), Type.pc, counter); } catch (final CertificateEncodingException e) { log.error("Could not encode certificate?", e); } }
@Subscribe public void onKscopeAd(final KscopeAdEvent event) { final LanternKscopeAdvertisement ad = event.getAd(); // It is possible and even likely we already know about this peer // through some other means, in which case we have to update the data // about that peer as necessary. log.debug("Adding peer through kscope ad..."); final String jid = ad.getJid(); final URI uri = LanternUtils.newURI(jid); final Peer existing = this.model.getPeerCollector().getPeer(uri); final LanternRosterEntry entry = this.roster.getRosterEntry(jid); if (existing == null) { // The following can be null. final Peer peer = new Peer( uri, "", ad.hasMappedEndpoint(), 0, 0, Type.pc, ad.getAddress(), ad.getPort(), Mode.give, false, null, entry); this.model.getPeerCollector().addPeer(uri, peer); updateGeoData(peer, ad.getAddress()); } else { existing.setIp(ad.getAddress()); existing.setPort(ad.getPort()); existing.setMode(Mode.give); existing.setMapped(ad.hasMappedEndpoint()); if (existing.getRosterEntry() == null) { // Ours could be null too, but can't hurt to set. existing.setRosterEntry(entry); } existing.setVersion(ad.getLanternVersion()); updateGeoData(existing, ad.getAddress()); } }