29 #include <sys/types.h>
33 #include <linux/reboot.h>
34 #include <sys/reboot.h>
37 #include <QStringList>
38 #include <QtCore/QTimer>
39 #include <QtWebSockets/QWebSocketServer>
40 #include <QtWebSockets/QWebSocket>
42 #include <QtCore/QFile>
43 #include <QtNetwork/QSslCertificate>
44 #include <QtNetwork/QSslKey>
47 #include "safec_lib.h"
50 #define DYNAMIC_PASSCODE_UTILITY "/usr/bin/rdkssacli"
51 #define STATIC_PASSCODE_UTILITY "/usr/bin/GetConfigFile"
52 #define DYNAMIC_PASSCODE_ARG "\"{STOR=GET,SRC=kquhqtoczcbx,DST=/dev/stdout}\""
53 #define STATIC_PASSCODE_ARG_FILE "/tmp/.cfgStaticxpki"
56 const char* trmPropertiesPath =
"/etc/trmProxySetup.properties";
57 const char* caKeyTagName =
"CA_CHAIN_CERTIFICATE";
58 const char* privateKeyTagName =
"CA_SERVER_PRIVATE_KEY";
59 const char* publicKeyTagName =
"CA_SERVER_PUBLIC_CERTIFICATE";
60 const char* xpkiDynamicCertificate =
"CA_SERVER_XPKI_DYNAMIC_CERTIFICATE";
61 const char* xpkiStaticCertificate =
"CA_SERVER_XPKI_STATIC_CERTIFICATE";
64 #include "qt_websocketproxy.h"
66 #include "tcpOpensslProxyServer.h"
67 extern int begin_request_callback(
void *conn);
68 extern void end_request_callback(
void *conn,
int reply_status_code);
69 extern int websocket_connect_callback(
void *conn);
70 extern int websocket_disconnect_callback(
void *conn);
71 extern void websocket_ready_callback(
void *conn) ;
72 extern int log_message_callback(
const void *conn,
const char *message) ;
73 extern int websocket_data_callback(
void *conn,
int flags,
char *trm_data,
size_t trm_data_length) ;
74 extern int websocket_data_callback_with_clientId(
int connection_id,
int flags,
char *trm_data,
size_t trm_data_length);
75 static void onWebsocketMessageReceivedSSL(
int clientId,
char* message,
size_t len);
76 static void onPongSSL(
int clientId,
long long unsigned int ticks, QByteArray &qmsg);
82 PingPongTask::PingPongTask(QWebSocket* wssocket)
83 : wssocket(wssocket), stopped(false)
86 m_sslProxyServer = NULL;
88 std::cout <<
"Ping-Pong created for socket " << (
void *)wssocket << std::endl;
89 connect(&timer, SIGNAL(timeout()),
this, SLOT(onTimeout()));
90 connect(wssocket, SIGNAL(pong(quint64, QByteArray)),
this, SLOT(onPong(quint64, QByteArray)));
95 : m_sslProxyServer(sslProxyServer), m_clientId(clientId), stopped(false)
99 std::cout <<
"Ping-Pong created for socket " << (
void *)wssocket <<
" m_sslProxyServer "<< m_sslProxyServer <<std::endl;
100 connect(&timer, SIGNAL(timeout()),
this, SLOT(onTimeout()));
105 PingPongTask::~PingPongTask(
void)
109 std::cout <<
" Assert: PING-PONG not stopped before deleting\r\n";
113 void PingPongTask::start(
void)
117 if (NULL != wssocket) {
120 if (NULL != m_sslProxyServer) {
121 m_sslProxyServer->ping (m_clientId);
123 timer.setInterval(5000);
124 timer.setSingleShot(
true);
129 void PingPongTask::stop(
void)
132 std::cout <<
"Ping-Pong stopped for socket " << (
void *)wssocket <<
" m_sslProxyServer "<< m_sslProxyServer <<std::endl;
137 void PingPongTask::onTimeout(
void)
148 std::cout <<
"Ping-Pong Would have closed socket " << (
void *)wssocket <<
" m_sslProxyServer "<< m_sslProxyServer <<std::endl;
153 std::cout <<
"Ping-Pong Timeout closing socket " << (
void *)wssocket <<
" m_sslProxyServer "<< m_sslProxyServer <<
" m_clientId " << m_clientId << std::endl;
154 if (NULL != wssocket) {
157 if (NULL != m_sslProxyServer) {
158 m_sslProxyServer->closeClient (m_clientId);
164 void PingPongTask::onPong(quint64 elapsedTime, QByteArray)
167 if (elapsedTime > 10000) {
169 std::cout <<
"Ping-Pong Slow: pong received for socket " << (
void *)wssocket <<
" m_sslProxyServer "<< m_sslProxyServer <<std::endl;
170 std::cout <<
" At [" << QTime::currentTime().toString().toUtf8().data();
171 std::cout <<
" ] PONG received epapsedTime = " << elapsedTime << std::endl;
178 static int exec_sys_command(
char* cmd)
181 FILE *syscmd = popen(cmd,
"re");
183 std::cout <<
"popen failed with error code"<< syscmd <<
"to execute system command: "<< cmd;
186 memset(buff, 0, 128);
187 std::cout <<
"Executing system command " << cmd << std::endl;
189 while(fgets(buff,
sizeof(buff), syscmd) != 0) {
190 std::cout <<
"read syscmd buff : " << buff << std::endl;
196 #define TRM_USE_RFC 1
197 static bool rfc_get_trmssl_status()
199 bool isTRMSSLEnabled =
true;
201 int sysRet = system(
". /lib/rdk/isFeatureEnabled.sh NOTRMSSL");
202 if((WEXITSTATUS(sysRet) ==
true) && (WIFEXITED(sysRet) ==
true))
204 std::cout <<
"RFC NOTRMSSL feature Enabled "<<std::endl;
205 isTRMSSLEnabled =
false;
208 isTRMSSLEnabled =
true;
211 std::cout <<
"RFC TRMSSL feature status:"<< isTRMSSLEnabled<<std::endl;
212 return isTRMSSLEnabled;
220 static bool is_TRM_RFC_param_enable (TRM_RFC_PARAM_TYPE type)
223 RFC_ParamData_t param;
224 WDMP_STATUS status = WDMP_ERR_INVALID_PARAMETER_NAME;
225 std::string parmName = std::string (
"invalid");
228 status = getRFCParameter((
const char*)
"WSPROXY", (
const char*)
"Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.TRM.EnableMTLS", ¶m);
229 parmName = std::string (
"mTLS");
231 case RFC_PARM_qtPort:
232 status = getRFCParameter((
const char*)
"WSPROXY", (
const char*)
"Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.TRMQtSecurePort.Enable", ¶m);
233 parmName = std::string (
"qtPort");
236 if (status == WDMP_SUCCESS) {
237 printf (
"name = %s, type = %d, value = %s\n", param.name, param.type, param.value);
239 if (!strncmp(param.value,
"true", strlen(
"true"))) {
240 printf (
"TRM %s is enabled.\n", parmName.c_str());
244 printf (
"TRM %s is disabled.\n", parmName.c_str());
248 printf (
"getRFCParameter Failed : %s\n", getRFCErrorString(status));
249 if (RFC_PARM_qtPort == type){
257 static void delete_file (QString &fileName)
260 sprintf(cmd,
"%s %s",
"rm", fileName.toLatin1().data());
261 exec_sys_command(cmd);
264 static void executeCommand (std::string command, std::string &output){
267 int maxResult = 1024;
268 char result[maxResult] = {
'\0'};
269 fp = popen(command.c_str(),
"r");
271 printf(
"\n%s:%d Failed to run command: %s\n", __FUNCTION__, __LINE__, command.c_str());
277 while (fgets(result,
sizeof(result), fp) != NULL) {
278 if (i++ > maxResult) {
283 printf(
"\n%s:%d command %s . running in endless loop\n", __FUNCTION__, __LINE__, command.c_str());
293 static bool selectPk12KeyAndPass (std::string dynKeyFilePath, std::string staticKeyFilePath,
294 std::string &outputKeyFilePath, std::string &pass)
297 if (!(access(dynKeyFilePath.c_str(), F_OK) == 0)) {
299 printf(
"\n%s:%d dynamic pk12 certificate:%s not exists. Pass key wont be generated\n", __FUNCTION__, __LINE__, dynKeyFilePath.c_str());
302 if (!(access(DYNAMIC_PASSCODE_UTILITY, F_OK) == 0)) {
304 printf(
"\n%s:%d dynamic pk12 pass key generator not exists\n", __FUNCTION__, __LINE__);
310 std::string command = DYNAMIC_PASSCODE_UTILITY;
311 command.append (
" ");
312 command.append (DYNAMIC_PASSCODE_ARG);
313 executeCommand (command, pass);
315 pass.erase(std::remove(pass.begin(), pass.end(),
'\n'), pass.end());
318 outputKeyFilePath =
"";
320 outputKeyFilePath = dynKeyFilePath;
321 printf(
"\n%s:%d using dynamic pk12 certificate\n", __FUNCTION__, __LINE__);
325 printf(
"\n%s:%d dynamic pk12 pass key null\n", __FUNCTION__, __LINE__);
328 printf(
"\n%s:%d dynamic pk12 certificate check failed\n", __FUNCTION__, __LINE__);
330 outputKeyFilePath =
""; pass=
"";
332 if (!(access(staticKeyFilePath.c_str(), F_OK) == 0)) {
334 printf(
"\n%s:%d static pk12 certificate:%s not exists\n", __FUNCTION__, __LINE__, staticKeyFilePath.c_str());
338 if (!(access(STATIC_PASSCODE_UTILITY, F_OK) == 0)) {
340 printf(
"\n%s:%d static pk12 pass key generator not exists\n", __FUNCTION__, __LINE__);
346 std::string command = STATIC_PASSCODE_UTILITY;
347 command.append (
" ");
348 command.append (STATIC_PASSCODE_ARG_FILE);
349 executeCommand (command, pass);
350 if (!(access(STATIC_PASSCODE_ARG_FILE, F_OK) == 0)) {
351 printf(
"\n%s:%d static config file generation failed\n", __FUNCTION__, __LINE__);
358 command.append (STATIC_PASSCODE_ARG_FILE);
359 executeCommand (command, pass);
361 pass.erase(std::remove(pass.begin(), pass.end(),
'\n'), pass.end());
363 outputKeyFilePath =
"";
365 outputKeyFilePath = staticKeyFilePath;
366 printf(
"\n%s:%d using static pk12 certificate\n", __FUNCTION__, __LINE__);
370 printf(
"\n%s:%d static pk12 pass key null\n", __FUNCTION__, __LINE__);
373 printf(
"\n%s:%d dynamic and static pk12 certificate check failed\n", __FUNCTION__, __LINE__);
378 WebSocketProxy::WebSocketProxy(
const QStringList &boundIPs, quint16 port, QObject *parent) :
379 QObject(parent), proxyServers(), connections()
382 if(rfc_get_trmssl_status() ==
true)
385 QSettings trmSetting( trmPropertiesPath, QSettings::IniFormat );
386 QString caChainFile = trmSetting.value( caKeyTagName ).toString();
387 QString keyFileName = trmSetting.value( privateKeyTagName ).toString();
388 QString certFileName = trmSetting.value( publicKeyTagName ).toString();
389 QString xpkiDynFilePath = trmSetting.value( xpkiDynamicCertificate ).toString();
390 QString xpkiStaticFilePath = trmSetting.value( xpkiStaticCertificate ).toString();
392 if(caChainFile.isNull() || keyFileName.isNull() || certFileName.isNull())
395 std::cout <<
"Missing TRM configuration information";
399 QStringList::const_iterator it = boundIPs.constBegin();
400 while (it != boundIPs.constEnd())
402 if (proxyServers.constFind(*it) == proxyServers.constEnd())
405 if(is_TRM_RFC_param_enable(RFC_PARM_qtPort)) {
407 std::cout <<
"QT secure port enabled"<<std::endl;
409 QFile certFile(certFileName);
410 QFile keyFile(keyFileName);
411 if(!keyFile.exists())
413 std::cout <<
"Server private key not exist. Don't start the server ";
416 certFile.open(QIODevice::ReadOnly);
417 keyFile.open(QIODevice::ReadOnly);
418 QSslCertificate certificate(&certFile, QSsl::Pem);
419 QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem,QSsl::PrivateKey,QByteArray(
""));
422 QSslConfiguration sslConfiguration;
424 if (
true == is_TRM_RFC_param_enable(RFC_PARM_mTLS))
426 std::cout <<
"QSslSocket peerVerifyMode is QSslSocket::VerifyPeer. " << std::endl;
427 sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyPeer);
431 std::cout <<
"QSslSocket peerVerifyMode is QSslSocket::QueryPeer. " << std::endl;
432 sslConfiguration.setPeerVerifyMode(QSslSocket::QueryPeer);
435 sslConfiguration.setLocalCertificate(certificate);
436 sslConfiguration.setPrivateKey(sslKey);
437 sslConfiguration.setProtocol(QSsl::TlsV1_2);
438 sslConfiguration.setPeerVerifyDepth(2);
440 QList<QSslCertificate> caCerts = QSslCertificate::fromPath(caChainFile);
442 sslConfiguration.setCaCertificates(caCerts);
443 QWebSocketServer *proxyServer =
new QWebSocketServer(QString(
"TRM SecureMode WebsocketServer IP: ") + *it , QWebSocketServer::SecureMode,
this);
445 proxyServer->setSslConfiguration(sslConfiguration);
447 std::cout <<
"TRM WebsocketProxy starting server on ip: " <<(*it).toUtf8().data()
448 <<
" SecureMode: "<<proxyServer->secureMode()<< std::endl;
449 if (proxyServer->listen(QHostAddress(*it), port))
451 connect(proxyServer, SIGNAL(newConnection()),
this,
452 SLOT(onNewConnection()));
453 connect(proxyServer,&QWebSocketServer::sslErrors ,
this,
454 &WebSocketProxy::onSslErrors);
455 connect(proxyServer, &QWebSocketServer::acceptError,
this,
456 &WebSocketProxy::onAcceptError);
457 connect(proxyServer, &QWebSocketServer::peerVerifyError,
this,
458 &WebSocketProxy::onPeerVerifyError);
459 proxyServers[*it] = proxyServer;
463 std::cout <<
"TRM WebsocketProxy Failed to listen" << std::endl;
467 std::string pk12keyFilePath =
"";
468 std::string pk12pass =
"";
469 bool isPk12CertAvailable = selectPk12KeyAndPass (xpkiDynFilePath.toStdString(), xpkiStaticFilePath.toStdString(), pk12keyFilePath, pk12pass);
470 if (isPk12CertAvailable) {
471 std::cout <<
"Opening tcpOpensslProxyServer with ip"<<(*it).toStdString().c_str() << std::endl;
473 sslProxyServer->onMessageReceivedCallBack = onWebsocketMessageReceivedSSL;
474 sslProxyServer->onPongCallBack = onPongSSL;
478 int ret = sslProxyServer->setUpServer(
479 (*it).toStdString().c_str(), 9990,
480 caChainFile.toStdString().c_str(),
481 pk12keyFilePath.c_str(),
486 connect(sslProxyServer,SIGNAL(sslErrors (QString)) ,
this,
487 SLOT(onSslErrorsSSL(QString)));
488 connect(sslProxyServer, SIGNAL(acceptError (QString)),
this,
489 SLOT(onAcceptErrorSSL(QString)));
490 connect(sslProxyServer, SIGNAL(peerVerifyError (QString)),
this,
491 SLOT(onPeerVerifyErrorSSL(QString)));
493 connect(sslProxyServer, SIGNAL(connected (
int, QString)),
this,
494 SLOT(onWebsocketConnectSSL(
int, QString)));
495 connect(sslProxyServer, SIGNAL(disconnected (
int)),
this,
496 SLOT(onWebsocketDisconnectedSSL(
int)));
497 connect(sslProxyServer, SIGNAL(socketError (QString)),
this,
498 SLOT(onWebsocketErrorSSL(QString)));
499 sslProxyServers[*it] = sslProxyServer;
501 std::cout <<
"TRM WebsocketProxy Failed to listen" << std::endl;
504 std::cout <<
"TRM WebsocketProxy pk12 certificates depenedncies not available" << std::endl;
510 std::cout <<
"TRM WebsocketProxy already listen on "
511 << (*it).toUtf8().data() <<
":" << port << std::endl;
516 delete_file (keyFileName);
525 QStringList::const_iterator itr = boundIPs.constBegin();
526 while (itr != boundIPs.constEnd()) {
527 QWebSocketServer *proxyServer =
new QWebSocketServer(
528 QString(
"TRM NonSecureMode WebsocketServer IP: ") + *itr,
529 QWebSocketServer::NonSecureMode,
this);
530 std::cout <<
"TRM WebsocketProxy starting server on ip: " <<(*itr).toUtf8().data()
531 <<
" SecureMode: "<<proxyServer->secureMode()<< std::endl;
532 if (proxyServer->listen(QHostAddress(*itr), port)) {
533 connect(proxyServer, SIGNAL(newConnection()),
this,
534 SLOT(onNewConnection()));
535 proxyServers[*itr] = proxyServer;
539 std::cout <<
"TRM WebsocketProxy Failed to listen" << std::endl;
549 void WebSocketProxy::onSslErrorsSSL(QString err){
550 std::cout <<
"onSslErrorsSSL:" << err.toStdString()<<endl;
552 void WebSocketProxy::onAcceptErrorSSL(QString err) {
553 std::cout<<
"onAcceptErrorSSL occured:"<<err.toStdString()<<endl;
555 void WebSocketProxy::onPeerVerifyErrorSSL(QString err) {
556 std::cout<<
"onPeerVerifyErrorSSL :"<<err.toStdString()<<endl;
559 void WebSocketProxy::onWebsocketConnectSSL(
int clientId, QString msg)
562 std::cout <<
"TRM WebsocketProxy onWebsocketConnectSSL" << std::endl;
563 if (sslProxyPingPongTasks.find(clientId) != sslProxyPingPongTasks.end()){
569 static void onWebsocketMessageReceivedSSL(
int clientId,
char* message,
size_t len)
572 std::cout <<
"TRM WebsocketProxy onWebsocketMessageReceivedSSL msg:" << message << std::endl;
573 websocket_data_callback_with_clientId (clientId, 0, message, len);
576 static void onPongSSL(
int clientId, uint64_t ticks, QByteArray &qmsg)
580 if (m_proxy->sslProxyPingPongTasks.find(clientId) != m_proxy->sslProxyPingPongTasks.end()){
582 m_proxy->sslProxyPingPongTasks[clientId]->onPong (ticks, qmsg);
587 void WebSocketProxy::onWebsocketDisconnectedSSL(
int clientId) {
589 std::cout <<
"TRM WebsocketProxy onWebsocketDisconnectedSSL clientId:"<< clientId << std::endl;
590 if (sslProxyMap.find(clientId) != sslProxyMap.end()){
591 sslProxyMap.erase (clientId);
593 if (sslProxyPingPongTasks.find(clientId) != sslProxyPingPongTasks.end()){
596 sslProxyPingPongTasks.erase(clientId);
598 oldPp->deleteLater();
602 void WebSocketProxy::onWebsocketErrorSSL(QString err)
605 std::cout <<
"TRM WebsocketProxy onWebsocketErrorSSL = " << err.toStdString()<<endl;
610 void WebSocketProxy::onSslErrors(
const QList<QSslError> &sslError)
612 qDebug() <<
"onSslErrors :" << sslError;
615 void WebSocketProxy::onAcceptError(QAbstractSocket::SocketError socketError)
617 std::cout<<
" onAcceptError occured:"<<socketError<<std::endl;
619 void WebSocketProxy::onPeerVerifyError(
const QSslError &error)
621 qDebug() <<
"onPeerVerifyError :" << error;
627 std::cout<<
" WebSocketProxy::onNewConnectionSSl:"<<std::endl;
630 static const char *has_livestream_client_flag_filename =
"/tmp/mnt/diska3/persistent/.has_livestream_client";
633 int ret = ::lstat(has_livestream_client_flag_filename, &st);
635 sslProxyMap [clientId] = ss;
637 if (sslProxyPingPongTasks.find(clientId) != sslProxyPingPongTasks.end()){
640 sslProxyPingPongTasks.erase(clientId);
642 oldPP->deleteLater();
644 sslProxyPingPongTasks [clientId] = ppTask;
647 void WebSocketProxy::onNewConnection()
652 static const char *has_livestream_client_flag_filename =
"/tmp/mnt/diska3/persistent/.has_livestream_client";
655 int ret = ::lstat(has_livestream_client_flag_filename, &st);
656 #define USE_DELIA_GATEWAY
657 #ifndef USE_DELIA_GATEWAY
662 int fd = ::open(has_livestream_client_flag_filename, O_WRONLY|O_CREAT, 0666);
666 std::cout <<
"Rebooting STB on the initial xi3 connection \r\n" << std::endl;
669 ::system(
"sh /rebootNow.sh -s websocketproxyinit" );
673 #endif //USE_DELIA_GATEWAY
677 QWebSocketServer *proxyServer = qobject_cast<QWebSocketServer *>(sender());
679 std::cout <<
"TRM WebsocketProxy connection from server " << (
void *)proxyServer <<
" of name:" << proxyServer->serverName().toUtf8().data() << std::endl;
681 QWebSocket *wssocket = proxyServer->nextPendingConnection();
682 websocket_connect_callback((
void *)wssocket);
684 std::cout <<
"TRM WebsocketProxy peerAddress: " << qPrintable(wssocket->peerAddress().toString()) <<
" Port: "<< wssocket->peerPort();
686 std::cout <<
"TRM WebsocketProxy accept connection " << (
void *)wssocket << std::endl;
690 websocket_ready_callback((
void *)wssocket);
691 connections << wssocket;
692 pingPongTasks.insert(wssocket,
new PingPongTask(wssocket));
695 connect(wssocket, SIGNAL(connected()),
this, SLOT(onWebsocketConnect()));
697 connect(wssocket, SIGNAL(binaryMessageReceived(QByteArray)),
this, SLOT(onWebsocketBinaryMessageReceived(QByteArray)));
698 connect(wssocket, SIGNAL(textMessageReceived(QString)),
this, SLOT(onWebsocketTextMessageReceived(QString)));
702 connect(wssocket, SIGNAL(disconnected()),
this, SLOT(onWebsocketDisconnected()));
703 connect(wssocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(onWebsocketError(QAbstractSocket::SocketError)));
706 connect(wssocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this, SLOT(onWebsocketStateChanged(QAbstractSocket::SocketState)));
708 connect(wssocket, SIGNAL(aboutToClose()),
this, SLOT(onWebSocketAboutClose()));
710 ((pingPongTasks.find(wssocket)).value())->start();
714 void WebSocketProxy::onWebsocketConnect(
void)
717 std::cout <<
"TRM WebsocketProxy onWebsocketConnect" << std::endl;
720 void WebSocketProxy::onWebsocketBinaryMessageReceived(QByteArray byteArray)
723 std::cout <<
"TRM WebsocketProxy onWebsocketBinaryMessageReceived" << std::endl;
724 QWebSocket *wssocket = qobject_cast<QWebSocket *>(sender());
725 websocket_data_callback((
void *)wssocket, 0, byteArray.data(), byteArray.size());
728 void WebSocketProxy::onWebsocketTextMessageReceived(QString message)
731 std::cout <<
"TRM WebsocketProxy onWebsocketTextMessageReceived" << std::endl;
732 QWebSocket *wssocket = qobject_cast<QWebSocket *>(sender());
733 websocket_data_callback((
void *)wssocket, 0, message.toUtf8().data(), message.size());
736 void WebSocketProxy::onWebsocketBytesWritten(qint64)
740 void WebSocketProxy::onWebsocketDisconnected(
void)
742 QWebSocket *wssocket = qobject_cast<QWebSocket *>(sender());
744 std::cout <<
"TRM WebsocketProxy onWebsocketDisconnected " << (
void *)wssocket << std::endl;
745 if (wssocket && connections.contains(wssocket)) {
747 wssocket->deleteLater();
749 connections.removeAll(wssocket);
750 websocket_disconnect_callback((
void *)wssocket);
752 ((pingPongTasks.find(wssocket)).value())->stop();
753 ((pingPongTasks.find(wssocket)).value())->deleteLater();
754 pingPongTasks.remove(wssocket);
758 void WebSocketProxy::onWebsocketStateChanged(QAbstractSocket::SocketState state)
760 static const char *states_name[] = {
769 QWebSocket *wssocket = qobject_cast<QWebSocket *>(sender());
771 std::cout <<
"TRM WebsocketProxy onWebsocketStateChanged " << (
void *)wssocket <<
" New State =" << states_name[state] << std::endl;
774 void WebSocketProxy::onWebSocketAboutClose(
void)
778 void WebSocketProxy::onWebsocketError(QAbstractSocket::SocketError error)
781 std::cout <<
"TRM WebsocketProxy onWebsocketError = " << error << std::endl;
782 QWebSocket *wssocket = qobject_cast<QWebSocket *>(sender());
789 int WebSocketProxy::onWebsocketHasDataToWriteSSL(
int clientId,
char* data,
int len)
791 if (sslProxyMap.find(clientId) != sslProxyMap.end()){
793 printf (
"WebSocketProxy::%s:%d data: masked len:%d\n", __FUNCTION__, __LINE__, len);
794 ss->sendTextMessage (clientId, data, len);
801 void WebSocketProxy::onWebsocketHasDataToWrite(
void *conn,
void *data)
807 QWebSocket *wssocket = (QWebSocket *)conn;
808 QByteArray *byteArray= (QByteArray *)data;
809 if (connections.contains(wssocket)) {
810 wssocket->sendTextMessage(QString(byteArray->constData()));
816 void WebSocketProxy::onRemoveConnection(
void *conn)
822 std::cout <<
"onRemoveConnection" << std::endl;
824 QWebSocket *wssocket = (QWebSocket *)conn;
825 if (connections.contains(wssocket)) {
826 emit wssocket->close();
830 void WebSocketProxy::onRemoveAllOpenSslConnections() {
831 QMap<QString, tcpOpensslProxyServer *>::iterator iter;
832 for (iter = sslProxyServers.begin(); iter != sslProxyServers.end(); ++iter) {
834 sslProxyServer->closeAllClients();
838 int mg_websocket_write_ssl(
int clientId,
char *data,
int data_len)
841 printf (
"%s:%d data masked data_len:%d\n", __FUNCTION__, __LINE__, data_len);
842 if (NULL != m_proxy) {
843 ret = m_proxy->onWebsocketHasDataToWriteSSL (clientId, data, data_len);
848 int mg_websocket_write(
void * conn,
const char *data,
size_t data_len)
850 QWebSocket *wssocket = (QWebSocket *)(conn);
852 QByteArray *byteArray =
new QByteArray(data, data_len);
853 byteArray->append(
'\0');
854 QMetaObject::invokeMethod(m_proxy,
"onWebsocketHasDataToWrite", Qt::QueuedConnection, Q_ARG(
void *, wssocket), Q_ARG(
void *, byteArray));
859 int mg_websocket_close_all_ssl()
861 printf (
"%s:%d \n", __FUNCTION__, __LINE__);
862 if (NULL != m_proxy) {
863 m_proxy->onRemoveAllOpenSslConnections();
868 int mg_websocket_close(
void * conn)
871 std::cout <<
"mg_websocket_close" << std::endl;
873 printf(
"[%s] THREAD SELF is %p\r\n", __FUNCTION__, (
void *)pthread_self());
875 QWebSocket *wssocket = (QWebSocket *)(conn);
877 QMetaObject::invokeMethod(m_proxy,
"onRemoveConnection", Qt::QueuedConnection, Q_ARG(
void *, wssocket));