我正在尝试建立一个安全的websocket连接,但是我的数据似乎没有得到解密。尝试了几种读取数据的方法,但不起作用。
有没有人知道是什么原因引起的?
我的套接字服务器
//Create the main socket
$context = stream_context_create();
// local_cert must be in PEM format
stream_context_set_option($context, 'ssl', 'local_cert', '/storage/chat/Jongeren.pem');
stream_context_set_option($context, 'ssl', 'passphrase', '*****');
// Create the server socket
$this->master = stream_socket_server(
"tls://$addr:$port",
$errno,
$errstr,
STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,
$context
);
stream_set_blocking($this->master, 0);
$this->Sockets['master'] = $this->master;
if($errno > 0){ //An error has occured
log::debug("Main socket error ($errno): $errstr");
die();
} else {
log::info("Main socket started, listening on $addr:$port");
}
while(true) {
//Select sockets witch activity
$read = $this->Sockets;
$changed = stream_select($read,$write,$except,1);
//Process changed
if( (false !== $changed) && ($changed > 0) ){
//Accept new connections
if( in_array($this->master, $read) ){
$new_client = stream_socket_accept($this->master, 1, $remote_host);
if ($new_client) {
$index = uniqid();
$this->Sockets[$index] = $new_client;
$this->users[$index] = new $this->userClass($remote_host);
$this->ConnectionSettings[$index] = new Connection();
log::info('incoming connection: '.$remote_host);
log::info('client count: '. (count($this->Sockets)-1) );
} else {
$err = socket_last_error($socket);
log::debug("Socket error: ".$err.": ".socket_strerror($err), NULL,NULL);
}
}
//delete the server socket from the read sockets
unset($read['master'], $new_client, $remote_host);
//Process changed sockets
foreach ($read as $socket) {
if( false !== ($buffer = stream_socket_recvfrom($socket,8192) ) ){
$sID = $this->getSocketID($socket);
if( empty( $buffer ) ){
$this->disconnect($sID, false);
} else {
log::info( 'Data: '.$buffer );
}
}
}
}
}
数据输出:
2015-12-03T21:35:46+01:00 >> Main socket started, listening on 192.168.1.100:9090
2015-12-03T21:35:51+01:00 >> incoming connection: 192.168.1.1:43022
2015-12-03T21:35:51+01:00 >> client count: 1
2015-12-03T21:35:51+01:00 >> Data: lw���ٮ(0�����MO��gyp��³sy��,b7��>��-�F�}ǮsF'g�EMg�c8�
�F
�z�F�k{ 5�dP�IQ�+��ڞ���~�Q������h5v��Z��FEɸ�� .�~�͉���ªgf\=���Y>�2�
H�_��E�qS��� ����I��.�W
/�V �
�Z\���
�u���o�����"�V�)J[���;>�O��y���3?��,��'Ğ-�y�3ݱ1qid�;He�2o�(wyo��C����rt��{[K%�U2�'����"�_�ζA���m
7�4* ���4ؑ����_���Y�7RO�˝�}J�t���Д�Iݼ`�&KƉ�pƫ,�x�
�l'~/���NL,��@��<ݿ�U{��'�7r5��w��X�|Va�~f���^���R��5��������Y�K0��.QN�c'�p{-c��y�{��Ыr���:ZʬH͞$\�Tl��SQ��*��[���7|#
S0�����F���Sh)bL3&�E
���czE��J�#��Q.��Q �����'�E
答案 0 :(得分:1)
我终于解决了它。
//Create the main socket
$context = stream_context_create();
// local_cert must be in PEM format
stream_context_set_option($context, 'ssl', 'local_cert', 'file.pem');
stream_context_set_option($context, 'ssl', 'passphrase', '***');
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);
// Create the server socket
$this->master = stream_socket_server(
"ssl://$addr:$port",
$errno,
$errstr,
STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,
$context
);
$this->Sockets['master'] = $this->master;
if($errno > 0){ //An error has occured
log::debug("Main socket error ($errno): $errstr");
die();
} else {
log::info("Main socket started, listening on $addr:$port");
}
while(true) {
//Select sockets witch activity
$read = $this->Sockets;
stream_select($read,$write,$except,60);
if(count($read) > 0){
$t = count($this->Sockets)-1;
$r = count($read);
$w = count($write);
$e = count($except);
log::info( 'T:'.$t.' R:'.$r.' W:'.$w.' E:'.$e );
}
//Accept new connections
if( in_array($this->master, $read) ){
$new_client = stream_socket_accept($this->master, 1, $remote_host);
if ($new_client) {
$index = uniqid();
$this->Sockets[$index] = $new_client;
$this->users[$index] = new $this->userClass($remote_host);
$this->ConnectionSettings[$index] = new Connection();
log::info('Accepted connection: '.$remote_host);
} else {
$err = socket_last_error($socket);
log::debug("Socket error: ".$err.": ".socket_strerror($err), NULL,NULL);
}
}
//delete the server socket from the read sockets
unset($read['master'], $new_client, $remote_host );
//Process changed sockets
foreach ($read as $socket) {
$buffer = fread($socket, 2046);
$sID = $this->getSocketID($socket);
if( empty( $buffer ) ){
$this->disconnect($sID, false);
} else {
log::info( 'Data: '.$buffer );
if (!$this->ConnectionSettings[$sID]->shake) { //HANDSHAKE
$this->doHandshake($sID,$buffer);
} else if ($message = $this->deframe($sID, $buffer)) { //MESSAGE
$this->process($sID, utf8_encode($message));
}
}
}
//cleanup
unset($read,$r,$write,$w,$except,$e,$socket,$buffer);
}
问题是我没有使用正确的证书。我多么愚蠢:(
所以这些事情很重要: