add --code-length, to configure the size of the PAKE code (in bytes/words)
This commit is contained in:
parent
d678e2fa66
commit
af1e3c51ec
|
@ -177,9 +177,9 @@ class Initiator(Common):
|
||||||
self.timeout = 3*MINUTE
|
self.timeout = 3*MINUTE
|
||||||
self.side = "initiator"
|
self.side = "initiator"
|
||||||
|
|
||||||
def get_code(self):
|
def get_code(self, code_length=2):
|
||||||
self.channel_id = self._allocate() # allocate channel
|
self.channel_id = self._allocate() # allocate channel
|
||||||
self.code = codes.make_code(self.channel_id)
|
self.code = codes.make_code(self.channel_id, code_length)
|
||||||
self.sp = SPAKE2_A(self.code.encode("ascii"),
|
self.sp = SPAKE2_A(self.code.encode("ascii"),
|
||||||
idA=self.appid+":Initiator",
|
idA=self.appid+":Initiator",
|
||||||
idB=self.appid+":Receiver")
|
idB=self.appid+":Receiver")
|
||||||
|
@ -225,8 +225,9 @@ class Receiver(Common):
|
||||||
channel_ids = r.json()["channel-ids"]
|
channel_ids = r.json()["channel-ids"]
|
||||||
return channel_ids
|
return channel_ids
|
||||||
|
|
||||||
def input_code(self, prompt="Enter wormhole code: "):
|
def input_code(self, prompt="Enter wormhole code: ", code_length=2):
|
||||||
code = codes.input_code_with_completion(prompt, self.list_channels)
|
code = codes.input_code_with_completion(prompt, self.list_channels,
|
||||||
|
code_length)
|
||||||
return code
|
return code
|
||||||
|
|
||||||
def set_code(self, code):
|
def set_code(self, code):
|
||||||
|
|
|
@ -3,10 +3,15 @@ import os
|
||||||
from .wordlist import (byte_to_even_word, byte_to_odd_word,
|
from .wordlist import (byte_to_even_word, byte_to_odd_word,
|
||||||
even_words_lowercase, odd_words_lowercase)
|
even_words_lowercase, odd_words_lowercase)
|
||||||
|
|
||||||
def make_code(channel_id):
|
def make_code(channel_id, code_length):
|
||||||
odd_word = byte_to_odd_word[os.urandom(1)]
|
words = []
|
||||||
even_word = byte_to_even_word[os.urandom(1)]
|
for i in range(code_length):
|
||||||
return "%d-%s-%s" % (channel_id, odd_word.lower(), even_word.lower())
|
# we start with an "odd word"
|
||||||
|
if i % 2 == 0:
|
||||||
|
words.append(byte_to_odd_word[os.urandom(1)].lower())
|
||||||
|
else:
|
||||||
|
words.append(byte_to_even_word[os.urandom(1)].lower())
|
||||||
|
return str(channel_id) + "-" + "-".join(words)
|
||||||
|
|
||||||
def extract_channel_id(code):
|
def extract_channel_id(code):
|
||||||
channel_id = int(code.split("-")[0])
|
channel_id = int(code.split("-")[0])
|
||||||
|
@ -16,8 +21,9 @@ import readline
|
||||||
#import sys
|
#import sys
|
||||||
|
|
||||||
class CodeInputter:
|
class CodeInputter:
|
||||||
def __init__(self, get_channel_ids):
|
def __init__(self, get_channel_ids, code_length):
|
||||||
self.get_channel_ids = get_channel_ids
|
self.get_channel_ids = get_channel_ids
|
||||||
|
self.code_length = code_length
|
||||||
self.last_text = None # memoize for a speedup
|
self.last_text = None # memoize for a speedup
|
||||||
self.last_matches = None
|
self.last_matches = None
|
||||||
|
|
||||||
|
@ -45,7 +51,7 @@ class CodeInputter:
|
||||||
matches = self.last_matches
|
matches = self.last_matches
|
||||||
#print(" old matches", len(matches), file=sys.stderr)
|
#print(" old matches", len(matches), file=sys.stderr)
|
||||||
else:
|
else:
|
||||||
if len(pieces) < 2:
|
if len(pieces) <= 1:
|
||||||
channel_ids = self.get_channel_ids()
|
channel_ids = self.get_channel_ids()
|
||||||
matches = [str(channel_id) for channel_id in channel_ids
|
matches = [str(channel_id) for channel_id in channel_ids
|
||||||
if str(channel_id).startswith(last)]
|
if str(channel_id).startswith(last)]
|
||||||
|
@ -63,15 +69,15 @@ class CodeInputter:
|
||||||
if state >= len(matches):
|
if state >= len(matches):
|
||||||
return None
|
return None
|
||||||
match = matches[state]
|
match = matches[state]
|
||||||
if len(pieces) < 3:
|
if len(pieces) < 1+self.code_length:
|
||||||
match += "-"
|
match += "-"
|
||||||
#print(" match: '%s'" % match, file=sys.stderr)
|
#print(" match: '%s'" % match, file=sys.stderr)
|
||||||
#sys.stderr.flush()
|
#sys.stderr.flush()
|
||||||
return match
|
return match
|
||||||
|
|
||||||
|
|
||||||
def input_code_with_completion(prompt, get_channel_ids):
|
def input_code_with_completion(prompt, get_channel_ids, code_length):
|
||||||
c = CodeInputter(get_channel_ids)
|
c = CodeInputter(get_channel_ids, code_length)
|
||||||
readline.parse_and_bind("tab: complete")
|
readline.parse_and_bind("tab: complete")
|
||||||
readline.set_completer(c.wrap_completer)
|
readline.set_completer(c.wrap_completer)
|
||||||
readline.set_completer_delims("")
|
readline.set_completer_delims("")
|
||||||
|
@ -79,5 +85,5 @@ def input_code_with_completion(prompt, get_channel_ids):
|
||||||
return code
|
return code
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
code = input_code_with_completion("Enter wormhole code: ", lambda: [])
|
code = input_code_with_completion("Enter wormhole code: ", lambda: [], 2)
|
||||||
print("code is:", code)
|
print("code is:", code)
|
||||||
|
|
|
@ -19,7 +19,8 @@ def receive_file(args):
|
||||||
r = Receiver(APPID, mydata, args.relay_url)
|
r = Receiver(APPID, mydata, args.relay_url)
|
||||||
code = args.code
|
code = args.code
|
||||||
if not code:
|
if not code:
|
||||||
code = r.input_code("Enter receive-file wormhole code: ")
|
code = r.input_code("Enter receive-file wormhole code: ",
|
||||||
|
args.code_length)
|
||||||
r.set_code(code)
|
r.set_code(code)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -10,7 +10,8 @@ def receive_text(args):
|
||||||
r = Receiver(APPID, data, args.relay_url)
|
r = Receiver(APPID, data, args.relay_url)
|
||||||
code = args.code
|
code = args.code
|
||||||
if not code:
|
if not code:
|
||||||
code = r.input_code("Enter receive-text wormhole code: ")
|
code = r.input_code("Enter receive-text wormhole code: ",
|
||||||
|
args.code_length)
|
||||||
r.set_code(code)
|
r.set_code(code)
|
||||||
try:
|
try:
|
||||||
them_bytes = r.get_data()
|
them_bytes = r.get_data()
|
||||||
|
|
|
@ -25,7 +25,7 @@ def send_file(args):
|
||||||
}).encode("utf-8")
|
}).encode("utf-8")
|
||||||
|
|
||||||
i = Initiator(APPID, data, args.relay_url)
|
i = Initiator(APPID, data, args.relay_url)
|
||||||
code = i.get_code()
|
code = i.get_code(args.code_length)
|
||||||
print("On the other computer, please run: wormhole receive-file")
|
print("On the other computer, please run: wormhole receive-file")
|
||||||
print("Wormhole code is '%s'" % code)
|
print("Wormhole code is '%s'" % code)
|
||||||
print()
|
print()
|
||||||
|
|
|
@ -10,7 +10,7 @@ def send_text(args):
|
||||||
data = json.dumps({"message": message,
|
data = json.dumps({"message": message,
|
||||||
}).encode("utf-8")
|
}).encode("utf-8")
|
||||||
i = Initiator(APPID, data, args.relay_url)
|
i = Initiator(APPID, data, args.relay_url)
|
||||||
code = i.get_code()
|
code = i.get_code(args.code_length)
|
||||||
print("On the other computer, please run: wormhole receive-text")
|
print("On the other computer, please run: wormhole receive-text")
|
||||||
print("Wormhole code is: %s" % code)
|
print("Wormhole code is: %s" % code)
|
||||||
print("")
|
print("")
|
||||||
|
|
|
@ -18,6 +18,8 @@ g.add_argument("--relay-url", default=public_relay.RENDEZVOUS_RELAY,
|
||||||
metavar="URL", help="rendezvous relay to use")
|
metavar="URL", help="rendezvous relay to use")
|
||||||
g.add_argument("--transit-helper", default=public_relay.TRANSIT_RELAY,
|
g.add_argument("--transit-helper", default=public_relay.TRANSIT_RELAY,
|
||||||
metavar="tcp:HOST:PORT", help="transit relay to use")
|
metavar="tcp:HOST:PORT", help="transit relay to use")
|
||||||
|
g.add_argument("-c", "--code-length", type=int, default=2,
|
||||||
|
metavar="WORDS", help="length of code (in bytes/words)")
|
||||||
subparsers = parser.add_subparsers(title="subcommands",
|
subparsers = parser.add_subparsers(title="subcommands",
|
||||||
dest="subcommand")
|
dest="subcommand")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user