Hyperlink 从服务器端生成torrent链接

Hyperlink 从服务器端生成torrent链接,hyperlink,bittorrent,Hyperlink,Bittorrent,我对torrent了解不多,至少不足以理解某些网站如何提供普通下载链接和torrent链接来下载用户上传的文件 正在生成一个torrent链接,这是一个常见且易于实现的功能。我需要安装服务器吗 我从Java源代码中创建了一个丑陋的C#实现,为了确保我的一些编码变量是正确的,我使用了Lars Warholm的代码 // There are 'args' because I'm using it from command-line. (arg0 is an option not used h

我对torrent了解不多,至少不足以理解某些网站如何提供普通下载链接和torrent链接来下载用户上传的文件

正在生成一个torrent链接,这是一个常见且易于实现的功能。我需要安装服务器吗

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

    // 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.
}

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

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


  • 要创建
    .torrent
    文件,您必须阅读有关文件格式的说明。一个生成
    .torrent
    文件的Java代码在

    中给出,我来看看文件格式,谢谢。奇怪的是,这两个网站似乎都崩溃了,它们不是网站。只需跟踪URL。
        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();
        }