package net.i2p.router.web; import java.io.File; import java.io.IOException; import java.io.Writer; import java.text.DateFormat; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import net.i2p.app.ClientAppManager; import net.i2p.crypto.SigType; import net.i2p.data.DataHelper; import net.i2p.router.RouterContext; import net.i2p.router.news.NewsEntry; import net.i2p.router.news.NewsManager; import net.i2p.util.PortMapper; import net.i2p.util.SystemVersion; /** * Refactored from summarynoframe.jsp to save ~100KB * */ class SummaryBarRenderer { static final String ALL_SECTIONS[] = {"HelpAndFAQ", "I2PServices", "I2PInternals", "General", "ShortGeneral", "NetworkReachability", "UpdateStatus", "RestartStatus", "Peers", "FirewallAndReseedStatus", "Bandwidth", "Tunnels", "Congestion", "TunnelStatus", "Destinations", "NewsHeadings", "Advanced" }; static final Map SECTION_NAMES; static { Map aMap = new HashMap();; aMap.put("HelpAndFAQ", "Help & FAQ"); aMap.put("I2PServices", "I2P Services"); aMap.put("I2PInternals", "I2P Internals"); aMap.put("General", "General"); aMap.put("ShortGeneral", "Short General"); aMap.put("NetworkReachability", "Network Reachability"); aMap.put("UpdateStatus", "Update Status"); aMap.put("RestartStatus", "Restart Status"); aMap.put("Peers", "Peers"); aMap.put("FirewallAndReseedStatus", "Firewall & Reseed Status"); aMap.put("Bandwidth", "Bandwidth"); aMap.put("Tunnels", "Tunnels"); aMap.put("Congestion", "Congestion"); aMap.put("TunnelStatus", "Tunnel Status"); aMap.put("Destinations", "Local Tunnels"); aMap.put("NewsHeadings", "News & Updates"); aMap.put("Advanced", "Advanced"); SECTION_NAMES = Collections.unmodifiableMap(aMap); } private final RouterContext _context; private final SummaryHelper _helper; public SummaryBarRenderer(RouterContext context, SummaryHelper helper) { _context = context; _helper = helper; } /** * Note - ensure all links in here are absolute, as the summary bar may be displayed * on lower-level directory errors. */ public void renderSummaryHTML(Writer out) throws IOException { String requestURI = _helper.getRequestURI(); String page = requestURI.replace("/", "").replace(".jsp", ""); List sections = _helper.getSummaryBarSections(page); StringBuilder buf = new StringBuilder(8*1024); for (String section : sections) { // Commented out because broken. Replaced by if-elseif blob below. /*try { String section = (String)ALL_SECTIONS.get(sections[i]).invoke(this); if (section != null && section != "") { out.write("
" + i + "
\n" + section); } } catch (Exception e) { out.write("
" +i + " - Exception
\n" + e); }*/ buf.setLength(0); buf.append("
\n"); if ("HelpAndFAQ".equals(section)) buf.append(renderHelpAndFAQHTML()); else if ("I2PServices".equals(section)) buf.append(renderI2PServicesHTML()); else if ("I2PInternals".equals(section)) buf.append(renderI2PInternalsHTML()); else if ("Advanced".equals(section)) buf.append(renderAdvancedHTML()); else if ("General".equals(section)) buf.append(renderGeneralHTML()); else if ("ShortGeneral".equals(section)) buf.append(renderShortGeneralHTML()); else if ("NetworkReachability".equals(section)) buf.append(renderNetworkReachabilityHTML()); else if ("UpdateStatus".equals(section)) buf.append(renderUpdateStatusHTML()); else if ("RestartStatus".equals(section)) buf.append(renderRestartStatusHTML()); else if ("Peers".equals(section)) buf.append(renderPeersHTML()); else if ("FirewallAndReseedStatus".equals(section)) buf.append(renderFirewallAndReseedStatusHTML()); else if ("Bandwidth".equals(section)) buf.append(renderBandwidthHTML()); else if ("Tunnels".equals(section)) buf.append(renderTunnelsHTML()); else if ("Congestion".equals(section)) buf.append(renderCongestionHTML()); else if ("TunnelStatus".equals(section)) buf.append(renderTunnelStatusHTML()); else if ("Destinations".equals(section)) buf.append(renderDestinationsHTML()); else if ("NewsHeadings".equals(section)) buf.append(renderNewsHeadingsHTML()); // Only output section if there's more than the
to print if (buf.length() > 5) out.write(buf.toString()); } } public String renderHelpAndFAQHTML() { StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("Help & FAQ")) .append("

"); return buf.toString(); } public String renderI2PServicesHTML() { StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("I2P Services")) .append("

\n" + "
" + "") .append(nbsp(_t("Email"))) .append("\n" + "") .append(nbsp(_t("Torrents"))) .append("\n" + "") .append(nbsp(_t("Web Server"))) .append("\n") .append(NavHelper.getClientAppLinks(_context)) .append("
\n"); return buf.toString(); } public String renderI2PInternalsHTML() { StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("I2P Internals")) .append("


\n" + "
\n" + "") .append(nbsp(_t("Addressbook"))) .append("\n"); if (!StatSummarizer.isDisabled()) { buf.append("") .append(nbsp(_t("Graphs"))) .append("\n"); } buf.append("") .append(nbsp(_t("Hidden Services Manager"))) .append("\n" + // "") // .append(_t("Jobs")) // .append("\n" + "") .append(nbsp(_t("Logs"))) .append("\n" + "") .append(nbsp(_t("NetDB"))) .append("\n" + "") .append(nbsp(_t("Peers"))) .append("\n" + "") .append(nbsp(_t("Profiles"))) .append("\n" + "") .append(nbsp(_t("Tunnels"))) .append("\n"); buf.append("
\n"); return buf.toString(); } public String renderAdvancedHTML() { StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("Advanced")) .append("

\n") .append("
") .append("") .append(nbsp(_t("Certs"))) .append("\n") .append("") .append(nbsp(_t("Changelog"))) .append("\n") .append("") .append(nbsp(_t("Debug"))) .append("\n") .append("") .append(nbsp(_t("Jars"))) .append("\n"); File javadoc = new File(_context.getBaseDir(), "docs/javadoc/index.html"); if (javadoc.exists()) buf.append("Javadoc\n"); buf.append("") .append(nbsp(_t("LeaseSets"))) .append("\n") .append("") .append(nbsp(_t("NetDB Search"))) .append("\n") .append("") .append(nbsp(_t("Proof"))) .append("\n") .append("") .append(nbsp(_t("Stats"))) .append("\n") .append("") .append(nbsp(_t("Sybils"))) .append("\n") .append("
"); return buf.toString(); } public String renderGeneralHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("General")) .append("


\n" + "" + "" + "\n" + "
") .append(_t("Local Identity")) .append(":" + "") .append(_t("show")) .append("
" + // fix for some rows with a big left side and some with a big right side "" + "" + "\n" + "
") .append(_t("Version")) .append(":") .append(_helper.getVersion()) .append("
" + // fix for some rows with a big left side and some with a big right side "" + "" + "
") .append(_t("Uptime")) .append(":") .append(_helper.getUptime()) .append("
\n"); return buf.toString(); } public String renderShortGeneralHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); buf.append("" + "" + "" + "\n" + "" + "" + "
") .append(_t("Version")) .append(":") .append(_helper.getVersion()) .append("
") .append(_t("Uptime")) .append(":") .append(_helper.getUptime()) .append("
\n"); return buf.toString(); } public String renderNetworkReachabilityHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); SummaryHelper.NetworkStateMessage reachability = _helper.getReachability(); buf.append("

") .append(_t("Network")) .append(": ") .append(reachability.getMessage()) .append("

\n"); if (!SigType.ECDSA_SHA256_P256.isAvailable()) { buf.append("
\n

") .append(_t("Warning: ECDSA is not available. Update your Java or OS")) .append("

\n"); } if (!SystemVersion.isJava7()) { buf.append("

") .append(_t("Warning: Java version {0} is no longer supported by I2P.", System.getProperty("java.version"))) .append(' ') .append(_t("Update Java to version {0} or higher to receive I2P updates.", "7")) .append("

\n"); } return buf.toString(); } public String renderUpdateStatusHTML() { if (_helper == null) return ""; String updateStatus = _helper.getUpdateStatus(); if ("".equals(updateStatus)) return ""; StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("I2P Update")) .append("


\n"); buf.append(updateStatus); return buf.toString(); } public String renderRestartStatusHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); buf.append(_helper.getRestartStatus()); return buf.toString(); } public String renderPeersHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("Peers")) .append("


\n" + "\n" + "" + "\n" + "" + "\n" + "" + "\n" + "" + "\n" + "" + "\n" + "
") .append(_t("Active")) .append(":"); int active = _helper.getActivePeers(); buf.append(active) .append(SummaryHelper.THINSP) .append(Math.max(active, _helper.getActiveProfiles())) .append("
") .append(_t("Fast")) .append(":") .append(_helper.getFastPeers()) .append("
") .append(_t("High capacity")) .append(":") .append(_helper.getHighCapacityPeers()) .append("
") .append(_t("Integrated")) .append(":") .append(_helper.getWellIntegratedPeers()) .append("
") .append(_t("Known")) .append(":") .append(_helper.getAllPeers()) .append("
\n"); return buf.toString(); } public String renderFirewallAndReseedStatusHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); buf.append(_helper.getFirewallAndReseedStatus()); return buf.toString(); } public String renderBandwidthHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("Bandwidth in/out")) .append("


" + "\n" + "\n"); if (_context.router().getUptime() > 6*60*1000) { buf.append("\n"); } if (_context.router().getUptime() > 2*60*1000) { buf.append("\n"); } buf.append("\n" + "
") .append(DataHelper.formatDuration2(3 * 1000)) // lie and say 3 sec since 1 sec would appear as 1000 ms .append(":") .append(_helper.getSecondKBps()) .append("Bps
") .append(DataHelper.formatDuration2(5 * 60 * 1000)) // 5 min .append(":") .append(_helper.getFiveMinuteKBps()) .append("Bps
") .append(_t("Total")) .append(":") .append(_helper.getLifetimeKBps()) .append("Bps
") .append(_t("Used")) .append(":") .append(_helper.getInboundTransferred()) .append(SummaryHelper.THINSP) .append(_helper.getOutboundTransferred()) .append("
\n"); return buf.toString(); } public String renderTunnelsHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("Tunnels")) .append("


" + "\n" + "" + "\n" + "" + "\n" + "" + "\n" + "" + "\n" + "
") .append(_t("Exploratory")) .append(":") .append(_helper.getInboundTunnels() + _helper.getOutboundTunnels()) .append("
") .append(_t("Client")) .append(":") .append(_helper.getInboundClientTunnels() + _helper.getOutboundClientTunnels()) .append("
") .append(_t("Participating")) .append(":") .append(_helper.getParticipatingTunnels()) .append("
") .append(_t("Share ratio")) .append(":") .append(_helper.getShareRatio()) .append("
\n"); return buf.toString(); } public String renderCongestionHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); buf.append("

") .append(_t("Congestion")) .append("


" + "\n" + "" + "\n" + "" + "\n"); if (!_context.getBooleanPropertyDefaultTrue("router.disableTunnelTesting")) { buf.append("" + "\n"); } buf.append("" + "\n" + "
") .append(_t("Job lag")) .append(":") .append(_helper.getJobLag()) .append("
") .append(_t("Message delay")) .append(":") .append(_helper.getMessageDelay()) .append("
") .append(_t("Tunnel lag")) .append(":") .append(_helper.getTunnelLag()) .append("
") .append(_t("Backlog")) .append(":") .append(_helper.getInboundBacklog()) .append("
\n"); return buf.toString(); } public String renderTunnelStatusHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(50); buf.append("

") .append(_t(_helper.getTunnelStatus())) .append("

\n"); return buf.toString(); } public String renderDestinationsHTML() { if (_helper == null) return ""; StringBuilder buf = new StringBuilder(512); buf.append(_helper.getDestinations()); return buf.toString(); } /** @since 0.9.1 */ public String renderNewsHeadingsHTML() { if (_helper == null) return ""; NewsHelper newshelper = _helper.getNewsHelper(); if (newshelper == null || newshelper.shouldShowNews()) return ""; StringBuilder buf = new StringBuilder(512); String consoleNonce = CSSHelper.getNonce(); if (consoleNonce != null) { // Set up title and pre-headings stuff. //buf.append("

") buf.append("

") .append(_t("News & Updates")) .append("


\n"); // Get news content. List entries = Collections.emptyList(); ClientAppManager cmgr = _context.clientAppManager(); if (cmgr != null) { NewsManager nmgr = (NewsManager) cmgr.getRegisteredApp(NewsManager.APP_NAME); if (nmgr != null) entries = nmgr.getEntries(); } if (!entries.isEmpty()) { buf.append("\n"); //buf.append("") // .append(_t("Show all news")) // .append("\n"); } else { buf.append("
") .append(_t("none")) .append("
"); } // Add post-headings stuff. buf.append("
\n"); } return buf.toString(); } /** translate a string */ private String _t(String s) { return Messages.getString(s, _context); } /** @since 0.9.23 */ private String _t(String s, Object o) { return Messages.getString(s, o, _context); } /** * Where the translation is to two words or more, * prevent splitting across lines * * @since 0.9.18 */ private static String nbsp(String s) { // if it's too long, this makes it worse if (s.length() <= 30) return s.replace(" ", " "); else return s; } }