Changeset fa0d63f for apps


Ignore:
Timestamp:
Dec 16, 2018 1:20:59 PM (18 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
2a805dd
Parents:
9437e2c
Message:

Sybil: Refactoring to separate analysis and display, part 4
Load/store offline analysis
Split up sections into separate tabs

Location:
apps/routerconsole
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • apps/routerconsole/java/src/net/i2p/router/sybil/Analysis.java

    r9437e2c rfa0d63f  
    250250    }
    251251
    252     private double closestDistance(Hash h, List<RouterInfo> ris) {
    253         BigInteger min = (new BigInteger("2")).pow(256);
     252    private static final BigInteger BI_MAX = (new BigInteger("2")).pow(256);
     253
     254    private static double closestDistance(Hash h, List<RouterInfo> ris) {
     255        BigInteger min = BI_MAX;
    254256        for (RouterInfo info : ris) {
    255257            BigInteger dist = HashDistance.getDistance(h, info.getHash());
  • apps/routerconsole/java/src/net/i2p/router/sybil/PersistSybil.java

    r9437e2c rfa0d63f  
    9696            } catch (NumberFormatException nfe) {}
    9797        }
    98         Collections.sort(rv);
     98        Collections.sort(rv, Collections.reverseOrder());
    9999        return rv;
    100100    }
  • apps/routerconsole/java/src/net/i2p/router/web/helpers/NetDbHelper.java

    r9437e2c rfa0d63f  
    1414    private String _country;
    1515    private String _family, _caps, _ip, _sybil, _mtu, _ssucaps, _ipv6, _transport;
    16     private int _full, _port, _cost, _page;
     16    private int _full, _port, _cost, _page, _mode;
     17    private long _date;
    1718    private int _limit = DEFAULT_LIMIT;
    1819    private boolean _lease;
     
    142143    }
    143144
     145    /** @since 0.9.38 */
     146    public void setMode(String f) {
     147        try {
     148            _mode = Integer.parseInt(f);
     149        } catch (NumberFormatException nfe) {}
     150    }
     151
     152    /** @since 0.9.38 */
     153    public void setDate(String f) {
     154        try {
     155            _date = Long.parseLong(f);
     156        } catch (NumberFormatException nfe) {}
     157    }
     158
    144159    public void setFull(String f) {
    145160        try {
     
    199214                renderer.renderLeaseSetHTML(_out, _debug);
    200215            else if (_full == 3)
    201                 (new SybilRenderer(_context)).getNetDbSummary(_out);
     216                (new SybilRenderer(_context)).getNetDbSummary(_out, _mode, _date);
    202217            else if (_full == 4)
    203218                renderLookupForm();
  • apps/routerconsole/java/src/net/i2p/router/web/helpers/SybilRenderer.java

    r9437e2c rfa0d63f  
    1313import java.util.Date;
    1414import java.util.HashMap;
     15import java.util.Iterator;
    1516import java.util.List;
    1617import java.util.Locale;
     
    3334import net.i2p.router.sybil.Analysis;
    3435import net.i2p.router.sybil.Pair;
     36import net.i2p.router.sybil.PersistSybil;
    3537import net.i2p.router.sybil.Points;
    3638import static net.i2p.router.sybil.Util.biLog2;
     
    7173    /**
    7274     *   Entry point
    73      */
    74     public String getNetDbSummary(Writer out) throws IOException {
    75         renderRouterInfoHTML(out, (String)null);
     75     *
     76     *  @param mode what tab to show
     77     *  @param date only for mode = 12
     78     */
     79    public String getNetDbSummary(Writer out, int mode, long date) throws IOException {
     80        renderRouterInfoHTML(out, mode, date);
    7681        return "";
    7782    }
     
    116121     *  The whole thing
    117122     *
    118      *  @param routerPrefix ignored
    119      */
    120     private void renderRouterInfoHTML(Writer out, String routerPrefix) throws IOException {
     123     *  @param mode what tab to show
     124     *  @param date only for mode = 12
     125     */
     126    private void renderRouterInfoHTML(Writer out, int mode, long date) throws IOException {
    121127        Hash us = _context.routerHash();
    122         Hash ourRKey = _context.router().getRouterInfo().getRoutingKey();
    123128        Analysis analysis = new Analysis(_context);
    124129        List<RouterInfo> ris = analysis.getFloodfills(us);
     
    132137                   "Possible \"threats\" are summarized at the bottom, however these are unlikely to be real threats. " +
    133138                   "If you see anything you would like to discuss with the devs, contact us on IRC #i2p-dev.</b></p>" +
    134                    "<div id=\"sybilnav\"><ul><li><a href=\"#known\">FF Summary</a>" +
    135                    "</li><li><a href=\"#family\">Same Family</a>" +
    136                    "</li><li><a href=\"#ourIP\">IP close to us</a>" +
    137                    "</li><li><a href=\"#sameIP\">Same IP</a>" +
    138                    "</li><li><a href=\"#same24\">Same /24</a>" +
    139                    "</li><li><a href=\"#same16\">Same /16</a>" +
    140                    "</li><li><a href=\"#pairs\">Pair distance</a>" +
    141                    "</li><li><a href=\"#ritoday\">Close to us</a>" +
    142                    "</li><li><a href=\"#ritmrw\">Close to us tomorrow</a>" +
    143                    "</li><li><a href=\"#dht\">DHT neighbors</a>" +
    144                    "</li><li><a href=\"#dest\">Close to our destinations</a>" +
    145                    "</li><li><a href=\"#threats\">Highest threats</a>" +
     139                   "<div id=\"sybilnav\"><ul><li><a href=\"netdb?f=3\">Review stored analysis</a>" +
     140                   "</li><li><a href=\"netdb?f=3&amp;m=13\">Run new analysis</a>" +
     141                   "</li><li><a href=\"netdb?f=3&amp;m=1\">Floodfill Summary</a>" +
     142                   "</li><li><a href=\"netdb?f=3&amp;m=2\">Same Family</a>" +
     143                   "</li><li><a href=\"netdb?f=3&amp;m=3\">IP close to us</a>" +
     144                   "</li><li><a href=\"netdb?f=3&amp;m=4\">Same IP</a>" +
     145                   "</li><li><a href=\"netdb?f=3&amp;m=5\">Same /24</a>" +
     146                   "</li><li><a href=\"netdb?f=3&amp;m=6\">Same /16</a>" +
     147                   "</li><li><a href=\"netdb?f=3&amp;m=7\">Pair distance</a>" +
     148                   "</li><li><a href=\"netdb?f=3&amp;m=8\">Close to us</a>" +
     149                   "</li><li><a href=\"netdb?f=3&amp;m=9\">Close to us tomorrow</a>" +
     150                   "</li><li><a href=\"netdb?f=3&amp;m=10\">DHT neighbors</a>" +
     151                   "</li><li><a href=\"netdb?f=3&amp;m=11\">Close to our destinations</a>" +
    146152                   "</li></ul></div>");
    147 
     153        writeBuf(out, buf);
     154
     155        double avgMinDist = analysis.getAvgMinDist(ris);
     156        Map<Hash, Points> points = new HashMap<Hash, Points>(64);
     157
     158        if (mode == 0) {
     159            renderOverview(out, buf, analysis);
     160        } else if (mode == 1) {
     161            renderFFSummary(out, buf, ris, avgMinDist);
     162        } else if (mode == 2) {
     163            renderFamilySummary(out, buf, analysis, ris, points);
     164        } else if (mode == 3) {
     165            renderIPUsSummary(out, buf, analysis, ris, points);
     166        } else if (mode == 4) {
     167            renderIP32Summary(out, buf, analysis, ris, points);
     168        } else if (mode == 5) {
     169            renderIP24Summary(out, buf, analysis, ris, points);
     170        } else if (mode == 6) {
     171            renderIP16Summary(out, buf, analysis, ris, points);
     172        } else if (mode == 7) {
     173            renderPairSummary(out, buf, analysis, ris, points);
     174        } else if (mode == 8) {
     175            renderCloseSummary(out, buf, analysis, avgMinDist, ris, points);
     176        } else if (mode == 9) {
     177            renderCloseTmrwSummary(out, buf, analysis, us, avgMinDist, ris, points);
     178        } else if (mode == 10) {
     179            renderDHTSummary(out, buf, analysis, us, avgMinDist, ris, points);
     180        } else if (mode == 11) {
     181            renderDestSummary(out, buf, analysis, avgMinDist, ris, points);
     182        } else if (mode == 12) {
     183            PersistSybil ps = new PersistSybil(_context);
     184            try {
     185                points = ps.load(date);
     186            } catch (IOException ioe) {
     187                out.write("<b>No analysis found for " + new Date(date) + "</b>");
     188                return;
     189            }
     190            if (points.isEmpty()) {
     191                out.write("<b>No analysis found for " + new Date(date) + "</b>");
     192            } else {
     193                renderThreatsHTML(out, buf, date, points);
     194            }
     195        } else if (mode == 13) {
     196            long now = _context.clock().now();
     197            points = analysis.backgroundAnalysis();
     198            if (!points.isEmpty()) {
     199                PersistSybil ps = new PersistSybil(_context);
     200                try {
     201                    ps.store(now, points);
     202                } catch (IOException ioe) {
     203                    out.write("<b>Failed to store analysis " + ioe + "</b>");
     204                }
     205            }
     206            renderThreatsHTML(out, buf, now, points);
     207        } else {
     208            out.write("Unknown mode " + mode);
     209        }
     210        writeBuf(out, buf);
     211    }
     212
     213    /**
     214     *  @since 0.9.38
     215     */
     216    private void renderOverview(Writer out, StringBuilder buf, Analysis analysis) throws IOException {
     217        PersistSybil ps = new PersistSybil(_context);
     218        List<Long> dates = ps.load();
     219        if (dates.isEmpty()) {
     220            out.write("No stored analysis");
     221        } else {
     222            buf.append("<form action=\"netdb\" method=\"GET\">\n" +
     223                       "<input type=\"hidden\" name=\"f\" value=\"3\">\n" +
     224                       "<input type=\"hidden\" name=\"m\" value=\"12\">\n" +
     225                       "Select stored analysis: " +
     226                       "<select name=\"date\">\n");
     227            boolean first = true;
     228            for (Long date : dates) {
     229                buf.append("<option value=\"").append(date).append('\"');
     230                if (first) {
     231                    buf.append(" selected=\"selected\"");
     232                    first = false;
     233                }
     234                buf.append('>').append(new Date(date.longValue())).append("</option>\n");
     235            }       
     236            buf.append("</select>\n" +
     237                       "<input type=\"submit\" name=\"action\" class=\"go\" value=\"Review analysis\" />" +
     238                       "</form>\n");
     239        }       
     240        buf.append("<br><a href=\"netdb?f=3&amp;m=13\">Run new analysis</a>");
     241        writeBuf(out, buf);
     242    }
     243
     244    /**
     245     *  @since 0.9.38 split out from renderRouterInfoHTML()
     246     */
     247    private void renderFFSummary(Writer out, StringBuilder buf, List<RouterInfo> ris, double avgMinDist) throws IOException {
    148248        renderRouterInfo(buf, _context.router().getRouterInfo(), null, true, false);
    149249        buf.append("<h3 id=\"known\" class=\"sybils\">Known Floodfills: ").append(ris.size()).append("</h3>");
    150 
    151         double avgMinDist = analysis.getAvgMinDist(ris);
    152250        buf.append("<div id=\"sybils_summary\">\n" +
    153251                   "<b>Average closest floodfill distance:</b> ").append(fmt.format(avgMinDist)).append("<br>\n" +
     
    157255           .append("\" <b>Rotates in:</b> ").append(DataHelper.formatDuration(_context.routerKeyGenerator().getTimeTillMidnight())).append("\n" +
    158256                   "</div>\n");
    159 
    160         Map<Hash, Points> points = new HashMap<Hash, Points>(64);
    161 
    162         // IP analysis
     257        writeBuf(out, buf);
     258    }
     259
     260    /**
     261     *  @since 0.9.38 split out from renderRouterInfoHTML()
     262     */
     263    private void renderFamilySummary(Writer out, StringBuilder buf, Analysis analysis, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
    163264        Map<String, List<RouterInfo>> fmap = analysis.calculateIPGroupsFamily(ris, points);
    164265        renderIPGroupsFamily(out, buf, fmap);
     266    }
     267
     268    /**
     269     *  @since 0.9.38 split out from renderRouterInfoHTML()
     270     */
     271    private void renderIPUsSummary(Writer out, StringBuilder buf, Analysis analysis, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
    165272        List<RouterInfo> ri32 = new ArrayList<RouterInfo>(4);
    166273        List<RouterInfo> ri24 = new ArrayList<RouterInfo>(4);
     
    168275        analysis.calculateIPGroupsUs(ris, points, ri32, ri24, ri16);
    169276        renderIPGroupsUs(out, buf, ri32, ri24, ri16);
     277    }
     278
     279    /**
     280     *  @since 0.9.38 split out from renderRouterInfoHTML()
     281     */
     282    private void renderIP32Summary(Writer out, StringBuilder buf, Analysis analysis, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
    170283        Map<Integer, List<RouterInfo>> map = analysis.calculateIPGroups32(ris, points);
    171284        renderIPGroups32(out, buf, map);
    172         map = analysis.calculateIPGroups24(ris, points);
     285    }
     286
     287    /**
     288     *  @since 0.9.38 split out from renderRouterInfoHTML()
     289     */
     290    private void renderIP24Summary(Writer out, StringBuilder buf, Analysis analysis, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
     291        Map<Integer, List<RouterInfo>> map = analysis.calculateIPGroups24(ris, points);
    173292        renderIPGroups24(out, buf, map);
    174         map = analysis.calculateIPGroups16(ris, points);
    175         //renderIPGroups16(out, buf, map);
    176 
     293    }
     294
     295    /**
     296     *  @since 0.9.38 split out from renderRouterInfoHTML()
     297     */
     298    private void renderIP16Summary(Writer out, StringBuilder buf, Analysis analysis, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
     299        Map<Integer, List<RouterInfo>> map = analysis.calculateIPGroups16(ris, points);
     300        renderIPGroups16(out, buf, map);
     301    }
     302
     303    /**
     304     *  @since 0.9.38 split out from renderRouterInfoHTML()
     305     */
     306    private void renderPairSummary(Writer out, StringBuilder buf, Analysis analysis, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
    177307        // Pairwise distance analysis
    178308        List<Pair> pairs = new ArrayList<Pair>(PAIRMAX);
    179309        double avg = analysis.calculatePairDistance(ris, points, pairs);
    180310        renderPairDistance(out, buf, pairs, avg);
    181 
     311    }
     312
     313    /**
     314     *  @since 0.9.38 split out from renderRouterInfoHTML()
     315     */
     316    private void renderCloseSummary(Writer out, StringBuilder buf, Analysis analysis, double avgMinDist, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
    182317        // Distance to our router analysis
    183318        buf.append("<h3 id=\"ritoday\" class=\"sybils\">Closest Floodfills to Our Routing Key (Where we Store our RI)</h3>");
    184319        buf.append("<p class=\"sybil_info\"><a href=\"/netdb?caps=f&amp;sybil\">See all</a></p>");
     320        Hash ourRKey = _context.router().getRouterInfo().getRoutingKey();
    185321        analysis.calculateRouterInfo(ourRKey, "our rkey", ris, points);
    186322        renderRouterInfoHTML(out, buf, ourRKey, avgMinDist, ris);
     323    }
     324
     325    /**
     326     *  @since 0.9.38 split out from renderRouterInfoHTML()
     327     */
     328    private void renderCloseTmrwSummary(Writer out, StringBuilder buf, Analysis analysis, Hash us, double avgMinDist, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
     329        // Distance to our router analysis
    187330        RouterKeyGenerator rkgen = _context.routerKeyGenerator();
    188331        Hash nkey = rkgen.getNextRoutingKey(us);
     
    191334        analysis.calculateRouterInfo(nkey, "our rkey (tomorrow)", ris, points);
    192335        renderRouterInfoHTML(out, buf, nkey, avgMinDist, ris);
    193 
     336    }
     337
     338    /**
     339     *  @since 0.9.38 split out from renderRouterInfoHTML()
     340     */
     341    private void renderDHTSummary(Writer out, StringBuilder buf, Analysis analysis, Hash us, double avgMinDist, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
    194342        buf.append("<h3 id=\"dht\" class=\"sybils\">Closest Floodfills to Our Router Hash (DHT Neighbors if we are Floodfill)</h3>");
    195343        analysis.calculateRouterInfo(us, "our router", ris, points);
    196344        renderRouterInfoHTML(out, buf, us, avgMinDist, ris);
    197 
     345    }
     346
     347    /**
     348     *  @since 0.9.38 split out from renderRouterInfoHTML()
     349     */
     350    private void renderDestSummary(Writer out, StringBuilder buf, Analysis analysis, double avgMinDist, List<RouterInfo> ris, Map<Hash, Points> points) throws IOException {
    198351        // Distance to our published destinations analysis
     352        RouterKeyGenerator rkgen = _context.routerKeyGenerator();
    199353        buf.append("<h3 id=\"dest\" class=\"sybils\">Floodfills Close to Our Destinations</h3>");
    200354        Map<Hash, TunnelPool> clientInboundPools = _context.tunnelManager().getInboundClientPools();
    201355        List<Hash> destinations = new ArrayList<Hash>(clientInboundPools.keySet());
    202         //boolean debug = _context.getBooleanProperty(HelperBase.PROP_ADVANCED);
     356        for (Iterator<Hash> iter = destinations.iterator(); iter.hasNext(); ) {
     357            Hash client = iter.next();
     358            if (!_context.clientManager().isLocal(client) ||
     359                !_context.clientManager().shouldPublishLeaseSet(client) ||
     360                _context.netDb().lookupLeaseSetLocally(client) == null) {
     361                iter.remove();
     362            }
     363        }
     364        if (destinations.isEmpty()) {
     365            buf.append("<p class=\"notfound\">None</p>");
     366            writeBuf(out, buf);
     367            return;
     368        }
    203369        for (Hash client : destinations) {
    204             boolean isLocal = _context.clientManager().isLocal(client);
    205             if (!isLocal)
    206                 continue;
    207             if (! _context.clientManager().shouldPublishLeaseSet(client))
    208                 continue;
    209370            LeaseSet ls = _context.netDb().lookupLeaseSetLocally(client);
    210371            if (ls == null)
     
    217378            analysis.calculateRouterInfo(rkey, name, ris, points);
    218379            renderRouterInfoHTML(out, buf, rkey, avgMinDist, ris);
    219             nkey = rkgen.getNextRoutingKey(ls.getHash());
     380            Hash nkey = rkgen.getNextRoutingKey(ls.getHash());
    220381            buf.append("<h3 class=\"sybils\">Closest floodfills to Tomorrow's Routing Key for " + name + " (where we will store our LS)</h3>");
    221382            buf.append("<p class=\"sybil_info\"><a href=\"/netdb?caps=f&amp;sybil=" + ls.getHash().toBase64() + "\">See all</a></p>");
     
    223384            renderRouterInfoHTML(out, buf, nkey, avgMinDist, ris);
    224385        }
    225 
    226         // Profile analysis
    227         analysis.addProfilePoints(ris, points);
    228         analysis.addVersionPoints(ris, points);
    229 
     386    }
     387
     388    /**
     389     *  @since 0.9.38 split out from renderRouterInfoHTML()
     390     */
     391    private void renderThreatsHTML(Writer out, StringBuilder buf, long date, Map<Hash, Points> points) throws IOException {
    230392        if (!points.isEmpty()) {
    231393            List<Hash> warns = new ArrayList<Hash>(points.keySet());
    232394            Collections.sort(warns, new PointsComparator(points));
    233             buf.append("<h3 id=\"threats\" class=\"sybils\">Routers with Most Threat Points</h3>");
     395            buf.append("<h3 id=\"threats\" class=\"sybils\">Routers with Most Threat Points as of " + new Date(date) + "</h3>");
    234396            for (Hash h : warns) {
    235397                RouterInfo ri = _context.netDb().lookupRouterInfoLocally(h);
     
    254416            }
    255417        }
    256 
    257         out.write(buf.toString());
    258         out.flush();
    259         buf.setLength(0);
     418        writeBuf(out, buf);
    260419    }
    261420
     
    278437            renderRouterInfo(buf, p.r2, null, false, false);
    279438        }
    280 
    281         out.write(buf.toString());
    282         out.flush();
    283         buf.setLength(0);
     439        writeBuf(out, buf);
    284440    }
    285441
     
    341497        if (!found)
    342498            buf.append("<p class=\"notfound\">None</p>");
    343         out.write(buf.toString());
    344         out.flush();
    345         buf.setLength(0);
     499        writeBuf(out, buf);
    346500    }
    347501
     
    373527        if (!found)
    374528            buf.append("<p class=\"notfound\">None</p>");
    375         out.write(buf.toString());
    376         out.flush();
    377         buf.setLength(0);
     529        writeBuf(out, buf);
    378530    }
    379531
     
    404556        if (!found)
    405557            buf.append("<p class=\"notfound\">None</p>");
    406         out.write(buf.toString());
    407         out.flush();
    408         buf.setLength(0);
     558        writeBuf(out, buf);
    409559    }
    410560
     
    436586        if (!found)
    437587            buf.append("<p class=\"notfound\">None</p>");
    438         out.write(buf.toString());
    439         out.flush();
    440         buf.setLength(0);
     588        writeBuf(out, buf);
    441589    }
    442590
     
    468616            buf.append("<p class=\"notfound\">None</p>");
    469617        buf.append("</div>");
    470         out.write(buf.toString());
    471         out.flush();
    472         buf.setLength(0);
     618        writeBuf(out, buf);
    473619    }
    474620
     
    521667                   "&nbsp; <span class=\"netdb_name\">MAX:</span> " + fmt.format(max) +
    522668                   "</p>\n");
     669        writeBuf(out, buf);
     670    }
     671
     672    private static void writeBuf(Writer out, StringBuilder buf) throws IOException {
    523673        out.write(buf.toString());
    524674        out.flush();
  • apps/routerconsole/jsp/netdb.jsp

    r9437e2c rfa0d63f  
    3939 <jsp:setProperty name="netdbHelper" property="limit" value="<%=request.getParameter(\"ps\")%>" />
    4040 <jsp:setProperty name="netdbHelper" property="page" value="<%=request.getParameter(\"pg\")%>" />
     41 <jsp:setProperty name="netdbHelper" property="mode" value="<%=request.getParameter(\"m\")%>" />
     42 <jsp:setProperty name="netdbHelper" property="date" value="<%=request.getParameter(\"date\")%>" />
    4143 <jsp:getProperty name="netdbHelper" property="netDbSummary" />
    4244</div></body></html>
Note: See TracChangeset for help on using the changeset viewer.