bigsnitch/plugins/mitmproxy/mitmproxy_network.h

95 lines
2.1 KiB
C
Raw Permalink Normal View History

#pragma once
#include "api.h"
2021-01-09 10:17:50 +00:00
#include "proxyinterface.h"
#include <QObject>
2021-03-19 16:40:33 +00:00
#include <QtPlugin>
2021-06-29 14:55:52 +00:00
#include <QDebug>
2021-03-03 16:12:12 +00:00
#include <set>
#include <zmq.hpp>
2021-06-29 14:55:52 +00:00
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;
}
};
};
2021-03-19 16:40:33 +00:00
/*
mitmproxy base plugin
This plugin implements the interface for the basic mitmproxy plugin.
*/
2021-03-20 01:37:12 +00:00
class mitmproxyPlugin : public QObject, public http::ProxyInterface
2021-01-09 10:17:50 +00:00
{
Q_OBJECT
2021-03-19 16:40:33 +00:00
Q_PLUGIN_METADATA(IID HTTPProxyInterfaceIID FILE "mitmproxy.json")
2021-01-09 10:17:50 +00:00
Q_INTERFACES(http::ProxyInterface)
2021-03-03 16:12:12 +00:00
private:
zmq::context_t context;
zmq::socket_t *socket;
2021-06-29 14:55:52 +00:00
std::map<std::string, Flow> flows;
2021-03-03 16:12:12 +00:00
bool is_connected = false;
void connect();
void reconnect();
2021-06-29 14:55:52 +00:00
void handle_packet(const Packet p);
2021-03-20 01:37:12 +00:00
2021-01-09 10:17:50 +00:00
public:
2021-03-20 21:56:03 +00:00
//! name of the plugin
QString getName() override;
2021-03-03 16:12:12 +00:00
//! returns, whether the proxy is currently connected
bool connected() override;
//! disconnects the handle
void disconnect() override;
2021-03-20 01:37:12 +00:00
public slots:
//! called in a custom thread, may block.
void process();
signals:
void finished();
void error(QString err);
void message(http::Flow flow);
2021-01-09 10:17:50 +00:00
};