I am using the certificate, and the private key
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $certfile);
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.xyz.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
Its running in my local XAMPP Server, but its not working in the external server:
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /home/biranchi/public_html/push.php on line 42
Failed to connect 110
What is the error? Do i need to change some setting in the server?
I had fixed the issue by opening the port 2195 on the production server. You can verify by following command $telnet gateway.push.apple.com 2195
-bash-3.2# telnet gateway.push.apple.com 2195
Trying 17.149.38.141...
Connected to gateway.push.apple.com (17.149.38.141).
Escape character is '^]'.
Connection closed by foreign host.
Check your personal firewall settings and make sure you're not blocking this out. Try disabling the firewall.
Also, some APIs like requests to come from an actual domain rather than a desktop. I don't have reason to believe Apple works this way, but that's something to check also.
Also make sure and ping gateway.sandbox.push.apple.com and make sure you have a good connection.
You have to set your firewall to allow all the 17.0.0.0/8 block (it all belongs to Apple!). Check THIS ANSWER
And according to Apple:
The APNs servers use load balancing, so your devices won't always connect to the same public IP address for notifications. It's best to allow access to these ports on the entire 17.0.0.0/8 address block, which is assigned to Apple.
If you are using CSF firewall (like me), I'd recommend to add this line to csf.allow file:
tcp|out|d=2195|d=17.0.0.0/8
Then restart CSF. Using the above instead of just "17.0.0.0/8" will allow only outbond connections to Apple and specifically to port 2195. NSA won't like it but this is much more precise and safe! ;)
Related
I have hosted my PHP code on google cloud.
I want to send push notifications to ios app. I have enabled port 2195 and 2196.
While sending the push notification I got the following error :
Warning: stream_socket_client(): SSL: Connection reset by peer
Warning: stream_socket_client(): Failed to enable crypto
Warning: stream_socket_client(): unable to connect to
ssl://gateway.push.apple.com:2195 (Unknown error)
I am not much familiar with Google Cloud. What should I do to make it working?
Here is code:
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', PEM_FILE_PATH . 'apns-dev.pem');
$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
$data['msgs'] = "Failed to connect $err $errstr \n";
} else {
$payload = json_encode($body);
$msg = chr(0) . pack("n", 32) . pack("H*", str_replace(" ", "", $deviceToken)) . pack("n", strlen($payload)) . $payload;
$result = fwrite($fp, $msg);
if (!$result) {
$data['msgs'] = 'Message not delivered'; //. PHP_EOL;
} else {
$data['msgs'] = 'Success'; //. PHP_EOL;
}
fclose($fp);
}
return $data;
The main problem when we are trying to send data to the APNS (Apple Push Notification Service) servers is the SSL certificates.
APNS uses this technology in order to serve more secure connection to its users.
As it is said at APNS documentation: "Each certificate is limited to a single app and is also limited to one of two development environments, each with its own assigned hostname". So you can use two environments
Development (testing environment): ssl://gateway.sandbox.push.apple.com:2195
Production (once the app is launched): ssl://gateway.push.apple.com:2195
If you want to test if you can connect to APNS server, just try the following command:
$ telnet gateway.sandbox.push.apple.com 2195
Trying 17.172.232.226...
Connected to gateway.sandbox.push-apple.com.akadns.net.
Escape character is '^]'.
If you get an error then make sure your firewall allows outgoing connections on port 2195.
Then you can test if your SSL certificate and private key are working and it can be set up a secure connection:
$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert YourDevCert.pem -key YourPrivateKey.pem
Enter pass phrase for YourPrivateKey.pem: ******
If this works it means that your certificates are correctly set up (you should see a whole bunch of output, which is openssl letting you know what is going on under the hood).
Once knowing all of this information, I see that you have one mistake in your code and also you should check something else:
Check if you have a good connection with APNS server.
Check that your $payload variable is a json string.
Check that you have a correct $deviceToken.
Check that you are using the right certificate with the right environtment. In this case, you are setting a apns-dev.pem certificate and you are sending it to the production environment (I interpret that your production certificate is apns-prod.pem so check it).
Check that your PHP file can find your certificate.
One of your problems, you haven't set any password for your private key. You should add the following line once you have added your certificate:
stream_context_set_option($ctx, "ssl", "passphrase", "your_private_key");
If you have some troubles or doubts, I followed this tutorial to send my first APNS Push Notifications.
I am currently using a tcp socket to exchange data between a server and a client using socket streams in PHP. I want each client to have a unique certificate (which I will generate for them) to access my server so that only people I know will be able to use it.
I have searched and found only how to use certificate on the server side, which will enable me to make a SSL socket connection, but what I actually need is to use a client server certificate to identify the client that is connecting to my server. Is this possible using PHP ?
I'd imagine the client to connect like this:
$clientCert = './clientCert.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $clientCert);
$server = stream_socket_client('ssl://IP', $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
But how does the server handle (identify, etc) this client side certificate ?
Hope I was clear with my question.
Thanks in advance,
Rafael
You should use stream_context_set_option to set verify_peer to true; this will require the other side to verify that the client's certificate is trusted. The verification is done by checking that the certificate is signed by a trusted authority, so you need to specify where the authority's public key can be found through either the cafile or capath option.
Note that the above holds true both on the server side (to allow you to authenticate your clients) and on the client side -- the server is also not authenticated unless you explicitly make it so.
Problem:
I cannot access APNS server. I get a 110 Connection Time Out error.
My situation:
I've contacted my host (hostmonster). They said my port 2195,2196 are already open.
My certificate and passphrase is no problem, for I've tested it from my local Mac.
I use a very simple PHP to test. Here's my code:
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apn.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'aaa');
$fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err,
$errstr,60,STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,$ctx);
if ($fp)
echo ('aa');
else
echo ($err.$errstr);
I had the same exact problem, but with Bluehost. In my case, it turned out the Bluehost firewall was not properly open. The first line chat support from Bluehost said the ports 2195 & 2196 are open, and the problem was not with them. To demonstrate the problem was on their side, I asked them to run the following telnet command from the Bluehost server:
telnet gateway.push.apple.com 2195
Trying 17.149.36.246...
Trying 17.149.35.166...
Trying 17.149.35.170...
Trying 17.149.35.177...
... Connection timed out
The output should be:
telnet gateway.push.apple.com 2195
Trying 17.149.36.230...
Connected to gateway.push.apple.com.
Escape character is '^]'.
Then i submitted a support ticket to Bluehost with this information. They resolved the issue in 6 hours by properly opening the firewall.
I have to send push notification to iOS devices. My connection has to be enabled through a proxy. I tried everything but without success. I have an error 110 Connection Timed Out. It's working with cURL if I just try to connect to Apple push's address. I don't know where the problem is. Proxy config ? PHP stream_context wrong implementation ?
Here's my code :
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'certificate.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'my_passphrase');
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'http', 'proxy', 'tcp://my-proxy.net:8080');
stream_context_set_option($ctx, 'http', 'request_fulluri', true);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
var_dump($fp);
var_dump($err);
var_dump($errstr);
exit;
Do you have an idea ?
EDIT:
Can it be directly linked to Squid ? I just figured out the proxy is running with Squid.
I also try with fopen() function instead of stream_socket_client() but it seems it doesn't allow ssl protocol.
Here's my var_dump outputs : bool(false) int(110) string(20) "Connection timed out"
I also have this warning : Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /share/www/website/test.php on line 22
It could simply be your proxy does not allow port 2195 to be opened.
iPhone Push Notification Unable to Connect to the SSL Server
I guess you either:
Need to talk to the people who run the proxy to see if port 2195 is open or not.
or
Setup a test server listening on port 2195 and then try to do a test connection to it through the proxy. That should allow you to test if it is the proxy that is blocking the connection requests.
or
Test whether Curl can open the connection using a proxy.
Which is done by setting the options:
// sets the proxy to go through
curl_setopt($ch, CURLOPT_PROXY, $proxy);
// sets to use a tunnel proxy which most http proxies are
curl_setopt($ch, CURLOPT_HTTPTUNNELPROXY, $proxy);
Full testing code here.
create the SSL context
open a tcp socket to the proxy
send request to the proxy to connect to APNs
once connexion is accepted enable SSL
Have a look at my reply in this post: Send Push Notification to APNS through proxy
I have a PHP file with the following content that works perfectly on development ceritficates, but when I switch to a production certificate the PHP errors and gives the below message, but it only does this about 50% of the time. The other 50% it works. Anyone know why this might be happening?
<?php
// masked for security reason
$deviceToken = 'xxxxxx'; // jq
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', dirname(__FILE__)."/prod.pem");
$number = 5;
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstr\n";
}
else {
print "Connection OK\n";
$msg = $_GET['msg'];
$payload['aps'] = array('alert' => $msg, 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
}
?>
The PHP error:
Warning: stream_socket_client() [function.stream-socket-client]: Unable to set local cert chain file `/var/www/vhosts/thissite.com/httpdocs/prod.pem'; Check that your cafile/capath settings include details of your certificate and its issuer in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19
Warning: stream_socket_client() [function.stream-socket-client]: failed to create an SSL handle in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19
Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19
Failed to connect 0
I had the same issue. You have to make a persistent socket connection with Apple's Push Notification Server. I've written up a tutorial for a python daemon called pyapns (http://github.com/samuraisam/pyapns) which worked very well for me:
http://www.how2s.org/index.php/How_to_get_started_with_Apple_Push_Notifications_for_iPhone_or_iPhone_Touch
This works assuming you are running Debian and have root access to install the required packages such as python-twisted, libcurl4-openssl-dev etc.
Sounds like too many connects. Apple's docs state that you need to hold the connection open and send as many as you can at the same time. Re-opening is considered DOS attack. So try making it persistent and see if you get same error.
I don't know if the error you are experiencing are because of too many connects to the push servers... In my experience, those limits are a bit hard to reach.
But PHP on the other hand have been acting strange when I've tried to send batches of push notifications. I'm not sure from your sample code, but I guess you do a stream_socket_client() and fclose() for every message? Using that technique with SSL sockets in PHP, the only thing I've personally have accomplished is failure...
I'm not sure if you have the possibility to run Ruby on your server, but if you can, I recommend switching to ruby-apns-daemon to handle the talk with Apple's servers. It's lightweight and easy to implement in PHP (you practically compose the same payload-JSON, but send it to ruby-apns-daemon instead of through a socket).
I've had the same issue and certificate was in fault. You can see solutions here How can I do an SSL connection with PHP and here Error using ssl cert with PHP.
Hope it'll help you.
And for the record you are not obliged to make a persistent connection with APNS. Though it's best to send all your messages at once, you can connect and disconnect multiple times. I quote Apple's website :
You should also retain connections
with APNs across multiple
notifications. APNs may consider
connections that are rapidly and
repeatedly established and torn down
as a denial-of-service attack. Upon
error, APNs closes the connection on
which the error occurred.
If you don't create hundred of connections at a time you should not get troubles.