带有openSSL的libwebsocket服务器不接受连接

libwebsocket server with openSSL not accepting connection

本文关键字:不接受 连接 服务器 libwebsocket openSSL 带有      更新时间:2023-10-16

我在(libwebsocket库(的帮助下编写了web套接字服务器,它接受非SSL的web套接字客户端连接。现在我想让它接受SSL连接,所以我生成了自签名证书和密钥,在创建web套接字上下文时,我还提供了密钥和证书路径以及选项LWS_SERVER_option_DO_SSL_GLOBAL_INIT。但是在使用进行https连接时wss://ip:7681我正在从中获取连接请求回调,即LWS_callback_SERVER_NEW_CLIENT_INSTANTIATED,之后LWS_callback_WSI_DESTROY和浏览器中获取关于无法连接的控制台错误。

Firefox can’t establish a connection to the server at wss://192.168.4.254:7681/.    

请检查以下用于创建基于openSSL的web套接字服务器的服务器端代码。

struct lws_protocols WebSocketCommon::protocols[ 2 ] = { {"wss", WebSocketCommon::callback, 0,    0 },{ NULL, NULL, 0, 0 } };
int callback ( struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len ) {
switch ( reason ) {
case LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED:
{
//code 
break;      
}
case LWS_CALLBACK_WSI_DESTROY: 
{
//code 
break;
}  
case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: {
Log::d( m_r_logger, TAG, "LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTSn");
SSL_CTX_load_verify_locations( (SSL_CTX*) user, NULL, getenv(SSL_CERT_FILE_PATH) );
break;
}
default: {
break;
}
}
return lws_callback_http_dummy(wsi, reason, user, in, len);
} 
void createContext (bool useSSL) {
struct lws_context_creation_info info;
memset( &info, 0, sizeof(struct lws_context_creation_info) );
info.port = 7681;
info.uid = -1;
info.gid = -1;
info.protocols = protocols;
info.mounts = &mount;
info.extensions = exts;
info.timeout_secs = 5;
info.ip_limit_ah = 24; /* for testing */
info.ip_limit_wsi = 400; /* for testing */
// Following options for openSSL certificate
if(useSSL){
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT | LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT | LWS_SERVER_OPTION_DISABLE_IPV6 | LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED | LWS_SERVER_OPTION_IGNORE_MISSING_CERT;
info.ssl_cert_filepath = SSL_CERT_FILE_PATH;
info.ssl_private_key_filepath = SSL_PRIVATE_KEY_PATH;
}
fContext = lws_create_context( &info );
}

在创建web套接字上下文和接受wss连接时,我收到了以下日志。

WebSocket.cpp:638...... :createContext ( ) - begin
WebSocket.cpp:640...... : createContext - fReferenceCount = 0
WebSocket.cpp:324...... : Creating Vhost 'default' port 7681, 1 protocols, IPv6 off
WebSocket.cpp:324...... :  Using SSL mode
WebSocket.cpp:324...... :  SSL ECDH curve 'prime256v1'
WebSocket.cpp:612...... : LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS
WebSocket.cpp:324...... : lws_tls_client_create_vhost_context: doing cert filepath /etc/nginx  /ssl/mycert.crt
WebSocket.cpp:324...... : Loaded client cert /etc/nginx/ssl/mycert.crt
WebSocket.cpp:324...... : lws_tls_client_create_vhost_context: doing private key filepath
WebSocket.cpp:324...... : Loaded client cert private key /etc/nginx/ssl/mykey.key
WebSocket.cpp:324...... : created client ssl context for default
WebSocket.cpp:684...... : lws_create_context SUCCEEDED
WebSocket.cpp:759...... : start  Starting Service Thread.
WebSocket.cpp:705...... : createContext - fReferenceCount = 1 
WebSocket.cpp:706...... : createContext - end

以下是我正在使用的库版本。

libwebsocket.so 13
OpenSSL 1.0.2o  27 Mar 2018

请告诉我出了什么问题?

这个问题可能与libwebsocket无关,而是与Firefox对允许连接到具有自签名证书的WSS过于挑剔有关。尝试从其他程序连接到服务器,例如,一个简单的python程序。

相关:

Websocket和自签名SSL证书有什么问题

Firefox断开自签名证书的websockets连接