检查PHP中的SOCKS4还是5?
基本上,我有一个代理列表。我想将它们分为 SOCKS4 和 SOCKS5。我想编写一个小型 PHP 脚本来为我完成此任务。我将如何在 PHP 中检测它的类型?
Basically, I have a list of proxies. I'm wanting to separate them into SOCKS4 and SOCKS5. I'd like to code up a small PHP script to do this for me. How would I go about detecting which type it is in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您能做的最好的事情就是首先尝试通过尝试最高版本 - 5 来建立 CURL 连接。
无论哪种方式,这都会给您答案。执行后检查
curl_error
。如果没有错误,则您使用的是 SOCKS5,否则您使用的是 SOCKS4。I think the best you can do is to first try to establish a CURL connection by trying the highest version - 5.
This will give you the answer either way. Check
curl_error
after you execute it. If there is no error, you are using SOCKS5, else you are using SOCKS4.根据 RFC1928,要建立 SOCKS 连接,首先将这些字节发送到服务器:
服务器响应:
这在 SOCKS 的第 4 版和第 5 版之间很常见。因此,您可以从一个版本(例如 5)开始,如果服务器没有相应响应,则回退到另一个版本。
According to RFC1928, for establishing a SOCKS connection you start by sending these bytes to the server:
And server responds with
This is common between both 4th and 5th versions of SOCKS. So you can start by one version (5, for example) and fall back to another version if server doesn't respond accordingly.
您需要自己编写一些小代码来尝试连接任何代理并检查袜子版本。不同版本的连接协议和错误代码记录在有关 SOCKS 的维基百科页面上。
考虑到这一点,剩下的或多或少是与 PHP 的标准套接字连接。
示例:
输出:
此示例性实现仅检查 SOCKS4,但可以通过添加类似于
isSocks4()
的方法轻松扩展以测试 SOCK4a 和 SOCKS5:希望这会有所帮助。如果引入多个版本,则应该改进错误处理,并且如果在之前的测试中连接失败,则不要多次连接到同一主机。
You need to write yourself some little code that tries to connect with any of your proxies and inspect the socks version. Connection protocol for the different versions and error codes are documented on the wikipedia page about SOCKS.
Taking that into account, the rest is more or less standard socket connection with PHP.
Example:
Output:
This exemplary implementation does only check for SOCKS4 so, but it could be easily extended to test as well for SOCK4a and SOCKS5 by adding methods similar to
isSocks4()
:Hope this is helpful. If you introduce multiple versions, you should improve the error handling and don't connect more than once to the same host if the connection failed in a previous test.