Exemplo n.º 1
0
  protected BlockingConnection connect(boolean ultrapeer) throws Exception {
    ServerSocket ss = new ServerSocket();
    ss.setReuseAddress(true);
    ss.bind(new InetSocketAddress(0));
    connectionServices.connectToHostAsynchronously(
        "127.0.0.1", ss.getLocalPort(), ConnectType.PLAIN);
    Socket socket = ss.accept();
    ss.close();

    socket.setSoTimeout(3000);
    InputStream in = socket.getInputStream();
    String word = IOUtils.readWord(in, 9);
    if (!word.equals("GNUTELLA")) throw new IOException("Bad word: " + word);

    HandshakeResponder responder;
    if (ultrapeer) {
      responder = new UltrapeerResponder();
    } else {
      responder = new OldResponder();
    }
    BlockingConnection con = blockingConnectionFactory.createConnection(socket);
    con.initialize(null, responder, 1000);
    replyToPing(con, ultrapeer);
    return con;
  }
Exemplo n.º 2
0
  /**
   * Handle a Magnet request via a socket (for TCP handling). Deiconify the application, fire MAGNET
   * request and return true as a sign that LimeWire is running.
   */
  public void fireControlThread(Socket socket, boolean magnet) {
    LOG.trace("enter fireControl");

    Thread.currentThread().setName("IncomingControlThread");
    try {
      // Only allow control from localhost
      if (!NetworkUtils.isLocalHost(socket)) {
        if (LOG.isWarnEnabled())
          LOG.warn("Invalid control request from: " + socket.getInetAddress().getHostAddress());
        return;
      }

      // First read extra parameter
      socket.setSoTimeout(Constants.TIMEOUT);
      ByteReader br = new ByteReader(socket.getInputStream());
      // read the first line. if null, throw an exception
      String line = br.readLine();
      socket.setSoTimeout(0);

      BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
      String s = CommonUtils.getUserName() + "\r\n";
      // system internal, so use system encoding
      byte[] bytes = s.getBytes();
      out.write(bytes);
      out.flush();
      if (magnet) handleMagnetRequest(line);
      else handleTorrentRequest(line);
    } catch (IOException e) {
      LOG.warn("Exception while responding to control request", e);
    } finally {
      IOUtils.close(socket);
    }
  }
 /** @return the wrapped bytes, or a new empty array if there is a problem loading the array */
 private byte[] getWrappedBytes() {
   final byte[] bytes = getBytes(KEY_WRAPPED_BYTES);
   try {
     if (bytes != null) return IOUtils.inflate(bytes);
   } catch (IOException ignored) {
   }
   return new byte[0];
 }
 /**
  * Takes the given list and encodes it into the wrapping system of this message, so later changes
  * to the given list will NOT be reflected by this container.
  */
 public void setWrappedMessages(final List<MessageContainer> messages) {
   final ByteArrayOutputStream out = new ByteArrayOutputStream();
   for (MessageContainer message : messages) {
     try {
       out.write(message.encode());
     } catch (IOException ex) {
       throw new RuntimeException("IOException? WTF?", ex);
     }
   }
   put(KEY_WRAPPED_BYTES, IOUtils.deflate(out.toByteArray()));
 }
    public boolean requestComplete(HttpUriRequest request, HttpResponse response) {
      LOG.debug("http request method succeeded");

      // remember we made an attempt even if it didn't succeed
      UpdateSettings.LAST_SIMPP_FAILOVER.setValue(clock.now());
      final byte[] inflated;
      try {
        if (response.getStatusLine().getStatusCode() < 200
            || response.getStatusLine().getStatusCode() >= 300)
          throw new IOException("bad code " + response.getStatusLine().getStatusCode());

        byte[] resp = null;
        if (response.getEntity() != null) {
          resp = IOUtils.readFully(response.getEntity().getContent());
        }
        if (resp == null || resp.length == 0) throw new IOException("bad body");

        // inflate the response and process.
        inflated = IOUtils.inflate(resp);
      } catch (IOException failed) {
        httpRequestControl.requestFinished();
        LOG.warn("couldn't fetch data ", failed);
        return false;
      } finally {
        httpExecutor.get().releaseResources(response);
      }

      // Handle the data in the background thread.
      backgroundExecutor.execute(
          new Runnable() {
            public void run() {
              httpRequestControl.requestFinished();

              LOG.trace("Parsing new data...");
              handleDataInternal(inflated, UpdateType.FROM_HTTP, null);
            }
          });

      return false; // no more requests
    }
Exemplo n.º 6
0
 private QueryReply createReply(InputStream source) {
   ObjectInputStream in = null;
   try {
     in = new ObjectInputStream(source);
     byte[] payload = ((Data) in.readObject()).data;
     return queryReplyFactory.createFromNetwork(new byte[16], (byte) 1, (byte) 0, payload);
   } catch (Throwable t) {
     LOG.error("Unable to read serialized data", t);
     return null;
   } finally {
     IOUtils.close(in);
   }
 }
Exemplo n.º 7
0
  /** Does the work of setting new good & bad hosts. */
  private void refreshHostsImpl() {
    LOG.debug("refreshing hosts");

    // Load the local blacklist, stripping out invalid entries
    IPList newBad = new IPList();
    String[] allHosts = FilterSettings.BLACK_LISTED_IP_ADDRESSES.get();
    ArrayList<String> valid = new ArrayList<String>(allHosts.length);
    for (int i = 0; i < allHosts.length; i++) {
      if (newBad.add(allHosts[i])) valid.add(allHosts[i]);
    }
    if (valid.size() != allHosts.length) {
      allHosts = valid.toArray(new String[0]);
      FilterSettings.BLACK_LISTED_IP_ADDRESSES.set(allHosts);
    }

    // Load the local whitelist, stripping out invalid entries
    IPList newGood = new IPList();
    allHosts = FilterSettings.WHITE_LISTED_IP_ADDRESSES.get();
    valid = new ArrayList<String>(allHosts.length);
    for (int i = 0; i < allHosts.length; i++) {
      if (newGood.add(allHosts[i])) valid.add(allHosts[i]);
    }
    if (valid.size() != allHosts.length) {
      allHosts = valid.toArray(new String[0]);
      FilterSettings.WHITE_LISTED_IP_ADDRESSES.set(allHosts);
    }

    // Load data from hostiles.txt (if it wasn't already loaded!)...
    if (shouldLoadHostiles) {
      shouldLoadHostiles = false;

      LOG.debug("loading hostiles");
      File hostiles = new File(CommonUtils.getUserSettingsDir(), "hostiles.txt");
      BufferedReader reader = null;
      try {
        reader = new BufferedReader(new FileReader(hostiles));
        String read = null;
        while ((read = reader.readLine()) != null) {
          hostilesTXTHosts.add(read);
        }
      } catch (IOException ignored) {
        LOG.debug("iox loading hostiles", ignored);
      } finally {
        IOUtils.close(reader);
      }
    }

    badHosts = new MultiIPList(newBad, hostilesTXTHosts);
    goodHosts = newGood;
  }