refactor: make printer connection SSL agent-specific

This commit is contained in:
Ian Chua
2026-09-23 20:11:47 +08:00
parent a2a9b2ce94
commit 0acc8608b1
7 changed files with 35 additions and 44 deletions
+2 -12
View File
@@ -673,12 +673,7 @@ namespace Slic3r
m_agent->disconnect_printer(); m_agent->disconnect_printer();
it->second->reset(); it->second->reset();
#if !BBL_RELEASE_TO_PUBLIC it->second->connect();
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->set_lan_mode_connection_state(true); 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; BOOST_LOG_TRIVIAL(info) << "set_selected_machine: select new lan machine, dev_id =" << dev_id;
it->second->reset(); it->second->reset();
#if !BBL_RELEASE_TO_PUBLIC it->second->connect();
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->set_lan_mode_connection_state(true); it->second->set_lan_mode_connection_state(true);
} }
} }
+9 -3
View File
@@ -2619,14 +2619,19 @@ void MachineObject::update_print_progress(const json& value)
curr_task->task_progress = mc_print_percent; curr_task->task_progress = mc_print_percent;
} }
int MachineObject::connect(bool use_openssl) int MachineObject::connect()
{ {
if (get_dev_ip().empty()) return -1; if (get_dev_ip().empty()) return -1;
std::string username = m_agent ? m_agent->default_lan_username() : std::string(); std::string username = m_agent ? m_agent->default_lan_username() : std::string();
std::string password = get_access_code(); std::string password = get_access_code();
std::string port; 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; std::string ca_file;
if (GUI::wxGetApp().preset_bundle) { if (GUI::wxGetApp().preset_bundle) {
@@ -2639,6 +2644,7 @@ int MachineObject::connect(bool use_openssl)
if (host.empty()) if (host.empty())
host = get_dev_ip(); host = get_dev_ip();
if (m_agent) { if (m_agent) {
try { try {
PrinterConnectionParams params{ PrinterConnectionParams params{
@@ -2647,7 +2653,7 @@ int MachineObject::connect(bool use_openssl)
port, port,
username, username,
password, password,
use_openssl, use_ssl,
ca_file ca_file
}; };
return m_agent->connect_printer(params); return m_agent->connect_printer(params);
+1 -1
View File
@@ -678,7 +678,7 @@ public:
/* machine mqtt apis */ /* machine mqtt apis */
int connect(bool use_openssl = true); int connect();
int disconnect(); int disconnect();
json_diff print_json; json_diff print_json;
+12 -1
View File
@@ -4,6 +4,7 @@
#include "NetworkAgentFactory.hpp" #include "NetworkAgentFactory.hpp"
#include "NetworkAgent.hpp" #include "NetworkAgent.hpp"
#include "libslic3r/Utils.hpp" #include "libslic3r/Utils.hpp"
#include "slic3r/GUI/GUI_App.hpp"
#include <boost/format.hpp> #include <boost/format.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
@@ -264,8 +265,18 @@ int BBLPrinterAgent::connect_printer(const PrinterConnectionParams& params)
auto& plugin = BBLNetworkPlugin::instance(); auto& plugin = BBLNetworkPlugin::instance();
auto agent = plugin.get_agent(); auto agent = plugin.get_agent();
auto func = plugin.get_connect_printer(); 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) { 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; return -1;
} }
+5 -4
View File
@@ -34,13 +34,14 @@ struct AgentInfo {
std::string description; ///< Brief description of the agent's capabilities, e.g. "Orca printer agent" 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 dev_id;
std::string host; std::string host; // host address, usually the IP address without the http/https protocol
std::string port; std::string port; // optional
std::string username; std::string username;
std::string password; std::string password;
bool use_ssl = false; bool use_ssl = false; // indicates if http or https
std::string ca_file; std::string ca_file;
}; };
+5 -22
View File
@@ -1056,9 +1056,7 @@ bool MoonrakerPrinterAgent::init_device_info(const std::string& dev_id, const st
device_info.api_key = password; device_info.api_key = password;
device_info.model_name = printer_cfg.opt_string("printer_model"); device_info.model_name = printer_cfg.opt_string("printer_model");
device_info.model_id = preset.get_printer_type(preset_bundle); device_info.model_id = preset.get_printer_type(preset_bundle);
device_info.base_url = normalize_base_url(dev_ip, port); device_info.base_url = normalize_base_url(use_ssl, dev_ip, port);
if (use_ssl && boost::istarts_with(device_info.base_url, "http://"))
device_info.base_url.replace(0, 7, "https://");
device_info.dev_id = dev_id; device_info.dev_id = dev_id;
device_info.version = ""; device_info.version = "";
device_info.dev_name = device_info.dev_id; 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; }); 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); std::string value = use_ssl ? "https://" : "http://";
if (host.empty()) { value += host;
return ""; value += port.empty() ? "" : (":" + port);
}
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();
}
return value; return value;
} }
+1 -1
View File
@@ -102,7 +102,7 @@ protected:
// Helpers // Helpers
bool is_numeric(const std::string& value); 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 sanitize_filename(const std::string& filename);
std::string join_url(const std::string& base_url, const std::string& path) const; std::string join_url(const std::string& base_url, const std::string& path) const;