7 #include <QtNetwork/QSslCertificate>
8 #include <QtNetwork/QSslKey>
43 QSslSocket *serverSocket =
new QSslSocket(
this);
44 if (serverSocket->setSocketDescriptor(socketDescriptor))
46 serverSocket->setProtocol(QSsl::SecureProtocols);
49 connect(serverSocket, SIGNAL(
sslErrors(
const QList<QSslError> &)),
this, SLOT(
sslErrors(
const QList<QSslError> &)));
51 addPendingConnection(serverSocket);
53 connect(serverSocket, SIGNAL(encrypted()),
this, SLOT(
sslReady()));
54 connect(serverSocket, SIGNAL(
modeChanged(QSslSocket::SslMode)),
this, SLOT(
modeChanged(QSslSocket::SslMode)));
55 serverSocket->startServerEncryption();
58 {
delete serverSocket;
59 VFS::ERROR(
"Could not set socket descriptor. Aborting.");
63 QTcpServer::incomingConnection(socketDescriptor);
83 for (
int i=0;i<errors.size();i++)
84 VFS::ERROR( errors.at(i).errorString(), 0, this->metaObject()->className() );
147 , _listenAddress(address)
150 , _sslCertPath(sslCertPath)
151 , _sslKeyPath(sslKeyPath)
189 {
VFS::WARN( QString(
"%1 TCP socket was already listening! Close it first?").arg(
className()) );
194 connect(&
_socket, SIGNAL(acceptError(QAbstractSocket::SocketError)),
this, SLOT(
clientError(QAbstractSocket::SocketError)));
200 bool c=
false,k=
false;
205 if (!(c = certFile.open(QIODevice::ReadOnly)))
210 if (!(k = keyFile.open(QIODevice::ReadOnly)))
220 QSslCertificate certificate(&certFile, QSsl::Pem);
221 QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem);
256 return QString(
"address: %1\nport: %2\nlistening: %3").arg(
_listenAddress.toString()).arg(
_port).arg(
_socket.isListening()?
"true":
"false");
269 while(
_socket.hasPendingConnections())
271 QTcpSocket *s =
_socket.nextPendingConnection();
275 connect( client, SIGNAL( disconnected() ),
282 connect( client, SIGNAL( error(QAbstractSocket::SocketError) ),
283 this, SLOT(
clientError(QAbstractSocket::SocketError) ) );
285 QHostAddress a = s->peerAddress();
307 connect( client, SIGNAL( readyMessage(QByteArray)),
361 VFS::WARN( QString(
"Disconnection, however could not cast sender to VFS_tcp_client.") );
374 VFS::WARN( QString(
"%1 clientMessage() should be overridden in a subclass").arg(
className()) );
396 if (error != QAbstractSocket::RemoteHostClosedError )
VFS_node is the base class from which all other VFS_node classes derive.
virtual VFS_node * append(QString name, VFS_node *node, bool containerCheck=true, QString user="server")
Append a VFS_node as a child of this node.
virtual void metadata(VFS_request *r)
Fetch the metadata of this node.
QString uniqueChildName(QString name)
Generate a unique child name.
virtual void ls(VFS_request *r)
List the contents of this node.
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.
QJsonObject _metadata
the request payload
QString _address
The address resolved by peerAddress(), or the address to connect to.
virtual void close()
DOCME.
QString _sslKeyPath
ssl key file path
virtual void read(VFS_request *r)
Read the state of this node, which is the result of ls().
virtual void createNewConnection()
The server has received a new connection request.
virtual QString reportDetails()
Report details of this node.
virtual void clientError(QAbstractSocket::SocketError error)
A socket error has occurred.
virtual bool isContainer()
A server is a container.
QSslConfiguration _sslConfiguration
ssl config built up from below paths
QHostAddress _listenAddress
Address(es) allowed to connect, or 0.0.0.0 if all are allowed.
virtual void newConnection(VFS_tcp_client *c)
Attach the clientMessage handler.
virtual bool listen()
Open the server socket and listen for new connections.
virtual ~VFS_tcp_server()
virtual void metadata(VFS_request *r)
Fetch the metadata for this node.
VFSQTcpServer _socket
TCP socket used for connections.
QString _sslCertPath
ssl cert file path
virtual VFS_tcp_client * newClient(QTcpSocket *socket, bool ssl=false, QSslConfiguration sslConfiguration=QSslConfiguration())
Create a new VFS_tcp_client with the new socket.
virtual void clientMessage(QByteArray message)
A message has been received.
quint16 _port
TCP port to listen on, or 0 to find an available one.
VFS_tcp_server(quint16 port=0, QHostAddress address=QHostAddress::Any, bool ssl=false, QString sslCertPath="", QString sslKeyPath="")
Create a VFS_tcp_server node.
virtual void closeConnection()
Close a socket 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.
void setSslConfiguration(QSslConfiguration sslConfiguration)
Set the SSL configuration.
void incomingConnection(qintptr socketDescriptor)
Handle a new incoming connection.
QSslConfiguration _sslConfiguration
The ssl configuration, which will include cert files and ssl mode.
VFSQTcpServer(QObject *parent=nullptr, bool ssl=false)
Create a VFSQTcpServer, which allows to create QSslSocket instances if _ssl is true.
void modeChanged(QSslSocket::SslMode mode)
A slot for when the SSL mode has changed.
void sslErrors(const QList< QSslError > &errors)
One or more ssl errors have occurred.
void sslReady()
SSL is ready, but do nothing.
message(m)
Change the message of an existing arrowMessage.