diff --git a/CMakeLists.txt b/CMakeLists.txt index 7db07248..2b098615 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -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 diff --git a/src/service/core/smithproxy.hpp b/src/service/core/smithproxy.hpp index e10e8f7e..9e7aec4f 100644 --- a/src/service/core/smithproxy.hpp +++ b/src/service/core/smithproxy.hpp @@ -49,6 +49,8 @@ #include #include +#include + #include #include @@ -63,8 +65,6 @@ using theReceiver = ThreadedReceiver; using socksAcceptor = ThreadedAcceptor; using socksReceiver = ThreadedReceiver; - - class SmithProxy : public Service { SmithProxy() : Service() {}; @@ -136,6 +136,8 @@ class SmithProxy : public Service { static bool init_syslog(); bool load_config(std::string& config_f, bool reload = false); + + ObjAPI API; }; diff --git a/src/service/core/smithproxy_objapi.cpp b/src/service/core/smithproxy_objapi.cpp new file mode 100644 index 00000000..528d7146 --- /dev/null +++ b/src/service/core/smithproxy_objapi.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include + +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 { + 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(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(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; + } +} diff --git a/src/service/core/smithproxy_objapi.hpp b/src/service/core/smithproxy_objapi.hpp new file mode 100644 index 00000000..7fc673e1 --- /dev/null +++ b/src/service/core/smithproxy_objapi.hpp @@ -0,0 +1,8 @@ + +#pragma once + +#include + +struct ObjAPI { + nlohmann::json proxy_session_list_json(uint64_t oid, bool active_only, bool tls_info, bool verbose); +}; \ No newline at end of file diff --git a/src/service/httpd/jsonize.cpp b/src/service/http/jsonize.cpp similarity index 99% rename from src/service/httpd/jsonize.cpp rename to src/service/http/jsonize.cpp index 2d7a7c94..a88be46f 100644 --- a/src/service/httpd/jsonize.cpp +++ b/src/service/http/jsonize.cpp @@ -1,4 +1,4 @@ -#include +#include "jsonize.hpp" #include namespace jsonize { @@ -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; diff --git a/src/service/httpd/jsonize.hpp b/src/service/http/jsonize.hpp similarity index 98% rename from src/service/httpd/jsonize.hpp rename to src/service/http/jsonize.hpp index c213dc17..ae7ceac6 100644 --- a/src/service/httpd/jsonize.hpp +++ b/src/service/http/jsonize.hpp @@ -44,7 +44,7 @@ #include #include -#include +#include "src/proxy/mitmproxy.hpp" #include namespace jsonize { diff --git a/src/service/httpd/cfg/add.hpp b/src/service/httpd/cfg/add.hpp index 0b3aaf63..b14013a2 100644 --- a/src/service/httpd/cfg/add.hpp +++ b/src/service/httpd/cfg/add.hpp @@ -44,7 +44,7 @@ #include #include -#include +#include #include #include diff --git a/src/service/httpd/cfg/get.hpp b/src/service/httpd/cfg/get.hpp index 6956455b..82ebf530 100644 --- a/src/service/httpd/cfg/get.hpp +++ b/src/service/httpd/cfg/get.hpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include diff --git a/src/service/httpd/cfg/set.hpp b/src/service/httpd/cfg/set.hpp index d0c76420..53cb033c 100644 --- a/src/service/httpd/cfg/set.hpp +++ b/src/service/httpd/cfg/set.hpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include diff --git a/src/service/httpd/diag/daig_proxy.hpp b/src/service/httpd/diag/daig_proxy.hpp index f7aca856..6a6a6e70 100644 --- a/src/service/httpd/diag/daig_proxy.hpp +++ b/src/service/httpd/diag/daig_proxy.hpp @@ -41,107 +41,19 @@ #include #include -#include - +#include 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 l_(socle::sobjectDB::getlock()); - json ret; - bool flag_active_only = load_json_params(req, "active").value_or(false); bool flag_tlsinfo = load_json_params(req, "tlsinfo").value_or(false); bool flag_verbose = load_json_params(req, "verbose").value_or(false); - auto verbosity = flag_verbose ? iDIA : iINF; - - auto json_single_proxy = [&](MitmProxy* proxy) -> std::optional { - 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(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(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; - } } \ No newline at end of file diff --git a/src/service/httpd/diag/diag_ssl.hpp b/src/service/httpd/diag/diag_ssl.hpp index 95f54e07..35ca01dc 100644 --- a/src/service/httpd/diag/diag_ssl.hpp +++ b/src/service/httpd/diag/diag_ssl.hpp @@ -41,7 +41,7 @@ #include #include -#include +#include static nlohmann::json json_ssl_cache_stats(struct MHD_Connection* conn, std::string const& meth, std::string const& req) {