aboutsummaryrefslogtreecommitdiff
path: root/src/device_trezor
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2019-04-15 09:17:47 +0200
committerRiccardo Spagni <ric@spagni.net>2019-04-15 09:17:47 +0200
commite31559d3199552adbf5c57216633405923148be4 (patch)
treef7f6d5188974c0653c04b5bcc0d6ec6bcb0f5211 /src/device_trezor
parentMerge pull request #5396 (diff)
parentdevice/trezor: env-configurable ports (diff)
downloadmonero-e31559d3199552adbf5c57216633405923148be4.tar.xz
Merge pull request #5398
41901b8d device/trezor: env-configurable ports (Dusan Klinec) c97a1f79 tests: trezor tests fixes and improvements (Dusan Klinec)
Diffstat (limited to 'src/device_trezor')
-rw-r--r--src/device_trezor/trezor/transport.cpp64
-rw-r--r--src/device_trezor/trezor/transport.hpp10
2 files changed, 53 insertions, 21 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<std::string> device_path,
+ boost::optional<std::string> 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<std::string>(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<std::string> device_path,
boost::optional<std::shared_ptr<Protocol>> 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<std::string> device_path = boost::none,
- boost::optional<std::string> 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<std::string> bridge_host = boost::none);
virtual ~BridgeTransport() = default;