PHP stream_socket_client not using proxy - php

I'm trying to get stream_socket_client working with proxy server.
Peace of code:
<?php
$context = stream_context_create(['http' => ['proxy' => '147.135.210.114:54566', 'request_fulluri' => true]]);
//$file = file_get_contents("http://www.google.com", false, $context);
$fp = stream_socket_client("tcp://www.google.com:80", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fputs($fp, "GET / HTTP/1.0\r\nHost: www.google.com\r\nAccept: */*\r\n\r\n");
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
}
?>
While file_get_contents uses proxy (tcpdump -i any -A host 114.ip-147-135-210.eu) stream_socket_client simply omits it and goes directly to google.com. What am I doing wrong? My final goal is to connect to RabbitMQ (AMQP protocol) via proxy, but I can't even get simple HTTP connection working.

if anyone came here struggling, I ended up solving this by connecting to the proxy first then issuing http headers to get the content I wanted.
First create the socket to the proxy:
$sock = stream_socket_client(
"tcp://$proxy:$port",
$errno,
$errstr,30,
STREAM_CLIENT_CONNECT,
stream_context_create()
);
Second connect to the destination host you want:
$write = "CONNECT www.example.org HTTP/1.1\r\n";
$write .= "Proxy-Authorization: Basic ".base64_encode("$proxy_user:$proxy_pass)."\r\n";
$write .= "\r\n";
fwrite($sock, $write);
This should return a 200 code:
preg_match('/^HTTP\/\d\.\d 200/', fread($sock, 1024));
Now you can just issue GET (make sure you send all the HTTP header):
fwrite($sock, "GET / HTTP/1.1\r\n")
This has more details: https://stackoverflow.com/a/55010581/687976

Related

Stream socket Client ignores context

I am using a code to connect to an smtp server.
For this I am using stream_socket_client but when I a pass a proxy using stream_context_create the socket doesn't take into consideration the proxy.
Here is the code
$xt = stream_context_create(array(
'http' => array(
'proxy' => 'tcp://PROXY:PORT',
)
)
);
$fp = stream_socket_client("tcp://icanhazip.com:80", $errno, $errstr, 3,STREAM_CLIENT_CONNECT,$xt);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "GET / HTTP/1.0\r\nHost: icanhazip.com\r\nAccept: */*\r\n\r\n");
while (!feof($fp)) {
echo fgets($fp);
}
fclose($fp);
}
This code is for testing IP and it outputs the same IP. What am I doing wrong ?
Thanks

PHP fsockopen error, connection refused(111), try to connect fingerprint device using TCP/IP

i was want to connect to my fingerprint device useing TCP/IP
i was setting ip address in device,
my php code:
<?php
$fp = fsockopen("192.168.1.211", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
when i try to connect, i get error connection refused . but ping to device is work, i don't know where is the problem,
My fingerprint device specification:
Vendor :Solution
type : x101-c
i hope somebody can help and solve my problem,,
i really stack now....
but ping to device is work
Ping uses ICMP packets not TCP. If you're getting a connection refused then the (TCP) port is firewalled.

Use Proxy With fsockopen

I have a Pagerank checking script that i'd like to add proxies to. I usually use Curl so im not sure exactly how to go about making the request through a proxy. I want the request to pick a random proxy from the $proxies array and use that for the request. Can anyone walk me through how this is done using this script. Thanks in advance.
$proxyauth = "username:password";
$proxies = array(
"proxy1",
"proxy2",
"proxy3",
"proxy4",
"proxy5",
"proxy6",
"proxy7",
"proxy8",
"proxy9",
"proxy10"
);
function check($page){
// Open a socket to the toolbarqueries address, used by Google Toolbar
$socket = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
// If a connection can be established
if($socket) {
// Prep socket headers
$out = "GET /tbr?client=navclient-auto&ch=".$this->checkHash($this->createHash($page))
."&features=Rank&q=info:".$page."&num=100&filter=0 HTTP/1.1\r\n";
$out .= "Host: toolbarqueries.google.com\r\n";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
$out .= "Connection: Close\r\n\r\n";
// Write settings to the socket
fwrite($socket, $out);
// When a response is received...
$result = "";
while(!feof($socket)) {
$data = fgets($socket, 128);
$pos = strpos($data, "Rank_");
if($pos !== false){
$pagerank = substr($data, $pos + 9);
$result += $pagerank;
}
}
// Close the connection
fclose($socket);
// Return the rank!
return $result;
}
}
With fsockopen, instead of toolbarqueries.google.com you connect to the selected proxy. Then you send a complete URL with the GET request:
$socket = fsockopen("proxy5", 80, $errno, $errstr, 30);
...
$out = "GET http://toolbarqueries.google.com/tbr?client=..."
You can still send the Host header, but you don't need it.

Can I open socket in PHP from a specific IP (if the machine has two IPs)?

I'm using PHPMailer and it uses fsockopen to access the SMTP server.
But the machine has two IPs with different reverse DNS records. So in email headers I got the following:
Received: from one-server.tld (HELO another-server.tld) ...
I need to hide one-server.tld in favor of another-server.tld. But I need both IPs with their current RDNS settings.
I think its not possible using fsockopen. But its possible in curl, fopen and stream functions. What you need is stream_socket_client() function.
Here are some ways to achieve it.
Using context parameters which can be used in fopen function family and stream function family. See the example.
$opts = array(
'socket' => array(
'bindto' => '192.168.0.100:0',
),
);
// create the context...
$context = stream_context_create($opts);
$contents = fopen('http://www.example.com', 'r', false, $context);
Also stream_socket_client
$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $opts);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
}
Using socket_bind. PHP.NET got a simple example here.

PHP connect to external ip with port

I have a dedicated server running a MSSQL and an apache server, only listening to port 8080 (the apache), and i have some XML in there that i want to access from another domain(hosted server).
I tried fsockopen like this:
<?php
$fp = fsockopen("199.175.125.222", 8080, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
but got nothing, only a:
Warning: fsockopen() [function.fsockopen]: unable to connect to 199.175.125.222:8080 (Connection timed out) in /home/reb/public_html/testing/index.php on line 2
Connection timed out (110)
php_sockets is enabled and all...
Any ideas? now im trying WAMP on this dedicated server, to see if any config may help
Thank you

Categories