stream_socket_client fails on some servers - php

I have a vps with direct admin panel
and a web host with cpanel and litespeed.
I tested this code:
$errno = 0;
$errstr = '';
$fp = #stream_socket_client('uploaded.net:80', $errno, $errstr, 20, STREAM_CLIENT_CONNECT);
on both servers.
in vps server it works successfully but in web host server it fails with connection timeout.
after connection timeout values of my variables were these:
$errno = 110;
$errstr = 'Connection timed out';
now when I tested this code :
$errno = 0;
$errstr = '';
$fp = #stream_socket_client('mihandownload.com:80', $errno, $errstr, 20, STREAM_CLIENT_CONNECT);
it works successfully On both servers!!
but why ?? :((

Related

How do I implement proxy authentication with fsockopen in PHP?

I want to use a proxy with authentication to my below code to communicate and get data from whois servers.
My code is as follows:
$whoisserver = "whois.verisign-grs.com";
$port = 43;
$timeout = 10;
$fp = #fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("Socket Error " . $errno . " - " . $errstr);
fputs($fp, $domain . "\r\n");
$out = "";
while(!feof($fp)){
$out .= fgets($fp);
}
fclose($fp);
In the above I want to use a username and password to authenticate my proxy.
What do I change in the code above to achieve my desired outcome?

Implement MQTT client using PHP to connect MQTT broker

I'm new to the PHP world, facing problem while connecting to MQTT.
I'm using the phpMQTT.php library, and I'm using the IP address to connect to the MQTT broker.
I'm trying to publish to MQTT broker, getting error in phpMQTT.php library file
The error is:
stream_socket_client(): unable to connect to tcp://...*:8083 (Connection timed out)
facing problem in below code:
if ($this->cafile) {
$socketContext = stream_context_create(["ssl" => [
"verify_peer_name" => true,
"cafile" => $this->cafile
]]);
$this->socket = stream_socket_client("tls://" . $this->address . ":" . $this->port, $errno, $errstr, 60, STREAM_CLIENT_CONNECT, $socketContext);
} else {
$this->socket = stream_socket_client("tcp://" . $this->address . ":" . $this->port, $errno, $errstr, 60, STREAM_CLIENT_CONNECT);
}
From the phpMQTT.php library
( source at https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php ) ,
you have to set the following details as shown in the source code.
/* sets the broker details */
function broker($address, $port, $clientid, $cafile = NULL){
$this->address = $address;
$this->port = $port;
$this->clientid = $clientid;
$this->cafile = $cafile;
}
If you have a firewall running - do open the port that you are using as well.

PHPSeclib Proxy send username and password as arguments

Im using PHPSECLIB to send a file and a XML to a SFTP server.
In this case the server im trying to reach is outside my work network.
To connect to the internet outside we have a proxy to do that.
What i need to do is configure the proxy of this connection to the one i need.
EDIT --
I have the following code, how can i pass the username and password of my proxy ?
$proxyHost = '******'
$fsock = fsockopen($proxyHost, $proxyPort);
$address = '*****';
$port = '*****';
$request = "CONNECT $address:$port HTTP/1.0\r\nContent-Length: 0\r\n\r\n";
if(fputs($fsock, $request) != strlen($request)) {
exit("premature termination");
}
$response = fgets($fsock);
$sftp = new SFTP($fsock);
.......
Quoting https://github.com/phpseclib/phpseclib/issues/1339#issuecomment-462224179:
With authorization:
$fsock = fsockopen('127.0.0.1', 80, $errno, $errstr, 1);
if (!$fsock) {
echo $errstr; exit;
}
fputs($fsock, "CONNECT website.com:22 HTTP/1.0\r\n");
fputs($fsock, "Proxy-Authorization: Basic " . base64_encode('user:pass') . "\r\n");
fputs($fsock, "\r\n");
while ($line = fgets($fsock, 1024)) {
if ($line == "\r\n") {
break;
}
//echo $line;
}
$ssh = new Net_SSH2($fsock);
$ssh->login('user', 'pass');
echo $ssh->exec('ls -latr');
If that doesn't work then run the script and tell me what the headers you get back are. Digest authentication is more of a PITA then Basic but it's not impossible.
More info on how authorization works with HTTP proxies:
https://www.rfc-editor.org/rfc/rfc7235#section-4.3

prevent error warnings if connection could not be established

I have some code that makes a connection to public websites and returns information about the SSL certificate - code is working perfectly fine.
I would like to add an if statement where if the connection could not be established then it would return "could not establish connection".. right now, if it cannot connect it produces a whole heap of warnings.
The code responsible for making the socket connection is as follows
MAKE SOCKET CONNECTION
$ctx = stream_context_create( array("ssl" => $ssloptions) );
$result = stream_socket_client("ssl://$url:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
$cont = stream_context_get_params($result);
IF SOCKET CONNECTION FAILED
echo "could not establish connection"
IF CONNECTION SUCCESSFUL
foreach($cont["options"]["ssl"]["peer_certificate_chain"] as $cert) {
openssl_x509_export($cert, $pem_encoded);
echo $pem_encoded;
}
Appreciate the assistance.
$ctx = #stream_context_create( array("ssl" => $ssloptions) );
$result = #stream_socket_client("ssl://$url:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
if($result == false) {
echo "could not establish connection";
} else {
$cont = #stream_context_get_params($result);
foreach($cont["options"]["ssl"]["peer_certificate_chain"] as $cert) {
openssl_x509_export($cert, $pem_encoded);
echo $pem_encoded;
}
}

Apple Push Notification Service connection, through proxy, timed out with stream_socket_client

I'm trying to connect APNS, but i need to pass through a proxy, here the connection test code:
if (!extension_loaded('openssl')) {
exit("need openssl");
}
$http = array();
$http['http']['proxy'] = 'tcp://proxy.net:8080';
$http['http']['request_fulluri'] = true;
$ssl = array();
$ssl['ssl']['local_cert'] = 'ck.pem';
$ssl['ssl']['passphrase'] = 'passphrase';
$opts = array_merge($http,$ssl);
$context = stream_context_create($opts);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $context);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
The problem is that I have always a timeout error, like if i make a direct connection to apple gateway, like if options in stream_context_creat were ognored.
OpenSSL and Socket support are enabled, and port 2195 is open.
Any ideas?
Edit 1:
Trying connect to proxy only it works
$fp = stream_socket_client('tcp://proxy-dmz.pgol.net:8080', $err, $errstr, 60, STREAM_CLIENT_CONNECT);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected!' . PHP_EOL;
Edit 2:
And a script like this (found in some forum) seems to work... i'm stuck
$ip = "tcp://proxy.net"; // proxy IP
$port = "8080"; // proxy port
$url = "http://search.yahoo.com/search?p=test";
$request = "GET $url HTTP/1.0\r\nHost:www.yahoo.com:80\r\n\r\n";
$fp = fsockopen($ip,$port); // connect to proxy
fputs($fp, $request);
$data="";
while (!feof($fp)) $data.=fgets($fp,64000);
fclose($fp);
print $data;

Categories