简体   繁体   English

尝试对“find_node”查询进行编码时收到不可读的文本

[英]receiving unreadable text while trying to bencode a "find_node" query

Im using the bencod.NET trying to send a find_node query and receive an answer using a bootstrapping node.我正在使用bencod.NET尝试发送 find_node 查询并使用引导节点接收答案。 it seems like the request works well and I do get a response on wireshark and c#. the problem is when decoding (using bencoding ofc) I get unreadable and unusable dictionary code:看起来请求运行良好,我确实在 wireshark 和 c# 上得到了响应。问题是在解码时(使用 bencoding ofc)我得到了不可读和不可用的字典代码:

IPAddress[] ipAddresses = Dns.GetHostAddresses("router.bittorrent.com");
var endpoint = new IPEndPoint(ipAddresses[0], 6881);

// Create a new bencoded dictionary for the query
BDictionary query = new BDictionary();
query["t"] = new BString("aa"); // transaction ID
query["y"] = new BString("q"); // query type
query["q"] = new BString("find_node"); // query name
// Add additional parameters to the query
BDictionary argsDict = new BDictionary();
argsDict["id"] = new BString(id); // target node ID
Console.WriteLine(BMethods.BytesToId(Encoding.ASCII.GetBytes(argsDict["id"].ToString())) + "penis");
argsDict["target"] = new BString(id); // target ID
query["a"] = argsDict;

Console.WriteLine(argsDict.Get<BString>("id")+"here");
var parser = new BencodeParser(Encoding.GetEncoding("ISO-8859-1"));
// Encode the query as a byte array
var queryBytes = query.EncodeAsBytes();
// Create a new socket

UdpClient udpServer = new UdpClient(11000);

udpServer.Send(queryBytes, queryBytes.Length, endpoint); // reply back
var data = udpServer.Receive(ref endpoint); // listen on port 11000
Console.WriteLine(Encoding.GetEncoding("ISO-8859-1").GetString(data));
BDictionary response = parser.Parse<BDictionary>(data);
foreach (KeyValuePair<BString, IBObject> i in response)
{
    if(i.Value is BDictionary)
    {
        BDictionary b = (BDictionary)i.Value;
        foreach (KeyValuePair<BString, IBObject> j in b)
        {

            Console.WriteLine(j.Key + ": " + BMethods.BytesToId(BMethods.ConvertHexToByte((((BString)j.Value).ToString()))));

        }
        continue;
    }
    Console.WriteLine(i.Key + ": " + ((i.Value).ToString()));
}

id is a random 20 byte array if you're wondering the response output usually looks something like this: id 是一个随机的 20 字节数组,如果您想知道响应 output 通常看起来像这样:

ip: ?Z?w*o id: 2oNisQÿJì)Iº«òûaF|Ag nodes: I??"2/IªLO?d?å?Y3?/lYA??cwIov0ahrth_EqU½?G??▌Ä?÷0ï%(I¼A?ÿ? ¡9I? òµ??°O0?~x,B ?ªO] ;'?UDz½??cUP¿}ïWLx?;K=&o¼âC}ÑWAº3{àO\AOé81Ñ BF¥)ú21?c_ìvyêo?AMe6lcp\g?èöUÉ°_6OrEu[?)??']km?èU¡(á t: aa y: r ip: ?Z?w*o id: 2oNisQÿJì)Iº«òûaF|Ag 节点:I???"2/IªLO?d?å?Y3?/lYA??cwIov0ahrth_EqU½?G??▌Ä?÷0ï%(I¼A ?ÿ? ¡9I? òµ??°O0?~x,B ?ªO] ;'?UDz½??cUP¿}ïWLx?;K=&o¼âC}ÑWAº3{àO\AOé81Ñ BF¥)ú21?c_ìvyêo?AMe6lcp\g ?èöUÉ°_6OrEu[?)??']km?èU¡(á t: aa y: r

with obviously id and nodes being completely unreadable and unusable.显然 id 和节点是完全不可读和不可用的。

anyone wondering how a proper response should look like its in BEP 5任何人想知道在BEP 5中正确的响应应该是什么样子

now with id I could get around this by just taking the bytes and turning the bytes to hex.现在有了 id,我可以通过获取字节并将字节转换为十六进制来解决这个问题。 the problem is the nodes key which holds a lot more information than just a sha-1 id (that being ip and port).问题是节点密钥包含的信息比 sha-1 id(即 ip 和端口)更多。

Edit: forgot to add sometimes the response dictionary doesn't contain a "nodes" key at all despite Wireshark showing it should编辑:有时忘记添加响应字典根本不包含“节点”键,尽管 Wireshark 显示它应该

This is normal because bencoding is foremost a binary format which just sometimes happens to be human-readable.这是正常的,因为编码首先是一种二进制格式,有时恰好是人类可读的。 Bencode "strings" really are byte[] , BEP52 clarifies this. Bencode“字符串”确实是byte[]BEP52澄清了这一点。 The examples in BEP5 use values that just happen to be in the ascii range. BEP5 中的示例使用恰好在 ascii 范围内的值。

I don't have the code with me right now but I'll add it later.我现在没有代码,但我稍后会添加。 Essentially "nodes" key as written in bep 5 is composed of 20 bytes of the id, 4 bytes of the IP, and 2 bytes of the port.本质上,bep 5 中编写的“节点”密钥由 20 个字节的 ID、4 个字节的 IP 和 2 个字节的端口组成。 Using this information you can use this mess of a dump and composed it into a byte array which you can turn into a list of nodes with the information above使用此信息,您可以使用这个乱七八糟的转储并将其组合成一个字节数组,您可以将其转换为具有上述信息的节点列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM