简体   繁体   English

通过代理进行PHP套接字连接

[英]PHP Socket Connection Via Proxy

I am working on an existing library, and i want it to establish connection to socket via proxy only. 我正在处理现有的库,我希望它只通过代理建立与套接字的连接。 Current code is 目前的代码是

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, "www.host.com", 8080);

Where i want this connection to be established via proxy (SOCK4/5) 我希望通过代理建立此连接(SOCK4 / 5)

I've Tried with 我试过了

socket_bind($socket, '127.0.0.1', '9150');

Which is vidalia server, also I've tried some proxies from the internet which got working on firefox as sock but couldn't get this code connecting through it. 哪个是vidalia服务器,我也尝试过一些来自互联网的代理,它们在firefox上作为袜子工作,但无法通过它连接这个代码。

When i try putting above line i got following error. 当我尝试放在线上时,我得到了以下错误。

Warning : socket_bind(): unable to bind address [10048]: Only one usage of each socket address (protocol/network address/port) is normally permitted. 警告 :socket_bind():无法绑定地址[10048]:通常只允许使用每个套接字地址(协议/网络地址/端口)。

Don't want to offend you but it appears that you don't understand basics of TCP/IP networking. 不想冒犯你,但似乎你不了解TCP / IP网络的基础知识。 socket_ functions will not help you to deal with SOCKS proxy as they don't have SOCKS client. socket_函数无法帮助您处理SOCKS代理,因为它们没有SOCKS客户端。

Function socket_bind() is intended for setting source address for connection. 函数socket_bind()用于设置连接的源地址。 Not for binding it to remote service. 不适用于将其绑定到远程服务。 Basically you instruct your socket to appear as coming from 127.0.0.1:9150 and it complains as you probably have your SOCKS proxy running there. 基本上你指示你的套接字显示为来自127.0.0.1:9150并且它抱怨你可能在那里运行你的SOCKS代理。 Even if you would have this address/port available it would not help: you just cannot connect from localhost anywhere else. 即使您有这个地址/端口可用也无济于事:您无法在其他任何地方从localhost连接。

Only correct usage of socket_bind() is after socket_create() and before socket_connect() - after connection is established you cannot change source address. 只有正确使用socket_bind()在socket_create()之后和socket_connect()之前 - 建立连接后,您无法更改源地址。

Having this issue cleared let's get to your issue of having "establish connection to socket via proxy only". 清除此问题后,让我们解决您“仅通过代理与套接字建立连接”的问题。 Honestly your question is really vague on details: what do you want to achieve? 老实说,你的问题在细节上是模糊的:你想要达到什么目的? You need a stream or you just need to call some remote web server with HTTP request? 您需要一个流,或者您只需要通过HTTP请求呼叫一些远程Web服务器?

Given your misunderstanding of sockets_ and that you have "www.host.com", 8080 in your question I believe you looking for some web page pulled from www.host.com:8080 via SOCKS proxy. 鉴于您对套接字的误解,并且您有“www.host.com”,8080在您的问题中我相信您正在寻找通过SOCKS代理从www.host.com:8080拉出的网页。 In this case you don't need to reinvent the wheel and use cURL: see How to use a SOCKS 5 proxy with cURL? 在这种情况下,您不需要重新发明轮子并使用cURL:请参阅如何使用cURL 使用SOCKS 5代理?

If my hunch was wrong and you really need a stream through SOCKS proxy then you need some external library which will do it for you. 如果我的预感错了,你真的需要一个通过SOCKS代理的流,那么你需要一些外部库来为你做。 There no standard PHP extension which will do this for you. 没有标准的PHP扩展可以为您执行此操作。 You may look at https://github.com/clue/php-socks to get data streams over SOCKS. 您可以查看https://github.com/clue/php-socks以获取SOCKS上的数据流。

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

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