From 41901b8deb9acbfe094c50ecfd0d450ed32949e8 Mon Sep 17 00:00:00 2001 From: Dusan Klinec Date: Wed, 3 Apr 2019 23:58:34 +0200 Subject: [PATCH] device/trezor: env-configurable ports --- src/device_trezor/trezor/transport.cpp | 64 +++++++++++++++++++++----- src/device_trezor/trezor/transport.hpp | 10 +--- tests/trezor/trezor_tests.cpp | 2 +- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/device_trezor/trezor/transport.cpp b/src/device_trezor/trezor/transport.cpp index 991ba3395..dd9b0b52f 100644 --- a/src/device_trezor/trezor/transport.cpp +++ b/src/device_trezor/trezor/transport.cpp @@ -223,6 +223,11 @@ namespace trezor{ msg = msg_wrap; } + static void assert_port_number(uint32_t port) + { + CHECK_AND_ASSERT_THROW_MES(port >= 1024 && port < 65535, "Invalid port number: " << port); + } + Transport::Transport(): m_open_counter(0) { } @@ -263,6 +268,29 @@ namespace trezor{ const char * BridgeTransport::PATH_PREFIX = "bridge:"; + BridgeTransport::BridgeTransport( + boost::optional device_path, + boost::optional bridge_host): + m_device_path(device_path), + m_bridge_host(bridge_host ? bridge_host.get() : DEFAULT_BRIDGE), + m_response(boost::none), + m_session(boost::none), + m_device_info(boost::none) + { + const char *env_bridge_port = nullptr; + if (!bridge_host && (env_bridge_port = getenv("TREZOR_BRIDGE_PORT")) != nullptr) + { + uint16_t bridge_port; + CHECK_AND_ASSERT_THROW_MES(epee::string_tools::get_xtype_from_string(bridge_port, env_bridge_port), "Invalid bridge port: " << env_bridge_port); + assert_port_number(bridge_port); + + m_bridge_host = std::string("127.0.0.1:") + boost::lexical_cast(env_bridge_port); + MDEBUG("Bridge host: " << m_bridge_host); + } + + m_http_client.set_server(m_bridge_host, boost::none, epee::net_utils::ssl_support_t::e_ssl_support_disabled); + } + std::string BridgeTransport::get_path() const { if (!m_device_path){ return ""; @@ -401,28 +429,40 @@ namespace trezor{ const char * UdpTransport::DEFAULT_HOST = "127.0.0.1"; const int UdpTransport::DEFAULT_PORT = 21324; + static void parse_udp_path(std::string &host, int &port, std::string path) + { + if (boost::starts_with(path, UdpTransport::PATH_PREFIX)) + { + path = path.substr(strlen(UdpTransport::PATH_PREFIX)); + } + + auto delim = path.find(':'); + if (delim == std::string::npos) { + host = path; + } else { + host = path.substr(0, delim); + port = std::stoi(path.substr(delim + 1)); + } + } + UdpTransport::UdpTransport(boost::optional device_path, boost::optional> proto) : m_io_service(), m_deadline(m_io_service) { + m_device_host = DEFAULT_HOST; m_device_port = DEFAULT_PORT; + const char *env_trezor_path = nullptr; + if (device_path) { - const std::string device_str = device_path.get(); - auto delim = device_str.find(':'); - if (delim == std::string::npos) { - m_device_host = device_str; - } else { - m_device_host = device_str.substr(0, delim); - m_device_port = std::stoi(device_str.substr(delim + 1)); - } + parse_udp_path(m_device_host, m_device_port, device_path.get()); + } else if ((env_trezor_path = getenv("TREZOR_PATH")) != nullptr && boost::starts_with(env_trezor_path, UdpTransport::PATH_PREFIX)){ + parse_udp_path(m_device_host, m_device_port, std::string(env_trezor_path)); + MDEBUG("Applied TREZOR_PATH: " << m_device_host << ":" << m_device_port); } else { m_device_host = DEFAULT_HOST; } - if (m_device_port <= 1024 || m_device_port > 65535){ - throw std::invalid_argument("Port number invalid"); - } - + assert_port_number((uint32_t)m_device_port); if (m_device_host != "localhost" && m_device_host != DEFAULT_HOST){ throw std::invalid_argument("Local endpoint allowed only"); } diff --git a/src/device_trezor/trezor/transport.hpp b/src/device_trezor/trezor/transport.hpp index 2945b3184..cde862547 100644 --- a/src/device_trezor/trezor/transport.hpp +++ b/src/device_trezor/trezor/transport.hpp @@ -163,15 +163,7 @@ namespace trezor { public: BridgeTransport( boost::optional device_path = boost::none, - boost::optional bridge_host = boost::none): - m_device_path(device_path), - m_bridge_host(bridge_host ? bridge_host.get() : DEFAULT_BRIDGE), - m_response(boost::none), - m_session(boost::none), - m_device_info(boost::none) - { - m_http_client.set_server(m_bridge_host, boost::none, epee::net_utils::ssl_support_t::e_ssl_support_disabled); - } + boost::optional bridge_host = boost::none); virtual ~BridgeTransport() = default; diff --git a/tests/trezor/trezor_tests.cpp b/tests/trezor/trezor_tests.cpp index 6691125be..31c2471ec 100644 --- a/tests/trezor/trezor_tests.cpp +++ b/tests/trezor/trezor_tests.cpp @@ -138,7 +138,7 @@ int main(int argc, char* argv[]) // Bootstrapping common chain & accounts const uint8_t initial_hf = (uint8_t)get_env_long("TEST_MIN_HF", 11); const uint8_t max_hf = (uint8_t)get_env_long("TEST_MAX_HF", 11); - MINFO("Test versions " << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL); + MINFO("Test versions " << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")"); MINFO("Testing hardforks [" << (int)initial_hf << ", " << (int)max_hf << "]"); cryptonote::core core_obj(nullptr);