还是关于PHP的二进制流有关问题
还是关于PHP的二进制流问题
之前发了一帖: http://bbs.csdn.net/topics/391024843
版主给了回答,也能够解析出来,但却发现出来的结果与真实结果完全不一样,比如服务器返回给我的是: ip: 107.145.107.140, port: 26773
但我解析出来却变成了: ip: 46.48.46.48, port: 63271
这样就差的远了, 我用PHP去获取nodes信息,然后将nodes信息自己解析输出一遍,顺便把未解析数据发送给pthon解析一遍,然后两边对比,发现结果却不一样
PHP(使用了swoole):
<?php<br />$serv = new swoole_server('0.0.0.0', 6882, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);<br />$serv->set(array(<br /> 'worker_num' => WORKER_NUM,<br /> 'daemonize' => false,<br /> 'max_request' => MAX_REQUEST,<br /> 'dispatch_mode' => 2,<br /> 'debug_mode' => 1<br />));<br />$serv->on('Start', function($serv){<br /> echo "DHT Server start...\n";<br /> $nid = get_node_id();<br /><br /> $msg = array(<br /> 't' => entropy(2),<br /> 'y' => 'q',<br /> 'q' => 'find_node',<br /> 'a' => array(<br /> 'id' => $nid,<br /> 'target' => $nid<br /> )<br /> );<br /><br /> $serv->sendto(gethostbyname('router.bittorrent.com'), 6881, encode($msg));<br />});<br />$serv->on('Receive', function($serv, $fd, $from_id, $data){<br /> echo "New receive from ip: ";<br /> $msg = decode($data);<br /> $fdinfo = $serv->connection_info($fd);<br /> echo $fdinfo['remote_ip'] . "\n";<br /><br /> if($msg['y'] == 'r'){<br /> if(array_key_exists('nodes', $msg['r']))<br /> //$this->response_actions($msg, array($fdinfo['remote_ip'], $fdinfo['remote_port']));<br /> $nodes = decode_nodes($msg['r']['nodes']);<br /> foreach($nodes as $node){<br /> echo "nid: " . $node->nid . ", ip: " . $node->ip . ", port: " . $node->port . "\n";<br /> }<br /> $serv->sendto('127.0.0.1', 6813, $data);<br /> }<br />});<br /><br />function entropy($length=20){<br /> $s = '';<br /><br /> for($i=0;$i<$length;$i++)<br /> $s .= chr(mt_rand(0, 255));<br /><br /> return $s;<br /> }<br /><br />function get_node_id(){<br /> return sha1(entropy());<br /> }<br /><br />function get_neighbor($target, $nid){<br /> return substr($target, 0, 10) . substr($nid, 0, -10);<br /> }<br /><br />function encode($msg){<br /> return Bencode::encode($msg);<br /> }<br /><br />function decode($msg){<br /> return Bencode::decode($msg);<br /> }<br /><br />function decode_nodes($msg){<br /> $n = array();<br /> $length = strlen($msg);<br /><br /> // 由于每个node都为26位, 若总长度不等于26的倍数则直接返回<br /> if(($length % 26) != 0)<br /> return $n;<br /><br /> $i = 0;<br /><br /> while($i<$length){<br /> //$s = substr($msg, $i, 26);<br /> //$d = unpack('a20nid/Lip/Sport', $s);<br /> //var_dump($d);<br /> //$d = unpack('a20nid/lip/sport', $s);<br /> //var_dump($d);<br /> //$n[] = new Node($d['nid'], long2ip($d['ip']), $d['port']);<br /> $nid = substr($msg, $i, 20);<br /> var_dump($nid);<br /> $ip = substr($msg, $i+20, 4);<br /> var_dump($ip);<br /> $ip = long2ip(unpack('L', $ip)[1]);<br /> $port = substr($msg, $i+24, 2);<br /> var_dump($port);<br /> $port = unpack('s', $port)[1];<br /> var_dump($port);<br /> //$n[] = new Node($nid, $ip, $port);<br /><br /> $i += 26;<br /> }<br /><br /> return $n;<br /> }<br /><br />$serv->start();
python:
#!/usr/bin/env python<br />#encoding: utf-8<br /><br />import socket<br />from hashlib import sha1<br />from random import randint<br />from struct import unpack<br />from socket import inet_ntoa<br />from threading import Timer, Thread<br />from time import sleep<br />from collections import deque<br />from bencode import bencode, bdecode<br /><br />def decode_nodes(nodes):<br /> n = []<br /> length = len(nodes)<br /> if(length % 26) != 0:<br /> return n<br /><br /> for i in range(0, length, 26):<br /> nid = nodes[i:i+20]<br /> ip = inet_ntoa(nodes[i+20:i+24])<br /> ip2 = nodes[i+20:i+24]<br /> print ip2<br /> port = unpack("!H", nodes[i+24:i+26])[0]<br /> port2 = nodes[i+24:i+26]<br /> print port2<br /> print "decode_nodes: nid: %s, ip: %s, port: %s\n" % (nid, ip, port)<br /><br />class DHTServer():<br /> def __init__(self):<br /> self.ufd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)<br /> self.ufd.bind(("0.0.0.0", 6813))<br /><br /> def process_find_node_response(self, msg, address):<br /> nodes = decode_nodes(msg["r"]["nodes"])<br /><br /> for node in nodes:<br /> (nid, ip, port) = node<br /><br /> print "find_node: nid: %s, ip: %s, port: %s\n" % (nid, ip, port)<br /><br /> def run(self):<br /> while True:<br /> try:<br /> (data, address) = self.ufd.recvfrom(65536)<br /> msg = bdecode(data)<br /> self.on_message(msg, address)<br /> except Exception:<br /> pass<br /><br /> def on_message(self, msg, address):<br /> try:<br /> if msg["y"] == "r":<br /> if msg["r"].has_key("nodes"):<br /> self.process_find_node_response(msg, address)<br /> except KeyError:<br /> pass<br /><br />if __name__ == "__main__":<br /> # max_node_qsize bigger, bandwith bigger, spped higher<br /> dht = DHTServer()<br /> dht.run()
------解决思路----------------------
$s = 'P9LI9UagRY0oDfVScSnuyKZHmjRO68KLdlc/0sj1RqBFjSgN9VJxKe7IpkeaNE7rwot2Vz/SyPVGoEWNKA31UnEp7simR5o0TuvCi3ZXP/wj7jU8hMYdclU8RKIZNM7tvZSxt+rIH5A//CPuNTyExh1yVTxEohk0zu29lLG36sgfkD/8I+41PITGHXJVPESiGTTO7b2UsbfqyB+QP/9M6KfuKOE8aeW+E0SS7Ug7UHZlzEY+GuE//0zop+4o4Txp5b4TRJLtSDtQdmXMRj4a4T//TOin7ijhPGnlvhNEku1IO1B2ZcxGPhrhPk6NAzs25ZjxFKrQJrZEjbfkAhg8GYaAGuE+To0DOzblmPEUqtAmtkSNt+QCGDwZhoAa4T5OjQM7NuWY8RSq0Ca2RI235AIYPBmGgBrhPs9ltmz/1Jul2AA0wRDx0d4e2AFR4CzD2+A+z2W2bP/Um6XYADTBEPHR3h7YAVHgLMPb4D7PZbZs/9SbpdgANMEQ8dHeHtgBUeAsw9vgPajAE2YlZG+/uqPNCgQzuP6WjQ7fpv0NGuE=';<br />$s = base64_decode($s);<br />foreach(str_split($s, 26) as $s) {<br /> $r = unpack('a20n/Nip/np', $s);<br /> $r['ip'] = long2ip($r['ip']);<br /> print_r($r);<br />}
Array<br />(<br /> [n] => ????F E?(<br />?Rq)???G?4<br /> [ip] => 78.235.194.139<br /> [p] => 30295<br />)<br />Array<br />(<br /> [n] => ????F E?(<br />?Rq)???G?4<br /> [ip] => 78.235.194.139<br /> [p] => 30295<br />)<br />Array<br />(<br /> [n] => ????F E?(<br />?Rq)???G?4<br /> [ip] => 78.235.194.139<br /> [p] => 30295<br />)<br />Array<br />(<br /> [n] => ?ü#?5<??rU<D?4?í?”<br /> [ip] => 177.183.234.200<br /> [p] => 8080<br />)<br />Array<br />(<br /> [n] => ?ü#?5<??rU<D?4?í?”<br /> [ip] => 177.183.234.200<br /> [p] => 8080<br />)<br />Array<br />(<br /> [n] => ?ü#?5<??rU<D?4?í?”<br /> [ip] => 177.183.234.200<br /> [p] => 8080<br />)<br />Array<br />(<br /> [n] => ??Lè§?(á<i??D’íH;Pv<br /> [ip] => 101.204.70.62<br /> [p] => 6881<br />)<br />Array<br />(<br /> [n] => ??Lè§?(á<i??D’íH;Pv<br /> [ip] => 101.204.70.62<br /> [p] => 6881<br />)<br />Array<br />(<br /> [n] => ??Lè§?(á<i??D’íH;Pv<br /> [ip] => 101.204.70.62<br /> [p] => 6881<br />)<br />Array<br />(<br /> [n] => >N?;6?????&?D?·?<br /> [ip] => 60.25.134.128<br /> [p] => 6881<br />)<br />Array<br />(<br /> [n] => >N?;6?????&?D?·?<br /> [ip] => 60.25.134.128<br /> [p] => 6881<br />)<br />Array<br />(<br /> [n] => >N?;6?????&?D?·?<br /> [ip] => 60.25.134.128<br /> [p] => 6881<br />)<br />Array<br />(<br /> [n] => >?e?l?????4?????<br /> [ip] => 81.224.44.195<br /> [p] => 56288<br />)<br />Array<br />(<br /> [n] => >?e?l?????4?????<br /> [ip] => 81.224.44.195<br /> [p] => 56288<br />)<br />Array<br />(<br /> [n] => >?e?l?????4?????<br /> [ip] => 81.224.44.195<br /> [p] => 56288<br />)<br />Array<br />(<br /> [n] => =¨?f%do????<br />3??–?<br /> [ip] => 223.166.253.13<br /> [p] => 6881<br />)<br />

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

1. Black/white list IP restricted access configuration nginx There are several ways to configure black and white lists. Here are only two commonly used methods. 1. The first method: allow, denydeny and allow instructions belong to ngx_http_access_module. nginx loads this module by default, so it can be used directly. This method is the simplest and most direct. The setting is similar to the firewall iptable. How to use: Add directly to the configuration file: #Whitelist settings, followed by allow is accessible IPlocation/{allow123.13.123.12;allow23.53.32.1/100;denyall;}#Blacklist settings,
