From: Drew Fisher Date: Tue, 13 May 2014 06:31:50 +0000 (-0700) Subject: Switch ribbon to use unix sockets X-Git-Url: http://git.zarvox.org/login?a=commitdiff_plain;h=0a71bf71f0d18711ffb95648ca71a4ec60f4900f;p=imoo.git Switch ribbon to use unix sockets They're more secure and I can instantiate multiple sockets in separate directories without having to probe server ports. --- diff --git a/reactornado/ribbon_passthrough.py b/reactornado/ribbon_passthrough.py index 2603f67..3719aa5 100644 --- a/reactornado/ribbon_passthrough.py +++ b/reactornado/ribbon_passthrough.py @@ -57,9 +57,9 @@ class WSHandler(tornado.websocket.WebSocketHandler): # Create a new connection to ribbon. self.ribbon_ready = False self.backlog = [] # A list of bytestrings queued to be sent on the socket. - self.ribbon_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) + self.ribbon_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0) self.ribbon_stream = tornado.iostream.IOStream(self.ribbon_socket) - self.ribbon_stream.connect( ("localhost", 8888), self.on_ribbon_ready) + self.ribbon_stream.connect( os.path.join(pwd, "state", "socket"), self.on_ribbon_ready) log.info('new connection') self.write_message('{"key":"value"}') def on_message(self, message): diff --git a/ribbon/main.cpp b/ribbon/main.cpp index 5617fd9..b324b25 100644 --- a/ribbon/main.cpp +++ b/ribbon/main.cpp @@ -1,6 +1,7 @@ //#include #include #include +#include #include #include @@ -40,7 +41,12 @@ int main(int argc, char** argv) qDebug() << "libpurple initialized, version" << purple->get_version(); RibbonServer* server = new RibbonServer(); - server->listen(QHostAddress::Any, 8888); + QDir dir(purple->get_state_dir()); + QString server_socket_path = dir.absoluteFilePath("socket"); + bool success = server->listen(server_socket_path); + if (!success) { + return 1; + } QObject::connect(purple, SIGNAL(event(QString,QString,QVariantMap)), server, SLOT(sendEventToClient(QString,QString,QVariantMap))); diff --git a/ribbon/ribbonserver.cpp b/ribbon/ribbonserver.cpp index 04ef9ff..c6106e7 100644 --- a/ribbon/ribbonserver.cpp +++ b/ribbon/ribbonserver.cpp @@ -2,8 +2,8 @@ #include "ribbonsocket.h" #include "ribbonutil.h" -#include -#include +#include +#include #include #include #include @@ -11,7 +11,7 @@ RibbonServer::RibbonServer(QObject* parent) : QObject(parent) { - server = new QTcpServer(this); + server = new QLocalServer(this); QObject::connect(server, SIGNAL(newConnection()), this, SLOT(onNewConnection())); } @@ -20,17 +20,27 @@ RibbonServer::~RibbonServer() { } -bool RibbonServer::listen(const QHostAddress& address, quint16 port) +bool RibbonServer::listen(const QString& name) { - _addr = address; - _port = port; - return server->listen(_addr, _port); + _socketname = name; + bool success; + success = QLocalServer::removeServer(name); + if (!success) { + return success; + } + success = server->listen(name); + if (success) { + qDebug() << "Server listening on" << _socketname; + } else { + qDebug() << "Failed to listen:" << server->serverError() << server->errorString(); + } + return success; } void RibbonServer::onNewConnection() { - QTcpSocket* sock = server->nextPendingConnection(); - qDebug() << "Accepted connection from" << sock->peerAddress() << ":" << sock->peerPort(); + QLocalSocket* sock = server->nextPendingConnection(); + qDebug() << "Accepted connection"; RibbonSocket* rsock = new RibbonSocket(sock, this); sockets.append(rsock); if (sockets.length() >= 1) { @@ -49,14 +59,14 @@ void RibbonServer::onDisconnection(RibbonSocket* r) qDebug() << "Socket disconnected:" << r->s(); r->deleteLater(); if (!server->isListening()) { - server->listen(_addr, _port); - qDebug() << "Server listening on" << _addr << ":" << _port; + server->listen(_socketname); + qDebug() << "Server listening on" << _socketname; } } void RibbonServer::onMessageReceived(RibbonSocket* r, QByteArray buf) { - qDebug() << "server: got data from" << r->s()->peerAddress() << r->s()->peerPort(); + qDebug() << "server: got data"; QJsonParseError err; QJsonDocument json = QJsonDocument::fromJson(buf, &err); if (json.isNull()) { diff --git a/ribbon/ribbonserver.h b/ribbon/ribbonserver.h index 17143c4..885365d 100644 --- a/ribbon/ribbonserver.h +++ b/ribbon/ribbonserver.h @@ -7,8 +7,8 @@ #include // forward declarations -class QTcpServer; -class QTcpSocket; +class QLocalServer; +class QLocalSocket; class RibbonSocket; class RibbonServer : public QObject { @@ -16,7 +16,7 @@ class RibbonServer : public QObject { public: RibbonServer(QObject* parent = 0); ~RibbonServer(); - bool listen(const QHostAddress& address = QHostAddress::Any, quint16 port = 0); + bool listen(const QString& name); public slots: //void dispatch(QByteArray ba); void sendEventToClient(QString sender, QString method, QVariantMap ctx); @@ -24,9 +24,8 @@ signals: void newConnection(); void messageReceived(QString destination, QString method, QVariantMap ctx); private: - QTcpServer* server; - QHostAddress _addr; - quint16 _port; + QLocalServer* server; + QString _socketname; QList sockets; private slots: void onNewConnection(); diff --git a/ribbon/ribbonsocket.cpp b/ribbon/ribbonsocket.cpp index c2611c4..a02cbfc 100644 --- a/ribbon/ribbonsocket.cpp +++ b/ribbon/ribbonsocket.cpp @@ -1,6 +1,6 @@ #include "ribbonsocket.h" -RibbonSocket::RibbonSocket(QTcpSocket* sock, QObject* parent) : +RibbonSocket::RibbonSocket(QLocalSocket* sock, QObject* parent) : QObject(parent), socket(sock), state(WANT_LENGTH), @@ -19,7 +19,7 @@ RibbonSocket::~RibbonSocket() { } -QTcpSocket* RibbonSocket::s() +QLocalSocket* RibbonSocket::s() { return socket; } diff --git a/ribbon/ribbonsocket.h b/ribbon/ribbonsocket.h index b1f02d7..42fcb58 100644 --- a/ribbon/ribbonsocket.h +++ b/ribbon/ribbonsocket.h @@ -2,7 +2,7 @@ #define RIBBONSOCKET_H #include -#include +#include class RibbonSocket : public QObject { Q_OBJECT @@ -12,16 +12,16 @@ class RibbonSocket : public QObject { }; public: - RibbonSocket(QTcpSocket* socket, QObject* parent=0); + RibbonSocket(QLocalSocket* socket, QObject* parent=0); // Consider s to be final. Don't modify it. - QTcpSocket* s(); + QLocalSocket* s(); void abort(); signals: void disconnected(RibbonSocket* s); void messageReceived(RibbonSocket* s, QByteArray buf); private: virtual ~RibbonSocket(); - QTcpSocket* socket; + QLocalSocket* socket; QByteArray buf; RibbonSocketState state; quint32 bytes_wanted;