more rearranging
This commit is contained in:
parent
512c7c0a59
commit
8079340bff
|
@ -1,7 +1,6 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import os, sys, json, binascii, six, tempfile, zipfile
|
import os, sys, six, tempfile, zipfile
|
||||||
from ..errors import handle_server_error, TransferError
|
from ..errors import handle_server_error, TransferError
|
||||||
from .progress import start_progress, update_progress, finish_progress
|
|
||||||
|
|
||||||
APPID = u"lothar.com/wormhole/text-or-file-xfer"
|
APPID = u"lothar.com/wormhole/text-or-file-xfer"
|
||||||
|
|
||||||
|
@ -39,7 +38,8 @@ def send(args):
|
||||||
other_cmd += " -0"
|
other_cmd += " -0"
|
||||||
print("On the other computer, please run: %s" % other_cmd)
|
print("On the other computer, please run: %s" % other_cmd)
|
||||||
|
|
||||||
rc = _send_blocking(APPID, args, phase1, fd_to_send)
|
from .cmd_send_blocking import send_blocking
|
||||||
|
rc = send_blocking(APPID, args, phase1, fd_to_send)
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
def _build_phase1_data(args):
|
def _build_phase1_data(args):
|
||||||
|
@ -88,99 +88,3 @@ def _build_phase1_data(args):
|
||||||
print("Sending directory (%d bytes compressed) named '%s'"
|
print("Sending directory (%d bytes compressed) named '%s'"
|
||||||
% (filesize, basename))
|
% (filesize, basename))
|
||||||
return phase1, fd_to_send
|
return phase1, fd_to_send
|
||||||
|
|
||||||
def _send_blocking(appid, args, phase1, fd_to_send):
|
|
||||||
from ..blocking.transcribe import Wormhole, WrongPasswordError
|
|
||||||
from ..blocking.transit import TransitSender
|
|
||||||
|
|
||||||
transit_sender = TransitSender(args.transit_helper)
|
|
||||||
transit_data = {
|
|
||||||
"direct_connection_hints": transit_sender.get_direct_hints(),
|
|
||||||
"relay_connection_hints": transit_sender.get_relay_hints(),
|
|
||||||
}
|
|
||||||
phase1["transit"] = transit_data
|
|
||||||
|
|
||||||
with Wormhole(appid, args.relay_url) as w:
|
|
||||||
if args.code:
|
|
||||||
w.set_code(args.code)
|
|
||||||
code = args.code
|
|
||||||
else:
|
|
||||||
code = w.get_code(args.code_length)
|
|
||||||
if not args.zeromode:
|
|
||||||
print("Wormhole code is: %s" % code)
|
|
||||||
print("")
|
|
||||||
|
|
||||||
if args.verify:
|
|
||||||
_do_verify(w)
|
|
||||||
|
|
||||||
my_phase1_bytes = json.dumps(phase1).encode("utf-8")
|
|
||||||
w.send_data(my_phase1_bytes)
|
|
||||||
try:
|
|
||||||
them_phase1_bytes = w.get_data()
|
|
||||||
except WrongPasswordError as e:
|
|
||||||
raise TransferError(e.explain())
|
|
||||||
# note: 'w' is still valid, and we use w.derive_key() below, which can't
|
|
||||||
# raise an error that needs to be handled in the 'with' block
|
|
||||||
|
|
||||||
them_phase1 = json.loads(them_phase1_bytes.decode("utf-8"))
|
|
||||||
|
|
||||||
if fd_to_send is None:
|
|
||||||
if them_phase1["message_ack"] == "ok":
|
|
||||||
print("text message sent")
|
|
||||||
return 0
|
|
||||||
raise TransferError("error sending text: %r" % (them_phase1,))
|
|
||||||
|
|
||||||
return _send_file_blocking(w, them_phase1, fd_to_send, transit_sender)
|
|
||||||
|
|
||||||
def _do_verify(w):
|
|
||||||
verifier = binascii.hexlify(w.get_verifier()).decode("ascii")
|
|
||||||
while True:
|
|
||||||
ok = six.moves.input("Verifier %s. ok? (yes/no): " % verifier)
|
|
||||||
if ok.lower() == "yes":
|
|
||||||
break
|
|
||||||
if ok.lower() == "no":
|
|
||||||
reject_data = json.dumps({"error": "verification rejected",
|
|
||||||
}).encode("utf-8")
|
|
||||||
w.send_data(reject_data)
|
|
||||||
raise TransferError("verification rejected, abandoning transfer")
|
|
||||||
|
|
||||||
def _send_file_blocking(w, them_phase1, fd_to_send, transit_sender):
|
|
||||||
|
|
||||||
# we're sending a file, if they accept it
|
|
||||||
|
|
||||||
if "error" in them_phase1:
|
|
||||||
raise TransferError("remote error, transfer abandoned: %s"
|
|
||||||
% them_phase1["error"])
|
|
||||||
if them_phase1.get("file_ack") != "ok":
|
|
||||||
raise TransferError("ambiguous response from remote, "
|
|
||||||
"transfer abandoned: %s" % (them_phase1,))
|
|
||||||
|
|
||||||
tdata = them_phase1["transit"]
|
|
||||||
transit_key = w.derive_key(APPID+"/transit-key")
|
|
||||||
transit_sender.set_transit_key(transit_key)
|
|
||||||
transit_sender.add_their_direct_hints(tdata["direct_connection_hints"])
|
|
||||||
transit_sender.add_their_relay_hints(tdata["relay_connection_hints"])
|
|
||||||
record_pipe = transit_sender.connect()
|
|
||||||
|
|
||||||
print("Sending (%s).." % transit_sender.describe())
|
|
||||||
|
|
||||||
CHUNKSIZE = 64*1024
|
|
||||||
fd_to_send.seek(0,2)
|
|
||||||
filesize = fd_to_send.tell()
|
|
||||||
fd_to_send.seek(0,0)
|
|
||||||
with fd_to_send as f:
|
|
||||||
sent = 0
|
|
||||||
next_update = start_progress(filesize)
|
|
||||||
while sent < filesize:
|
|
||||||
plaintext = f.read(CHUNKSIZE)
|
|
||||||
record_pipe.send_record(plaintext)
|
|
||||||
sent += len(plaintext)
|
|
||||||
next_update = update_progress(next_update, sent, filesize)
|
|
||||||
finish_progress(filesize)
|
|
||||||
|
|
||||||
print("File sent.. waiting for confirmation")
|
|
||||||
ack = record_pipe.receive_record()
|
|
||||||
if ack == b"ok\n":
|
|
||||||
print("Confirmation received. Transfer complete.")
|
|
||||||
return 0
|
|
||||||
raise TransferError("Transfer failed (remote says: %r)" % ack)
|
|
||||||
|
|
101
src/wormhole/scripts/cmd_send_blocking.py
Normal file
101
src/wormhole/scripts/cmd_send_blocking.py
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
from __future__ import print_function
|
||||||
|
import json, binascii, six
|
||||||
|
from ..errors import TransferError
|
||||||
|
from .progress import start_progress, update_progress, finish_progress
|
||||||
|
|
||||||
|
def send_blocking(appid, args, phase1, fd_to_send):
|
||||||
|
from ..blocking.transcribe import Wormhole, WrongPasswordError
|
||||||
|
from ..blocking.transit import TransitSender
|
||||||
|
|
||||||
|
transit_sender = TransitSender(args.transit_helper)
|
||||||
|
transit_data = {
|
||||||
|
"direct_connection_hints": transit_sender.get_direct_hints(),
|
||||||
|
"relay_connection_hints": transit_sender.get_relay_hints(),
|
||||||
|
}
|
||||||
|
phase1["transit"] = transit_data
|
||||||
|
|
||||||
|
with Wormhole(appid, args.relay_url) as w:
|
||||||
|
if args.code:
|
||||||
|
w.set_code(args.code)
|
||||||
|
code = args.code
|
||||||
|
else:
|
||||||
|
code = w.get_code(args.code_length)
|
||||||
|
if not args.zeromode:
|
||||||
|
print("Wormhole code is: %s" % code)
|
||||||
|
print("")
|
||||||
|
|
||||||
|
if args.verify:
|
||||||
|
_do_verify(w)
|
||||||
|
|
||||||
|
my_phase1_bytes = json.dumps(phase1).encode("utf-8")
|
||||||
|
w.send_data(my_phase1_bytes)
|
||||||
|
try:
|
||||||
|
them_phase1_bytes = w.get_data()
|
||||||
|
except WrongPasswordError as e:
|
||||||
|
raise TransferError(e.explain())
|
||||||
|
# note: 'w' is still valid, and we use w.derive_key() below, which can't
|
||||||
|
# raise an error that needs to be handled in the 'with' block
|
||||||
|
|
||||||
|
them_phase1 = json.loads(them_phase1_bytes.decode("utf-8"))
|
||||||
|
|
||||||
|
if fd_to_send is None:
|
||||||
|
if them_phase1["message_ack"] == "ok":
|
||||||
|
print("text message sent")
|
||||||
|
return 0
|
||||||
|
raise TransferError("error sending text: %r" % (them_phase1,))
|
||||||
|
|
||||||
|
return _send_file_blocking(w, appid, them_phase1, fd_to_send,
|
||||||
|
transit_sender)
|
||||||
|
|
||||||
|
def _do_verify(w):
|
||||||
|
verifier = binascii.hexlify(w.get_verifier()).decode("ascii")
|
||||||
|
while True:
|
||||||
|
ok = six.moves.input("Verifier %s. ok? (yes/no): " % verifier)
|
||||||
|
if ok.lower() == "yes":
|
||||||
|
break
|
||||||
|
if ok.lower() == "no":
|
||||||
|
reject_data = json.dumps({"error": "verification rejected",
|
||||||
|
}).encode("utf-8")
|
||||||
|
w.send_data(reject_data)
|
||||||
|
raise TransferError("verification rejected, abandoning transfer")
|
||||||
|
|
||||||
|
def _send_file_blocking(w, appid, them_phase1, fd_to_send, transit_sender):
|
||||||
|
|
||||||
|
# we're sending a file, if they accept it
|
||||||
|
|
||||||
|
if "error" in them_phase1:
|
||||||
|
raise TransferError("remote error, transfer abandoned: %s"
|
||||||
|
% them_phase1["error"])
|
||||||
|
if them_phase1.get("file_ack") != "ok":
|
||||||
|
raise TransferError("ambiguous response from remote, "
|
||||||
|
"transfer abandoned: %s" % (them_phase1,))
|
||||||
|
|
||||||
|
tdata = them_phase1["transit"]
|
||||||
|
transit_key = w.derive_key(appid+"/transit-key")
|
||||||
|
transit_sender.set_transit_key(transit_key)
|
||||||
|
transit_sender.add_their_direct_hints(tdata["direct_connection_hints"])
|
||||||
|
transit_sender.add_their_relay_hints(tdata["relay_connection_hints"])
|
||||||
|
record_pipe = transit_sender.connect()
|
||||||
|
|
||||||
|
print("Sending (%s).." % transit_sender.describe())
|
||||||
|
|
||||||
|
CHUNKSIZE = 64*1024
|
||||||
|
fd_to_send.seek(0,2)
|
||||||
|
filesize = fd_to_send.tell()
|
||||||
|
fd_to_send.seek(0,0)
|
||||||
|
with fd_to_send as f:
|
||||||
|
sent = 0
|
||||||
|
next_update = start_progress(filesize)
|
||||||
|
while sent < filesize:
|
||||||
|
plaintext = f.read(CHUNKSIZE)
|
||||||
|
record_pipe.send_record(plaintext)
|
||||||
|
sent += len(plaintext)
|
||||||
|
next_update = update_progress(next_update, sent, filesize)
|
||||||
|
finish_progress(filesize)
|
||||||
|
|
||||||
|
print("File sent.. waiting for confirmation")
|
||||||
|
ack = record_pipe.receive_record()
|
||||||
|
if ack == b"ok\n":
|
||||||
|
print("Confirmation received. Transfer complete.")
|
||||||
|
return 0
|
||||||
|
raise TransferError("Transfer failed (remote says: %r)" % ack)
|
Loading…
Reference in New Issue
Block a user