2016-02-18 01:22:54 +00:00
|
|
|
from __future__ import print_function
|
2016-05-29 01:19:45 +00:00
|
|
|
import os, sys, six, tempfile, zipfile, hashlib
|
2016-04-24 19:04:05 +00:00
|
|
|
from tqdm import tqdm
|
2016-11-12 03:01:21 +00:00
|
|
|
from humanize import naturalsize
|
2016-05-25 00:27:26 +00:00
|
|
|
from twisted.python import log
|
2016-02-18 01:22:54 +00:00
|
|
|
from twisted.protocols import basic
|
2016-04-20 07:02:05 +00:00
|
|
|
from twisted.internet import reactor
|
2016-02-18 01:22:54 +00:00
|
|
|
from twisted.internet.defer import inlineCallbacks, returnValue
|
rewrite Tor support (py2 only)
The new TorManager adds --launch-tor and --tor-control-port= arguments
(requiring the user to explicitly request a new Tor process, if that's what
they want). The default (when --tor is enabled) looks for a control port in
the usual places (/var/run/tor/control, localhost:9051, localhost:9151), then
falls back to hoping there's a SOCKS port in the usual
place (localhost:9050). (closes #64)
The ssh utilities should now accept the same tor arguments as ordinary
send/receive commands. There are now full tests for TorManager, and basic
tests for how send/receive use it. (closes #97)
Note that Tor is only supported on python2.7 for now, since txsocksx (and
therefore txtorcon) doesn't work on py3. You need to do "pip install
magic-wormhole[tor]" to get Tor support, and that will get you an inscrutable
error on py3 (referencing vcversioner, "install_requires must be a string or
list of strings", and "int object not iterable").
To run tests, you must install with the [dev] extra (to get "mock" and other
libraries). Our setup.py only includes "txtorcon" in the [dev] extra when on
py2, not on py3. Unit tests tolerate the lack of txtorcon (they mock out
everything txtorcon would provide), so they should provide the same coverage
on both py2 and py3.
2017-01-16 03:24:23 +00:00
|
|
|
from ..errors import TransferError, WormholeClosedError, NoTorError
|
2016-05-24 07:00:44 +00:00
|
|
|
from ..wormhole import wormhole
|
2016-05-24 23:22:37 +00:00
|
|
|
from ..transit import TransitSender
|
2016-11-12 03:01:21 +00:00
|
|
|
from ..util import dict_to_bytes, bytes_to_dict, bytes_to_hexstr
|
2016-04-16 01:00:42 +00:00
|
|
|
|
|
|
|
APPID = u"lothar.com/wormhole/text-or-file-xfer"
|
2016-06-18 05:03:58 +00:00
|
|
|
VERIFY_TIMER = 1
|
2016-04-16 01:00:42 +00:00
|
|
|
|
2016-04-25 05:49:18 +00:00
|
|
|
def send(args, reactor=reactor):
|
2016-04-26 00:11:52 +00:00
|
|
|
"""I implement 'wormhole send'. I return a Deferred that fires with None
|
|
|
|
(for success), or signals one of the following errors:
|
|
|
|
* WrongPasswordError: the two sides didn't use matching passwords
|
|
|
|
* Timeout: something didn't happen fast enough for our tastes
|
|
|
|
* TransferError: the receiver rejected the transfer: verifier mismatch,
|
|
|
|
permission not granted, ack not successful.
|
|
|
|
* any other error: something unexpected happened
|
|
|
|
"""
|
2016-05-25 00:43:17 +00:00
|
|
|
return Sender(args, reactor).go()
|
|
|
|
|
|
|
|
class Sender:
|
|
|
|
def __init__(self, args, reactor):
|
|
|
|
self._args = args
|
|
|
|
self._reactor = reactor
|
|
|
|
self._tor_manager = None
|
|
|
|
self._timing = args.timing
|
|
|
|
self._fd_to_send = None
|
|
|
|
self._transit_sender = None
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def go(self):
|
|
|
|
assert isinstance(self._args.relay_url, type(u""))
|
|
|
|
if self._args.tor:
|
|
|
|
with self._timing.add("import", which="tor_manager"):
|
|
|
|
from ..tor_manager import TorManager
|
rewrite Tor support (py2 only)
The new TorManager adds --launch-tor and --tor-control-port= arguments
(requiring the user to explicitly request a new Tor process, if that's what
they want). The default (when --tor is enabled) looks for a control port in
the usual places (/var/run/tor/control, localhost:9051, localhost:9151), then
falls back to hoping there's a SOCKS port in the usual
place (localhost:9050). (closes #64)
The ssh utilities should now accept the same tor arguments as ordinary
send/receive commands. There are now full tests for TorManager, and basic
tests for how send/receive use it. (closes #97)
Note that Tor is only supported on python2.7 for now, since txsocksx (and
therefore txtorcon) doesn't work on py3. You need to do "pip install
magic-wormhole[tor]" to get Tor support, and that will get you an inscrutable
error on py3 (referencing vcversioner, "install_requires must be a string or
list of strings", and "int object not iterable").
To run tests, you must install with the [dev] extra (to get "mock" and other
libraries). Our setup.py only includes "txtorcon" in the [dev] extra when on
py2, not on py3. Unit tests tolerate the lack of txtorcon (they mock out
everything txtorcon would provide), so they should provide the same coverage
on both py2 and py3.
2017-01-16 03:24:23 +00:00
|
|
|
self._tor_manager = TorManager(reactor,
|
|
|
|
self._args.launch_tor,
|
|
|
|
self._args.tor_control_port,
|
|
|
|
timing=self._timing)
|
|
|
|
if not self._tor_manager.tor_available():
|
|
|
|
raise NoTorError()
|
2016-05-25 00:43:17 +00:00
|
|
|
# For now, block everything until Tor has started. Soon: launch
|
|
|
|
# tor in parallel with everything else, make sure the TorManager
|
|
|
|
# can lazy-provide an endpoint, and overlap the startup process
|
|
|
|
# with the user handing off the wormhole code
|
|
|
|
yield self._tor_manager.start()
|
|
|
|
|
2016-12-22 20:44:13 +00:00
|
|
|
w = wormhole(self._args.appid or APPID, self._args.relay_url,
|
2016-05-25 00:43:17 +00:00
|
|
|
self._reactor, self._tor_manager,
|
|
|
|
timing=self._timing)
|
2016-06-26 18:46:09 +00:00
|
|
|
d = self._go(w)
|
|
|
|
d.addBoth(w.close) # must wait for ack from close()
|
|
|
|
yield d
|
2016-05-25 00:43:17 +00:00
|
|
|
|
2016-05-25 01:59:04 +00:00
|
|
|
def _send_data(self, data, w):
|
2016-05-29 01:19:45 +00:00
|
|
|
data_bytes = dict_to_bytes(data)
|
2016-05-25 01:59:04 +00:00
|
|
|
w.send(data_bytes)
|
|
|
|
|
2016-05-25 00:43:17 +00:00
|
|
|
@inlineCallbacks
|
|
|
|
def _go(self, w):
|
|
|
|
# TODO: run the blocking zip-the-directory IO in a thread, let the
|
|
|
|
# wormhole exchange happen in parallel
|
|
|
|
offer, self._fd_to_send = self._build_offer()
|
|
|
|
args = self._args
|
|
|
|
|
|
|
|
other_cmd = "wormhole receive"
|
|
|
|
if args.verify:
|
2016-08-15 01:06:04 +00:00
|
|
|
other_cmd = "wormhole receive --verify"
|
2016-05-25 00:43:17 +00:00
|
|
|
if args.zeromode:
|
|
|
|
assert not args.code
|
|
|
|
args.code = u"0-"
|
|
|
|
other_cmd += " -0"
|
|
|
|
|
|
|
|
print(u"On the other computer, please run: %s" % other_cmd,
|
2016-12-24 05:03:32 +00:00
|
|
|
file=args.stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
|
|
|
|
if args.code:
|
|
|
|
w.set_code(args.code)
|
|
|
|
code = args.code
|
|
|
|
else:
|
|
|
|
code = yield w.get_code(args.code_length)
|
|
|
|
|
|
|
|
if not args.zeromode:
|
2016-12-24 05:03:32 +00:00
|
|
|
print(u"Wormhole code is: %s" % code, file=args.stderr)
|
|
|
|
# flush stderr so the code is displayed immediately
|
|
|
|
args.stderr.flush()
|
|
|
|
print(u"", file=args.stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
|
2016-06-05 10:10:32 +00:00
|
|
|
yield w.establish_key()
|
|
|
|
def on_slow_connection():
|
|
|
|
print(u"Key established, waiting for confirmation...",
|
2016-06-18 05:03:58 +00:00
|
|
|
file=args.stderr)
|
|
|
|
notify = self._reactor.callLater(VERIFY_TIMER, on_slow_connection)
|
2016-06-05 07:11:28 +00:00
|
|
|
|
2016-05-25 00:43:17 +00:00
|
|
|
# TODO: don't stall on w.verify() unless they want it
|
2016-06-05 10:10:32 +00:00
|
|
|
try:
|
|
|
|
verifier_bytes = yield w.verify() # this may raise WrongPasswordError
|
|
|
|
finally:
|
|
|
|
if not notify.called:
|
|
|
|
notify.cancel()
|
|
|
|
|
2016-05-25 00:43:17 +00:00
|
|
|
if args.verify:
|
2016-05-29 01:19:45 +00:00
|
|
|
verifier = bytes_to_hexstr(verifier_bytes)
|
2016-05-25 00:43:17 +00:00
|
|
|
while True:
|
|
|
|
ok = six.moves.input("Verifier %s. ok? (yes/no): " % verifier)
|
|
|
|
if ok.lower() == "yes":
|
|
|
|
break
|
|
|
|
if ok.lower() == "no":
|
|
|
|
err = "sender rejected verification check, abandoned transfer"
|
2016-05-29 01:19:45 +00:00
|
|
|
reject_data = dict_to_bytes({"error": err})
|
2016-05-25 00:43:17 +00:00
|
|
|
w.send(reject_data)
|
|
|
|
raise TransferError(err)
|
|
|
|
|
|
|
|
if self._fd_to_send:
|
|
|
|
ts = TransitSender(args.transit_helper,
|
2016-06-03 22:17:47 +00:00
|
|
|
no_listen=(not args.listen),
|
2016-05-25 00:43:17 +00:00
|
|
|
tor_manager=self._tor_manager,
|
|
|
|
reactor=self._reactor,
|
|
|
|
timing=self._timing)
|
|
|
|
self._transit_sender = ts
|
2016-05-25 01:59:04 +00:00
|
|
|
|
|
|
|
# for now, send this before the main offer
|
2016-05-26 23:26:00 +00:00
|
|
|
sender_abilities = ts.get_connection_abilities()
|
|
|
|
sender_hints = yield ts.get_connection_hints()
|
|
|
|
sender_transit = {"abilities-v1": sender_abilities,
|
|
|
|
"hints-v1": sender_hints,
|
|
|
|
}
|
|
|
|
self._send_data({u"transit": sender_transit}, w)
|
2016-05-25 00:43:17 +00:00
|
|
|
|
|
|
|
# TODO: move this down below w.get()
|
|
|
|
transit_key = w.derive_key(APPID+"/transit-key",
|
|
|
|
ts.TRANSIT_KEY_LENGTH)
|
|
|
|
ts.set_transit_key(transit_key)
|
|
|
|
|
2016-05-25 01:59:04 +00:00
|
|
|
self._send_data({"offer": offer}, w)
|
2016-05-25 00:43:17 +00:00
|
|
|
|
|
|
|
want_answer = True
|
|
|
|
done = False
|
|
|
|
|
2016-02-18 01:22:54 +00:00
|
|
|
while True:
|
2016-05-25 00:43:17 +00:00
|
|
|
try:
|
|
|
|
them_d_bytes = yield w.get()
|
|
|
|
except WormholeClosedError:
|
|
|
|
if done:
|
|
|
|
returnValue(None)
|
|
|
|
raise TransferError("unexpected close")
|
|
|
|
# TODO: get() fired, so now it's safe to use w.derive_key()
|
2016-05-29 01:19:45 +00:00
|
|
|
them_d = bytes_to_dict(them_d_bytes)
|
2016-05-25 01:59:04 +00:00
|
|
|
#print("GOT", them_d)
|
2016-05-25 02:30:55 +00:00
|
|
|
recognized = False
|
2016-05-26 05:44:18 +00:00
|
|
|
if u"error" in them_d:
|
|
|
|
raise TransferError("remote error, transfer abandoned: %s"
|
|
|
|
% them_d["error"])
|
2016-05-25 01:59:04 +00:00
|
|
|
if u"transit" in them_d:
|
2016-05-25 02:30:55 +00:00
|
|
|
recognized = True
|
2016-05-25 01:59:04 +00:00
|
|
|
yield self._handle_transit(them_d[u"transit"])
|
2016-05-25 00:43:17 +00:00
|
|
|
if u"answer" in them_d:
|
2016-05-25 02:30:55 +00:00
|
|
|
recognized = True
|
2016-05-25 00:43:17 +00:00
|
|
|
if not want_answer:
|
|
|
|
raise TransferError("duplicate answer")
|
2016-05-25 01:59:04 +00:00
|
|
|
yield self._handle_answer(them_d[u"answer"])
|
2016-05-25 00:43:17 +00:00
|
|
|
done = True
|
2016-05-25 00:27:26 +00:00
|
|
|
returnValue(None)
|
2016-05-25 02:30:55 +00:00
|
|
|
if not recognized:
|
|
|
|
log.msg("unrecognized message %r" % (them_d,))
|
2016-05-25 00:43:17 +00:00
|
|
|
|
2016-05-25 07:11:17 +00:00
|
|
|
def _handle_transit(self, receiver_transit):
|
2016-05-25 01:59:04 +00:00
|
|
|
ts = self._transit_sender
|
2016-05-25 07:11:17 +00:00
|
|
|
ts.add_connection_hints(receiver_transit.get("hints-v1", []))
|
2016-05-25 01:59:04 +00:00
|
|
|
|
2016-05-25 00:43:17 +00:00
|
|
|
def _build_offer(self):
|
|
|
|
offer = {}
|
|
|
|
|
|
|
|
args = self._args
|
|
|
|
text = args.text
|
|
|
|
if text == "-":
|
2016-12-24 05:03:32 +00:00
|
|
|
print(u"Reading text message from stdin..", file=args.stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
text = sys.stdin.read()
|
|
|
|
if not text and not args.what:
|
|
|
|
text = six.moves.input("Text to send: ")
|
|
|
|
|
|
|
|
if text is not None:
|
2016-11-12 03:01:21 +00:00
|
|
|
print(u"Sending text message (%s)" % naturalsize(len(text)),
|
2016-12-24 05:03:32 +00:00
|
|
|
file=args.stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
offer = { "message": text }
|
|
|
|
fd_to_send = None
|
|
|
|
return offer, fd_to_send
|
|
|
|
|
|
|
|
what = os.path.join(args.cwd, args.what)
|
|
|
|
what = what.rstrip(os.sep)
|
|
|
|
if not os.path.exists(what):
|
|
|
|
raise TransferError("Cannot send: no file/directory named '%s'" %
|
|
|
|
args.what)
|
|
|
|
basename = os.path.basename(what)
|
|
|
|
|
|
|
|
if os.path.isfile(what):
|
|
|
|
# we're sending a file
|
|
|
|
filesize = os.stat(what).st_size
|
|
|
|
offer["file"] = {
|
|
|
|
"filename": basename,
|
|
|
|
"filesize": filesize,
|
|
|
|
}
|
2016-11-09 20:14:01 +00:00
|
|
|
print(u"Sending %s file named '%s'"
|
2016-11-12 03:01:21 +00:00
|
|
|
% (naturalsize(filesize), basename),
|
2016-12-24 05:03:32 +00:00
|
|
|
file=args.stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
fd_to_send = open(what, "rb")
|
|
|
|
return offer, fd_to_send
|
|
|
|
|
|
|
|
if os.path.isdir(what):
|
2016-12-24 05:03:32 +00:00
|
|
|
print(u"Building zipfile..", file=args.stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
# We're sending a directory. Create a zipfile in a tempdir and
|
|
|
|
# send that.
|
|
|
|
fd_to_send = tempfile.SpooledTemporaryFile()
|
|
|
|
num_files = 0
|
|
|
|
num_bytes = 0
|
|
|
|
tostrip = len(what.split(os.sep))
|
|
|
|
with zipfile.ZipFile(fd_to_send, "w", zipfile.ZIP_DEFLATED) as zf:
|
|
|
|
for path,dirs,files in os.walk(what):
|
|
|
|
# path always starts with args.what, then sometimes might
|
|
|
|
# have "/subdir" appended. We want the zipfile to contain
|
|
|
|
# "" or "subdir"
|
|
|
|
localpath = list(path.split(os.sep)[tostrip:])
|
|
|
|
for fn in files:
|
|
|
|
archivename = os.path.join(*tuple(localpath+[fn]))
|
|
|
|
localfilename = os.path.join(path, fn)
|
|
|
|
zf.write(localfilename, archivename)
|
|
|
|
num_bytes += os.stat(localfilename).st_size
|
|
|
|
num_files += 1
|
|
|
|
fd_to_send.seek(0,2)
|
|
|
|
filesize = fd_to_send.tell()
|
|
|
|
fd_to_send.seek(0,0)
|
|
|
|
offer["directory"] = {
|
|
|
|
"mode": "zipfile/deflated",
|
|
|
|
"dirname": basename,
|
|
|
|
"zipsize": filesize,
|
|
|
|
"numbytes": num_bytes,
|
|
|
|
"numfiles": num_files,
|
|
|
|
}
|
2016-11-09 20:14:01 +00:00
|
|
|
print(u"Sending directory (%s compressed) named '%s'"
|
2016-12-24 05:03:32 +00:00
|
|
|
% (naturalsize(filesize), basename), file=args.stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
return offer, fd_to_send
|
|
|
|
|
|
|
|
raise TypeError("'%s' is neither file nor directory" % args.what)
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def _handle_answer(self, them_answer):
|
|
|
|
if self._fd_to_send is None:
|
|
|
|
if them_answer["message_ack"] == "ok":
|
2016-12-24 05:03:32 +00:00
|
|
|
print(u"text message sent", file=self._args.stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
returnValue(None) # terminates this function
|
|
|
|
raise TransferError("error sending text: %r" % (them_answer,))
|
|
|
|
|
|
|
|
if them_answer.get("file_ack") != "ok":
|
|
|
|
raise TransferError("ambiguous response from remote, "
|
|
|
|
"transfer abandoned: %s" % (them_answer,))
|
|
|
|
|
2016-05-26 02:36:56 +00:00
|
|
|
yield self._send_file()
|
2016-05-25 00:43:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@inlineCallbacks
|
2016-05-26 02:36:56 +00:00
|
|
|
def _send_file(self):
|
2016-05-25 00:43:17 +00:00
|
|
|
ts = self._transit_sender
|
|
|
|
|
|
|
|
self._fd_to_send.seek(0,2)
|
|
|
|
filesize = self._fd_to_send.tell()
|
|
|
|
self._fd_to_send.seek(0,0)
|
|
|
|
|
|
|
|
record_pipe = yield ts.connect()
|
|
|
|
self._timing.add("transit connected")
|
|
|
|
# record_pipe should implement IConsumer, chunks are just records
|
2016-12-24 05:03:32 +00:00
|
|
|
stderr = self._args.stderr
|
|
|
|
print(u"Sending (%s).." % record_pipe.describe(), file=stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
|
2016-05-26 02:36:56 +00:00
|
|
|
hasher = hashlib.sha256()
|
2016-12-24 05:03:32 +00:00
|
|
|
progress = tqdm(file=stderr, disable=self._args.hide_progress,
|
2016-05-25 00:43:17 +00:00
|
|
|
unit="B", unit_scale=True,
|
|
|
|
total=filesize)
|
2016-05-26 02:36:56 +00:00
|
|
|
def _count_and_hash(data):
|
|
|
|
hasher.update(data)
|
2016-05-25 00:43:17 +00:00
|
|
|
progress.update(len(data))
|
|
|
|
return data
|
|
|
|
fs = basic.FileSender()
|
|
|
|
|
|
|
|
with self._timing.add("tx file"):
|
|
|
|
with progress:
|
|
|
|
yield fs.beginFileTransfer(self._fd_to_send, record_pipe,
|
2016-05-26 02:36:56 +00:00
|
|
|
transform=_count_and_hash)
|
2016-05-25 00:43:17 +00:00
|
|
|
|
2016-05-26 02:36:56 +00:00
|
|
|
expected_hash = hasher.digest()
|
2016-05-29 01:19:45 +00:00
|
|
|
expected_hex = bytes_to_hexstr(expected_hash)
|
2016-12-24 05:03:32 +00:00
|
|
|
print(u"File sent.. waiting for confirmation", file=stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
with self._timing.add("get ack") as t:
|
2016-05-26 02:36:56 +00:00
|
|
|
ack_bytes = yield record_pipe.receive_record()
|
2016-05-25 00:43:17 +00:00
|
|
|
record_pipe.close()
|
2016-05-29 01:19:45 +00:00
|
|
|
ack = bytes_to_dict(ack_bytes)
|
2016-05-26 02:36:56 +00:00
|
|
|
ok = ack.get(u"ack", u"")
|
|
|
|
if ok != u"ok":
|
2016-05-25 00:43:17 +00:00
|
|
|
t.detail(ack="failed")
|
|
|
|
raise TransferError("Transfer failed (remote says: %r)" % ack)
|
2016-05-26 02:36:56 +00:00
|
|
|
if u"sha256" in ack:
|
|
|
|
if ack[u"sha256"] != expected_hex:
|
|
|
|
t.detail(datahash="failed")
|
|
|
|
raise TransferError("Transfer failed (bad remote hash)")
|
2016-12-24 05:03:32 +00:00
|
|
|
print(u"Confirmation received. Transfer complete.", file=stderr)
|
2016-05-25 00:43:17 +00:00
|
|
|
t.detail(ack="ok")
|