Apple Push with proxy and stream_context
Asked Answered
S

2

6

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

Selfliquidating answered 19/12, 2012 at 12:3 Comment(13)
Could we see those var_dump outputs please?Emissary
@EduárdMoldován yes it's doneSelfliquidating
Any help here: #6372808? Is there any chance of your isp blocking the port? Or your own router?Emissary
Try this one: telnet gateway.sandbox.push.apple.com 2195Emissary
The answer here #14154735 says, that setting the proxy for http does not apply to ssl:// maybe that is the reason for your problem.Phelia
@BjörnKaiser yes I know but all example for proxy are using http key for stream_context_set_option.Selfliquidating
@Selfliquidating Sorry, haven't seen you asked this question :DPhelia
@BjörnKaiser :D I'm so f*** blocked with this problemSelfliquidating
Really dumb question - is allow_url_fopen enabled ?php.net/manual/en/…Avatar
@Avatar yes I've already checkedSelfliquidating
I think the only thing left I can recommend in using TCPDump to inspect the network packets leaving your server, and seeing if they go to your proxy correctly. If they are then it must be the proxy that is the issue. If the packets aren't leaving your box then it would be an issue with your server. If you're going to view the dumps in Wireshark then the command for tcpdump would be: tcpdump -i <interface> -s 65535 -w <some-file>Avatar
So was it the proxy just not allowing it?Avatar
Have a look at my reply in this post: Send Push Notification to APNS through proxyArachne
A
1

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.

Avatar answered 7/1, 2013 at 14:30 Comment(6)
No, ports 2195 and 2196 are open. If I use fopen() I can log an Apple connection through httpsSelfliquidating
Just to check, you mean something like this works? i.e. fopen with the context you've created? fopen('gateway.sandbox.push.apple.com:2195', "r", false, $ctx );Avatar
Hum no I have : "failed to open stream"Selfliquidating
So even fopen() fails when going through the proxy? It does sound like the proxy is the problem. I added a curl test, which should be the ultimate test of whether the proxy is working or not.Avatar
If I do it using cURL (with gateway.sandbox.push.apple.com:2195 and my certificate) i have : " Empty reply from server" code 52Selfliquidating
And what about fsockopen function with proxy ?Selfliquidating
A
0
  • 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

Arachne answered 6/10, 2015 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.