diff --git a/src/wormhole/blocking/transcribe.py b/src/wormhole/blocking/transcribe.py index 1b0a288..d10c43c 100644 --- a/src/wormhole/blocking/transcribe.py +++ b/src/wormhole/blocking/transcribe.py @@ -1,5 +1,5 @@ from __future__ import print_function -import sys, time, requests, json, textwrap +import sys, time, re, requests, json, textwrap from binascii import hexlify, unhexlify from spake2 import SPAKE2_A, SPAKE2_B from nacl.secret import SecretBox @@ -214,14 +214,22 @@ class Initiator(Common): self.key = None self.verifier = None - def get_code(self, code_length=2): - self.channel_id = self._allocate() # allocate channel - self.code = codes.make_code(self.channel_id, code_length) + def set_code(self, code): # used for human-made pre-generated codes + mo = re.search(r'^(\d+)-', code) + if not mo: + raise ValueError("code (%s) must start with NN-" % code) + self.channel_id = int(mo.group(1)) + self.code = code self.sp = SPAKE2_A(self.code.encode("ascii"), idA=self.appid+":Initiator", idB=self.appid+":Receiver") self._post_pake() - return self.code + + def get_code(self, code_length=2): + channel_id = self._allocate() # allocate channel + code = codes.make_code(channel_id, code_length) + self.set_code(code) + return code def _wait_for_key(self): if not self.key: diff --git a/src/wormhole/scripts/cmd_send_file.py b/src/wormhole/scripts/cmd_send_file.py index fc75a35..24007db 100644 --- a/src/wormhole/scripts/cmd_send_file.py +++ b/src/wormhole/scripts/cmd_send_file.py @@ -16,7 +16,11 @@ def send_file(args): transit_sender = TransitSender(args.transit_helper) i = Initiator(APPID, args.relay_url) - code = i.get_code(args.code_length) + if args.code: + i.set_code(args.code) + code = args.code + else: + code = i.get_code(args.code_length) print("On the other computer, please run: wormhole receive-file") print("Wormhole code is '%s'" % code) print() diff --git a/src/wormhole/scripts/cmd_send_text.py b/src/wormhole/scripts/cmd_send_text.py index c1385c4..3617027 100644 --- a/src/wormhole/scripts/cmd_send_text.py +++ b/src/wormhole/scripts/cmd_send_text.py @@ -10,7 +10,11 @@ def send_text(args): from ..blocking.transcribe import Initiator, WrongPasswordError i = Initiator(APPID, args.relay_url) - code = i.get_code(args.code_length) + if args.code: + i.set_code(args.code) + code = args.code + else: + code = i.get_code(args.code_length) print("On the other computer, please run: wormhole receive-text") print("Wormhole code is: %s" % code) print("") diff --git a/src/wormhole/scripts/runner.py b/src/wormhole/scripts/runner.py index 1a70a9e..e857da7 100644 --- a/src/wormhole/scripts/runner.py +++ b/src/wormhole/scripts/runner.py @@ -56,6 +56,7 @@ sp_restart.set_defaults(func=cmd_server.restart_server) # CLI: send-text p = subparsers.add_parser("send-text", description="Send a text mesasge", usage="wormhole send-text TEXT") +p.add_argument("--code", metavar="CODE", help="human-generated code phrase") p.add_argument("text", metavar="TEXT", help="the message to send (a string)") p.set_defaults(func=cmd_send_text.send_text) @@ -72,6 +73,7 @@ p.set_defaults(func=cmd_receive_text.receive_text) # CLI: send-file p = subparsers.add_parser("send-file", description="Send a file", usage="wormhole send-file FILENAME") +p.add_argument("--code", metavar="CODE", help="human-generated code phrase") p.add_argument("filename", metavar="FILENAME", help="The file to be sent") p.set_defaults(func=cmd_send_file.send_file)