Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi, walkor. Need PHPSocketIO secure(https) #35

Closed
saikat-squareloop opened this issue Aug 20, 2016 · 23 comments
Closed

Hi, walkor. Need PHPSocketIO secure(https) #35

saikat-squareloop opened this issue Aug 20, 2016 · 23 comments

Comments

@saikat-squareloop
Copy link

I am developing webrtc signaling server with phpsocketio for video chat application. So i need php socket io server as secure(https).. how to make php socket io server ssl .. This library is awesome. i can't leave this because of this silly reason. please help me out. Thanks. mail Id : saikatdutta1991@gmail.com
can you please help me out ?

@saikat-squareloop saikat-squareloop changed the title Hi, walkor Hi, walkor. Need PHPSocketIO secure(https) Aug 20, 2016
@walkor
Copy link
Owner

walkor commented Aug 20, 2016

You can use nginx as ssl proxy.

@saikat-squareloop
Copy link
Author

Thanks for you reply . But we are developing a dating script. So all of our clients cant have nginx server. Can't we do like this as we do in node js

var options = {
key : fs.readFileSync('private.key'),
cert : fs.readFileSync('public.cert')
};

var server = tls.createServer(options, function (res) {
res.write('Hello World!');
res.pipe(res);
}).listen(8000);

Thanks for you valuable reply.

@saikat-squareloop
Copy link
Author

saikat-squareloop commented Aug 22, 2016

I have gone through Workerman\Worker class and i have found that stream_context_set_option() php function is being used.. I have seen in stackoverflow that we make socket ssl with this context funcitons. But i dont know exactly how. can you help me out ?

@walkor
Copy link
Owner

walkor commented Aug 23, 2016

I've tried it before. The results is workerman using nonblocking socket which seems breaks the handshake of ssl.
So I do not think that making ssl with context funtions will work with workerman.

@saikat-squareloop
Copy link
Author

Oh..then it's my bad luck.. By default socketIO creates http server.. Can i change the protocol to wss ?

@walkor
Copy link
Owner

walkor commented Aug 23, 2016

If you want to use PHPSocket.IO and need wss, using Nginx/Stunnel as ssl proxy is the only way I can think of.

@saikat-squareloop
Copy link
Author

Ok. Thanks ..

@saikat-squareloop
Copy link
Author

Hey, man..
do something for ssl except proxytunnel.. Please help me.

@saikat-squareloop
Copy link
Author

saikat-squareloop commented Sep 6, 2016

I have solved ssl ..

i have added ssl context (my self signed certificated)

$context_option["ssl"] = [
"local_cert" => "/var/www/html/server.pem",
'passphrase' => "testing",
'allow_self_signed' => true,
'verify_peer' => false

        ];

But i have changed non blocking to blocking .. stream_set_blocking($this->_mainSocket, true);

this will cause any performace issue ?

this is working fine.. now i can use wss directly . . thanks for you awesome library.

@NodeLines
Copy link

saikat-squareloop, Hi, can u help me, in which file u added $context_option? Can u write detailed?

@adrianofoschi
Copy link
Contributor

adrianofoschi commented Jan 22, 2017

I would to know how to apply SSL directly too please

@topin topin mentioned this issue Jul 5, 2016
@punithbm
Copy link

punithbm commented Feb 3, 2017

Please refer this -- #73

@walkor
Copy link
Owner

walkor commented Feb 7, 2017

Apply SSL directly.

use Workerman\Worker;
use PHPSocketIO\SocketIO;

$context = array(
    'ssl' => array(
        'local_cert'  => '/your/path/of/server.pem',
        'local_pk'    => '/your/path/of/server.key',
        'verify_peer' => false,
    )
);
$io = new SocketIO(2021, $context);
$io->on('connection', function($socket)use($io){
  $socket->on('chat message', function($msg)use($io){
    $io->emit('chat message', $msg);
  });
});

Worker::runAll();

Refer #72 #71
walkor/workerman@f31dfd2
walkor/workerman@6ba6d72

@punithbm
Copy link

punithbm commented Feb 9, 2017

Hey, That not working for https .. it works with http://yoursite.com:2020

@walkor
Copy link
Owner

walkor commented Feb 9, 2017

That works fine for me.
For support SSL phpsocket.io>=v1.1.1 and workerman>=3.3.7 are required.

@punithbm
Copy link

punithbm commented Feb 9, 2017

@walkor Hey, Updated it .. working now .. Thank you

@anandselva
Copy link

anandselva commented May 31, 2017

@walkor

I have tried to connect HTTPS host and I am having latest phpsocket.io and workerman, but still I got below issue,

can you please help me out from this? May be I missed something.

JS

var socket = io('https://'+document.domain+':2120');

start.io

$context = array(
    'ssl' => array(
        'local_cert' => __DIR__. '/server.pem',
        'local_pk'   => __DIR__ .'/server.key',
        'verify_peer' => false,
    )
);
$sender_io = new SocketIO(2120, $context);

and

$sender_io->on('workerStart', function(){
   $inner_http_worker = new Worker('https://0.0.0.0:2121');

Web server:

$web = new WebServer('https://0.0.0.0:2121');

Exception I am getting when I start:

exception 'Exception' with message 'class \Protocols\Https not exist' in /var/www/ws_php/vendor/workerman/workerman/Worker.php:1420
Stack trace:
#0 /var/www/ws_php/start_io.php(99): Workerman\Worker->listen()
#1 [internal function]: {closure}(Object(Workerman\Worker))
#2 /var/www/ws_php/vendor/workerman/workerman/Worker.php(1524): call_user_func(Object(Closure), Object(Workerman\Worker))
#3 /var/www/ws_php/vendor/workerman/workerman/Worker.php(936): Workerman\Worker->run()
#4 /var/www/ws_php/vendor/workerman/workerman/Worker.php(899): Workerman\Worker::forkOneWorker(Object(Workerman\Worker))
#5 /var/www/ws_php/vendor/workerman/workerman/Worker.php(1050): Workerman\Worker::forkWorkers()
#6 /var/www/ws_php/vendor/workerman/workerman/Worker.php(434): Workerman\Worker::monitorWorkers()
#7 /var/www/ws_php/start.php(12): Workerman\Worker::runAll()
#8 {main}
WORKER EXIT UNEXPECTED 
worker[PHPSocketIO:8753] exit with status 64000

@walkor
Copy link
Owner

walkor commented May 31, 2017

$context = array(
    'ssl' => array(
        'local_cert' => __DIR__. '/server.pem',
        'local_pk'   => __DIR__ .'/server.key',
        'verify_peer' => false,
    )
);
//Do not set https but http.
$inner_http_worker = new Worker('http://0.0.0.0:2121', $context);
$inner_http_worker->transport = 'ssl';
$context = array(
    'ssl' => array(
        'local_cert' => __DIR__. '/server.pem',
        'local_pk'   => __DIR__ .'/server.key',
        'verify_peer' => false,
    )
);
// Do not set https but http.
$web = new WebServer('http://0.0.0.0:2121', $context);
$web->transport = 'ssl'; 

@anandselva
Copy link

anandselva commented May 31, 2017

Thank you for the quick reply @walkor

I have changed as you mentioned, please find below the structure;

JS:

var socket = io('https://'+document.domain+':2120');

start.io:

$context = array(
    'ssl' => array(
        'local_cert' => __DIR__. '/fit.pem',
        'local_pk'   => __DIR__ .'/fit.key',
        'verify_peer' => false,
    )
);
$sender_io = new SocketIO(2120, $context);
$context = array(
        'ssl' => array(
            'local_cert' => __DIR__. '/fit.pem',
            'local_pk'   => __DIR__ .'/fit.key',
            'verify_peer' => false,
        )
    );
$inner_http_worker = new Worker('http://0.0.0.0:2124', $context);
$inner_http_worker->transport = 'ssl';

start_web:

$context = array(
    'ssl' => array(
        'local_cert' => __DIR__. '/fit.pem',
        'local_pk'   => __DIR__ .'/fit.key',
        'verify_peer' => false,
    )
);
$web = new WebServer('http://0.0.0.0:2124', $context);
$web->transport = 'ssl'; 

I got exception like blow:

PHP Warning:  stream_socket_server(): unable to connect to tcp://0.0.0.0:2124 (Address already in use) in /var/www/ws_php/vendor/workerman/workerman/Worker.php on line 1443

Warning: stream_socket_server(): unable to connect to tcp://0.0.0.0:2124 (Address already in use) in /var/www/ws_php/vendor/workerman/workerman/Worker.php on line 1443
exception 'Exception' with message 'Address already in use' in /var/www/ws_php/vendor/workerman/workerman/Worker.php:1445
Stack trace:
#0 /var/www/ws_php/start_io.php(109): Workerman\Worker->listen()
#1 [internal function]: {closure}(Object(Workerman\Worker))
#2 /var/www/ws_php/vendor/workerman/workerman/Worker.php(1524): call_user_func(Object(Closure), Object(Workerman\Worker))
#3 /var/www/ws_php/vendor/workerman/workerman/Worker.php(936): Workerman\Worker->run()
#4 /var/www/ws_php/vendor/workerman/workerman/Worker.php(899): Workerman\Worker::forkOneWorker(Object(Workerman\Worker))
#5 /var/www/ws_php/vendor/workerman/workerman/Worker.php(1050): Workerman\Worker::forkWorkers()
#6 /var/www/ws_php/vendor/workerman/workerman/Worker.php(434): Workerman\Worker::monitorWorkers()
#7 /var/www/ws_php/start.php(12): Workerman\Worker::runAll()
#8 {main}
WORKER EXIT UNEXPECTED 
worker[PHPSocketIO:343] exit with status 64000

Please help me out, thanks in advance!

Edit: I have already changed some ports and exception still same.

-Anand

@anandselva
Copy link

anandselva commented May 31, 2017

@walkor , it seems the server has started but still got above error and it says "Address already in use" repeatedly until I use Ctrl-C to stop it.

When I tried to send a message using curl to the server while running it is not sending, it seems socket not available, curl returns 'false' after long execution time.

> php start.php start
Workerman[start.php] start in DEBUG mode
----------------------- WORKERMAN -----------------------------
Workerman version:3.3.6          PHP version:5.3.3
------------------------ WORKERS -------------------------------
user          worker        listen                   processes status
root          PHPSocketIO   socketIO://0.0.0.0:2120   1         [OK] 
root          WebServer     http://0.0.0.0:2124       1         [OK] 

And, my curl to send the message:

$to_uid = '';
$host_name = $_SERVER['HTTP_HOST'];
$push_api_url = "http://". $host_name .":2124/";
$post_data = array(
   'type' => 'publish',
   'content' => 'Be good!',
   'to' => $to_uid, 
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
$return = curl_exec ( $ch );
curl_close ( $ch );
print '<pre>'; print_r($return); print '</pre>';

Edit: Updated workerman version but still the same issue.

# php start.php start
Workerman[start.php] start in DEBUG mode
----------------------- WORKERMAN -----------------------------
Workerman version:3.4.2          PHP version:5.3.3
------------------------ WORKERS -------------------------------
user          worker        listen                   processes status
root          PHPSocketIO   socketIO://0.0.0.0:2120   1         [OK] 
root          WebServer     http://0.0.0.0:2124       1         [OK] 

@walkor
Copy link
Owner

walkor commented May 31, 2017

Yes you have another process bound to the same port 2124.
If $inner_http_worker bound to 2124 then $web can not bound to 2124 again.

@anandselva
Copy link

Yes @walkor, I have changed $inner_http_worker port into 2123 and server running without giving me any error, thanks much to you.

I just need one more help to send a message to the server, as I mentioned above I am sending a message using curl and my port is 2124, after taking some time for execution, it says 'Unable to connect'.

And I have tried the below one too,

https://my_host_name:2124/?type=publish&content=hello&to=1496290726000
https://my_host_name:2123/?type=publish&content=hello&to=1496290726000

Can you please give me some suggestion on this? I really need this.

Thanks in advance.

@walkor
Copy link
Owner

walkor commented Jun 1, 2017

Please create a new issue.
And show detail error message.

@walkor walkor mentioned this issue Dec 31, 2017
@walkor walkor closed this as completed Aug 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants