From 67e7e45779f8c3d8b2798f85324f4b89e15cbe2c Mon Sep 17 00:00:00 2001 From: zzz Date: Mon, 20 May 2019 18:27:29 +0000 Subject: [PATCH] Sybil: Skip comment lines in stored files Escape % in stored reasons Improve error handling when loading files --- .../java/src/net/i2p/router/sybil/PersistSybil.java | 6 ++++++ .../java/src/net/i2p/router/sybil/Points.java | 2 +- .../src/net/i2p/router/web/helpers/SybilRenderer.java | 9 +++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/apps/routerconsole/java/src/net/i2p/router/sybil/PersistSybil.java b/apps/routerconsole/java/src/net/i2p/router/sybil/PersistSybil.java index fde9c8ceb5..e459806f3a 100644 --- a/apps/routerconsole/java/src/net/i2p/router/sybil/PersistSybil.java +++ b/apps/routerconsole/java/src/net/i2p/router/sybil/PersistSybil.java @@ -64,6 +64,8 @@ public class PersistSybil { Writer out = null; try { out = new OutputStreamWriter(new GZIPOutputStream(new SecureFileOutputStream(file))); + out.write("# Format (one per line)\n"); + out.write("# Base64 router hash:total points%points:reason%points:reason ...\n"); for (Map.Entry entry : entries.entrySet()) { Hash h = entry.getKey(); Points p = entry.getValue(); @@ -114,6 +116,8 @@ public class PersistSybil { in = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)))); String line; while ((line = in.readLine()) != null) { + if (line.startsWith("#")) + continue; int colon = line.indexOf(':'); if (colon != 44) continue; @@ -192,6 +196,8 @@ public class PersistSybil { if (d < cutoff) { if (file.delete()) deleted++; + else if (_log.shouldWarn()) + _log.warn("Failed to delete: " + file); } } catch (NumberFormatException nfe) {} } diff --git a/apps/routerconsole/java/src/net/i2p/router/sybil/Points.java b/apps/routerconsole/java/src/net/i2p/router/sybil/Points.java index b1f7d65ca1..decb78970d 100644 --- a/apps/routerconsole/java/src/net/i2p/router/sybil/Points.java +++ b/apps/routerconsole/java/src/net/i2p/router/sybil/Points.java @@ -81,7 +81,7 @@ public class Points implements Comparable { public void toString(StringBuilder buf) { buf.append(points); for (String r : reasons) { - buf.append('%').append(r); + buf.append('%').append(r.replace("%", "%")); } } diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/SybilRenderer.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/SybilRenderer.java index f01e6020dc..0679b220b6 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/SybilRenderer.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/SybilRenderer.java @@ -62,6 +62,7 @@ import net.i2p.util.VersionComparator; public class SybilRenderer { private final RouterContext _context; + private final Log _log; private final DecimalFormat fmt = new DecimalFormat("#0.00"); private final DateFormat dfmt; @@ -74,6 +75,7 @@ public class SybilRenderer { public SybilRenderer(RouterContext ctx) { _context = ctx; + _log = ctx.logManager().getLog(SybilRenderer.class); dfmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT); dfmt.setTimeZone(SystemVersion.getSystemTimeZone(_context)); } @@ -206,11 +208,14 @@ public class SybilRenderer { try { points = ps.load(date); } catch (IOException ioe) { - out.write("No analysis found for " + new Date(date) + ""); + _log.error("loading stored analysis for date: " + date, ioe); + out.write("Failed to load analysis for " + dfmt.format(new Date(date)) + ": " + + DataHelper.escapeHTML(ioe.toString())); return; } if (points.isEmpty()) { - out.write("No analysis found for " + new Date(date) + ""); + _log.error("empty stored analysis or bad file format for date: " + date); + out.write("Corrupt analysis file for " + dfmt.format(new Date(date)) + ""); } else { renderThreatsHTML(out, buf, date, points); }