diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHandler.java deleted file mode 100644 index aac2767f3..000000000 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHandler.java +++ /dev/null @@ -1,84 +0,0 @@ -package net.i2p.router.web; - -import net.i2p.router.ClientTunnelSettings; - -/** - * Handler to deal with form submissions from the client config form and act - * upon the values. - * - */ -public class ConfigClientsHandler extends FormHandler { - private String _numClients; - private String _numTunnels; - private String _numHops; - private String _numHopsOutbound; - private boolean _shouldSave; - - public void ConfigNetHandler() { - _shouldSave = false; - } - - protected void processForm() { - if (_shouldSave) { - saveChanges(); - } else { - // noop - } - } - - public void setShouldsave(String moo) { _shouldSave = true; } - - public void setClientcount(String num) { - _numClients = (num != null ? num.trim(): null); - } - public void setTunnelcount(String num) { - _numTunnels = (num != null ? num.trim() : null); - } - public void setTunneldepth(String num) { - _numHops = (num != null ? num.trim() : null); - } - public void setTunneldepthoutbound(String num) { - _numHopsOutbound = (num != null ? num.trim() : null); - } - - /** - * The user made changes to the network config and wants to save them, so - * lets go ahead and do so. - * - */ - private void saveChanges() { - boolean saveRequired = false; - - if ( (_numClients != null) && (_numClients.length() > 0) ) { - _context.router().setConfigSetting("router.targetClients", _numClients); - addFormNotice("Updating estimated number of clients to " + _numClients); - saveRequired = true; - } - - if ( (_numTunnels != null) && (_numTunnels.length() > 0) ) { - _context.router().setConfigSetting(ClientTunnelSettings.PROP_NUM_INBOUND, _numTunnels); - addFormNotice("Updating default number of tunnels per client to " + _numTunnels); - saveRequired = true; - } - - if ( (_numHops != null) && (_numHops.length() > 0) ) { - _context.router().setConfigSetting(ClientTunnelSettings.PROP_DEPTH_INBOUND, _numHops); - addFormNotice("Updating default tunnel length to " + _numHops); - saveRequired = true; - } - - if ( (_numHopsOutbound != null) && (_numHopsOutbound.length() > 0) ) { - _context.router().setConfigSetting(ClientTunnelSettings.PROP_DEPTH_OUTBOUND, _numHopsOutbound); - addFormNotice("Updating default outbound tunnel length to " + _numHopsOutbound); - saveRequired = true; - } - - if (saveRequired) { - boolean saved = _context.router().saveConfig(); - if (saved) - addFormNotice("Configuration saved successfully"); - else - addFormNotice("Error saving the configuration (applied but not saved) - please see the error logs"); - } - } -} diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHelper.java deleted file mode 100644 index 215e13dab..000000000 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHelper.java +++ /dev/null @@ -1,144 +0,0 @@ -package net.i2p.router.web; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.util.List; -import java.util.Iterator; -import java.util.TreeMap; - -import net.i2p.util.Log; - -import net.i2p.router.RouterContext; -import net.i2p.router.ClientTunnelSettings; - -public class ConfigClientsHelper { - private RouterContext _context; - /** - * Configure this bean to query a particular router context - * - * @param contextId begging few characters of the routerHash, or null to pick - * the first one we come across. - */ - public void setContextId(String contextId) { - try { - _context = ContextHelper.getContext(contextId); - } catch (Throwable t) { - t.printStackTrace(); - } - } - - /** copied from the package private {@link net.i2p.router.tunnelmanager.TunnelPool} */ - public final static String TARGET_CLIENTS_PARAM = "router.targetClients"; - /** copied from the package private {@link net.i2p.router.tunnelmanager.TunnelPool} */ - public final static int TARGET_CLIENTS_DEFAULT = 3; - - public ConfigClientsHelper() {} - - public String getClientCountSelectBox() { - int count = TARGET_CLIENTS_DEFAULT; - String val = _context.router().getConfigSetting(TARGET_CLIENTS_PARAM); - if (val != null) { - try { - count = Integer.parseInt(val); - } catch (NumberFormatException nfe) { - // ignore, use default from above - } - } - StringBuffer buf = new StringBuffer(1024); - buf.append("\n"); - return buf.toString(); - } - - public String getTunnelCountSelectBox() { - int count = ClientTunnelSettings.DEFAULT_NUM_INBOUND; - String val = _context.router().getConfigSetting(ClientTunnelSettings.PROP_NUM_INBOUND); - if (val != null) { - try { - count = Integer.parseInt(val); - } catch (NumberFormatException nfe) { - // ignore, use default from above - } - } - StringBuffer buf = new StringBuffer(1024); - buf.append("\n"); - return buf.toString(); - } - - public String getTunnelDepthSelectBox() { - int count = ClientTunnelSettings.DEFAULT_DEPTH_INBOUND; - String val = _context.router().getConfigSetting(ClientTunnelSettings.PROP_DEPTH_INBOUND); - if (val != null) { - try { - count = Integer.parseInt(val); - } catch (NumberFormatException nfe) { - // ignore, use default from above - } - } - StringBuffer buf = new StringBuffer(1024); - buf.append("\n"); - return buf.toString(); - } - - public String getTunnelDepthOutboundSelectBox() { - int count = ClientTunnelSettings.DEFAULT_DEPTH_OUTBOUND; - String val = _context.router().getConfigSetting(ClientTunnelSettings.PROP_DEPTH_OUTBOUND); - if (val != null) { - try { - count = Integer.parseInt(val); - } catch (NumberFormatException nfe) { - // ignore, use default from above - } - } - StringBuffer buf = new StringBuffer(1024); - buf.append("\n"); - return buf.toString(); - } -} diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigTunnelsHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigTunnelsHandler.java new file mode 100644 index 000000000..73ebcf87e --- /dev/null +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigTunnelsHandler.java @@ -0,0 +1,154 @@ +package net.i2p.router.web; + +import java.util.HashMap; +import java.util.Map; +import net.i2p.data.Hash; +import net.i2p.data.DataFormatException; +import net.i2p.util.Log; +import net.i2p.router.TunnelPoolSettings; + +/** + * Handler to deal with form submissions from the tunnel config form and act + * upon the values. Holy crap, this is UUUUGLY + * + */ +public class ConfigTunnelsHandler extends FormHandler { + private Log _log; + private Map _settings; + private boolean _shouldSave; + + public ConfigTunnelsHandler() { + _shouldSave = false; + } + + protected void processForm() { + if (_shouldSave) { + saveChanges(); + } else { + // noop + } + } + + public void setShouldsave(String moo) { + if ( (moo != null) && (moo.equals("Save changes")) ) + _shouldSave = true; + } + + public void setSettings(Map settings) { _settings = new HashMap(settings); } + + /** + * The user made changes to the network config and wants to save them, so + * lets go ahead and do so. + * + */ + private void saveChanges() { + _log = _context.logManager().getLog(ConfigTunnelsHandler.class); + boolean saveRequired = false; + + if (_log.shouldLog(Log.DEBUG)) + _log.debug("Saving changes, with props = " + _settings); + + int updated = 0; + int index = 0; + while (true) { + Object val = _settings.get("pool." + index); + if (val == null) break; + Hash client = new Hash(); + + String poolName = (val instanceof String ? (String)val : ((String[])val)[0]); + + TunnelPoolSettings in = null; + TunnelPoolSettings out = null; + if ("exploratory".equals(poolName)) { + in = _context.tunnelManager().getInboundSettings(); + out = _context.tunnelManager().getOutboundSettings(); + } else { + try { + client.fromBase64(poolName); + } catch (DataFormatException dfe) { + addFormError("Internal error (pool name could not resolve - " + poolName + ")"); + index++; + continue; + } + in = _context.tunnelManager().getInboundSettings(client); + out = _context.tunnelManager().getOutboundSettings(client); + } + + if ( (in == null) || (out == null) ) { + addFormError("Internal error (pool settings cound not be fuond for " + poolName + ")"); + index++; + continue; + } + + in.setLength(getInt(_settings.get(index + ".depthInbound"))); + out.setLength(getInt(_settings.get(index + ".depthOutbound"))); + in.setLengthVariance(getInt(_settings.get(index + ".varianceInbound"))); + out.setLengthVariance(getInt(_settings.get(index + ".varianceOutbound"))); + in.setQuantity(getInt(_settings.get(index + ".quantityInbound"))); + out.setQuantity(getInt(_settings.get(index + ".quantityOutbound"))); + in.setBackupQuantity(getInt(_settings.get(index + ".backupInbound"))); + out.setBackupQuantity(getInt(_settings.get(index + ".backupOutbound"))); + + if ("exploratory".equals(poolName)) { + _context.router().setConfigSetting(TunnelPoolSettings.PREFIX_INBOUND_EXPLORATORY + + TunnelPoolSettings.PROP_LENGTH, in.getLength()+""); + _context.router().setConfigSetting(TunnelPoolSettings.PREFIX_OUTBOUND_EXPLORATORY + + TunnelPoolSettings.PROP_LENGTH, out.getLength()+""); + _context.router().setConfigSetting(TunnelPoolSettings.PREFIX_INBOUND_EXPLORATORY + + TunnelPoolSettings.PROP_LENGTH_VARIANCE, in.getLengthVariance()+""); + _context.router().setConfigSetting(TunnelPoolSettings.PREFIX_OUTBOUND_EXPLORATORY + + TunnelPoolSettings.PROP_LENGTH_VARIANCE, out.getLengthVariance()+""); + _context.router().setConfigSetting(TunnelPoolSettings.PREFIX_INBOUND_EXPLORATORY + + TunnelPoolSettings.PROP_QUANTITY, in.getQuantity()+""); + _context.router().setConfigSetting(TunnelPoolSettings.PREFIX_OUTBOUND_EXPLORATORY + + TunnelPoolSettings.PROP_QUANTITY, out.getQuantity()+""); + _context.router().setConfigSetting(TunnelPoolSettings.PREFIX_INBOUND_EXPLORATORY + + TunnelPoolSettings.PROP_BACKUP_QUANTITY, in.getBackupQuantity()+""); + _context.router().setConfigSetting(TunnelPoolSettings.PREFIX_OUTBOUND_EXPLORATORY + + TunnelPoolSettings.PROP_BACKUP_QUANTITY, out.getBackupQuantity()+""); + } + + if ("exploratory".equals(poolName)) { + if (_log.shouldLog(Log.DEBUG)) { + _log.debug("Inbound exploratory settings: " + in); + _log.debug("Outbound exploratory settings: " + out); + } + _context.tunnelManager().setInboundSettings(in); + _context.tunnelManager().setOutboundSettings(out); + } else { + if (_log.shouldLog(Log.DEBUG)) { + _log.debug("Inbound settings for " + client.toBase64() + ": " + in); + _log.debug("Outbound settings for " + client.toBase64() + ": " + out); + } + _context.tunnelManager().setInboundSettings(client, in); + _context.tunnelManager().setOutboundSettings(client, out); + } + + updated++; + saveRequired = true; + index++; + } + + if (updated > 0) + addFormNotice("Updated settings for " + updated + " pools"); + + if (saveRequired) { + boolean saved = _context.router().saveConfig(); + if (saved) + addFormNotice("Configuration saved successfully"); + else + addFormNotice("Error saving the configuration (applied but not saved) - please see the error logs"); + } + } + private static final int getInt(Object val) { + if (val == null) return 0; + String str = null; + if (val instanceof String) + str = (String)val; + else + str = ((String[])val)[0]; + + if (str.trim().length() <= 0) return 0; + try { return Integer.parseInt(str); } catch (NumberFormatException nfe) { return 0; } + } +} diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigTunnelsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigTunnelsHelper.java new file mode 100644 index 000000000..67e817b31 --- /dev/null +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigTunnelsHelper.java @@ -0,0 +1,251 @@ +package net.i2p.router.web; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.List; +import java.util.Iterator; +import java.util.Set; +import java.util.Properties; +import java.util.TreeMap; + +import net.i2p.util.Log; + +import net.i2p.data.Destination; +import net.i2p.router.RouterContext; +import net.i2p.router.TunnelPoolSettings; + +public class ConfigTunnelsHelper { + private RouterContext _context; + /** + * Configure this bean to query a particular router context + * + * @param contextId begging few characters of the routerHash, or null to pick + * the first one we come across. + */ + public void setContextId(String contextId) { + try { + _context = ContextHelper.getContext(contextId); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + public ConfigTunnelsHelper() {} + + + public String getForm() { + StringBuffer buf = new StringBuffer(1024); + buf.append("