Reputation: 51
I have used socket to check the status for proxy with ports (IP:Port) to see if the proxy is working or not, i have used socket to do that( not curl or fsockopen
I'm using this codes the second example with a little changes.
I will try to explain the current problem which i have with a live example.
IP: 109.195.98.21 have 2 open ports (80,3129)
Socket can open the both ports however 3129 is the real one which can connect and use it as a proxy and not possible for port 80 even it's open, i really can't understand the reason and how i just can detect the open port which is open and possible to connect with it like (3129) . any idea please? thanks.
Upvotes: 0
Views: 128
Reputation: 9162
So this simplified code will let you check if a port is open or not and that's it.
<?php
$socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
$response = socket_connect( $socket, '109.195.98.21', 3129 );
if ( $response === false ) {
printf( 'socket_connect failed: %s',
socket_strerror( socket_last_error( $socket ) ) );
} else {
printf( 'Connected!' );
socket_close( $socket );
}
?>
However, since you're after an actual proxy, and as you say, there is no difference between merely opening 80 vs. 3129 on your server you'll have to send some data downstream and see what happens.
This depends on what proxy is running. SOCKS? HTTP?
For HTTP, you need to send an HTTP request and expect a sane response. Some HTTP proxies redirect to unwanted locations regardless of what you request. So you'd send a tiny HTTP payload and see what it gets you.
For SOCKS, read the protocols http://en.wikipedia.org/wiki/SOCKS#Protocol and send the required payloads to verify whether they work or not.
Use socket_write
, socket_read
for this.
More information:
fsockopen
instead, which supports talking to proxies out of the box and easier than trying to emulate the protocolOf course, an easier way would be to use netcat
or curl
perhaps.
Upvotes: 1