示例#1
0
 /**
  * shut down everything
  *
  * @param force set to true, if everything shall go fast. For graceful end, set to false
  */
 public void close(boolean force) {
   Logger.logGeneral(Logger.INFO, "TorJava ist closing down");
   // shutdown mgmt
   mgmt.close();
   // shut down connections
   fnh.close(force);
   // shutdown/save directory
   dir.close(TorConfig.getCacheFilename());
   // write config file
   config.close();
   // close hidden services
   // TODO close hidden services, once they are implemented and work
   // kill logger
   Logger.logGeneral(Logger.INFO, "Tor.close(): CLOSED");
   log.close();
 }
示例#2
0
 void fireEvent(TorEvent event) {
   for (int i = 0; i < eventHandler.size(); ++i) {
     try {
       TorEventHandler eh = (TorEventHandler) eventHandler.elementAt(i);
       eh.ping(event);
     } catch (Exception e) {
       Logger.logGeneral(Logger.INFO, "Tor.fireEvent(): " + e.getMessage());
     }
   }
 }
示例#3
0
  private void init_phase1(boolean noLocalFileSystemAccess, boolean fastStartup)
      throws IOException {
    // install BC, if not already done
    if (Security.getProvider("BC") == null) {
      Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
      // Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(),2);
    }

    // logger and config
    Logger.logGeneral(
        Logger.INFO,
        "TorJava is starting up. Please don't use this implementation for strong anonymity!");
    // create identity
    privateKeyHandler = new PrivateKeyHandler(fastStartup);
    // determine end of startup-Phase
    startupPhaseWithoutConnects =
        System.currentTimeMillis() + TorConfig.startupDelaySeconds * 1000L;
    // init event-handler
    eventHandler = new Vector<TorEventHandler>();
  }
示例#4
0
 /** make sure that tor had some time to read the directory and build up soime circuits */
 private void checkStartup() {
   // start up is prooven to be over
   if (!startUp) return;
   // check if startup is over
   long now = System.currentTimeMillis();
   if (now >= startupPhaseWithoutConnects) {
     startUp = false;
     return;
   }
   // wait for startup to be over
   long sleep = startupPhaseWithoutConnects - System.currentTimeMillis();
   if (!gaveMessage) {
     gaveMessage = true;
     Logger.logGeneral(
         Logger.VERBOSE,
         "Tor.checkStartup(): TorJava is still in startup phase, sleeping for "
             + (sleep / 1000L)
             + " seconds");
   }
   try {
     Thread.sleep(sleep);
   } catch (Exception e) {
   }
 }
示例#5
0
  /**
   * makes a connection to a hidden service
   *
   * @param sp hostname, port to connect to and other stuff
   * @return some socket-thing
   */
  private TCPStream connectToHidden(TCPStreamProperties spo) throws IOException {
    // check, if tor is still in startup-phase
    checkStartup();
    Circuit myRendezvousCirc = null;
    Server hiddenServer;
    Node hiddenNode = null;

    // String address, x, y;
    String z;
    // Iterator it, i, i2;
    byte[] cookie = new byte[20];
    // boolean notFound;
    int j;

    z = (String) Encoding.parseHiddenAddress(spo.hostname).get("z");

    // Do we already have a connection to this address?
    if (hiddenServiceCircuits.containsKey(z)) {
      // TODO assess suitability of this circuit
      System.out.println("Reusing existing circuit");
      TCPStreamProperties tcpProps = new TCPStreamProperties("", spo.port);
      try {
        return new TCPStream(hiddenServiceCircuits.get(z), tcpProps);
      } catch (TorNoAnswerException e) {
        // Create a new circuit instead
        e.printStackTrace();
      } catch (TorException e) {
        // Create a new circuit instead
        e.printStackTrace();
      }
    }

    // get a copy from the service descriptor (either local cache or
    // retrieve form network)
    ServiceDescriptor sd = (ServiceDescriptor) cachedServiceDescriptors.get(z);
    if (sd == null || (!sd.checkTimeStampValidity())) {
      sd = ServiceDescriptor.loadFromDirectory(z, this);
      cachedServiceDescriptors.put(z, sd); // cache it
    }

    boolean establishedRendezvous = false;
    j = 0; // attempts counted
    // spo.connect_retries try to establish rendezvous
    while ((j < spo.connect_retries) && (!establishedRendezvous)) {
      j++;
      try {
        myRendezvousCirc = fnh.provideSuitableNewCircuit(new TCPStreamProperties());
        String rendezvousName =
            myRendezvousCirc.route[myRendezvousCirc.route_established - 1].server.nickname;

        Logger.logGeneral(
            Logger.INFO,
            "Tor.connectToHidden: establishing rendezvous point for "
                + z
                + " at "
                + rendezvousName);
        Random rnd = new Random();
        rnd.nextBytes(cookie);

        myRendezvousCirc.send_cell(new CellRelayEstablishRendezvous(myRendezvousCirc, cookie));
        myRendezvousCirc.streamHistory.add(spo.hostname);

        // wait for answer
        CellRelay rendezvousACK =
            myRendezvousCirc.queue.receiveRelayCell(CellRelay.CELL_RELAY_RENDEZVOUS_ESTABLISHED);
        if (rendezvousACK.length > 0) {
          throw new TorException("Tor.connectToHidden: Got NACK from RENDEZVOUS Point");
        }
        myRendezvousCirc.sd = sd;

        hiddenServer = new Server(this, sd.publicKey);
        hiddenNode = new Node(hiddenServer); // between HS and
        // Rendezvous point

        establishedRendezvous = true;

      } catch (IOException e) {
        e.printStackTrace();
      } catch (TorException e) {
        e.printStackTrace();
      }
    }

    if (!establishedRendezvous) {
      Logger.logGeneral(
          Logger.WARNING, "Tor.connectToHidden: coudn't establishing rendezvous point for " + z);
      throw new IOException("Tor.connectToHidden: coudn't establishing rendezvous point for " + z);
    }

    Iterator<IntroductionPoint> it3 = sd.introPoints.iterator();
    String rendezvousName =
        myRendezvousCirc.route[myRendezvousCirc.route.length - 1].server.nickname;

    while (it3.hasNext()) {
      IntroductionPoint iPoint = (IntroductionPoint) it3.next();
      Logger.logGeneral(
          Logger.INFO,
          "Tor.connectToHidden: contacting introduction point "
              + iPoint.getNickname()
              + " for "
              + z);

      // introduce rendezvous to the node
      TCPStreamProperties spIntro = new TCPStreamProperties();

      spIntro.exitPolicyRequired = false;
      //            Server sr = iPoint.getSrv();
      spIntro.setCustomExitpoint(iPoint.getNickname());

      try {
        // make new circ where the last node is intro point
        Circuit myIntroCirc = new Circuit(this, fnh, dir, spIntro);

        // System.out.println(" LAST NODE IS... " +
        // myIntroCirc.route[myIntroCirc.route.length-1].server.name);

        // and CellIntro1 data encrypted with PK of Hidden Service, and _not_ of the introPoint
        myIntroCirc.send_cell(
            new CellRelayIntroduce1(myIntroCirc, cookie, sd, rendezvousName, hiddenNode));

        // wait for ack
        CellRelay introACK =
            myIntroCirc.queue.receiveRelayCell(CellRelay.CELL_RELAY_COMMAND_INTRODUCE_ACK);
        if (introACK.length > 0)
          throw new TorException("Tor.connectToHidden: Got NACK from Introduction Point");
        // introduce ACK is received
        Logger.logGeneral(Logger.RAW_DATA, "Tor.connectToHidden: Got ACK from Intro Point");

        myIntroCirc.close(true);

        // wait for answer from the hidden service (RENDEZVOUS2)
        int oldTimeout = myRendezvousCirc.queue.timeout;
        if (oldTimeout < 120 * 1000) myRendezvousCirc.queue.timeout = 120 * 1000;
        CellRelay r2Relay =
            myRendezvousCirc.queue.receiveRelayCell(CellRelay.CELL_RELAY_RENDEZVOUS2);
        myRendezvousCirc.queue.timeout = oldTimeout;
        // finish diffie-hellman
        byte[] dh_gy = new byte[148];
        System.arraycopy(r2Relay.data, 0, dh_gy, 0, 148);
        hiddenNode.finish_dh(dh_gy);

        myRendezvousCirc.addNode(hiddenNode);

        Logger.logGeneral(
            Logger.INFO, "Tor.connectToHidden: succesfully established rendezvous with " + z);
        // address in begin cell set to "";
        TCPStreamProperties tcpProps = new TCPStreamProperties("", spo.port);

        if (hiddenServiceCircuits.containsKey(z)) hiddenServiceCircuits.remove(z);
        hiddenServiceCircuits.put(z, myRendezvousCirc);

        // connect
        return new TCPStream(myRendezvousCirc, tcpProps);
      } catch (TorException e) {
        e.printStackTrace();
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // all intros failed...
    // perhaps there is something wrong with out cached service descriptor
    cachedServiceDescriptors.remove(z);
    throw new IOException("Tor.connectToHidden: couldn't connect to an introduction point of " + z);
  }