Пример #1
0
 /** Destroy the destination itself */
 public synchronized void disconnect() {
   if (_dht != null) {
     _dht.stop();
     _dht = null;
   }
   I2PSocketManager mgr = _manager;
   // FIXME this can cause race NPEs elsewhere
   _manager = null;
   _banlist.clear();
   if (mgr != null) {
     if (_log.shouldLog(Log.DEBUG))
       _log.debug("Disconnecting from I2P", new Exception("I did it"));
     mgr.destroySocketManager();
   }
   // this will delete a .torrent file d/l in progress so don't do that...
   FileUtil.rmdir(_tmpDir, false);
   // in case the user will d/l a .torrent file next...
   _tmpDir.mkdirs();
 }
Пример #2
0
 /** @since DHT */
 public synchronized void setUseDHT(boolean yes) {
   _shouldUseDHT = yes;
   if (yes && _manager != null && _dht == null) {
     _dht = new KRPC(_context, _baseName, _manager.getSession(), _myNodeInfo, _customQueryHandler);
     if (_dhtInitCallback != null) {
       _dhtInitCallback.run();
     }
   } else if (!yes && _dht != null) {
     _dht.stop();
     _dht = null;
   }
 }
Пример #3
0
 /**
  * This updates ALL the session options (not just the bw) and tells the router
  *
  * @param limit KBps
  */
 public void setMaxUpBW(int limit) {
   _maxUpBW = limit;
   _opts.put(PROP_MAX_BW, Integer.toString(limit * (1024 * 6 / 5))); // add a little for overhead
   _configured = true;
   if (_manager != null) {
     I2PSession sess = _manager.getSession();
     if (sess != null) {
       Properties newProps = new Properties();
       newProps.putAll(_opts);
       sess.updateOptions(newProps);
     }
   }
 }
Пример #4
0
  @Override
  public File savePrivateDestinationInfo() {
    try {
      File f = File.createTempFile("dest", ".bin", i2pdir);
      PrivateKeyFile pkf = new PrivateKeyFile(f, manager.getSession());
      pkf.write();
      return f;
    } catch (Exception e) {
      log.info(e.getMessage());
    }

    return null;
  }
Пример #5
0
  @Override
  public void close() {
    try {
      server.close();
    } catch (I2PException e) {
      log.info(e.getMessage());
    }

    try {
      manager.destroySocketManager();
    } catch (Exception e) {
      log.info(e.getMessage());
    }
  }
Пример #6
0
 /** Base64 Hash or Hash.i2p or name.i2p using naming service */
 Destination getDestination(String ip) {
   if (ip == null) return null;
   if (ip.endsWith(".i2p")) {
     if (ip.length() < 520) { // key + ".i2p"
       if (_manager != null && ip.length() == BASE32_HASH_LENGTH + 8 && ip.endsWith(".b32.i2p")) {
         // Use existing I2PSession for b32 lookups if we have it
         // This is much more efficient than using the naming service
         I2PSession sess = _manager.getSession();
         if (sess != null) {
           byte[] b = Base32.decode(ip.substring(0, BASE32_HASH_LENGTH));
           if (b != null) {
             // Hash h = new Hash(b);
             Hash h = Hash.create(b);
             if (_log.shouldLog(Log.INFO)) _log.info("Using existing session for lookup of " + ip);
             try {
               return sess.lookupDest(h, 15 * 1000);
             } catch (I2PSessionException ise) {
             }
           }
         }
       }
       if (_log.shouldLog(Log.INFO)) _log.info("Using naming service for lookup of " + ip);
       return _context.namingService().lookup(ip);
     }
     if (_log.shouldLog(Log.INFO)) _log.info("Creating Destination for " + ip);
     try {
       return new Destination(ip.substring(0, ip.length() - 4)); // sans .i2p
     } catch (DataFormatException dfe) {
       return null;
     }
   } else {
     if (_log.shouldLog(Log.INFO)) _log.info("Creating Destination for " + ip);
     try {
       return new Destination(ip);
     } catch (DataFormatException dfe) {
       return null;
     }
   }
 }
Пример #7
0
 /** connect to the given destination */
 I2PSocket connect(PeerID peer) throws IOException {
   I2PSocketManager mgr = _manager;
   if (mgr == null) throw new IOException("No socket manager");
   Destination addr = peer.getAddress();
   if (addr == null) throw new IOException("Null address");
   if (addr.equals(getMyDestination())) throw new IOException("Attempt to connect to myself");
   Hash dest = addr.calculateHash();
   if (_banlist.contains(dest))
     throw new IOException("Not trying to contact " + dest.toBase64() + ", as they are banlisted");
   try {
     // TODO opts.setPort(xxx); connect(addr, opts)
     // DHT moved above 6881 in 0.9.9
     I2PSocket rv = _manager.connect(addr);
     if (rv != null) _banlist.remove(dest);
     return rv;
   } catch (I2PException ie) {
     _banlist.add(dest);
     _context.simpleScheduler().addEvent(new Unbanlist(dest), 10 * 60 * 1000);
     IOException ioe = new IOException("Unable to reach the peer " + peer);
     ioe.initCause(ie);
     throw ioe;
   }
 }
Пример #8
0
 /**
  * @return dest or null
  * @since 0.8.4
  */
 Destination getMyDestination() {
   if (_manager == null) return null;
   I2PSession sess = _manager.getSession();
   if (sess != null) return sess.getMyDestination();
   return null;
 }
Пример #9
0
 public I2PServerSocket getServerSocket() {
   I2PSocketManager mgr = _manager;
   if (mgr != null) return mgr.getServerSocket();
   else return null;
 }
Пример #10
0
 /** Connect to the router, if we aren't already */
 public synchronized boolean connect() {
   if (_manager == null) {
     _connecting = true;
     // try to find why reconnecting after stop
     if (_log.shouldLog(Log.DEBUG)) _log.debug("Connecting to I2P", new Exception("I did it"));
     Properties opts = _context.getProperties();
     if (_opts != null) {
       for (Map.Entry<String, String> entry : _opts.entrySet())
         opts.setProperty(entry.getKey(), entry.getValue());
     }
     if (opts.getProperty("inbound.nickname") == null)
       opts.setProperty("inbound.nickname", _baseName.replace("i2psnark", "I2PSnark"));
     if (opts.getProperty("outbound.nickname") == null)
       opts.setProperty("outbound.nickname", _baseName.replace("i2psnark", "I2PSnark"));
     if (opts.getProperty("outbound.priority") == null)
       opts.setProperty("outbound.priority", "-10");
     // Dont do this for now, it is set in I2PSocketEepGet for announces,
     // we don't need fast handshake for peer connections.
     // if (opts.getProperty("i2p.streaming.connectDelay") == null)
     //    opts.setProperty("i2p.streaming.connectDelay", "500");
     if (opts.getProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT) == null)
       opts.setProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT, "75000");
     if (opts.getProperty("i2p.streaming.inactivityTimeout") == null)
       opts.setProperty("i2p.streaming.inactivityTimeout", "240000");
     if (opts.getProperty("i2p.streaming.inactivityAction") == null)
       opts.setProperty("i2p.streaming.inactivityAction", "1"); // 1 == disconnect, 2 == ping
     if (opts.getProperty("i2p.streaming.initialWindowSize") == null)
       opts.setProperty("i2p.streaming.initialWindowSize", "1");
     if (opts.getProperty("i2p.streaming.slowStartGrowthRateFactor") == null)
       opts.setProperty("i2p.streaming.slowStartGrowthRateFactor", "1");
     // if (opts.getProperty("i2p.streaming.writeTimeout") == null)
     //    opts.setProperty("i2p.streaming.writeTimeout", "90000");
     // if (opts.getProperty("i2p.streaming.readTimeout") == null)
     //    opts.setProperty("i2p.streaming.readTimeout", "120000");
     if (opts.getProperty("i2p.streaming.maxConnsPerMinute") == null)
       opts.setProperty("i2p.streaming.maxConnsPerMinute", "2");
     if (opts.getProperty("i2p.streaming.maxTotalConnsPerMinute") == null)
       opts.setProperty("i2p.streaming.maxTotalConnsPerMinute", "8");
     if (opts.getProperty("i2p.streaming.maxConnsPerHour") == null)
       opts.setProperty("i2p.streaming.maxConnsPerHour", "20");
     if (opts.getProperty("i2p.streaming.enforceProtocol") == null)
       opts.setProperty("i2p.streaming.enforceProtocol", "true");
     if (opts.getProperty("i2p.streaming.disableRejectLogging") == null)
       opts.setProperty("i2p.streaming.disableRejectLogging", "true");
     if (opts.getProperty("i2p.streaming.answerPings") == null)
       opts.setProperty("i2p.streaming.answerPings", "false");
     if (_myPrivateKeyStream != null) {
       _manager =
           I2PSocketManagerFactory.createManager(_myPrivateKeyStream, _i2cpHost, _i2cpPort, opts);
     } else {
       _manager = I2PSocketManagerFactory.createManager(_i2cpHost, _i2cpPort, opts);
     }
     _connecting = false;
   }
   if (_shouldUseDHT && _manager != null && _dht == null) {
     _dht = new KRPC(_context, _baseName, _manager.getSession(), _myNodeInfo, _customQueryHandler);
     if (_dhtInitCallback != null) {
       _dhtInitCallback.run();
     }
   }
   return (_manager != null);
 }
Пример #11
0
 @Override
 public String getPublicDestinationInfo() {
   I2PSession ses = manager.getSession();
   return ses.getMyDestination().toBase64();
 }
Пример #12
0
 public I2PDestination(File dir, I2PSocketManager mngr) {
   manager = mngr;
   i2pdir = dir;
   server = manager.getServerSocket();
 }