예제 #1
0
  public ProbeRequest(SimpleFieldSet fs) throws MessageInvalidException {
    /* If not defined in the field set Identifier will be null. As adding a null value to the field set does
     * not actually add something under the key, it will also be omitted in the response messages.
     */
    this.identifier = fs.get(IDENTIFIER);

    try {
      this.type = Type.valueOf(fs.get(TYPE));

      // If HTL is not specified default to MAX_HTL.
      this.htl = fs.get(HTL) == null ? Probe.MAX_HTL : fs.getByte(HTL);

      if (this.htl < 0) {
        throw new MessageInvalidException(
            ProtocolErrorMessage.INVALID_MESSAGE, "hopsToLive cannot be negative.", null, false);
      }

    } catch (IllegalArgumentException e) {
      throw new MessageInvalidException(
          ProtocolErrorMessage.INVALID_MESSAGE,
          "Unrecognized parse probe type \"" + fs.get(TYPE) + "\": " + e,
          null,
          false);
    } catch (FSParseException e) {
      // Getting a String from a SimpleFieldSet does not throw - it can at worst return null.
      throw new MessageInvalidException(
          ProtocolErrorMessage.INVALID_MESSAGE,
          "Unable to parse hopsToLive \"" + fs.get(HTL) + "\": " + e,
          null,
          false);
    }
  }
  /**
   * Not synchronized, the involved identity might be deleted during the query - which is not really
   * a problem.
   */
  public List<WoTTrust> getReceivedTrusts(Identity trustee) throws Exception {
    List<WoTTrust> result = new ArrayList<WoTTrust>();

    SimpleFieldSet request = new SimpleFieldSet(true);
    request.putOverwrite("Message", "GetTrusters");
    request.putOverwrite("Context", "");
    request.putOverwrite("Identity", trustee.getID());
    try {
      SimpleFieldSet answer = sendFCPMessageBlocking(request, null, "Identities").params;
      for (int idx = 0; ; idx++) {
        String id = answer.get("Identity" + idx);
        if (id == null
            || id.equals("")) /* TODO: Figure out whether the second condition is necessary */
          break;
        try {
          final WoTTrust trust =
              new WoTTrust(
                  getIdentity(id),
                  trustee,
                  (byte) Integer.parseInt(answer.get("Value" + idx)),
                  answer.get("Comment" + idx));
          mWoTCache.putTrust(trust);
          result.add(trust);
        } catch (NoSuchIdentityException e) {
        } catch (InvalidParameterException e) {
        }
      }
    } catch (PluginNotFoundException e) {
      throw new WoTDisconnectedException();
    }
    return result;
  }
예제 #3
0
 /** Create a DirPutFile from a SimpleFieldSet. */
 public static DirPutFile create(
     SimpleFieldSet subset, String identifier, boolean global, BucketFactory bf)
     throws MessageInvalidException {
   String name = subset.get("Name");
   if (name == null)
     throw new MessageInvalidException(
         ProtocolErrorMessage.MISSING_FIELD, "Missing field Name", identifier, global);
   String contentTypeOverride = subset.get("Metadata.ContentType");
   if (contentTypeOverride != null
       && (!contentTypeOverride.equals(""))
       && !DefaultMIMETypes.isPlausibleMIMEType(contentTypeOverride)) {
     throw new MessageInvalidException(
         ProtocolErrorMessage.BAD_MIME_TYPE,
         "Bad MIME type in Metadata.ContentType",
         identifier,
         global);
   }
   String type = subset.get("UploadFrom");
   if ((type == null) || type.equalsIgnoreCase("direct")) {
     return DirectDirPutFile.create(name, contentTypeOverride, subset, identifier, global, bf);
   } else if (type.equalsIgnoreCase("disk")) {
     return DiskDirPutFile.create(name, contentTypeOverride, subset, identifier, global);
   } else if (type.equalsIgnoreCase("redirect")) {
     return RedirectDirPutFile.create(name, contentTypeOverride, subset, identifier, global);
   } else {
     throw new MessageInvalidException(
         ProtocolErrorMessage.INVALID_FIELD,
         "Unsupported or unknown UploadFrom: " + type,
         identifier,
         global);
   }
 }
  /**
   * Get a set of introduction puzzle IDs which the given own identity might solve.
   *
   * <p>The puzzle's data is not returned because when generating HTML for displaying the puzzles we
   * must reference them with a IMG-tag and we cannot embed the data of the puzzles in the IMG-tag
   * because embedding image-data has only recently been added to browsers and many do not support
   * it yet.
   *
   * @param ownIdentity The identity which wants to solve the puzzles.
   * @param amount The amount of puzzles to request.
   * @return A list of the IDs of the puzzles. The amount might be less than the requested amount
   *     and even zero if WoT has not downloaded puzzles yet.
   * @throws Exception
   */
  public List<String> getIntroductionPuzzles(WoTOwnIdentity ownIdentity, int amount)
      throws Exception {
    ArrayList<String> puzzleIDs = new ArrayList<String>(amount + 1);

    SimpleFieldSet params = new SimpleFieldSet(true);
    params.putOverwrite("Message", "GetIntroductionPuzzles");
    params.putOverwrite("Identity", ownIdentity.getID());
    params.putOverwrite("Type", "Captcha"); // TODO: Don't hardcode the String
    params.put("Amount", amount);

    try {
      SimpleFieldSet result = sendFCPMessageBlocking(params, null, "IntroductionPuzzles").params;

      for (int idx = 0; ; idx++) {
        String id = result.get("Puzzle" + idx);

        if (id == null
            || id.equals("")) /* TODO: Figure out whether the second condition is necessary */
          break;

        puzzleIDs.add(id);
      }
    } catch (PluginNotFoundException e) {
      Logger.error(this, "Getting puzzles failed", e);
    }

    return puzzleIDs;
  }
  /**
   * Sends a blocking FCP message to the WoT plugin, checks whether the reply is really the expected
   * reply message and throws an exception if not. Also checks whether the reply is an error message
   * and throws an exception if it is. Therefore, the purpose of this function is that the callee
   * can assume that no errors occurred if no exception is thrown.
   *
   * @param params The params of the FCP message.
   * @param expectedReplyMessage The excepted content of the "Message" field of the SimpleFieldSet
   *     of the reply message.
   * @return The unmodified HashResult object which was returned by the PluginTalker.
   * @throws WoTDisconnectedException If the connection to WoT was lost.
   * @throws Exception If the WoT plugin replied with an error message or not with the expected
   *     message.
   */
  private PluginTalkerBlocking.Result sendFCPMessageBlocking(
      SimpleFieldSet params, Bucket data, String expectedReplyMessage) throws Exception {
    if (mTalker == null) throw new WoTDisconnectedException();

    PluginTalkerBlocking.Result result;
    try {
      result = mTalker.sendBlocking(params, data);
    } catch (PluginNotFoundException e) {
      throw new WoTDisconnectedException();
    }

    if (result.params.get("Message").equals("Error")) {
      final String description = result.params.get("Description");

      if (description.indexOf("UnknownIdentityException") >= 0)
        throw new NoSuchIdentityException(description);

      throw new Exception(
          "FCP message " + result.params.get("OriginalMessage") + " failed: " + description);
    }

    if (result.params.get("Message").equals(expectedReplyMessage) == false)
      throw new Exception(
          "FCP message "
              + params.get("Message")
              + " received unexpected reply: "
              + result.params.get("Message"));

    return result;
  }
 private static FreenetURI insertData(File dataFile) throws IOException {
   long startInsertTime = System.currentTimeMillis();
   InetAddress localhost = InetAddress.getByName("127.0.0.1");
   Socket sock = new Socket(localhost, 9481);
   OutputStream sockOS = sock.getOutputStream();
   InputStream sockIS = sock.getInputStream();
   System.out.println("Connected to node.");
   LineReadingInputStream lis = new LineReadingInputStream(sockIS);
   OutputStreamWriter osw = new OutputStreamWriter(sockOS, "UTF-8");
   osw.write(
       "ClientHello\nExpectedVersion=0.7\nName=BootstrapPullTest-"
           + System.currentTimeMillis()
           + "\nEnd\n");
   osw.flush();
   String name = lis.readLine(65536, 128, true);
   SimpleFieldSet fs = new SimpleFieldSet(lis, 65536, 128, true, true, false, true);
   if (!name.equals("NodeHello")) {
     System.err.println("No NodeHello from insertor node!");
     System.exit(EXIT_INSERTER_PROBLEM);
   }
   System.out.println("Connected to " + sock);
   osw.write(
       "ClientPut\nIdentifier=test-insert\nURI=CHK@\nVerbosity=1023\nUploadFrom=direct\nMaxRetries=-1\nDataLength="
           + TEST_SIZE
           + "\nData\n");
   osw.flush();
   InputStream is = new FileInputStream(dataFile);
   FileUtil.copy(is, sockOS, TEST_SIZE);
   System.out.println("Sent data");
   while (true) {
     name = lis.readLine(65536, 128, true);
     fs = new SimpleFieldSet(lis, 65536, 128, true, true, false, true);
     System.out.println("Got FCP message: \n" + name);
     System.out.print(fs.toOrderedString());
     if (name.equals("ProtocolError")) {
       System.err.println("Protocol error when inserting data.");
       System.exit(EXIT_INSERTER_PROBLEM);
     }
     if (name.equals("PutFailed")) {
       System.err.println("Insert failed");
       System.exit(EXIT_INSERT_FAILED);
     }
     if (name.equals("PutSuccessful")) {
       long endInsertTime = System.currentTimeMillis();
       FreenetURI uri = new FreenetURI(fs.get("URI"));
       System.out.println(
           "RESULT: Insert took "
               + (endInsertTime - startInsertTime)
               + "ms ("
               + TimeUtil.formatTime(endInsertTime - startInsertTime)
               + ") to "
               + uri
               + " .");
       sockOS.close();
       sockIS.close();
       sock.close();
       return uri;
     }
   }
 }
  /**
   * Not synchronized, the involved identity might be deleted during the query - which is not really
   * a problem.
   */
  public int getReceivedTrustsCount(Identity trustee) throws Exception {
    SimpleFieldSet request = new SimpleFieldSet(true);
    request.putOverwrite("Message", "GetTrustersCount");
    request.putOverwrite("Identity", trustee.getID());
    request.putOverwrite("Context", Freetalk.WOT_CONTEXT);

    try {
      SimpleFieldSet answer = sendFCPMessageBlocking(request, null, "TrustersCount").params;
      return Integer.parseInt(answer.get("Value"));
    } catch (PluginNotFoundException e) {
      throw new WoTDisconnectedException();
    }
  }
  public IntroductionPuzzle getIntroductionPuzzle(String id) throws Exception {
    SimpleFieldSet params = new SimpleFieldSet(true);
    params.putOverwrite("Message", "GetIntroductionPuzzle");
    params.putOverwrite("Puzzle", id);

    try {
      SimpleFieldSet result = sendFCPMessageBlocking(params, null, "IntroductionPuzzle").params;

      try {
        return new IntroductionPuzzle(
            id, result.get("MimeType"), Base64.decodeStandard(result.get("Data")));
      } catch (RuntimeException e) {
        Logger.error(this, "Parsing puzzle failed", e);
      } catch (IllegalBase64Exception e) {
        Logger.error(this, "Parsing puzzle failed", e);
      }

      return null;
    } catch (PluginNotFoundException e) {
      Logger.error(this, "Getting puzzles failed", e);

      return null;
    }
  }
예제 #9
0
 public String getRawOption(String name) {
   if (config instanceof PersistentConfig) {
     PersistentConfig pc = (PersistentConfig) config;
     if (pc.finishedInit)
       throw new IllegalStateException(
           "getRawOption("
               + name
               + ") on "
               + this
               + " but persistent config has been finishedInit() already!");
     SimpleFieldSet fs = pc.origConfigFileContents;
     if (fs == null) return null;
     return fs.get(prefix + SimpleFieldSet.MULTI_LEVEL_CHAR + name);
   } else return null;
 }
예제 #10
0
 /** Set options from a SimpleFieldSet. Once we process an option, we must remove it. */
 public void setOptions(SimpleFieldSet sfs) {
   for (Entry<String, Option<?>> entry : map.entrySet()) {
     String key = entry.getKey();
     Option<?> o = entry.getValue();
     String val = sfs.get(key);
     if (val != null) {
       try {
         o.setValue(val);
       } catch (InvalidConfigValueException e) {
         String msg =
             "Invalid config value: "
                 + prefix
                 + SimpleFieldSet.MULTI_LEVEL_CHAR
                 + key
                 + " = "
                 + val
                 + " : error: "
                 + e;
         Logger.error(this, msg, e);
         System.err.println(msg); // might be about logging?
       } catch (NodeNeedRestartException e) {
         // Impossible
         String msg =
             "Impossible: "
                 + prefix
                 + SimpleFieldSet.MULTI_LEVEL_CHAR
                 + key
                 + " = "
                 + val
                 + " : error: "
                 + e;
         Logger.error(this, msg, e);
       }
     }
   }
 }
  @SuppressWarnings("unchecked")
  private void parseIdentities(SimpleFieldSet params, boolean bOwnIdentities) {
    if (bOwnIdentities) Logger.normal(this, "Parsing received own identities...");
    else Logger.normal(this, "Parsing received identities...");

    int idx;
    int ignoredCount = 0;
    int newCount = 0;

    for (idx = 0; ; idx++) {
      String identityID = params.get("Identity" + idx);
      if (identityID == null
          || identityID.equals("")) /* TODO: Figure out whether the second condition is necessary */
        break;
      String requestURI = params.get("RequestURI" + idx);
      String insertURI = bOwnIdentities ? params.get("InsertURI" + idx) : null;
      String nickname = params.get("Nickname" + idx);

      if (nickname == null || nickname.length() == 0) {
        // If an identity publishes an invalid nickname in one of its first WoT inserts then WoT
        // will return an empty
        // nickname for that identity until a new XML was published with a valid nickname. We ignore
        // the identity until
        // then to prevent confusing error logs.
        // TODO: Maybe handle this in WoT. Would require checks in many places though.
        continue;
      }

      synchronized (this) {
          /* We lock here and not during the whole function to allow other threads to execute */
        Query q = db.query(); // TODO: Encapsulate the query in a function...
        q.constrain(WoTIdentity.class);
        q.descend("mID").constrain(identityID);
        ObjectSet<WoTIdentity> result = q.execute();
        WoTIdentity id = null;

        if (result.size() == 0) {
          try {
            importIdentity(bOwnIdentities, identityID, requestURI, insertURI, nickname);
            ++newCount;
          } catch (Exception e) {
            Logger.error(this, "Importing a new identity failed.", e);
          }
        } else {
          Logger.debug(this, "Not importing already existing identity " + requestURI);
          ++ignoredCount;

          assert (result.size() == 1);
          id = result.next();
          id.initializeTransient(mFreetalk);

          if (bOwnIdentities != (id instanceof WoTOwnIdentity)) {
            // The type of the identity changed so we need to delete and re-import it.

            try {
              Logger.normal(this, "Identity type changed, replacing it: " + id);
              // We MUST NOT take the following locks because deleteIdentity does other locks
              // (MessageManager/TaskManager) which must happen before...
              // synchronized(id)
              // synchronized(db.lock())
              deleteIdentity(id, mFreetalk.getMessageManager(), mFreetalk.getTaskManager());
              importIdentity(bOwnIdentities, identityID, requestURI, insertURI, nickname);
            } catch (Exception e) {
              Logger.error(this, "Replacing a WoTIdentity with WoTOwnIdentity failed.", e);
            }

          } else { // Normal case: Update the last received time of the idefnt
            synchronized (id) {
              synchronized (db.lock()) {
                try {
                  // TODO: The thread sometimes takes hours to parse the identities and I don't know
                  // why.
                  // So right now its better to re-query the time for each identity.
                  id.setLastReceivedFromWoT(CurrentTimeUTC.getInMillis());
                  id.checkedCommit(this);
                } catch (Exception e) {
                  Persistent.checkedRollback(db, this, e);
                }
              }
            }
          }
        }
      }

      Thread.yield();
    }

    Logger.normal(
        this,
        "parseIdentities(bOwnIdentities=="
            + bOwnIdentities
            + " received "
            + idx
            + " identities. Ignored "
            + ignoredCount
            + "; New: "
            + newCount);
  }
예제 #12
0
  private void startInserter() {
    if (!canStart) {
      if (logMINOR) Logger.minor(this, darknetOpennetString + " ARK inserter can't start yet");
      return;
    }

    if (logMINOR) Logger.minor(this, "starting " + darknetOpennetString + " ARK inserter");

    SimpleFieldSet fs = crypto.exportPublicFieldSet(false, false, true);

    // Remove some unnecessary fields that only cause collisions.

    // Delete entire ark.* field for now. Changing this and automatically moving to the new may be
    // supported in future.
    fs.removeSubset("ark");
    fs.removeValue("location");
    fs.removeValue("sig");
    // fs.remove("version"); - keep version because of its significance in reconnection

    String s = fs.toString();

    byte[] buf;
    try {
      buf = s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new Error("Impossible: JVM doesn't support UTF-8: " + e, e);
    }

    Bucket b = new SimpleReadOnlyArrayBucket(buf);

    long number = crypto.myARKNumber;
    InsertableClientSSK ark = crypto.myARK;
    FreenetURI uri = ark.getInsertURI().setKeyType("USK").setSuggestedEdition(number);

    if (logMINOR)
      Logger.minor(
          this, "Inserting " + darknetOpennetString + " ARK: " + uri + "  contents:\n" + s);

    inserter =
        new ClientPutter(
            this,
            b,
            uri,
            new ClientMetadata("text/plain") /* it won't quite fit in an SSK anyway */,
            node.clientCore.makeClient((short) 0, true).getInsertContext(true),
            RequestStarter.INTERACTIVE_PRIORITY_CLASS,
            false,
            false,
            this,
            null,
            null,
            false);

    try {

      node.clientCore.clientContext.start(inserter, false);

      synchronized (this) {
        if (fs.get("physical.udp") == null) lastInsertedPeers = null;
        else {
          try {
            String[] all = fs.getAll("physical.udp");
            Peer[] peers = new Peer[all.length];
            for (int i = 0; i < all.length; i++) peers[i] = new Peer(all[i], false);
            lastInsertedPeers = peers;
          } catch (PeerParseException e1) {
            Logger.error(
                this,
                "Error parsing own "
                    + darknetOpennetString
                    + " ref: "
                    + e1
                    + " : "
                    + fs.get("physical.udp"),
                e1);
          } catch (UnknownHostException e1) {
            Logger.error(
                this,
                "Error parsing own "
                    + darknetOpennetString
                    + " ref: "
                    + e1
                    + " : "
                    + fs.get("physical.udp"),
                e1);
          }
        }
      }
    } catch (InsertException e) {
      onFailure(e, inserter, null);
    } catch (DatabaseDisabledException e) {
      // Impossible
    }
  }
예제 #13
0
  /**
   * Create a ClientGet from a request serialized to a SimpleFieldSet. Can throw, and does minimal
   * verification, as is dealing with data supposedly serialized out by the node.
   *
   * @throws IOException
   * @throws FetchException
   */
  public ClientGet(SimpleFieldSet fs, FCPClient client2, FCPServer server)
      throws IOException, FetchException {
    super(fs, client2);

    returnType = ClientGetMessage.parseValidReturnType(fs.get("ReturnType"));
    String f = fs.get("Filename");
    if (f != null) targetFile = new File(f);
    else targetFile = null;
    f = fs.get("TempFilename");
    if (f != null) tempFile = new File(f);
    else tempFile = null;
    boolean ignoreDS = Fields.stringToBool(fs.get("IgnoreDS"), false);
    boolean dsOnly = Fields.stringToBool(fs.get("DSOnly"), false);
    int maxRetries = Integer.parseInt(fs.get("MaxRetries"));
    fctx = new FetchContext(server.defaultFetchContext, FetchContext.IDENTICAL_MASK, false, null);
    fctx.eventProducer.addEventListener(this);
    // ignoreDS
    fctx.localRequestOnly = dsOnly;
    fctx.ignoreStore = ignoreDS;
    fctx.maxNonSplitfileRetries = maxRetries;
    fctx.maxSplitfileBlockRetries = maxRetries;
    binaryBlob = Fields.stringToBool(fs.get("BinaryBlob"), false);
    succeeded = Fields.stringToBool(fs.get("Succeeded"), false);
    if (finished) {
      if (succeeded) {
        foundDataLength = Long.parseLong(fs.get("FoundDataLength"));
        foundDataMimeType = fs.get("FoundDataMimeType");
        SimpleFieldSet fs1 = fs.subset("PostFetchProtocolError");
        if (fs1 != null) postFetchProtocolErrorMessage = new ProtocolErrorMessage(fs1);
      } else {
        getFailedMessage = new GetFailedMessage(fs.subset("GetFailed"), false);
      }
    }
    Bucket ret = null;
    if (returnType == ClientGetMessage.RETURN_TYPE_DISK) {
      if (succeeded) {
        ret = new FileBucket(targetFile, false, true, false, false, false);
      } else {
        ret = new FileBucket(tempFile, false, true, false, false, false);
      }
    } else if (returnType == ClientGetMessage.RETURN_TYPE_NONE) {
      ret = new NullBucket();
    } else if (returnType == ClientGetMessage.RETURN_TYPE_DIRECT) {
      try {
        ret =
            SerializableToFieldSetBucketUtil.create(
                fs.subset("ReturnBucket"),
                server.core.random,
                server.core.persistentTempBucketFactory);
        if (ret == null) throw new CannotCreateFromFieldSetException("ret == null");
      } catch (CannotCreateFromFieldSetException e) {
        Logger.error(this, "Cannot read: " + this + " : " + e, e);
        try {
          // Create a new temp bucket
          if (persistenceType == PERSIST_FOREVER)
            ret = server.core.persistentTempBucketFactory.makeBucket(fctx.maxOutputLength);
          else ret = server.core.tempBucketFactory.makeBucket(fctx.maxOutputLength);
        } catch (IOException e1) {
          Logger.error(this, "Cannot create bucket for temp storage: " + e, e);
          getter = null;
          returnBucket = null;
          throw new FetchException(FetchException.BUCKET_ERROR, e);
        }
      }
    } else {
      throw new IllegalArgumentException();
    }
    if (succeeded) {
      if (foundDataLength < ret.size()) {
        Logger.error(this, "Failing " + identifier + " because lost data");
        succeeded = false;
      }
    }
    if (ret == null)
      Logger.error(
          this, "Impossible: ret = null in SFS constructor for " + this, new Exception("debug"));
    returnBucket = ret;

    String[] allowed = fs.getAll("AllowedMIMETypes");
    if (allowed != null) {
      fctx.allowedMIMETypes = new HashSet<String>();
      for (String a : allowed) fctx.allowedMIMETypes.add(a);
    }

    getter =
        new ClientGetter(
            this,
            uri,
            fctx,
            priorityClass,
            lowLevelClient,
            binaryBlob ? new NullBucket() : returnBucket,
            binaryBlob ? returnBucket : null);

    if (finished && succeeded)
      allDataPending =
          new AllDataMessage(
              returnBucket,
              identifier,
              global,
              startupTime,
              completionTime,
              this.foundDataMimeType);
  }