Changeset 2952423


Ignore:
Timestamp:
Feb 18, 2012 5:58:54 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
b47aa34
Parents:
c957577
Message:

Disable PEX/metadata extensions and open trackers for private torrents
Handle announce URLs with parameters correctly

Location:
apps/i2psnark/java/src/org/klomp/snark
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/ExtensionHandler.java

    rc957577 r2952423  
    4040  /**
    4141   *  @param metasize -1 if unknown
     42   *  @param pexAndMetadata advertise these capabilities
    4243   *  @return bencoded outgoing handshake message
    4344   */
    44     public static byte[] getHandshake(int metasize) {
     45    public static byte[] getHandshake(int metasize, boolean pexAndMetadata) {
    4546        Map<String, Object> handshake = new HashMap();
    4647        Map<String, Integer> m = new HashMap();
    47         m.put(TYPE_METADATA, Integer.valueOf(ID_METADATA));
    48         m.put(TYPE_PEX, Integer.valueOf(ID_PEX));
    49         if (metasize >= 0)
    50             handshake.put("metadata_size", Integer.valueOf(metasize));
     48        if (pexAndMetadata) {
     49            m.put(TYPE_METADATA, Integer.valueOf(ID_METADATA));
     50            m.put(TYPE_PEX, Integer.valueOf(ID_PEX));
     51            if (metasize >= 0)
     52                handshake.put("metadata_size", Integer.valueOf(metasize));
     53        }
     54        // include the map even if empty so the far-end doesn't NPE
    5155        handshake.put("m", m);
    5256        handshake.put("p", Integer.valueOf(6881));
  • apps/i2psnark/java/src/org/klomp/snark/Peer.java

    rc957577 r2952423  
    269269                _log.debug("Peer supports extensions, sending reply message");
    270270            int metasize = metainfo != null ? metainfo.getInfoBytes().length : -1;
    271             out.sendExtension(0, ExtensionHandler.getHandshake(metasize));
     271            boolean pexAndMetadata = metainfo == null || !metainfo.isPrivate();
     272            out.sendExtension(0, ExtensionHandler.getHandshake(metasize, pexAndMetadata));
    272273        }
    273274
  • apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java

    rc957577 r2952423  
    11871187   */
    11881188  void sendPeers(Peer peer) {
     1189      if (metainfo != null && metainfo.isPrivate())
     1190          return;
    11891191      Map<String, BEValue> handshake = peer.getHandshakeMap();
    11901192      if (handshake == null)
  • apps/i2psnark/java/src/org/klomp/snark/PeerState.java

    rc957577 r2952423  
    490490  void extensionMessage(int id, byte[] bs)
    491491  {
     492      if (metainfo != null && metainfo.isPrivate() &&
     493          (id == ExtensionHandler.ID_METADATA || id == ExtensionHandler.ID_PEX)) {
     494          // shouldn't get this since we didn't advertise it but they could send it anyway
     495          if (_log.shouldLog(Log.WARN))
     496              _log.warn("Private torrent, ignoring ext msg " + id);
     497          return;
     498      }
    492499      ExtensionHandler.handleMessage(peer, listener, id, bs);
    493500      // Peer coord will get metadata from MagnetState,
  • apps/i2psnark/java/src/org/klomp/snark/TrackerClient.java

    rc957577 r2952423  
    157157    }
    158158    List tlist = _util.getOpenTrackers();
    159     if (tlist != null) {
     159    if (tlist != null && !meta.isPrivate()) {
    160160        for (int i = 0; i < tlist.size(); i++) {
    161161             String url = (String)tlist.get(i);
     
    349349
    350350            // Get peers from PEX
    351             if (left > 0 && coordinator.needPeers() && !stop) {
     351            if (left > 0 && coordinator.needPeers() && (!meta.isPrivate()) && !stop) {
    352352                Set<PeerID> pids = coordinator.getPEXPeers();
    353353                if (!pids.isEmpty()) {
     
    371371            // Get peers from DHT
    372372            // FIXME this needs to be in its own thread
    373             if (_util.getDHT() != null && !stop) {
     373            if (_util.getDHT() != null && (!meta.isPrivate()) && !stop) {
    374374                int numwant;
    375375                if (left == 0 || event.equals(STOPPED_EVENT) || !coordinator.needPeers())
     
    445445    throws IOException
    446446  {
     447    StringBuilder buf = new StringBuilder(512);
     448    buf.append(tr.announce);
     449    if (tr.announce.contains("?"))
     450        buf.append('&');
     451    else
     452        buf.append('?');
     453    buf.append("info_hash=").append(infoHash)
     454       .append("&peer_id=").append(peerID)
     455       .append("&port=").append(port)
     456       .append("&ip=" ).append(_util.getOurIPString()).append(".i2p")
     457       .append("&uploaded=").append(uploaded)
     458       .append("&downloaded=").append(downloaded)
     459       .append("&left=");
    447460    // What do we send for left in magnet mode? Can we omit it?
    448     long tleft = left >= 0 ? left : 1;
    449     String s = tr.announce
    450       + "?info_hash=" + infoHash
    451       + "&peer_id=" + peerID
    452       + "&port=" + port
    453       + "&ip=" + _util.getOurIPString() + ".i2p"
    454       + "&uploaded=" + uploaded
    455       + "&downloaded=" + downloaded
    456       + "&left=" + tleft
    457       + "&compact=1"   // NOTE: opentracker will return 400 for &compact alone
    458       + ((! event.equals(NO_EVENT)) ? ("&event=" + event) : "");
     461    if (left >= 0)
     462        buf.append(left);
     463    else
     464        buf.append('1');
     465    buf.append("&compact=1");  // NOTE: opentracker will return 400 for &compact alone
     466    if (! event.equals(NO_EVENT))
     467        buf.append("&event=").append(event);
     468    buf.append("&numwant=");
    459469    if (left == 0 || event.equals(STOPPED_EVENT) || !coordinator.needPeers())
    460         s += "&numwant=0";
     470        buf.append('0');
    461471    else
    462         s += "&numwant=" + _util.getMaxConnections();
     472        buf.append(_util.getMaxConnections());
     473    String s = buf.toString();
    463474    _util.debug("Sending TrackerClient request: " + s, Snark.INFO);
    464475     
Note: See TracChangeset for help on using the changeset viewer.