From 9788a80c6f12765fce23fd81789242b66624854a Mon Sep 17 00:00:00 2001 From: David Kocik Date: Tue, 30 Nov 2021 12:42:09 +0100 Subject: [PATCH 1/3] Added port number to resolved ip address if specified. fix of #7361 --- src/slic3r/Utils/OctoPrint.cpp | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/slic3r/Utils/OctoPrint.cpp b/src/slic3r/Utils/OctoPrint.cpp index b084b67571..58431691cf 100644 --- a/src/slic3r/Utils/OctoPrint.cpp +++ b/src/slic3r/Utils/OctoPrint.cpp @@ -115,8 +115,7 @@ bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, Erro std::string url; bool res = true; - if (m_host.find("https://") == 0 || test_msg.empty()) - { + if (m_host.find("https://") == 0 || test_msg.empty()) { // If https is entered we assume signed ceritificate is being used // IP resolving will not happen - it could resolve into address not being specified in cert url = make_url("api/files/local"); @@ -129,6 +128,34 @@ bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, Erro // put ipv6 into [] brackets (there shouldn't be any http:// if its resolved addr) if (resolved_addr.find(':') != std::string::npos && resolved_addr.at(0) != '[') resolved_addr = "[" + resolved_addr + "]"; + // port number is not part of resolved addr. If it is in m_host, we need to add it again + // if ipv6 the format would be [add]:port + if (size_t port_start = m_host.rfind(':'); port_start != std::string::npos) { + size_t count_of_colon = std::count(m_host.begin(), m_host.end(), ':'); + // ipv6 + if (size_t addr_end = m_host.rfind(']'); addr_end != port_start - 1 && count_of_colon > 2) + port_start = std::string::npos; + // http:// (https cant go to this else branch) + else if (m_host.find("http:") == 0 && port_start == 4) + port_start = std::string::npos; + // add port to resolved addres + if (port_start != std::string::npos && port_start < m_host.size()) { + std::string port_string = m_host.substr(port_start); + // last check - try casting port string to number. + bool cont = true; + for (size_t i = 1; i < port_string.size(); i++) { + // number smaller than 65535 + if (port_string[i] < '0' || port_string[i] > '9' || i > 6) { + cont = false; + BOOST_LOG_TRIVIAL(debug) << "IP resolve wrongly concidered string as port: " << port_string; + break; + } + } + if (cont) + resolved_addr += port_string; + + } + } url = make_url("api/files/local", resolved_addr); } From 0939dab807cc0aa8aa4ddb44e9c4a31f43e8b86b Mon Sep 17 00:00:00 2001 From: David Kocik Date: Tue, 30 Nov 2021 12:42:59 +0100 Subject: [PATCH 2/3] allow_ip_resolve in app config --- src/libslic3r/AppConfig.cpp | 3 +++ src/slic3r/Utils/OctoPrint.cpp | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/libslic3r/AppConfig.cpp b/src/libslic3r/AppConfig.cpp index 9c9e7e0f30..ab636a03ea 100644 --- a/src/libslic3r/AppConfig.cpp +++ b/src/libslic3r/AppConfig.cpp @@ -174,6 +174,9 @@ void AppConfig::set_defaults() if (get("show_hints").empty()) set("show_hints", "1"); + if (get("allow_ip_resolve").empty()) + set("allow_ip_resolve", "1"); + #ifdef _WIN32 if (get("use_legacy_3DConnexion").empty()) set("use_legacy_3DConnexion", "0"); diff --git a/src/slic3r/Utils/OctoPrint.cpp b/src/slic3r/Utils/OctoPrint.cpp index 58431691cf..df2e0d2b17 100644 --- a/src/slic3r/Utils/OctoPrint.cpp +++ b/src/slic3r/Utils/OctoPrint.cpp @@ -15,6 +15,7 @@ #include "slic3r/GUI/I18N.hpp" #include "slic3r/GUI/GUI.hpp" #include "Http.hpp" +#include "libslic3r/AppConfig.hpp" namespace fs = boost::filesystem; @@ -115,7 +116,9 @@ bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, Erro std::string url; bool res = true; - if (m_host.find("https://") == 0 || test_msg.empty()) { + bool allow_ip_resolve = GUI::get_app_config()->get("allow_ip_resolve") == "1"; + + if (m_host.find("https://") == 0 || test_msg.empty() || !allow_ip_resolve) { // If https is entered we assume signed ceritificate is being used // IP resolving will not happen - it could resolve into address not being specified in cert url = make_url("api/files/local"); From a4b4af8a1acda308257906cbdac95f6d2d47492a Mon Sep 17 00:00:00 2001 From: David Kocik Date: Thu, 2 Dec 2021 10:25:21 +0100 Subject: [PATCH 3/3] Change of 71082adbc9ac2101c39f46218d666e46ab4ecb7c Instead of just adding port, take original address and replace just host with resolved host. fix of #7389 --- src/slic3r/Utils/OctoPrint.cpp | 59 +++++++++++++++++----------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/slic3r/Utils/OctoPrint.cpp b/src/slic3r/Utils/OctoPrint.cpp index df2e0d2b17..0aec912c1d 100644 --- a/src/slic3r/Utils/OctoPrint.cpp +++ b/src/slic3r/Utils/OctoPrint.cpp @@ -24,6 +24,30 @@ namespace pt = boost::property_tree; namespace Slic3r { +namespace { +std::string substitute_host(const std::string& orig_addr, const std::string sub_addr) +{ + //URI = scheme ":"["//"[userinfo "@"] host [":" port]] path["?" query]["#" fragment] + std::string final_addr = orig_addr; + // http + size_t double_dash = orig_addr.find("//"); + size_t host_start = (double_dash == std::string::npos ? 0 : double_dash + 2); + // userinfo + size_t at = orig_addr.find("@"); + host_start = (at != std::string::npos && at > host_start ? at + 1 : host_start); + // end of host, could be port, subpath (could be query or fragment?) + size_t host_end = orig_addr.find_first_of(":/?#", host_start); + host_end = (host_end == std::string::npos ? orig_addr.length() : host_end); + // now host_start and host_end should mark where to put resolved addr + // check host_start. if its nonsense, lets just use original addr (or resolved addr?) + if (host_start >= orig_addr.length()) { + return final_addr; + } + final_addr.replace(host_start, host_end - host_start, sub_addr); + return final_addr; +} +} //namespace + OctoPrint::OctoPrint(DynamicPrintConfig *config) : m_host(config->opt_string("print_host")), m_apikey(config->opt_string("printhost_apikey")), @@ -128,38 +152,13 @@ bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, Erro // This new address returns in "test_msg" variable. // Solves troubles of uploades failing with name address. std::string resolved_addr = boost::nowide::narrow(test_msg); - // put ipv6 into [] brackets (there shouldn't be any http:// if its resolved addr) + // put ipv6 into [] brackets if (resolved_addr.find(':') != std::string::npos && resolved_addr.at(0) != '[') resolved_addr = "[" + resolved_addr + "]"; - // port number is not part of resolved addr. If it is in m_host, we need to add it again - // if ipv6 the format would be [add]:port - if (size_t port_start = m_host.rfind(':'); port_start != std::string::npos) { - size_t count_of_colon = std::count(m_host.begin(), m_host.end(), ':'); - // ipv6 - if (size_t addr_end = m_host.rfind(']'); addr_end != port_start - 1 && count_of_colon > 2) - port_start = std::string::npos; - // http:// (https cant go to this else branch) - else if (m_host.find("http:") == 0 && port_start == 4) - port_start = std::string::npos; - // add port to resolved addres - if (port_start != std::string::npos && port_start < m_host.size()) { - std::string port_string = m_host.substr(port_start); - // last check - try casting port string to number. - bool cont = true; - for (size_t i = 1; i < port_string.size(); i++) { - // number smaller than 65535 - if (port_string[i] < '0' || port_string[i] > '9' || i > 6) { - cont = false; - BOOST_LOG_TRIVIAL(debug) << "IP resolve wrongly concidered string as port: " << port_string; - break; - } - } - if (cont) - resolved_addr += port_string; - - } - } - url = make_url("api/files/local", resolved_addr); + // in original address (m_host) replace host for resolved ip + std::string final_addr = substitute_host(m_host, resolved_addr); + BOOST_LOG_TRIVIAL(debug) << "Upload address after ip resolve: " << final_addr; + url = make_url("api/files/local", final_addr); } BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Uploading file %2% at %3%, filename: %4%, path: %5%, print: %6%")