94 lines
2.1 KiB
C++
94 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include "api.h"
|
|
#include "proxyinterface.h"
|
|
#include <QObject>
|
|
#include <QtPlugin>
|
|
#include <QDebug>
|
|
|
|
#include <set>
|
|
#include <zmq.hpp>
|
|
|
|
enum PacketType {
|
|
NACK = 0,
|
|
ACK = 1,
|
|
KILL = 2,
|
|
WARNING = 3,
|
|
ERROR = 4,
|
|
PING = 5,
|
|
HTTP_REQUEST = 6,
|
|
HTTP_RESPONSE = 7
|
|
};
|
|
|
|
enum FlowState {
|
|
ERROR = 0,
|
|
UNSENT_HTTP_REQUEST = 1,
|
|
SENT_HTTP_REQUEST = 2,
|
|
UNSENT_HTTP_RESPONSE = 3,
|
|
SENT_HTTP_RESPONSE = 4
|
|
};
|
|
|
|
struct Flow {
|
|
FlowState state;
|
|
std::string id;
|
|
std::string data;
|
|
};
|
|
|
|
struct Packet {
|
|
PacketType ptype = PacketType::NACK;
|
|
std::string flowid = "";
|
|
std::string data = "";
|
|
Packet(json j) {
|
|
// input validation, on error use default values
|
|
try {
|
|
int ptype_int = j["ptype"];
|
|
if(ptype_int < PacketType::NACK ||
|
|
ptype_int > PacketType::HTTP_RESPONSE) {
|
|
return;
|
|
}
|
|
ptype = static_cast<PacketType>(ptype_int);
|
|
flowid = j["flowid"];
|
|
data = j["data"];
|
|
} catch (nlohmann::detail::parse_error& err) {
|
|
qDebug() << "broken packet constructed from json";
|
|
return;
|
|
}
|
|
};
|
|
};
|
|
|
|
/*
|
|
mitmproxy base plugin
|
|
|
|
This plugin implements the interface for the basic mitmproxy plugin.
|
|
*/
|
|
class mitmproxyPlugin : public QObject, public http::ProxyInterface
|
|
{
|
|
Q_OBJECT
|
|
Q_PLUGIN_METADATA(IID HTTPProxyInterfaceIID FILE "mitmproxy.json")
|
|
Q_INTERFACES(http::ProxyInterface)
|
|
private:
|
|
zmq::context_t context;
|
|
zmq::socket_t *socket;
|
|
std::map<std::string, Flow> flows;
|
|
bool is_connected = false;
|
|
void connect();
|
|
void reconnect();
|
|
void handle_packet(const Packet p);
|
|
|
|
public:
|
|
//! name of the plugin
|
|
QString getName() override;
|
|
//! returns, whether the proxy is currently connected
|
|
bool connected() override;
|
|
//! disconnects the handle
|
|
void disconnect() override;
|
|
|
|
public slots:
|
|
//! called in a custom thread, may block.
|
|
void process();
|
|
|
|
signals:
|
|
void finished();
|
|
void error(QString err);
|
|
void message(http::Flow flow);
|
|
};
|