summaryrefslogtreecommitdiff
path: root/pairing.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.net>2018-11-13 21:45:24 +0900
committerjaseg <git@jaseg.net>2018-11-13 21:45:24 +0900
commite16515bb643b9329318809ecc134f74c22a86382 (patch)
tree30efc4ce885ced6edae4299c3453fa2f28c20926 /pairing.py
parent6af635bd41b48da04edd1f70a6851cb3f811cea3 (diff)
downloadsecure-hid-e16515bb643b9329318809ecc134f74c22a86382.tar.gz
secure-hid-e16515bb643b9329318809ecc134f74c22a86382.tar.bz2
secure-hid-e16515bb643b9329318809ecc134f74c22a86382.zip
Pairing and passthrough mostly working, except it's too slow
Diffstat (limited to 'pairing.py')
-rwxr-xr-xpairing.py65
1 files changed, 29 insertions, 36 deletions
diff --git a/pairing.py b/pairing.py
index fccc554..605b002 100755
--- a/pairing.py
+++ b/pairing.py
@@ -5,8 +5,7 @@ import re
import serial
import gi
gi.require_version('Gtk', '3.0')
-gi.require_version('Pango', '1.0')
-from gi.repository import Gtk, Pango
+from gi.repository import Gtk, Gdk, Pango, GLib
import hexnoise
@@ -27,39 +26,27 @@ class PairingWindow(Gtk.Window):
self.label.set_markup('<b>Step 1</b>\n\nContacting device...')
self.vbox.pack_start(self.label, True, True, 0)
- self.textview = Gtk.TextView()
- self.textview.set_editable(False)
- self.textbuffer = self.textview.get_buffer()
- self.tag_nomatch = self.textbuffer.create_tag("nomatch", weight=Pango.Weight.BOLD)
- self.tag_match = self.textbuffer.create_tag("match", background='#AAFFAA', weight=Pango.Weight.BOLD)
-
- self.vbox.pack_start(self.textview, True, True, 0)
+ self.entry = Gtk.Entry()
+ self.entry.set_editable(False)
+ self.vbox.pack_start(self.entry, True, True, 0)
self.add(self.vbox)
- self.handshaker = threading.Thread(target=self.run_handshake, daemon=True)
+ self.handshaker = threading.Thread(target=self.pair, daemon=True)
self.handshaker.start()
- @classmethod
- def matchlen(self, ref, text):
- words = ref.split()
- parts = text.split()
- clean = lambda b: re.sub('^[^a-zA-Z0-9-]*', '', re.sub('[^a-zA-Z0-9-]*$', '', b)).lower()
-
- good = ''
- for a, b in zip(words[:-1], parts[:-1]):
- if a == clean(b):
- good = f'{good}b '
-
- rest = clean(parts[-1])
- if words[-1].startswith(rest):
- good = f'{good} {rest}'
- return len(good)
-
- def run_handshake(self):
+ def pair(self):
self.packetizer = hexnoise.Packetizer(self.serial, debug=self.debug)
self.noise = hexnoise.NoiseEngine(self.packetizer, debug=self.debug)
+ for i in range(10):
+ try:
+ self.run_handshake()
+ break
+ except hexnoise.ProtocolError as e:
+ print(e)
+
+ def run_handshake(self):
self.noise.perform_handshake()
binding_incantation = self.noise.channel_binding_incantation()
@@ -67,19 +54,25 @@ class PairingWindow(Gtk.Window):
f'Enter the following incantation, then press enter.\n'
f'<b>{binding_incantation}</b>')
- for user_input in self.noise.pairing_messages():
- print('got:', user_input)
- self.textbuffer.set_text(user_input)
- #i1, i2 = self.textbuffer.get_start_iter(), self.textbuffer.get_end_iter()
- #self.textbuffer.apply_tag(self.tag_nomatch, i1, i2)
+ def update_text(text):
+ self.entry.set_text(text)
+ self.entry.set_position(len(text))
- #i1, i2 = self.textbuffer.get_start_iter(), self.textbuffer.get_start_iter()
- #i2.forward_chars(self.matchlen(binding_incantation, user_input))
- #self.textbuffer.apply_tag(self.tag_match, i1, i2)
+ clean = lambda s: re.sub('[^a-z0-9-]', '', s.lower())
+ if clean(binding_incantation).startswith(clean(text)):
+ color = 0.9, 1.0, 0.9 # light red
+ else:
+ color = 1.0, 0.9, 0.9 # light green
+ self.entry.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(*color, 1.0))
+
+ for user_input in self.noise.pairing_messages():
+ print(f'User input: "{user_input}"')
+ GLib.idle_add(update_text, user_input)
self.label.set_markup(f'<b>Done!</b>')
- #noise.uinput_passthrough()
+ # FIXME demo
+ self.noise.uinput_passthrough()
if __name__ == '__main__':
import argparse