]> git.zarvox.org Git - imoo.git/commitdiff
Switch ribbon to use unix sockets
authorDrew Fisher <drew.m.fisher@gmail.com>
Tue, 13 May 2014 06:31:50 +0000 (23:31 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Tue, 13 May 2014 06:31:50 +0000 (23:31 -0700)
They're more secure and I can instantiate multiple sockets in separate
directories without having to probe server ports.

reactornado/ribbon_passthrough.py
ribbon/main.cpp
ribbon/ribbonserver.cpp
ribbon/ribbonserver.h
ribbon/ribbonsocket.cpp
ribbon/ribbonsocket.h

index 2603f67f06c6ebe5fa0741d6401dce2d103a210e..3719aa530d95558f41d0c228ab31995dc9fc2559 100644 (file)
@@ -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):
index 5617fd949a2f0474021d55684b7037f556ea9631..b324b257000562ef67da40a302aaee0653a6d0a0 100644 (file)
@@ -1,6 +1,7 @@
 //#include <QtCore/QCoreApplication>
 #include <QtCore/QCoreApplication>
 #include <QtCore/QCommandLineParser>
+#include <QDir>
 #include <QDebug>
 
 #include <QObject>
@@ -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)));
index 04ef9ffd180d69b4fd0f1eb14bd8c0efd1daecce..c6106e7d9cdcf58a46dfa2f8e262c9d1c214ef69 100644 (file)
@@ -2,8 +2,8 @@
 #include "ribbonsocket.h"
 #include "ribbonutil.h"
 
-#include <QtNetwork/QTcpServer>
-#include <QtNetwork/QTcpSocket>
+#include <QtNetwork/QLocalServer>
+#include <QtNetwork/QLocalSocket>
 #include <QJsonDocument>
 #include <QJsonObject>
 #include <QDebug>
@@ -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()) {
index 17143c46087ca8cd118baece24b3ff97d3451e2e..885365debb6a5623b0aa8d8ddb0be983b4b6a1a6 100644 (file)
@@ -7,8 +7,8 @@
 #include <QtNetwork/QHostAddress>
 
 // 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<RibbonSocket*> sockets;
 private slots:
        void onNewConnection();
index c2611c4a792b742139e1551f4e751222d3096bc3..a02cbfcfa159c6184e28849ea70c2340707a7f8a 100644 (file)
@@ -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;
 }
index b1f02d715748278a1485db21cd0ead298a5c5c6b..42fcb5882b38f5aa4373f907ccf11f8bf34a1ab0 100644 (file)
@@ -2,7 +2,7 @@
 #define RIBBONSOCKET_H
 
 #include <QtCore/QByteArray>
-#include <QtNetwork/QTcpSocket>
+#include <QtNetwork/QLocalSocket>
 
 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;