1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 23:08:14 +00:00
trezor-firmware/trezorlib/transport/bridge.py

109 lines
3.7 KiB
Python
Raw Normal View History

# This file is part of the Trezor project.
2016-11-25 21:53:55 +00:00
#
# Copyright (C) 2012-2018 SatoshiLabs and contributors
2016-11-25 21:53:55 +00:00
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
2016-11-25 21:53:55 +00:00
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
2016-11-25 21:53:55 +00:00
import logging
2016-05-26 15:20:44 +00:00
import requests
2018-01-29 16:46:24 +00:00
import binascii
from io import BytesIO
import struct
from .. import mapping
from .. import messages
from .. import protobuf
from . import Transport, TransportException
2014-07-26 14:27:28 +00:00
LOG = logging.getLogger(__name__)
2018-01-29 16:46:24 +00:00
TREZORD_HOST = 'http://127.0.0.1:21325'
2014-07-26 14:27:28 +00:00
2017-06-23 19:31:42 +00:00
def get_error(resp):
return ' (error=%d str=%s)' % (resp.status_code, resp.json()['error'])
2017-06-23 19:31:42 +00:00
class BridgeTransport(Transport):
'''
BridgeTransport implements transport through TREZOR Bridge (aka trezord).
'''
PATH_PREFIX = 'bridge'
2018-03-01 09:33:47 +00:00
HEADERS = {'Origin': 'https://python.trezor.io'}
def __init__(self, device):
super().__init__()
self.device = device
self.conn = requests.Session()
self.session = None
self.response = None
2018-02-06 20:10:30 +00:00
def get_path(self):
return '%s:%s' % (self.PATH_PREFIX, self.device['path'])
2018-03-01 09:33:47 +00:00
@classmethod
def enumerate(cls):
try:
2018-03-01 09:33:47 +00:00
r = requests.post(TREZORD_HOST + '/enumerate', headers=cls.HEADERS)
if r.status_code != 200:
raise TransportException('trezord: Could not enumerate devices' + get_error(r))
return [BridgeTransport(dev) for dev in r.json()]
except:
return []
def open(self):
2018-03-01 09:33:47 +00:00
r = self.conn.post(TREZORD_HOST + '/acquire/%s/null' % self.device['path'], headers=self.HEADERS)
2014-07-26 14:27:28 +00:00
if r.status_code != 200:
2018-01-29 16:46:24 +00:00
raise TransportException('trezord: Could not acquire session' + get_error(r))
self.session = r.json()['session']
2014-07-26 14:27:28 +00:00
def close(self):
if not self.session:
return
2018-03-01 09:33:47 +00:00
r = self.conn.post(TREZORD_HOST + '/release/%s' % self.session, headers=self.HEADERS)
2014-07-26 14:27:28 +00:00
if r.status_code != 200:
2018-01-29 16:46:24 +00:00
raise TransportException('trezord: Could not release session' + get_error(r))
self.session = None
def write(self, msg):
LOG.debug("sending message: {}".format(msg.__class__.__name__),
extra={'protobuf': msg})
2018-01-29 16:46:24 +00:00
data = BytesIO()
protobuf.dump_message(data, msg)
ser = data.getvalue()
header = struct.pack(">HL", mapping.get_type(msg), len(ser))
data = binascii.hexlify(header + ser).decode()
r = self.conn.post(
2018-03-01 09:33:47 +00:00
TREZORD_HOST + '/call/%s' % self.session, data=data, headers=self.HEADERS)
2014-07-26 14:27:28 +00:00
if r.status_code != 200:
2018-01-29 16:46:24 +00:00
raise TransportException('trezord: Could not write message' + get_error(r))
self.response = r.text
2014-07-26 14:27:28 +00:00
def read(self):
2016-05-26 15:20:44 +00:00
if self.response is None:
raise TransportException('No response stored')
2018-01-29 16:46:24 +00:00
data = binascii.unhexlify(self.response)
headerlen = struct.calcsize('>HL')
(msg_type, datalen) = struct.unpack('>HL', data[:headerlen])
data = BytesIO(data[headerlen:headerlen + datalen])
msg = protobuf.load_message(data, mapping.get_class(msg_type))
LOG.debug("received message: {}".format(msg.__class__.__name__),
extra={'protobuf': msg})
self.response = None
return msg
TRANSPORT = BridgeTransport