Tunnels: Improved logging and handling of offline signature expiration

Store back ref to controller in tunnel
Stop server controller on I2PException
Support generation of keys with fractional days expiration for testing
This commit is contained in:
zzz
2020-10-29 15:20:56 +00:00
parent 8d0b1214d2
commit 190b76d7fd
10 changed files with 93 additions and 23 deletions

View File

@@ -95,6 +95,7 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
private final long _tunnelId;
private final Properties _clientOptions;
private final Set<I2PSession> _sessions;
private final TunnelController _controller;
public static final int PACKET_DELAY = 100;
@@ -152,6 +153,16 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
this(nocli_args);
}
/**
* New standard constructor in router, with back ref to tc
* @param tc may be null
* @throws IllegalArgumentException
* @since 0.9.48
*/
public I2PTunnel(TunnelController tc) {
this(nocli_args, null, tc);
}
/**
* See usage() for options
* @throws IllegalArgumentException
@@ -166,10 +177,21 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
* @throws IllegalArgumentException
*/
public I2PTunnel(String[] args, ConnectionEventListener lsnr) {
this(args, lsnr, null);
}
/**
* @param lsnr may be null
* @param tc may be null
* @throws IllegalArgumentException
* @since 0.9.48
*/
private I2PTunnel(String[] args, ConnectionEventListener lsnr, TunnelController tc) {
super();
_context = I2PAppContext.getGlobalContext(); // new I2PAppContext();
_tunnelId = __tunnelId.incrementAndGet();
_log = _context.logManager().getLog(I2PTunnel.class);
_controller = tc;
// as of 0.8.4, include context properties
Properties p = _context.getProperties();
_clientOptions = p;
@@ -366,6 +388,13 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
*/
public Properties getClientOptions() { return _clientOptions; }
/**
* TunnelController that constructed this, or null.
* @return controller or null
* @since 0.9.48
*/
TunnelController getController() { return _controller; }
private void addtask(I2PTunnelTask tsk) {
tsk.setTunnel(this);
if (tsk.isOpen()) {

View File

@@ -307,12 +307,15 @@ public class I2PTunnelServer extends I2PTunnelTask implements Runnable {
if (session.isOffline()) {
long exp = session.getOfflineExpiration();
long remaining = exp - getTunnel().getContext().clock().now();
if (remaining <= 0)
throw new IllegalArgumentException("Offline signature expired " + DataHelper.formatTime(exp));
if (remaining <= 0) {
String msg = "Offline signature for tunnel expired " + DataHelper.formatTime(exp);
_log.log(Log.CRIT, msg);
throw new IllegalArgumentException(msg);
}
if (remaining < 60*24*60*60*1000L) {
String msg = "Offline signature expires in " + DataHelper.formatDuration(remaining);
String msg = "Offline signature for tunnel expires in " + DataHelper.formatDuration(remaining);
_log.logAlways(Log.WARN, msg);
l.log(msg);
l.log("WARNING: " + msg);
}
}
while (session.isClosed()) {
@@ -641,9 +644,15 @@ public class I2PTunnelServer extends I2PTunnelTask implements Runnable {
// so sockMgr will call ConnectionManager.setAllowIncomingConnections(true) again
i2pss = sockMgr.getServerSocket();
} catch (I2PException ipe) {
if (_log.shouldLog(Log.ERROR))
_log.error("Error accepting - KILLING THE TUNNEL SERVER", ipe);
open = false;
String s = "Error accepting - KILLING THE TUNNEL SERVER";
_log.log(Log.CRIT, s, ipe);
l.log(s + ": " + ipe);
// Tell TunnelController so it will change state
TunnelController tc = getTunnel().getController();
if (tc != null)
tc.stopTunnel();
else
close(true);
if (i2ps != null) try { i2ps.close(); } catch (IOException ioe) {}
break;
} catch (ConnectException ce) {

View File

@@ -213,7 +213,7 @@ public class TunnelController implements Logging {
* overwrite existing ones)
*/
public TunnelController(Properties config, String prefix, boolean createKey) {
_tunnel = new I2PTunnel();
_tunnel = new I2PTunnel(this);
_log = I2PAppContext.getGlobalContext().logManager().getLog(TunnelController.class);
setConfig(config, prefix);
_messages = new ArrayList<String>(4);