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;
}
}
Related
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.
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
There is a PHP Websocket client: https://github.com/symbiose/php-websocket-client repository. And it works fine for not-secured connection.
What I need is to connect to a websocket server from php code through secure connection. To implement this task I took the code above and modified it a bit. The connect() method of WebSocketClient now looks like this in my code:
public function connect()
{
$root = $this;
if ($this->getPort() == 443) {
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);
$client = stream_socket_client("tls://{$this->getHost()}:{$this->getPort()}", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
} else {
$client = stream_socket_client("tcp://{$this->getHost()}:{$this->getPort()}", $errno, $errstr);
}
if (!$client) {
throw new RuntimeException('Cannot connect to socket ([#'.$errno.'] '.$errstr.')');
}
$this->setSocket(new Connection($client, $this->getLoop()));
$this->getSocket()->on('data', function ($data) use ($root) {
$data = $root->parseIncomingRaw($data);
$root->parseData($data);
});
$this->getSocket()->write($this->createHeader());
return $this;
}
As a result I almost managed to connect to a server. Websocket server saw my connection, replied that everything is ok and wrote it to the log.
But unfortunately, the library doesn't understand, that it has successfully connected to the server.
This "if":
if (base64_encode(pack('H*', sha1($this->key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'))) === $response['Sec-Websocket-Accept']) {
$this->connected = true;
}
never gets executed. And so I can't establish the connection.
I can also confirm, that if we use 80 port - everything works like a charm.
So if you have any ideas on why this can happen, i"d really really appreciate them. Cause I've already ran out of ideas.
Best regards,
Kostya
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 ?? :((
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;