This commit is contained in:
Tim Blume 2021-03-20 17:04:13 +01:00
parent 1baee8c1e7
commit 8b7b40d3ab
5 changed files with 22 additions and 127 deletions

View file

@ -3,6 +3,14 @@
//major minor patch //major minor patch
#define LITTLESNITCH_VERSION 100 #define LITTLESNITCH_VERSION 100
#include <QtCore/QtGlobal>
#if defined(IS_PROXY_LIBRARY)
# define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
#else
# define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
#endif
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <vector> #include <vector>

View file

@ -11,22 +11,23 @@ namespace http {
/*! /*!
Interface for implementing proxies Interface for implementing proxies
There are no signals, because apparently Qt Plugins do not support interfaces, which inherit from QObject. You need to implement the following signals:
(it will fail upon loading the plugin)
void process(std::string path); // called in a separate thread, has to block, will not be called again
void finished(); // thread finished and can be deleted
void error(QString err); // an error occured
void message(http::Flow flow); // a http::Flow was received
*/ */
class ProxyInterface { class ProxyInterface {
public: public:
std::string path;
virtual ~ProxyInterface() = default; virtual ~ProxyInterface() = default;
//! called in a custom thread, may block.
virtual void process(std::string path) = 0;
//! returns, whether the proxy is currently connected //! returns, whether the proxy is currently connected
virtual bool connected() = 0; virtual bool connected() = 0;
//! disconnects the handle //! disconnects the handle
virtual void disconnect() = 0; virtual void disconnect() = 0;
// options/settings // options/settings
std::function<void()> finished;
std::function<void(QString)> error;
std::function<void(http::Flow flow)> message;
}; };
} }

View file

@ -1,87 +0,0 @@
#include "networkthread.h"
#include <iostream>
NetworkThread::NetworkThread(QObject *parent) :
QObject(parent),
context(1)
{
}
void NetworkThread::connect() {
socket = new zmq::socket_t(context, zmq::socket_type::pair);
int linger_time = 0;
socket->set(zmq::sockopt::linger, linger_time);
try {
socket->bind("tcp://127.0.0.1:12345");
} catch (zmq::error_t err) {
qDebug() << "failed binding socket" << err.what();
emit error(err.what());
throw;
};
qDebug() << "connected";
}
void NetworkThread::disconnect() {
delete socket;
qDebug() << "disconnected";
}
void NetworkThread::reconnect() {
this->disconnect();
this->connect();
}
void NetworkThread::process() {
connect();
while(true) {
zmq::message_t response;
const auto ret = socket->recv(response, zmq::recv_flags::dontwait);
if(!ret) {
continue;
}
json j;
try {
j = json::parse(response.to_string());
} catch (nlohmann::detail::parse_error& err) {
qDebug() << err.what();
qDebug() << "malformed json received " << response.to_string().c_str();
}
//std::cout << std::setw(4) << j << "\n\n";
std::string msg_type;
if(!json_get(j, msg_type, "msg")) {
qDebug() << "broken message received " << response.to_string().c_str();
} else if(msg_type == "responseheaders") {
qDebug() << "received " << msg_type.c_str();
} else if(msg_type == "response") {
qDebug() << "received " << msg_type.c_str();
emit httpMessage(j);
} else if(msg_type == "requestheaders") {
qDebug() << "received " << msg_type.c_str();
} else if(msg_type == "request") {
qDebug() << "received " << msg_type.c_str();
} else if(msg_type == "ping") {
qDebug() << "received " << msg_type.c_str();
} else if(msg_type == "error") {
qDebug() << "received " << msg_type.c_str();
// websocket events
} else if(msg_type == "websocket_handshake") {
qDebug() << "received " << msg_type.c_str();
} else if(msg_type == "websocket_start") {
qDebug() << "received " << msg_type.c_str();
std::cout << std::setw(4) << j << "\n\n";
} else if(msg_type == "websocket_message") {
qDebug() << "received " << msg_type.c_str();
} else if(msg_type == "websocket_end") {
qDebug() << "received " << msg_type.c_str();
} else if(msg_type == "websocket_error") {
} else {
qDebug() << "unknown or broken message type received: " << msg_type.c_str();
}
qDebug() << "sending ack";
std::string m = "{\"msg\": \"ack\"}";
socket->send(zmq::buffer(m.c_str(), m.length()), zmq::send_flags::dontwait);
}
}

View file

@ -1,29 +0,0 @@
#pragma once
#include <QObject>
#include <includes.h>
#include <httpflow.h>
#include <set>
#include <zmq.hpp>
class NetworkThread : public QObject
{
Q_OBJECT
private:
std::set<std::string> received;
zmq::context_t context;
zmq::socket_t *socket;
std::set<std::string> accepted_flows;
void connect();
void disconnect();
void reconnect();
public:
explicit NetworkThread(QObject *parent = nullptr);
public slots:
void process();
signals:
void finished();
void error(QString err);
void httpMessage(http::Flow flow);
};

View file

@ -12,14 +12,16 @@ bool http::ProxyHandler::loadPlugin(QObject *proxy)
auto thread = new QThread; auto thread = new QThread;
proxy->moveToThread(thread); proxy->moveToThread(thread);
// TODO: get path from config or something
inst->path = "tcp://127.0.0.1:12345";
QObject::connect(thread, SIGNAL (started()), proxy, SLOT (process())); QObject::connect(thread, SIGNAL (started()), proxy, SLOT (process()));
//connect(worker, SIGNAL (error(QString)), this, SLOT (errorString(QString))); //QObject::connect(proxy, SIGNAL (error(QString)), this, SLOT (errorString(QString)));
QObject::connect(worker, SIGNAL (httpMessage(http::Flow)), current_session, SLOT (saveFlow(http::Flow)), Qt::QueuedConnection); //QObject::connect(proxy, SIGNAL (message(http::Flow)), current_session, SLOT (saveFlow(http::Flow)), Qt::QueuedConnection);
QObject::connect(worker, SIGNAL (httpMessage(http::Flow)), this, SLOT (updateHistory()), Qt::QueuedConnection); //QObject::connect(proxy, SIGNAL (message(http::Flow)), this, SLOT (updateHistory()), Qt::QueuedConnection);
thread->start(); thread->start();
proxies.insert({"foo", inst, thread}); //proxies.insert({"foo", inst, thread});
return true; return true;
} }