Changeset 8332385 for apps


Ignore:
Timestamp:
Oct 9, 2018 4:37:29 PM (21 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
06914f9
Parents:
19b1b3c
Message:

Sybil: More prep and refactor for background analysis
Put dest name in too-close reasons

File:
1 edited

Legend:

Unmodified
Added
Removed
  • apps/routerconsole/java/src/net/i2p/router/web/helpers/SybilRenderer.java

    r19b1b3c r8332385  
    106106     *  A total score and a List of reason Strings
    107107     */
    108     private static class Points implements Comparable<Points> {
     108    public static class Points implements Comparable<Points> {
    109109         private double points;
    110110         private final List<String> reasons;
     
    157157    }
    158158
     159    /**
     160     *  Merge points1 into points2.
     161     *  points1 is unmodified.
     162     */
     163    private void mergePoints(Map<Hash, Points> points1, Map<Hash, Points> points2) {
     164        for (Map.Entry<Hash, Points> e : points1.entrySet()) {
     165             Hash h = e.getKey();
     166             Points p1 = e.getValue();
     167             Points p2 = points2.get(h);
     168             if (p2 != null) {
     169                 p2.points += p1.points;
     170                 p2.reasons.addAll(p1.reasons);
     171             } else {
     172                 points2.put(h, p1);
     173             }
     174        }
     175    }
     176
    159177    private void addPoints(Map<Hash, Points> points, Hash h, double d, String reason) {
    160178        String rsn = "<b>" + fmt.format(d) + ":</b> " + reason;
     
    169187
    170188    /**
    171      *  The whole thing
    172      *
    173      *  @param routerPrefix ignored
    174      */
    175     private void renderRouterInfoHTML(Writer out, String routerPrefix) throws IOException {
     189     *  All the floodfills, not including us
     190     *  @since 0.9.38 split out from renderRouterInfoHTML
     191     */
     192    private List<RouterInfo> getFloodfills(Hash us) {
    176193        Set<Hash> ffs = _context.peerManager().getPeersByCapability('f');
    177194        List<RouterInfo> ris = new ArrayList<RouterInfo>(ffs.size());
    178         Hash us = _context.routerHash();
    179         Hash ourRKey = _context.router().getRouterInfo().getRoutingKey();
    180195        for (Hash ff : ffs) {
    181196             if (ff.equals(us))
     
    185200                 ris.add(ri);
    186201        }
     202        return ris;
     203    }
     204
     205    private double getAvgMinDist(List<RouterInfo> ris) {
     206        double tot = 0;
     207        int count = 200;
     208        byte[] b = new byte[32];
     209        for (int i = 0; i < count; i++) {
     210            _context.random().nextBytes(b);
     211            Hash h = new Hash(b);
     212            double d = closestDistance(h, ris);
     213            tot += d;
     214        }
     215        double avgMinDist = tot / count;
     216        return avgMinDist;
     217    }
     218
     219    /**
     220     *  The whole thing
     221     *
     222     *  @param routerPrefix ignored
     223     */
     224    private void renderRouterInfoHTML(Writer out, String routerPrefix) throws IOException {
     225        Hash us = _context.routerHash();
     226        Hash ourRKey = _context.router().getRouterInfo().getRoutingKey();
     227        List<RouterInfo> ris = getFloodfills(us);
    187228        if (ris.isEmpty()) {
    188229            out.write("<h3 class=\"sybils\">No known floodfills</h3>");
     
    211252        buf.append("<h3 id=\"known\" class=\"sybils\">Known Floodfills: ").append(ris.size()).append("</h3>");
    212253
    213         double tot = 0;
    214         int count = 200;
    215         byte[] b = new byte[32];
    216         for (int i = 0; i < count; i++) {
    217             _context.random().nextBytes(b);
    218             Hash h = new Hash(b);
    219             double d = closestDistance(h, ris);
    220             tot += d;
    221         }
    222         double avgMinDist = tot / count;
     254        double avgMinDist = getAvgMinDist(ris);
    223255        buf.append("<div id=\"sybils_summary\">\n" +
    224256                   "<b>Average closest floodfill distance:</b> ").append(fmt.format(avgMinDist)).append("<br>\n" +
     
    244276        buf.append("<h3 id=\"ritoday\" class=\"sybils\">Closest Floodfills to Our Routing Key (Where we Store our RI)</h3>");
    245277        buf.append("<p class=\"sybil_info\"><a href=\"/netdb?caps=f&amp;sybil\">See all</a></p>");
    246         renderRouterInfoHTML(out, buf, ourRKey, avgMinDist, ris, points);
     278        renderRouterInfoHTML(out, buf, ourRKey, "our rkey", avgMinDist, ris, points);
    247279        RouterKeyGenerator rkgen = _context.routerKeyGenerator();
    248280        Hash nkey = rkgen.getNextRoutingKey(us);
    249281        buf.append("<h3 id=\"ritmrw\" class=\"sybils\">Closest Floodfills to Tomorrow's Routing Key (Where we will Store our RI)</h3>");
    250282        buf.append("<p class=\"sybil_info\"><a href=\"/netdb?caps=f&amp;sybil\">See all</a></p>");
    251         renderRouterInfoHTML(out, buf, nkey, avgMinDist, ris, points);
     283        renderRouterInfoHTML(out, buf, nkey, "our rkey (tomorrow)", avgMinDist, ris, points);
    252284
    253285        buf.append("<h3 id=\"dht\" class=\"sybils\">Closest Floodfills to Our Router Hash (DHT Neighbors if we are Floodfill)</h3>");
    254         renderRouterInfoHTML(out, buf, us, avgMinDist, ris, points);
     286        renderRouterInfoHTML(out, buf, us, "our router", avgMinDist, ris, points);
    255287
    256288        // Distance to our published destinations analysis
     
    270302            Hash rkey = ls.getRoutingKey();
    271303            TunnelPool in = clientInboundPools.get(client);
    272             String name = (in != null) ? in.getSettings().getDestinationNickname() : client.toBase64().substring(0,4);
    273             buf.append("<h3 class=\"sybils\">Closest floodfills to the Routing Key for " + DataHelper.escapeHTML(name) + " (where we store our LS)</h3>");
     304            String name = (in != null) ? DataHelper.escapeHTML(in.getSettings().getDestinationNickname()) : client.toBase64().substring(0,4);
     305            buf.append("<h3 class=\"sybils\">Closest floodfills to the Routing Key for " + name + " (where we store our LS)</h3>");
    274306            buf.append("<p class=\"sybil_info\"><a href=\"/netdb?caps=f&amp;sybil=" + ls.getHash().toBase64() + "\">See all</a></p>");
    275             renderRouterInfoHTML(out, buf, rkey, avgMinDist, ris, points);
     307            renderRouterInfoHTML(out, buf, rkey, name, avgMinDist, ris, points);
    276308            nkey = rkgen.getNextRoutingKey(ls.getHash());
    277             buf.append("<h3 class=\"sybils\">Closest floodfills to Tomorrow's Routing Key for " + DataHelper.escapeHTML(name) + " (where we will store our LS)</h3>");
     309            buf.append("<h3 class=\"sybils\">Closest floodfills to Tomorrow's Routing Key for " + name + " (where we will store our LS)</h3>");
    278310            buf.append("<p class=\"sybil_info\"><a href=\"/netdb?caps=f&amp;sybil=" + ls.getHash().toBase64() + "\">See all</a></p>");
    279             renderRouterInfoHTML(out, buf, nkey, avgMinDist, ris, points);
     311            renderRouterInfoHTML(out, buf, nkey, name + " (tomorrow)", avgMinDist, ris, points);
    280312        }
    281313
     
    310342        out.flush();
    311343        buf.setLength(0);
     344    }
     345
     346    /**
     347     *  Analyze threats. No output.
     348     *  Return separate maps for each cause instead?
     349     *  @since 0.9.38
     350     */
     351    public Map<Hash, Points> backgroundAnalysis() throws IOException {
     352        Hash us = _context.routerHash();
     353        List<RouterInfo> ris = getFloodfills(us);
     354
     355        double avgMinDist = getAvgMinDist(ris);
     356        Map<Hash, Points> points = new HashMap<Hash, Points>(64);
     357
     358        // IP analysis
     359        renderIPGroupsFamily(null, null, ris, points);
     360        renderIPGroupsUs(null, null, ris, points);
     361        renderIPGroups32(null, null, ris, points);
     362        renderIPGroups24(null, null, ris, points);
     363        renderIPGroups16(null, null, ris, points);
     364
     365        // Pairwise distance analysis
     366        renderPairDistance(null, null, ris, points);
     367
     368        // Distance to our router analysis
     369        // closest to our routing key today
     370        Hash ourRKey = _context.router().getRouterInfo().getRoutingKey();
     371        renderRouterInfoHTML(null, null, ourRKey, "our rkey", avgMinDist, ris, points);
     372        // closest to our routing key tomorrow
     373        RouterKeyGenerator rkgen = _context.routerKeyGenerator();
     374        Hash nkey = rkgen.getNextRoutingKey(us);
     375        renderRouterInfoHTML(null, null, nkey, "our rkey (tomorrow)", avgMinDist, ris, points);
     376        // closest to us
     377        renderRouterInfoHTML(null, null, us, "our router", avgMinDist, ris, points);
     378
     379        // Distance to our published destinations analysis
     380        Map<Hash, TunnelPool> clientInboundPools = _context.tunnelManager().getInboundClientPools();
     381        List<Hash> destinations = new ArrayList<Hash>(clientInboundPools.keySet());
     382        for (Hash client : destinations) {
     383            boolean isLocal = _context.clientManager().isLocal(client);
     384            if (!isLocal)
     385                continue;
     386            if (! _context.clientManager().shouldPublishLeaseSet(client))
     387                continue;
     388            LeaseSet ls = _context.netDb().lookupLeaseSetLocally(client);
     389            if (ls == null)
     390                continue;
     391            Hash rkey = ls.getRoutingKey();
     392            TunnelPool in = clientInboundPools.get(client);
     393            String name = (in != null) ? DataHelper.escapeHTML(in.getSettings().getDestinationNickname()) : client.toBase64().substring(0,4);
     394            // closest to routing key today
     395            renderRouterInfoHTML(null, null, rkey, name, avgMinDist, ris, points);
     396            // closest to routing key tomorrow
     397            nkey = rkgen.getNextRoutingKey(ls.getHash());
     398            renderRouterInfoHTML(null, null, nkey, name + " (tomorrow)", avgMinDist, ris, points);
     399        }
     400
     401        // Profile analysis
     402        addProfilePoints(ris, points);
     403        addVersionPoints(ris, points);
     404        return points;
    312405    }
    313406
     
    381474    }
    382475
    383     private double closestDistance(Hash h, List<RouterInfo> ris) throws IOException {
     476    private double closestDistance(Hash h, List<RouterInfo> ris) {
    384477        BigInteger min = (new BigInteger("2")).pow(256);
    385478        for (RouterInfo info : ris) {
     
    800893     *  @param out null for background analysis
    801894     *  @param buf null for background analysis
    802      */
    803     private void renderRouterInfoHTML(Writer out, StringBuilder buf, Hash us, double avgMinDist,
     895     *  @param usName HTML escaped
     896     */
     897    private void renderRouterInfoHTML(Writer out, StringBuilder buf, Hash us, String usName, double avgMinDist,
    804898                                      List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
    805899        Collections.sort(ris, new RouterInfoRoutingKeyComparator(us));
     
    844938            if (point > 0) {
    845939                point *= OUR_KEY_FACTOR;
    846                 addPoints(points, ri.getHash(), point, "Very close (" + fmt.format(dist) + ") to our key " + us.toBase64());
     940                addPoints(points, ri.getHash(), point, "Very close (" + fmt.format(dist) + ") to our key " + usName + ": " + us.toBase64());
    847941            }
    848942            if (i >= MAX - 1)
Note: See TracChangeset for help on using the changeset viewer.