Exemple #1
0
  /**
   * initializes a new hidden service
   *
   * @param service all data needed to init the thingy
   */
  public String setupHiddenService(HiddenServiceProperties service)
      throws IOException, TorException {

    if (service.handler == null)
      throw new TorException("Tor.provideHiddenService: need a handler for incoming connections");

    Logger.logHiddenService(
        Logger.INFO, "Tor.provideHiddenService: setting up hidden service " + service.getName());
    // check, if tor is still in startup-phase
    checkStartup();

    // Store the service
    String url = service.getName();
    hiddenServices.put(url, service);

    // Update hidden service status
    service.setStatus(HiddenServiceProperties.STATUS_STARTED, true);
    // establish choosen introduction points
    Iterator<IntroductionPoint> i = service.introPoints.iterator();
    while (i.hasNext()) {
      IntroductionPoint ip = i.next();
      String ip_name = ip.getSrv().nickname;
      Logger.logHiddenService(
          Logger.INFO, "Tor.provideHiddenService: establish introduction point at " + ip_name);
      // create circuit properties to end the circuit at the
      // intro-point
      TCPStreamProperties sp_intro = new TCPStreamProperties();
      sp_intro.setCustomExitpoint(ip_name);
      establishIntroductionPoint(service, sp_intro);
    }
    // choose additional random introduction points, if none given
    while (service.introPoints.size() < service.minimum_number_of_intro_points) {
      TCPStreamProperties sp_intro = new TCPStreamProperties();
      sp_intro.connect_retries = 1;
      Circuit c = establishIntroductionPoint(service, sp_intro);
      if (c != null) {
        String ip_name = c.route[c.route_established - 1].server.nickname;
        Logger.logHiddenService(
            Logger.INFO, "Tor.provideHiddenService: establish introduction point at " + ip_name);
        service.addIntroPoint(ip_name, dir);
      }
      ;
    }
    // Update hidden service status
    service.setStatus(HiddenServiceProperties.STATUS_INTROPOINTS, true);
    // advertise introduction points
    int advertise_success = 0;
    Iterator<String> i2 = config.trustedServers.keySet().iterator();
    while (i2.hasNext()) {
      String nick = i2.next();
      HashMap<String, Object> trustedServer = config.trustedServers.get(nick);
      String address = (String) trustedServer.get("ip");
      int port = ((Integer) trustedServer.get("port")).intValue();
      int tries = 2;
      while (tries > 0) {
        Logger.logHiddenService(
            Logger.VERBOSE,
            "Tor.provideHiddenService: advertise service at "
                + nick
                + " ("
                + address
                + ":"
                + port
                + ")");
        TCPStreamProperties sp = new TCPStreamProperties(address, port);
        TCPStream stream = null;
        // advertise data for hidden service's rendevouz anonymously
        try {
          stream = connect(sp);
          // advertise service-descriptor with HTTP-POST
          byte[] body = service.sd_v0.toByteArray();
          String headerStr =
              "POST /tor/rendezvous/publish HTTP/1.0\r\n"
                  + "Content-Length: "
                  + body.length
                  + "\r\n"
                  + "\r\n";
          byte[] header = headerStr.getBytes();
          byte[] out = new byte[header.length + body.length];
          System.arraycopy(header, 0, out, 0, header.length);
          System.arraycopy(body, 0, out, header.length, body.length);
          String answer =
              StreamsAndHTTP.HTTPBinaryRequest(
                  stream.getOutputStream(), stream.getInputStream(), out);
          // analyse answer
          if (!answer.startsWith("HTTP/1.0 200 "))
            throw new TorException(
                "Tor.provideHiddenService: no success posting service descriptor " + answer);
          Logger.logHiddenService(Logger.INFO, "Successfully advertised at " + nick);
          stream.close();
          ++advertise_success;
          if (advertise_success > 1) {
            // Update hidden service status
            service.setStatus(HiddenServiceProperties.STATUS_ADVERTISED, true);
          }
          break;
        } catch (Exception e) {
          Logger.logHiddenService(
              Logger.WARNING,
              "Tor.provideHiddenService: error advertising service at "
                  + nick
                  + " ("
                  + address
                  + ":"
                  + port
                  + ")\n"
                  + e.getMessage());
        }
        if (--tries <= 0)
          Logger.logHiddenService(
              Logger.WARNING,
              "Tor.provideHiddenService: final error advertising service at "
                  + nick
                  + " ("
                  + address
                  + ":"
                  + port
                  + ")");
      }
    }
    // at least one advertisement?
    if (advertise_success < 1)
      throw new TorException("Tor.provideHiddenService: no successful advertisement");
    // success
    return url;
  }