Skip to content

Commit

Permalink
decouple object API from HTTP API
Browse files Browse the repository at this point in the history
 - sounds cryptic, but I moved code which iterates sobject database
   from http api part, making it more generically usable.
  • Loading branch information
astibal committed Nov 20, 2023
1 parent bc3e0e2 commit 3f2c55e
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 102 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ add_executable(smithproxy
src/service/dnsupd/smithdnsupd.cpp
src/service/core/smithproxy.hpp
src/service/core/smithproxy.cpp
src/service/core/smithproxy_objapi.hpp
src/service/core/smithproxy_objapi.cpp
src/service/cmd/cmdserver.cpp
src/service/cfgapi/cfgvalue.hpp
src/service/cfgapi/cfgvalue.cpp
Expand All @@ -325,7 +327,7 @@ add_executable(smithproxy
src/service/httpd/httpd.cpp
src/service/httpd/handlers/handlers.cpp
src/service/httpd/handlers/dispatchers.cpp
src/service/httpd/jsonize.cpp
src/service/http/jsonize.cpp
src/service/http/request.hpp
src/service/http/async_request.hpp
src/service/http/webhooks.hpp
Expand Down
6 changes: 4 additions & 2 deletions src/service/core/smithproxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
#include <service/cfgapi/cfgapi.hpp>

#include <service/core/service.hpp>
#include <service/core/smithproxy_objapi.hpp>

#include <service/daemon.hpp>
#include <service/netservice.hpp>

Expand All @@ -63,8 +65,6 @@ using theReceiver = ThreadedReceiver<MitmUdpProxy>;
using socksAcceptor = ThreadedAcceptor<MitmSocksProxy>;
using socksReceiver = ThreadedReceiver<MitmSocksUdpProxy>;



class SmithProxy : public Service {

SmithProxy() : Service() {};
Expand Down Expand Up @@ -136,6 +136,8 @@ class SmithProxy : public Service {

static bool init_syslog();
bool load_config(std::string& config_f, bool reload = false);

ObjAPI API;
};


Expand Down
108 changes: 108 additions & 0 deletions src/service/core/smithproxy_objapi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <optional>
#include <thread>
#include <mutex>

#include <nlohmann/json.hpp>

#include <socle/sobject.hpp>
#include <log/logan.hpp>

#include <service/core/smithproxy.hpp>
#include <service/core/smithproxy_objapi.hpp>
#include <service/http/jsonize.hpp>
#include <proxy/mitmproxy.hpp>

nlohmann::json ObjAPI::proxy_session_list_json(uint64_t oid, bool active_only, bool tls_info, bool verbose) {
using nlohmann::json;
auto const& instance = SmithProxy::instance();

auto lc_ = std::scoped_lock(socle::sobjectDB::getlock());
json ret;

auto verbosity = verbose ? iDIA : iINF;

auto json_single_proxy = [&](MitmProxy* proxy) -> std::optional<nlohmann::json> {
if(active_only) {
if(proxy->stats().mtr_up.get() == 0L and proxy->stats().mtr_down.get() == 0L)
return std::nullopt;
}

if(proxy->lsize() == 0 or proxy->rsize() == 0) {
return std::nullopt;
}

auto proxy_detail = jsonize::from(proxy, verbosity);

if(tls_info) {
nlohmann::json left;
nlohmann::json right;

if(proxy->first_left()) {
left = jsonize::from(proxy->first_left()->com(), verbosity);
}
if(proxy->first_right()) {
right = jsonize::from(proxy->first_right()->com(), verbosity);
}

proxy_detail["tlsinfo"] = { { "left", left },
{ "right", right }
};
}
return proxy_detail;
};



if(oid != 0ULL) {
auto it = socle::sobjectDB::oid_db().find(oid);
if(it != socle::sobjectDB::oid_db().end()) {

std::string what = it->second->c_type();
if (what == "MitmProxy" || what == "SocksProxy") {
auto *proxy = dynamic_cast<MitmProxy *>(it->second.get());
if (proxy) {
auto single_ret = json_single_proxy(proxy);
if (single_ret.has_value()) ret.push_back(single_ret.value());
return ret;
}
}
}
return nlohmann::json::array();
} else {

auto list_worker = [&json_single_proxy, &ret](const char* title, auto& listener) {
for (auto const& acc: listener) {
for(auto const& wrk: acc->tasks()) {

auto lc_ = std::scoped_lock(wrk.second->proxy_lock());

for(auto const& [ p, _ ] : wrk.second->proxies()) {
if(auto* proxy = dynamic_cast<MitmProxy*>(p.get()); p != nullptr) {
auto single_ret = json_single_proxy(proxy);
if (single_ret.has_value()) {
single_ret.value()["origin"] = title;
ret.push_back(single_ret.value());
}
}
}
}
}
};

list_worker("plain acceptor", instance.plain_proxies);
list_worker("tls acceptor", instance.ssl_proxies);

list_worker("udp receiver", instance.udp_proxies);
list_worker("dtls receiver", instance.dtls_proxies);

list_worker("socks acceptor", instance.socks_proxies);

list_worker("plain redirect acceptor", instance.redir_plain_proxies);
list_worker("dns redirect receiver", instance.redir_udp_proxies);
list_worker("tls redirect acceptor", instance.redir_ssl_proxies);

if (ret.empty()) return nlohmann::json::array();

return ret;
}
}
8 changes: 8 additions & 0 deletions src/service/core/smithproxy_objapi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

#pragma once

#include <nlohmann/json.hpp>

struct ObjAPI {
nlohmann::json proxy_session_list_json(uint64_t oid, bool active_only, bool tls_info, bool verbose);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <service/httpd/jsonize.hpp>
#include "jsonize.hpp"
#include <libconfig.h++>

namespace jsonize {
Expand Down Expand Up @@ -201,8 +201,7 @@ namespace jsonize {
right.emplace_back(jsonize::from((MitmHostCX *) nullptr, verbosity));
}


ret["oid"] = what->oid();
ret["oid"] = what->to_connection_ID();

ret["left"] = left;
ret["right"] = right;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#include <openssl/x509.h>
#include <nlohmann/json.hpp>

#include <proxy/mitmproxy.hpp>
#include "src/proxy/mitmproxy.hpp"
#include <libconfig.h++>

namespace jsonize {
Expand Down
2 changes: 1 addition & 1 deletion src/service/httpd/cfg/add.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

#include <ext/lmhpp/include/lmhttpd.hpp>
#include <service/httpd/httpd.hpp>
#include <service/httpd/jsonize.hpp>
#include <service/http/jsonize.hpp>

#include <main.hpp>
#include <service/cfgapi/cfgapi.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/service/httpd/cfg/get.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <ext/lmhpp/include/lmhttpd.hpp>
#include <service/httpd/httpd.hpp>
#include <service/httpd/jsonize.hpp>
#include <service/http/jsonize.hpp>

#include <main.hpp>
#include <service/cfgapi/cfgapi.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/service/httpd/cfg/set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <ext/lmhpp/include/lmhttpd.hpp>
#include <service/httpd/httpd.hpp>
#include <service/httpd/jsonize.hpp>
#include <service/http/jsonize.hpp>

#include <main.hpp>
#include <service/cfgapi/cfgapi.hpp>
Expand Down
94 changes: 3 additions & 91 deletions src/service/httpd/diag/daig_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,107 +41,19 @@

#include <ext/lmhpp/include/lmhttpd.hpp>
#include <service/httpd/util.hpp>
#include <service/httpd/jsonize.hpp>

#include <service/http/jsonize.hpp>


static nlohmann::json json_proxy_session_list(struct MHD_Connection * connection, std::string const& meth, std::string const& req) {

using nlohmann::json;
auto oid = connection_ull_param(connection, "oid", 0ULL);
using namespace jsonize;

std::scoped_lock<std::recursive_mutex> l_(socle::sobjectDB::getlock());
json ret;

bool flag_active_only = load_json_params<bool>(req, "active").value_or(false);
bool flag_tlsinfo = load_json_params<bool>(req, "tlsinfo").value_or(false);
bool flag_verbose = load_json_params<bool>(req, "verbose").value_or(false);
auto verbosity = flag_verbose ? iDIA : iINF;

auto json_single_proxy = [&](MitmProxy* proxy) -> std::optional<nlohmann::json> {
if(flag_active_only) {
if(proxy->stats().mtr_up.get() == 0L and proxy->stats().mtr_down.get() == 0L)
return std::nullopt;
}

if(proxy->lsize() == 0 or proxy->rsize() == 0) {
return std::nullopt;
}

auto proxy_detail = jsonize::from(proxy, verbosity);

if(flag_tlsinfo) {
nlohmann::json left;
nlohmann::json right;

if(proxy->first_left()) {
left = jsonize::from(proxy->first_left()->com(), verbosity);
}
if(proxy->first_right()) {
right = jsonize::from(proxy->first_right()->com(), verbosity);
}

proxy_detail["tlsinfo"] = { { "left", left },
{ "right", right }
};
}
return proxy_detail;
};


auto oid = connection_ull_param(connection, "oid", 0ULL);
if(oid != 0ULL) {
auto it = socle::sobjectDB::oid_db().find(oid);
if(it != socle::sobjectDB::oid_db().end()) {

std::string what = it->second->c_type();
if (what == "MitmProxy" || what == "SocksProxy") {
auto *proxy = dynamic_cast<MitmProxy *>(it->second.get());
if (proxy) {
auto single_ret = json_single_proxy(proxy);
if (single_ret.has_value()) ret.push_back(single_ret.value());
return ret;
}
}
}
return nlohmann::json::array();
} else {

auto& sx = SmithProxy::instance();

auto list_worker = [&json_single_proxy, &ret](const char* title, auto& listener) {
for (auto const& acc: listener) {
for(auto const& wrk: acc->tasks()) {

auto lc_ = std::scoped_lock(wrk.second->proxy_lock());

for(auto const& [ p, _ ] : wrk.second->proxies()) {
if(auto* proxy = dynamic_cast<MitmProxy*>(p.get()); p != nullptr) {
auto single_ret = json_single_proxy(proxy);
if (single_ret.has_value()) {
single_ret.value()["origin"] = title;
ret.push_back(single_ret.value());
}
}
}
}
}
};

list_worker("plain acceptor", sx.plain_proxies);
list_worker("tls acceptor", sx.ssl_proxies);

list_worker("udp receiver", sx.udp_proxies);
list_worker("dtls receiver", sx.dtls_proxies);

list_worker("socks acceptor", sx.socks_proxies);

list_worker("plain redirect acceptor", sx.redir_plain_proxies);
list_worker("dns redirect receiver", sx.redir_udp_proxies);
list_worker("tls redirect acceptor", sx.redir_ssl_proxies);

if (ret.empty()) return nlohmann::json::array();
return SmithProxy::instance().API.proxy_session_list_json(oid, flag_active_only, flag_tlsinfo, flag_verbose);

return ret;
}
}
2 changes: 1 addition & 1 deletion src/service/httpd/diag/diag_ssl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

#include <ext/lmhpp/include/lmhttpd.hpp>
#include <service/httpd/util.hpp>
#include <service/httpd/jsonize.hpp>
#include <service/http/jsonize.hpp>


static nlohmann::json json_ssl_cache_stats(struct MHD_Connection* conn, std::string const& meth, std::string const& req) {
Expand Down

0 comments on commit 3f2c55e

Please sign in to comment.