From 0acc8608b15aa644e1458577c2180f5f0b69fb75 Mon Sep 17 00:00:00 2001 From: Ian Chua Date: Wed, 23 Sep 2026 20:11:47 +0800 Subject: [PATCH] refactor: make printer connection SSL agent-specific --- src/slic3r/GUI/DeviceCore/DevManager.cpp | 14 ++--------- src/slic3r/GUI/DeviceManager.cpp | 12 +++++++--- src/slic3r/GUI/DeviceManager.hpp | 2 +- src/slic3r/Utils/BBLPrinterAgent.cpp | 13 ++++++++++- src/slic3r/Utils/IPrinterAgent.hpp | 9 ++++---- src/slic3r/Utils/MoonrakerPrinterAgent.cpp | 27 ++++------------------ src/slic3r/Utils/MoonrakerPrinterAgent.hpp | 2 +- 7 files changed, 35 insertions(+), 44 deletions(-) diff --git a/src/slic3r/GUI/DeviceCore/DevManager.cpp b/src/slic3r/GUI/DeviceCore/DevManager.cpp index dbc2547445..5a0e4d768f 100644 --- a/src/slic3r/GUI/DeviceCore/DevManager.cpp +++ b/src/slic3r/GUI/DeviceCore/DevManager.cpp @@ -673,12 +673,7 @@ namespace Slic3r m_agent->disconnect_printer(); it->second->reset(); -#if !BBL_RELEASE_TO_PUBLIC - AppConfig* config = get_app_config(); - it->second->connect(config && config->get("enable_ssl_for_mqtt") == "true"); -#else - it->second->connect(it->second->local_use_ssl); -#endif + it->second->connect(); it->second->set_lan_mode_connection_state(true); } } @@ -700,12 +695,7 @@ namespace Slic3r { BOOST_LOG_TRIVIAL(info) << "set_selected_machine: select new lan machine, dev_id =" << dev_id; it->second->reset(); -#if !BBL_RELEASE_TO_PUBLIC - AppConfig* config = get_app_config(); - it->second->connect(config && config->get("enable_ssl_for_mqtt") == "true"); -#else - it->second->connect(it->second->local_use_ssl); -#endif + it->second->connect(); it->second->set_lan_mode_connection_state(true); } } diff --git a/src/slic3r/GUI/DeviceManager.cpp b/src/slic3r/GUI/DeviceManager.cpp index 06cf365752..ec21f72255 100644 --- a/src/slic3r/GUI/DeviceManager.cpp +++ b/src/slic3r/GUI/DeviceManager.cpp @@ -2619,14 +2619,19 @@ void MachineObject::update_print_progress(const json& value) curr_task->task_progress = mc_print_percent; } -int MachineObject::connect(bool use_openssl) +int MachineObject::connect() { if (get_dev_ip().empty()) return -1; std::string username = m_agent ? m_agent->default_lan_username() : std::string(); std::string password = get_access_code(); std::string port; - std::string host = Http::get_host_from_url(get_dev_ip(), &port); + std::string input = get_dev_ip(); + + const bool use_ssl = input.rfind("https", 0) == 0; + + // This strips out the http/https prefix + std::string host = Http::get_host_from_url(input, &port); std::string ca_file; if (GUI::wxGetApp().preset_bundle) { @@ -2639,6 +2644,7 @@ int MachineObject::connect(bool use_openssl) if (host.empty()) host = get_dev_ip(); + if (m_agent) { try { PrinterConnectionParams params{ @@ -2647,7 +2653,7 @@ int MachineObject::connect(bool use_openssl) port, username, password, - use_openssl, + use_ssl, ca_file }; return m_agent->connect_printer(params); diff --git a/src/slic3r/GUI/DeviceManager.hpp b/src/slic3r/GUI/DeviceManager.hpp index 2182c2a645..b69e2f8f5a 100644 --- a/src/slic3r/GUI/DeviceManager.hpp +++ b/src/slic3r/GUI/DeviceManager.hpp @@ -678,7 +678,7 @@ public: /* machine mqtt apis */ - int connect(bool use_openssl = true); + int connect(); int disconnect(); json_diff print_json; diff --git a/src/slic3r/Utils/BBLPrinterAgent.cpp b/src/slic3r/Utils/BBLPrinterAgent.cpp index 613df8834f..1f2dc74003 100644 --- a/src/slic3r/Utils/BBLPrinterAgent.cpp +++ b/src/slic3r/Utils/BBLPrinterAgent.cpp @@ -4,6 +4,7 @@ #include "NetworkAgentFactory.hpp" #include "NetworkAgent.hpp" #include "libslic3r/Utils.hpp" +#include "slic3r/GUI/GUI_App.hpp" #include #include @@ -264,8 +265,18 @@ int BBLPrinterAgent::connect_printer(const PrinterConnectionParams& params) auto& plugin = BBLNetworkPlugin::instance(); auto agent = plugin.get_agent(); auto func = plugin.get_connect_printer(); +#if !BBL_RELEASE_TO_PUBLIC + const bool use_ssl_for_mqtt = GUI::wxGetApp().app_config && + GUI::wxGetApp().app_config->get_bool("enable_ssl_for_mqtt"); +#else + bool use_ssl_for_mqtt = true; + if (auto* dev_manager = GUI::wxGetApp().getDeviceManager()) { + if (auto* machine = dev_manager->get_my_machine(params.dev_id)) + use_ssl_for_mqtt = machine->local_use_ssl; + } +#endif if (func && agent) { - return func(agent, params.dev_id, params.host, params.username, params.password, params.use_ssl); + return func(agent, params.dev_id, params.host, params.username, params.password, use_ssl_for_mqtt); } return -1; } diff --git a/src/slic3r/Utils/IPrinterAgent.hpp b/src/slic3r/Utils/IPrinterAgent.hpp index ad018d466e..293b0af2b8 100644 --- a/src/slic3r/Utils/IPrinterAgent.hpp +++ b/src/slic3r/Utils/IPrinterAgent.hpp @@ -34,13 +34,14 @@ struct AgentInfo { std::string description; ///< Brief description of the agent's capabilities, e.g. "Orca printer agent" }; -struct PrinterConnectionParams { +struct PrinterConnectionParams +{ std::string dev_id; - std::string host; - std::string port; + std::string host; // host address, usually the IP address without the http/https protocol + std::string port; // optional std::string username; std::string password; - bool use_ssl = false; + bool use_ssl = false; // indicates if http or https std::string ca_file; }; diff --git a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp index 77f5955d5d..7d11878d5e 100644 --- a/src/slic3r/Utils/MoonrakerPrinterAgent.cpp +++ b/src/slic3r/Utils/MoonrakerPrinterAgent.cpp @@ -1056,9 +1056,7 @@ bool MoonrakerPrinterAgent::init_device_info(const std::string& dev_id, const st device_info.api_key = password; device_info.model_name = printer_cfg.opt_string("printer_model"); device_info.model_id = preset.get_printer_type(preset_bundle); - device_info.base_url = normalize_base_url(dev_ip, port); - if (use_ssl && boost::istarts_with(device_info.base_url, "http://")) - device_info.base_url.replace(0, 7, "https://"); + device_info.base_url = normalize_base_url(use_ssl, dev_ip, port); device_info.dev_id = dev_id; device_info.version = ""; device_info.dev_name = device_info.dev_id; @@ -2107,26 +2105,11 @@ bool MoonrakerPrinterAgent::is_numeric(const std::string& value) return !value.empty() && std::all_of(value.begin(), value.end(), [](unsigned char c) { return std::isdigit(c) != 0; }); } -std::string MoonrakerPrinterAgent::normalize_base_url(std::string host, const std::string& port) +std::string MoonrakerPrinterAgent::normalize_base_url(bool use_ssl, const std::string& host, const std::string& port) { - boost::trim(host); - if (host.empty()) { - return ""; - } - - std::string value = host; - if (is_numeric(port) && value.find("://") == std::string::npos && value.find(':') == std::string::npos) { - value += ":" + port; - } - - if (!boost::istarts_with(value, "http://") && !boost::istarts_with(value, "https://")) { - value = "http://" + value; - } - - if (value.size() > 1 && value.back() == '/') { - value.pop_back(); - } - + std::string value = use_ssl ? "https://" : "http://"; + value += host; + value += port.empty() ? "" : (":" + port); return value; } diff --git a/src/slic3r/Utils/MoonrakerPrinterAgent.hpp b/src/slic3r/Utils/MoonrakerPrinterAgent.hpp index 9af738e413..07365cb93c 100644 --- a/src/slic3r/Utils/MoonrakerPrinterAgent.hpp +++ b/src/slic3r/Utils/MoonrakerPrinterAgent.hpp @@ -102,7 +102,7 @@ protected: // Helpers bool is_numeric(const std::string& value); - std::string normalize_base_url(std::string host, const std::string& port); + std::string normalize_base_url(bool use_ssl, const std::string& host, const std::string& port); std::string sanitize_filename(const std::string& filename); std::string join_url(const std::string& base_url, const std::string& path) const;