/** @since 0.8.3 */ public boolean isRouterContext() { return _context.isRouterContext(); }
private int getStartupDelayMinutes() { if (!_context.isRouterContext()) return 0; try { return Integer.parseInt(_config.getProperty(PROP_STARTUP_DELAY)); } catch (NumberFormatException nfe) { return DEFAULT_STARTUP_DELAY; } }
/** * Save memory by not creating stats unless they are required for router operation. * For backward compatibility of any external clients, always returns false if not in router context. * * @param statName ignored * @return true if the stat should be ignored. */ public boolean ignoreStat(String statName) { return _context.isRouterContext() && !_context.getBooleanProperty(PROP_STAT_FULL); }
/** * Get I2CP host from the config * @since 0.9.7 was in loadConfig() */ private String getHost() { if (_context.isRouterContext()) // just for logging return "[internal connection]"; else if (SystemVersion.isAndroid() && Boolean.parseBoolean(_options.getProperty(PROP_DOMAIN_SOCKET))) // just for logging return "[Domain socket connection]"; return _options.getProperty(I2PClient.PROP_TCP_HOST, "127.0.0.1"); }
/** * Convenience method to return either a Socket or an InternalSocket * @param port > 0 */ public static Socket getSocket(String host, int port) throws IOException { if (I2PAppContext.getGlobalContext().isRouterContext() && (host.equals("127.0.0.1") || host.equals("localhost"))) { try { return new InternalSocket(port); } catch (IOException ioe) {} // guess it wasn't really internal... } return new Socket(host, port); }
private static void log(I2PAppContext ctx, int level, String msg, Throwable t) { if (level >= Log.WARN && !ctx.isRouterContext()) { System.out.println(msg); if (t != null) t.printStackTrace(); } Log l = ctx.logManager().getLog(KeyStoreUtil.class); l.log(level, msg, t); }
/** * Get all themes * @return String[] -- Array of all the themes found, non-null, unsorted */ public String[] getThemes() { String[] themes; if (_context.isRouterContext()) { File dir = new File(_context.getBaseDir(), "docs/themes/snark"); FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { return file.isDirectory(); } }; File[] dirnames = dir.listFiles(fileFilter); if (dirnames != null) { List<String> th = new ArrayList<String>(dirnames.length); boolean skipOld = _context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD); for (int i = 0; i < dirnames.length; i++) { String name = dirnames[i].getName(); if (skipOld && (name.equals("midnight") || name.equals("classic"))) continue; th.add(name); } themes = th.toArray(new String[th.size()]); } else { themes = new String[0]; } } else { themes = new String[] { "dark", "light", "ubergine", "vanilla" }; } return themes; }
public String getI2CPPort(int tunnel) { if (_context.isRouterContext()) return _t("internal"); TunnelController tun = getController(tunnel); if (tun != null) return tun.getI2CPPort(); else return Integer.toString(I2PClient.DEFAULT_LISTEN_PORT); }
/** * Parse the config for anything we know about. * Also fill in the authorization properties if missing. */ private final Properties loadConfig(Properties opts) { Properties options = new Properties(); options.putAll(filter(opts)); // auto-add auth if required, not set in the options, and we are not in the same JVM if ((!_context.isRouterContext()) && _context.getBooleanProperty("i2cp.auth") && ((!opts.containsKey(PROP_USER)) || (!opts.containsKey(PROP_PW)))) { String configUser = _context.getProperty(PROP_USER); String configPW = _context.getProperty(PROP_PW); if (configUser != null && configPW != null) { options.setProperty(PROP_USER, configUser); options.setProperty(PROP_PW, configPW); } } if (options.getProperty(I2PClient.PROP_FAST_RECEIVE) == null) options.setProperty(I2PClient.PROP_FAST_RECEIVE, "true"); if (options.getProperty(I2PClient.PROP_RELIABILITY) == null) options.setProperty(I2PClient.PROP_RELIABILITY, "none"); return options; }
/** * Get I2CP port from the config * @since 0.9.7 was in loadConfig() */ private int getPort() { if (_context.isRouterContext() || (SystemVersion.isAndroid() && Boolean.parseBoolean(_options.getProperty(PROP_DOMAIN_SOCKET)))) // just for logging return 0; String portNum = _options.getProperty(I2PClient.PROP_TCP_PORT, LISTEN_PORT + ""); try { return Integer.parseInt(portNum); } catch (NumberFormatException nfe) { if (_log.shouldLog(Log.WARN)) _log.warn(getPrefix() + "Invalid port number specified, defaulting to " + LISTEN_PORT, nfe); return LISTEN_PORT; } }
public String getI2CPHost(int tunnel) { if (_context.isRouterContext()) return _t("internal"); TunnelController tun = getController(tunnel); if (tun != null) return tun.getI2CPHost(); else return "127.0.0.1"; }
/** * @param name b64 or b32 or host name * @since 0.9.11 */ private Destination lookup(String name) { I2PAppContext ctx = I2PAppContext.getGlobalContext(); boolean b32 = name.length() == 60 && name.toLowerCase(Locale.US).endsWith(".b32.i2p"); if (ctx.isRouterContext() && !b32) { // Local lookup. // Even though we could do b32 outside router ctx here, // we do it below instead so we can use the session, // which we can't do with lookup() Destination dest = ctx.namingService().lookup(name); if (dest != null || ctx.isRouterContext() || name.length() >= 516) return dest; } try { I2PSession sess = sockMgr.getSession(); return sess.lookupDest(name); } catch (I2PSessionException ise) { _log.error("Error looking up " + name, ise); return null; } } }
/** @param routerVersion as rcvd in the SetDateMessage, may be null for very old routers */ void dateUpdated(String routerVersion) { boolean isrc = _context.isRouterContext(); _routerSupportsFastReceive = isrc || (routerVersion != null && routerVersion.length() > 0 && VersionComparator.comp(routerVersion, MIN_FAST_VERSION) >= 0); _routerSupportsHostLookup = isrc || TEST_LOOKUP || (routerVersion != null && routerVersion.length() > 0 && VersionComparator.comp(routerVersion, MIN_HOST_LOOKUP_VERSION) >= 0); _routerSupportsSubsessions = isrc || (routerVersion != null && routerVersion.length() > 0 && VersionComparator.comp(routerVersion, MIN_SUBSESSION_VERSION) >= 0); _routerSupportsLS2 = isrc || (routerVersion != null && routerVersion.length() > 0 && VersionComparator.comp(routerVersion, MIN_LS2_VERSION) >= 0); synchronized (_stateLock) { if (_state == State.OPENING) { changeState(State.GOTDATE); } } }
/** * Start running the I2PTunnelServer. * Warning, blocks while connecting to router and building tunnels; * * @throws IllegalArgumentException if the I2CP configuration is b0rked so * badly that we cant create a socketManager */ public synchronized void startRunning() { connectManager(); // prevent JVM exit when running outside the router boolean isDaemon = getTunnel().getContext().isRouterContext(); Thread t = new I2PAppThread(this, "Server " + remoteHost + ':' + remotePort, isDaemon); t.start(); }
/** * For webapp. * @param ctxPath generally "/i2psnark" * @param ctxName generally "i2psnark" * @since 0.9.6 */ public SnarkManager(I2PAppContext ctx, String ctxPath, String ctxName) { _snarks = new ConcurrentHashMap<String, Snark>(); _magnets = new ConcurrentHashSet<String>(); _addSnarkLock = new Object(); _context = ctx; _contextPath = ctxPath; _contextName = ctxName; _log = _context.logManager().getLog(SnarkManager.class); _messages = new UIMessages(MAX_MESSAGES); _util = new I2PSnarkUtil(_context, ctxName); String cfile = ctxName + CONFIG_FILE_SUFFIX; File configFile = new File(cfile); if (!configFile.isAbsolute()) configFile = new File(_context.getConfigDir(), cfile); _configDir = migrateConfig(configFile); _configFile = new File(_configDir, CONFIG_FILE); _trackerMap = new ConcurrentHashMap<String, Tracker>(4); loadConfig(null); if (!ctx.isRouterContext()) Runtime.getRuntime().addShutdownHook(new Thread(new TempDeleter(_util.getTempDir()), "Snark Temp Dir Deleter")); }
/** * @throws IllegalStateException if not in RouterContext */ public boolean validateRoutingKey() { I2PAppContext ctx = I2PAppContext.getGlobalContext(); if (!ctx.isRouterContext()) throw new IllegalStateException("Not in router context"); RoutingKeyGenerator gen = ctx.routingKeyGenerator(); Hash destKey = getHash(); Hash rk = gen.getRoutingKey(destKey); return rk.equals(getRoutingKey()); }
/** * Get the routing key for the structure using the current modifier in the RoutingKeyGenerator. * This only calculates a new one when necessary though (if the generator's key modifier changes) * * @throws IllegalStateException if not in RouterContext */ public Hash getRoutingKey() { I2PAppContext ctx = I2PAppContext.getGlobalContext(); if (!ctx.isRouterContext()) throw new IllegalStateException("Not in router context"); RoutingKeyGenerator gen = ctx.routingKeyGenerator(); long mod = gen.getLastChanged(); if (mod != _routingKeyGenMod) { _currentRoutingKey = gen.getRoutingKey(getHash()); _routingKeyGenMod = mod; } return _currentRoutingKey; }
/** * ClientApp interface * @throws IllegalArgumentException if unable to load config from file * @since 0.9.4 */ public void startup() { try { loadControllers(_configFile); } catch (IllegalArgumentException iae) { if (DEFAULT_CONFIG_FILE.equals(_configFile) && !_context.isRouterContext()) { // for i2ptunnel command line synchronized (this) { _controllersLoaded = true; } _log.logAlways(Log.WARN, "Not in router context and no preconfigured tunnels"); } else { throw iae; } } startControllers(); if (_mgr != null) _mgr.register(this); // RouterAppManager registers its own shutdown hook else _context.addShutdownTask(new Shutdown()); }
/** * In I2PAppContext will instantiate if necessary and always return non-null. * As of 0.9.4, when in RouterContext, will return null (except in Android) * if the TCG has not yet been started by the router. * * @throws IllegalArgumentException if unable to load from i2ptunnel.config */ public static TunnelControllerGroup getInstance() { synchronized (TunnelControllerGroup.class) { if (_instance == null) { I2PAppContext ctx = I2PAppContext.getGlobalContext(); if (SystemVersion.isAndroid() || !ctx.isRouterContext()) { _instance = new TunnelControllerGroup(ctx, null, null); if (!SystemVersion.isAndroid()) _instance.startup(); } // else wait for the router to start it } return _instance; } }
public void handleMessage(I2CPMessage message, I2PSessionImpl session) { if (_log.shouldLog(Log.DEBUG)) _log.debug("Handle message " + message); SetDateMessage msg = (SetDateMessage) message; // Only do this if we are NOT in the router context; // otherwise, it sets getUpdatedSuccessfully() in Clock when all // we did was get the time from ourselves. if (!_context.isRouterContext()) Clock.getInstance().setNow(msg.getDate().getTime()); // This saves the various support capabilities based on // the router's version string for future reference session.dateUpdated(msg.getVersion()); if (session.isOffline() && !session.supportsLS2()) { // TODO check other options also? see RLSMH.requiresLS2() session.propogateError("Router does not support offline keys", new Exception()); session.destroySession(false); } } }