从服务器端生成torrent链接

时间:2012-12-11 08:11:09

标签: hyperlink bittorrent

我不太了解种子,至少不足以理解某些网站如何提供正常的下载链接和torrent链接来下载用户上传的文件。

生成一个常见且易于实现的torrent链接。我需要安装服务器吗?

我从Java源代码中创建了一个丑陋的C#实现,为了确保我的一些编码变量是正确的,我使用了Lars Warholm的NBEncode

    // There are 'args' because I'm using it from command-line. (arg0 is an option not used here)
    // Source file
    args[1] = Directory.GetCurrentDirectory() + args[1];
    // Name to give to the torrent file
    args[2] = Directory.GetCurrentDirectory() + args[2];

    var inFileStream = new FileStream(args[1], FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    var filename = args[2];

    //BEncoding with NBEencode
    var transform = new BObjectTransform();
    MemoryStream s = new MemoryStream();
    OSS.NBEncode.Entities.BDictionary bod = new OSS.NBEncode.Entities.BDictionary();
    OSS.NBEncode.Entities.BDictionary meta = new OSS.NBEncode.Entities.BDictionary();

    // Preparing the first part of the file by creating BEncoded objects
    string announceURL = "https://www.mysite.com/announce";
    int pieceLength = 512 * 1024;
    bod.Value.Add(new BByteString("name"), new OSS.NBEncode.Entities.BByteString(filename));
    bod.Value.Add(new BByteString("length"), new OSS.NBEncode.Entities.BInteger(inFileStream.Length));
    bod.Value.Add(new BByteString("piece length"), new OSS.NBEncode.Entities.BInteger(pieceLength));
    bod.Value.Add(new BByteString("pieces"), new BByteString(""));
    meta.Value.Add(new BByteString("announce"), new BByteString(announceURL));
    meta.Value.Add(new BByteString("info"), bod);
    byte[] pieces = hashPieces(args[1], pieceLength);
    transform.EncodeObject(meta, s);
    s.Close();

    // Notice that we finish with a dictionary entry of "pieces" with an empty string.
    byte[] trs = s.ToArray();
    s.Close();
    inFileStream.Close();

    // I don't know how to write array of bytes using NBEncode library, so let's continue manually

    // All data has been written a MemoryStreamp, except the byte array with the hash info about each parts of the file

    Stream st = new FileStream(filename + ".torrent", FileMode.Create);
    BinaryWriter bw = new BinaryWriter(st);

    // Let's write these Bencoded variables to the torrent file:
    // The -4 is there to skip the current end of the file created by NBEncode

    for (int i = 0; i < trs.Length - 4; i++)
    {
        bw.BaseStream.WriteByte(trs[i]);
    }

    // We'll add the length of the pieces SHA1 hashes:
    var bt = stringToBytes(pieces.Length.ToString() + ":");

    // Then we'll close the Bencoded dictionary with 'ee'
    var bu = stringToBytes("ee");

    // Let's append this to the end of the file.
    foreach (byte b in bt)
    {
        bw.BaseStream.WriteByte(b);
    }
    foreach (byte b in pieces)
    {
        bw.BaseStream.WriteByte(b);
    }
    foreach (byte b in bu)
    {
        bw.BaseStream.WriteByte(b);
    }
    bw.Close();
    st.Close();                    

    // That's it.
}

使用的功能:

    private static byte[] stringToBytes(String str)
    {
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        Byte[] bytes = encoding.GetBytes(str);
        return bytes;
    }

   private static byte[] hashPieces(string file, int pieceLength)
    {

        SHA1 sha1 = new SHA1CryptoServiceProvider();

        StreamReader inn = new StreamReader(file);
        MemoryStream pieces = new MemoryStream();
        byte[] bytes = new byte[pieceLength];
        byte[] digest = new byte[20];
        int pieceByteCount = 0, readCount = inn.BaseStream.Read(bytes, 0, pieceLength);
        while (readCount != 0)
        {
            pieceByteCount += readCount;
            digest = sha1.ComputeHash(bytes, 0, readCount);
            if (pieceByteCount == pieceLength)
            {
                pieceByteCount = 0;
                foreach (byte b in digest)
                {
                    pieces.WriteByte(b);
                }
            }
            readCount = inn.BaseStream.Read(bytes, 0, pieceLength - pieceByteCount);
        }
        inn.Close();


        if (pieceByteCount > 0)
            foreach (byte b in digest)
            {
                pieces.WriteByte(b);
            }
        return pieces.ToArray();
    }

1 个答案:

答案 0 :(得分:1)

这取决于你是如何创建它的。如果您运行一个网站,并希望从上传的文件生成torrent文件,那么您显然需要服务器端代码。

生成torrent文件包括:将您想要的文件添加到torrent,并添加跟踪器信息。一些流行的跟踪器是:

  1. http://open.tracker.thepiratebay.org/announce
  2. http://www.torrent-downloads.to:2710/announce
  3. 要创建.torrent文件,您必须阅读有关文件格式的信息。生成.torrent文件的Java片段在https://stackoverflow.com/a/2033298/384155

    处给出