Sybil: Skip comment lines in stored files

Escape % in stored reasons
Improve error handling when loading files
This commit is contained in:
zzz
2019-05-20 18:27:29 +00:00
parent cafdca9a7b
commit 67e7e45779
3 changed files with 14 additions and 3 deletions

View File

@ -64,6 +64,8 @@ public class PersistSybil {
Writer out = null; Writer out = null;
try { try {
out = new OutputStreamWriter(new GZIPOutputStream(new SecureFileOutputStream(file))); 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<Hash, Points> entry : entries.entrySet()) { for (Map.Entry<Hash, Points> entry : entries.entrySet()) {
Hash h = entry.getKey(); Hash h = entry.getKey();
Points p = entry.getValue(); Points p = entry.getValue();
@ -114,6 +116,8 @@ public class PersistSybil {
in = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)))); in = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file))));
String line; String line;
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
if (line.startsWith("#"))
continue;
int colon = line.indexOf(':'); int colon = line.indexOf(':');
if (colon != 44) if (colon != 44)
continue; continue;
@ -192,6 +196,8 @@ public class PersistSybil {
if (d < cutoff) { if (d < cutoff) {
if (file.delete()) if (file.delete())
deleted++; deleted++;
else if (_log.shouldWarn())
_log.warn("Failed to delete: " + file);
} }
} catch (NumberFormatException nfe) {} } catch (NumberFormatException nfe) {}
} }

View File

@ -81,7 +81,7 @@ public class Points implements Comparable<Points> {
public void toString(StringBuilder buf) { public void toString(StringBuilder buf) {
buf.append(points); buf.append(points);
for (String r : reasons) { for (String r : reasons) {
buf.append('%').append(r); buf.append('%').append(r.replace("%", "&#x25;"));
} }
} }

View File

@ -62,6 +62,7 @@ import net.i2p.util.VersionComparator;
public class SybilRenderer { public class SybilRenderer {
private final RouterContext _context; private final RouterContext _context;
private final Log _log;
private final DecimalFormat fmt = new DecimalFormat("#0.00"); private final DecimalFormat fmt = new DecimalFormat("#0.00");
private final DateFormat dfmt; private final DateFormat dfmt;
@ -74,6 +75,7 @@ public class SybilRenderer {
public SybilRenderer(RouterContext ctx) { public SybilRenderer(RouterContext ctx) {
_context = ctx; _context = ctx;
_log = ctx.logManager().getLog(SybilRenderer.class);
dfmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT); dfmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
dfmt.setTimeZone(SystemVersion.getSystemTimeZone(_context)); dfmt.setTimeZone(SystemVersion.getSystemTimeZone(_context));
} }
@ -206,11 +208,14 @@ public class SybilRenderer {
try { try {
points = ps.load(date); points = ps.load(date);
} catch (IOException ioe) { } catch (IOException ioe) {
out.write("<b>No analysis found for " + new Date(date) + "</b>"); _log.error("loading stored analysis for date: " + date, ioe);
out.write("<b>Failed to load analysis for " + dfmt.format(new Date(date)) + "</b>: " +
DataHelper.escapeHTML(ioe.toString()));
return; return;
} }
if (points.isEmpty()) { if (points.isEmpty()) {
out.write("<b>No analysis found for " + new Date(date) + "</b>"); _log.error("empty stored analysis or bad file format for date: " + date);
out.write("<b>Corrupt analysis file for " + dfmt.format(new Date(date)) + "</b>");
} else { } else {
renderThreatsHTML(out, buf, date, points); renderThreatsHTML(out, buf, date, points);
} }