Search in sources :

Example 1 with XORComparator

use of net.i2p.kademlia.XORComparator in project i2p.i2p by i2p.

the class SybilRenderer method renderSybilHTML.

/**
 *  Called from NetDbRenderer
 *
 *  @since 0.9.28
 */
public static void renderSybilHTML(Writer out, RouterContext ctx, List<Hash> sybils, String victim) throws IOException {
    if (sybils.isEmpty())
        return;
    final DecimalFormat fmt = new DecimalFormat("#0.00");
    XORComparator<Hash> xor = new XORComparator<Hash>(Hash.FAKE_HASH);
    out.write("<h3 class=\"tabletitle\">Group Distances</h3><table class=\"sybil_distance\"><tr><th>Hash<th>Distance from previous</tr>\n");
    Collections.sort(sybils, xor);
    Hash prev = null;
    for (Hash h : sybils) {
        String hh = h.toBase64();
        out.write("<tr><td><a href=\"#" + hh.substring(0, 6) + "\"><tt>" + hh + "</tt></a><td>");
        if (prev != null) {
            BigInteger dist = HashDistance.getDistance(prev, h);
            writeDistance(out, fmt, dist);
        }
        prev = h;
        out.write("</tr>\n");
    }
    out.write("</table>\n");
    out.flush();
    RouterKeyGenerator rkgen = ctx.routerKeyGenerator();
    long now = ctx.clock().now();
    final int start = -3;
    now += start * 24 * 60 * 60 * 1000L;
    final int days = 10;
    Hash from = ctx.routerHash();
    if (victim != null) {
        Hash v = ConvertToHash.getHash(victim);
        if (v != null)
            from = v;
    }
    out.write("<h3>Distance to " + from.toBase64() + "</h3>");
    prev = null;
    final int limit = Math.min(10, sybils.size());
    for (int i = start; i <= days; i++) {
        out.write("<h3 class=\"tabletitle\">Distance for " + new Date(now) + "</h3><table class=\"sybil_distance\"><tr><th>Hash<th>Distance<th>Distance from previous</tr>\n");
        Hash rkey = rkgen.getRoutingKey(from, now);
        xor = new XORComparator<Hash>(rkey);
        Collections.sort(sybils, xor);
        for (int j = 0; j < limit; j++) {
            Hash h = sybils.get(j);
            String hh = h.toBase64();
            out.write("<tr><td><a href=\"#" + hh.substring(0, 6) + "\"><tt>" + hh + "</tt></a><td>");
            BigInteger dist = HashDistance.getDistance(rkey, h);
            writeDistance(out, fmt, dist);
            out.write("<td>");
            if (prev != null) {
                dist = HashDistance.getDistance(prev, h);
                writeDistance(out, fmt, dist);
            }
            prev = h;
            out.write("</tr>\n");
        }
        out.write("</table>\n");
        out.flush();
        now += 24 * 60 * 60 * 1000;
        prev = null;
    }
}
Also used : DecimalFormat(java.text.DecimalFormat) XORComparator(net.i2p.kademlia.XORComparator) RouterKeyGenerator(net.i2p.data.router.RouterKeyGenerator) BigInteger(java.math.BigInteger) Hash(net.i2p.data.Hash) ConvertToHash(net.i2p.util.ConvertToHash) Date(java.util.Date)

Aggregations

BigInteger (java.math.BigInteger)1 DecimalFormat (java.text.DecimalFormat)1 Date (java.util.Date)1 Hash (net.i2p.data.Hash)1 RouterKeyGenerator (net.i2p.data.router.RouterKeyGenerator)1 XORComparator (net.i2p.kademlia.XORComparator)1 ConvertToHash (net.i2p.util.ConvertToHash)1