bigsnitch/plugins/mitmproxy/mitmproxy_network.cpp

133 lines
3.7 KiB
C++
Raw Permalink Normal View History

#include <mitmproxy_network.h>
2021-03-03 16:12:12 +00:00
void mitmproxyPlugin::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(this->path);
} catch (zmq::error_t err) {
qDebug() << "failed binding socket" << err.what();
2021-03-19 16:40:33 +00:00
error(err.what());
2021-03-03 16:12:12 +00:00
throw;
};
is_connected = true;
qDebug() << "connected";
}
void mitmproxyPlugin::reconnect()
{
this->disconnect();
this->connect();
}
2021-06-29 14:55:52 +00:00
void mitmproxyPlugin::handle_packet(const Packet p)
{
switch(p.ptype) {
case PacketType::NACK: {
// probably input validation fail
return;
}
case PacketType::KILL: {
// the client should never kill packets, but ok
return;
}
case PacketType::ERROR: {
}
case PacketType::WARNING: {
}
case PacketType::ACK: {
}
case PacketType::HTTP_REQUEST: {
}
case PacketType::HTTP_RESPONSE: {
}
case PacketType::PING: {
}
}
}
2021-03-20 21:56:03 +00:00
QString mitmproxyPlugin::getName()
{
return QString("mitmproxy bridge");
}
2021-03-20 01:37:12 +00:00
void mitmproxyPlugin::process()
2021-03-03 16:12:12 +00:00
{
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();
}
2021-06-29 14:55:52 +00:00
auto p = Packet(j);
handle_packet(p);
/*
2021-03-03 16:12:12 +00:00
//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();
2021-03-21 01:35:40 +00:00
//qDebug() << "message: " << QString(response.to_string().c_str());
2021-03-20 21:56:03 +00:00
emit message(j);
2021-03-03 16:12:12 +00:00
} else if(msg_type == "requestheaders") {
qDebug() << "received " << msg_type.c_str();
} else if(msg_type == "request") {
qDebug() << "received " << msg_type.c_str();
2021-03-21 01:35:40 +00:00
qDebug() << "message: " << QString(response.to_string().c_str());
2021-03-03 16:12:12 +00:00
} else if(msg_type == "ping") {
qDebug() << "received " << msg_type.c_str();
} else if(msg_type == "error") {
qDebug() << "received " << msg_type.c_str();
// websocket events
2021-03-20 21:56:03 +00:00
emit error("error received");
2021-03-03 16:12:12 +00:00
} 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();
}
2021-06-29 14:55:52 +00:00
*/
2021-03-03 16:12:12 +00:00
qDebug() << "sending ack";
std::string m = "{\"msg\": \"ack\"}";
socket->send(zmq::buffer(m.c_str(), m.length()), zmq::send_flags::dontwait);
}
}
2021-03-19 16:40:33 +00:00
bool mitmproxyPlugin::connected()
{
return is_connected;
}
2021-03-03 16:12:12 +00:00
void mitmproxyPlugin::disconnect()
{
delete socket;
is_connected = false;
qDebug() << "disconnected";
}