private void doNormalTest(boolean settingOn, boolean expectTLS) throws Exception {
        
        setAccepted(true);
        
        if(settingOn)
            networkManagerStub.setIncomingTLSEnabled(true);
        
        BlockingConnectionUtils.drain(testUP[0]);
        // some setup
        byte[] clientGUID = GUID.makeGuid();

        // construct and send a query
        byte[] guid = GUID.makeGuid();
        searchServices.query(guid, "golf is awesome");

        // the testUP[0] should get it
        Message m;
        do {
            m = testUP[0].receive(TIMEOUT);
        } while (!(m instanceof QueryRequest));

        // send a reply with NO PushProxy info
        Response[] res = new Response[1];
        res[0] = responseFactory.createResponse(10, 10, "golf is awesome", UrnHelper.SHA1);
        m = queryReplyFactory.createQueryReply(m.getGUID(), (byte) 1, 6355,
                myIP(), 0, res, clientGUID, new byte[0], false, false, true,
                true, false, false, null);
        testUP[0].send(m);
        testUP[0].flush();

        // wait a while for Leaf to process result
        assertNotNull(callback.getRFD());

        // tell the leaf to download the file, should result in normal TCP
        // PushRequest
        Downloader downloader = downloadServices.download(
                (new RemoteFileDesc[] { callback.getRFD() }), true, new GUID(m.getGUID()));

        // await a PushRequest
        do {
            m = testUP[0].receive(25 * TIMEOUT);
        } while (!(m instanceof PushRequest));
        
        PushRequest pr = (PushRequest)m;
        assertNotNull(pr);
        assertEquals(expectTLS, pr.isTLSCapable());
        assertEquals(clientGUID, pr.getClientGUID());
        assertEquals(networkManagerStub.getAddress(), pr.getIP());
        assertEquals(networkManagerStub.getPort(), pr.getPort());
        assertEquals(10, pr.getIndex());
        assertFalse(pr.isFirewallTransferPush());
        
        downloader.stop();
    }
Example #2
0
  /**
   * If there are problems with the request, just ignore it. There's no point in sending them a GIV
   * to have them send a GET just to return a 404 or Busy or Malformed Request, etc..
   */
  public void handlePushRequest(PushRequest pushRequest, ReplyHandler handler) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("push: " + pushRequest + "\nfrom: " + handler);
    }

    // Ignore push request from banned hosts.
    if (handler.isPersonalSpam(pushRequest)) {
      LOG.debug("discarded as personal spam");
      return;
    }

    byte[] ip = pushRequest.getIP();
    String host = NetworkUtils.ip2string(ip);

    // check whether we serviced this push request already
    GUID guid = new GUID(pushRequest.getGUID());
    if (GUID_REQUESTS.put(guid, guid) != null) {
      LOG.debug("already serviced");
      return;
    }

    // make sure the guy isn't hammering us
    AtomicInteger i = PUSH_REQUESTS.get(host);
    if (i == null) {
      i = new AtomicInteger(1);
      PUSH_REQUESTS.put(host, i);
    } else {
      i.addAndGet(1);
      // if we're over the max push requests for this host, exit.
      if (i.get() > UploadSettings.MAX_PUSHES_PER_HOST.getValue()) {
        LOG.debug("over max pushes per host");
        return;
      }
    }

    // if the IP is banned, don't accept it
    if (!ipFilterProvider.get().allow(ip)) {
      LOG.debug("blocked by ip filter");
      return;
    }

    int port = pushRequest.getPort();
    // if invalid port, exit
    if (!NetworkUtils.isValidAddressAndPort(host, port)) {
      LOG.debug("invalid host or port");
      return;
    }

    try {
      Connectable address = new ConnectableImpl(host, port, pushRequest.isTLSCapable());
      pushManager
          .get()
          .acceptPushUpload(
              address,
              new GUID(pushRequest.getClientGUID()),
              pushRequest.isMulticast(), // force accept
              pushRequest.isFirewallTransferPush());
    } catch (UnknownHostException e) {
      throw new RuntimeException(e);
    }
  }