|
| 1 | +#!/usr/bin/env python |
| 2 | +from __future__ import print_function |
| 3 | + |
| 4 | +from os import close, error |
| 5 | +from flask import Flask, Response, request, jsonify |
| 6 | +from flask_cors import CORS, cross_origin |
| 7 | + |
| 8 | +import sys |
| 9 | +sys.path = ['../',] + sys.path |
| 10 | + |
| 11 | +from keepkeylib import client |
| 12 | +from keepkeylib.client import KeepKeyClient |
| 13 | +from keepkeylib.transport_webusb import WebUsbTransport |
| 14 | +from keepkeylib import messages_pb2 as messages |
| 15 | + |
| 16 | +import json |
| 17 | +import binascii |
| 18 | + |
| 19 | + |
| 20 | +PACKET_SIZE = 64 |
| 21 | + |
| 22 | +kkClient = None |
| 23 | + |
| 24 | +def create_app(): |
| 25 | + app = Flask(__name__) |
| 26 | + CORS(app) |
| 27 | + app.config['CORS_HEADERS'] = 'Content-Type' |
| 28 | + |
| 29 | + def initDevice(): |
| 30 | + global kkClient |
| 31 | + if (kkClient != None): |
| 32 | + kkClient.close() |
| 33 | + kkClient = None |
| 34 | + |
| 35 | + # List all connected KeepKeys on USB |
| 36 | + devices = WebUsbTransport.enumerate() |
| 37 | + |
| 38 | + # Check whether we found any |
| 39 | + if len(devices) == 0: |
| 40 | + return None |
| 41 | + |
| 42 | + # Use first connected device |
| 43 | + transport = WebUsbTransport(devices[0]) |
| 44 | + |
| 45 | + # Creates object for manipulating KeepKey |
| 46 | + client = KeepKeyClient(transport) |
| 47 | + |
| 48 | + return client |
| 49 | + |
| 50 | + @app.route('/init') |
| 51 | + def initKK(): |
| 52 | + global kkClient |
| 53 | + |
| 54 | + kkClient = initDevice() |
| 55 | + |
| 56 | + if (kkClient == None): |
| 57 | + data = "No KeepKey found" |
| 58 | + return Response(str(data), status=400, mimetype='application/json') |
| 59 | + else: |
| 60 | + data = kkClient.features |
| 61 | + return Response(str(data), status=200, mimetype='application/json') |
| 62 | + |
| 63 | + @app.route('/ping') |
| 64 | + def pingKK(): |
| 65 | + global kkClient |
| 66 | + |
| 67 | + if (kkClient == None): |
| 68 | + kkClient = initDevice() |
| 69 | + else: |
| 70 | + pass |
| 71 | + |
| 72 | + if (kkClient == None): |
| 73 | + data = "No KeepKey found" |
| 74 | + return Response(str(data), status=404, mimetype='application/json') |
| 75 | + |
| 76 | + try: |
| 77 | + ping = kkClient.call(messages.Ping(message='Duck, a bridge!', button_protection = True)) |
| 78 | + except: |
| 79 | + kkClient.close() |
| 80 | + kkClient = None |
| 81 | + data = "No KeepKey found" |
| 82 | + return Response(str(data), status=404, mimetype='application/json') |
| 83 | + |
| 84 | + return Response(str(ping), status=200, mimetype='application/json') |
| 85 | + |
| 86 | + @app.route('/exchange/<string:kind>', methods=['GET', 'POST']) |
| 87 | + @cross_origin() |
| 88 | + def rest_api(kind): |
| 89 | + global kkClient |
| 90 | + |
| 91 | + if (kkClient == None): |
| 92 | + kkClient = initDevice() |
| 93 | + else: |
| 94 | + pass |
| 95 | + |
| 96 | + if (kkClient == None): |
| 97 | + data = "No KeepKey found" |
| 98 | + return Response(str(data), status=404, mimetype='application/json') |
| 99 | + |
| 100 | + if request.method == 'POST': |
| 101 | + content = request.get_json(silent=True) |
| 102 | + msg = bytearray.fromhex(content["data"]) |
| 103 | + try: |
| 104 | + kkClient.call_bridge(msg) |
| 105 | + except: |
| 106 | + kkClient.close() |
| 107 | + kkClient = None |
| 108 | + kkClient = initDevice() |
| 109 | + return Response('{}', status=404, mimetype='application/json') |
| 110 | + return Response('{}', status=200, mimetype='application/json') |
| 111 | + |
| 112 | + if request.method == 'GET': |
| 113 | + data = kkClient.call_bridge_read() |
| 114 | + body = '{"data":"' + binascii.hexlify(data).decode("utf-8") + '"}' |
| 115 | + return Response(body, status=200, mimetype='application/json') |
| 116 | + |
| 117 | + return Response('{}', status=404, mimetype='application/json') |
| 118 | + |
| 119 | + return app |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + |
| 123 | + app = create_app() |
| 124 | + app.run(port='1646') |
| 125 | + #app.run() |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
0 commit comments