8 #include <QtNetwork/QSslCertificate>
9 #include <QtNetwork/QSslKey>
35 , _listenAddress(addr)
38 , _sslCertPath(sslCertPath)
39 , _sslKeyPath(sslKeyPath)
40 , _socket(
"REMOTO_WS",ssl,this)
41 , _authPaths(authPaths.split(
","))
42 , _sessionsPath(sessions)
47 _authPaths.replaceInStrings(QRegExp(
"\\s+(.*)"),
"\\1");
48 _authPaths.replaceInStrings(QRegExp(
"(.*)\\s+"),
"\\1");
95 {
VFS::WARN( QString(
"Websocket was already listening! Close it first?"), 0,
className() );
99 _socket.setMaxPendingConnections(200);
102 connect(&
_socket, SIGNAL(acceptError(QAbstractSocket::SocketError)),
this, SLOT(
clientError(QAbstractSocket::SocketError)));
105 if (
_ssl==QWebSocketServer::SecureMode)
117 bool c=
false,k=
false;
119 QSslConfiguration sslConfiguration;
124 if (!(c = certFile.open(QIODevice::ReadOnly)))
129 if (!(k = keyFile.open(QIODevice::ReadOnly)))
139 QSslCertificate certificate(&certFile, QSsl::Pem);
140 QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem);
144 sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone);
145 sslConfiguration.setLocalCertificate(certificate);
146 sslConfiguration.setPrivateKey(sslKey);
148 sslConfiguration.setProtocol(QSsl::SecureProtocols);
150 _socket.setSslConfiguration(sslConfiguration);
158 VFS::LOG( QString(
"%1 listening on port %2 (%3)").arg(
className()).arg(
_port).arg((
_ssl==QWebSocketServer::SecureMode?
"secure":
"insecure")), 2 );
174 while(
_socket.hasPendingConnections())
176 QWebSocket *s =
_socket.nextPendingConnection();
180 connect( client, SIGNAL( disconnected() ),
183 connect( client, SIGNAL( readyBinaryMessage(QByteArray)),
186 connect( client, SIGNAL( readyTextMessage(QString)),
189 connect( client, SIGNAL( error(QAbstractSocket::SocketError)),
190 this, SLOT(
clientError(QAbstractSocket::SocketError)) );
232 credentials.remove(
"expire");
234 QString token = credentials[
"authtoken"].toString(
"");
235 QString tokenonce = credentials[
"authtokenonce"].toString(
"");
236 QString user = credentials[
"username"].toString();
248 else if (user.isEmpty() && tokenonce.isEmpty())
255 if (!credentials.contains(
"authindex"))
258 authindex = credentials[
"authindex"].toInt()+1;
262 VFS::ERROR( QString(
"Exhausted available authpaths, can't authenticate '%1'").arg(user) );
270 VFS::LOG( QString(
"Authenticating user '%1' on '%2'...").arg(user).arg(authpath), 7 );
272 VFS::LOG( QString(
"Authenticating on '%1'...").arg(authpath), 7 );
274 credentials[
"authindex"] = authindex;
275 credentials[
"authpath"] = authpath;
276 credentials[
"authtoken"] =
"";
300 QString user = authdata[
"username"].toString();
335 VFS::ERROR(
"Client was not a VFS_websocket_client... not sure what to do." );
358 VFS::WARN( QString(
"%1 clientBinaryMessage() should be overridden in a subclass").arg(
className()) );
369 VFS::WARN( QString(
"%1 clientBinaryMessage() called without client!").arg(
className()) );
390 VFS::WARN( QString(
"%1 clientTextMessage() should be overridden in a subclass").arg(
className()) );
401 VFS::WARN( QString(
"%1 clientTextMessage() called without client!").arg(
className()) );
412 if ( error != QAbstractSocket::RemoteHostClosedError &&
413 error != QAbstractSocket::NetworkError )
416 VFS::WARN( QString(
"(%1) Seems like something should happen here.").arg(
className()) );
437 for (
int i=0;i<errors.size();i++)
VFS_node is the base class from which all other VFS_node classes derive.
virtual VFS_request * createRequest(VFS_request::requestType type, QString path, QString user="unknown", QJsonDocument data=QJsonDocument(), QJsonObject metadata=QJsonObject(), bool dontDelete=false)
Create a new VFS_request with this object as _origin.
VFS_children _children
This node's children.
virtual void issueRequest(VFS_request *t)
A convenience function.
QString className()
Return the class name of a node.
void remove(bool andDelete)
Remove a child node.
The base class for all requests between nodes.
@ read
read full contents (4)
@ create
create a new file/path (2)
A VFS_node that manages a QWebSocket connection.
virtual void close()
Close the socket if it is connected.
virtual ~VFS_websocket_server()
VFS_websocket_server destructor.
virtual bool authenticate(VFS_websocket_client *client, QJsonObject credentials)
Authenticate a client based on received credentials.
QHostAddress _listenAddress
Address to listen on, or 0.0.0.0 for all.
QStringList _authPaths
The list of auth paths to query when a user request authentication.
quint16 _port
Port to listen on.
virtual void sslErrors(const QList< QSslError > &errors)
One or more ssl errors have occurred.
QString _sslCertPath
ssl cert file path
QString _sessionsPath
The path to the sessionManager node.
QWebSocketServer _socket
The QWebSocket server object that will be listening.
virtual void clientTextMessage(QString message)
A client has received a text websocket message.
virtual void serverError(QWebSocketProtocol::CloseCode closeCode)
A server socket error has occurred.
virtual void clientBinaryMessage(QByteArray message)
The client has received a binary websocket message.
virtual void newConnection()
A new connection has been requested, and is pending.
virtual void clientError(QAbstractSocket::SocketError error)
A socket error has occurred.
friend class VFS_websocket_client
VFS_websocket_server(quint16 port, QString authPaths, QString sessions, QHostAddress addr=QHostAddress::Any, QWebSocketServer::SslMode ssl=QWebSocketServer::NonSecureMode, QString sslCertPath="", QString sslKeyPath="")
VFS_websocket_server constructor.
QString _sslKeyPath
ssl key file path
virtual void authorize(VFS_websocket_client *client, QJsonObject authdata)
Authorize a client and create a session based on type and username.
virtual bool isContainer()
A VFS_websocket_server is always a container; it's children will be the connected clients.
QWebSocketServer::SslMode _ssl
Use ssl?
virtual bool listen()
Start listening on the _socket for incoming connections.
virtual void closeConnection()
Close a child client connection, and remove() it from this node's child list.
virtual VFS_websocket_client * createWebsocketClient(QWebSocket *s)
Create a VFS_websocket_client to host a new connection.
static VFS * root()
Return the root node of the VFS filesystem.
static void LOG(QString message, int level=0, QString user="server")
Send a message to the VFS::_messages VFS_stream.
static void ERROR(QString message, int level=0, QString user="server")
Send a message to the VFS::_errors VFS_stream.
static void WARN(QString message, int level=0, QString user="server")
Send a message to the VFS::_warnings VFS_stream.
message(m)
Change the message of an existing arrowMessage.