cmd_receive: add underscore prefix to all internal methods

This commit is contained in:
Brian Warner 2016-05-24 19:14:56 -07:00
parent 812fd0b4da
commit 1b5f3e125e

View File

@ -33,7 +33,7 @@ class TwistedReceiver:
self._tor_manager = None
self._transit_receiver = None
def msg(self, *args, **kwargs):
def _msg(self, *args, **kwargs):
print(*args, file=self.args.stdout, **kwargs)
@inlineCallbacks
@ -67,9 +67,9 @@ class TwistedReceiver:
@inlineCallbacks
def _go(self, w):
yield self.handle_code(w)
yield self._handle_code(w)
verifier = yield w.verify()
self.show_verifier(verifier)
self._show_verifier(verifier)
want_offer = True
done = False
@ -89,7 +89,7 @@ class TwistedReceiver:
if not want_offer:
raise TransferError("duplicate offer")
try:
yield self.parse_offer(them_d[u"offer"], w)
yield self._parse_offer(them_d[u"offer"], w)
except RespondError as r:
self._send_data({"error": r.response}, w)
raise TransferError(r.response)
@ -111,7 +111,7 @@ class TwistedReceiver:
returnValue(them_d)
@inlineCallbacks
def handle_code(self, w):
def _handle_code(self, w):
code = self.args.code
if self.args.zeromode:
assert not code
@ -122,10 +122,10 @@ class TwistedReceiver:
yield w.input_code("Enter receive wormhole code: ",
self.args.code_length)
def show_verifier(self, verifier):
def _show_verifier(self, verifier):
verifier_hex = binascii.hexlify(verifier).decode("ascii")
if self.args.verify:
self.msg(u"Verifier %s." % verifier_hex)
self._msg(u"Verifier %s." % verifier_hex)
@inlineCallbacks
def _parse_transit(self, sender_hints, w):
@ -159,65 +159,65 @@ class TwistedReceiver:
# TODO: send more hints as the TransitReceiver produces them
@inlineCallbacks
def parse_offer(self, them_d, w):
def _parse_offer(self, them_d, w):
if "message" in them_d:
self.handle_text(them_d, w)
self._handle_text(them_d, w)
returnValue(None)
# transit will be created by this point, but not connected
if "file" in them_d:
f = self.handle_file(them_d)
f = self._handle_file(them_d)
self._send_permission(w)
rp = yield self._establish_transit()
yield self._transfer_data(rp, f)
self.write_file(f)
yield self.close_transit(rp)
self._write_file(f)
yield self._close_transit(rp)
elif "directory" in them_d:
f = self.handle_directory(them_d)
f = self._handle_directory(them_d)
self._send_permission(w)
rp = yield self._establish_transit()
yield self._transfer_data(rp, f)
self.write_directory(f)
yield self.close_transit(rp)
self._write_directory(f)
yield self._close_transit(rp)
else:
self.msg(u"I don't know what they're offering\n")
self.msg(u"Offer details: %r" % (them_d,))
self._msg(u"I don't know what they're offering\n")
self._msg(u"Offer details: %r" % (them_d,))
raise RespondError("unknown offer type")
def handle_text(self, them_d, w):
def _handle_text(self, them_d, w):
# we're receiving a text message
self.msg(them_d["message"])
self._msg(them_d["message"])
self._send_data({"answer": {"message_ack": "ok"}}, w)
def handle_file(self, them_d):
def _handle_file(self, them_d):
file_data = them_d["file"]
self.abs_destname = self.decide_destname("file",
self.abs_destname = self._decide_destname("file",
file_data["filename"])
self.xfersize = file_data["filesize"]
self.msg(u"Receiving file (%d bytes) into: %s" %
self._msg(u"Receiving file (%d bytes) into: %s" %
(self.xfersize, os.path.basename(self.abs_destname)))
self.ask_permission()
self._ask_permission()
tmp_destname = self.abs_destname + ".tmp"
return open(tmp_destname, "wb")
def handle_directory(self, them_d):
def _handle_directory(self, them_d):
file_data = them_d["directory"]
zipmode = file_data["mode"]
if zipmode != "zipfile/deflated":
self.msg(u"Error: unknown directory-transfer mode '%s'" % (zipmode,))
self._msg(u"Error: unknown directory-transfer mode '%s'" % (zipmode,))
raise RespondError("unknown mode")
self.abs_destname = self.decide_destname("directory",
self.abs_destname = self._decide_destname("directory",
file_data["dirname"])
self.xfersize = file_data["zipsize"]
self.msg(u"Receiving directory (%d bytes) into: %s/" %
self._msg(u"Receiving directory (%d bytes) into: %s/" %
(self.xfersize, os.path.basename(self.abs_destname)))
self.msg(u"%d files, %d bytes (uncompressed)" %
self._msg(u"%d files, %d bytes (uncompressed)" %
(file_data["numfiles"], file_data["numbytes"]))
self.ask_permission()
self._ask_permission()
return tempfile.SpooledTemporaryFile()
def decide_destname(self, mode, destname):
def _decide_destname(self, mode, destname):
# the basename() is intended to protect us against
# "~/.ssh/authorized_keys" and other attacks
destname = os.path.basename(destname)
@ -227,12 +227,12 @@ class TwistedReceiver:
# get confirmation from the user before writing to the local directory
if os.path.exists(abs_destname):
self.msg(u"Error: refusing to overwrite existing %s %s" %
self._msg(u"Error: refusing to overwrite existing %s %s" %
(mode, destname))
raise RespondError("%s already exists" % mode)
return abs_destname
def ask_permission(self):
def _ask_permission(self):
with self.args.timing.add("permission", waiting="user") as t:
while True and not self.args.accept_file:
ok = six.moves.input("ok? (y/n): ")
@ -255,7 +255,7 @@ class TwistedReceiver:
@inlineCallbacks
def _transfer_data(self, record_pipe, f):
# now receive the rest of the owl
self.msg(u"Receiving (%s).." % record_pipe.describe())
self._msg(u"Receiving (%s).." % record_pipe.describe())
with self.args.timing.add("rx file"):
progress = tqdm(file=self.args.stdout,
@ -267,21 +267,21 @@ class TwistedReceiver:
# except TransitError
if received < self.xfersize:
self.msg()
self.msg(u"Connection dropped before full file received")
self.msg(u"got %d bytes, wanted %d" % (received, self.xfersize))
self._msg()
self._msg(u"Connection dropped before full file received")
self._msg(u"got %d bytes, wanted %d" % (received, self.xfersize))
raise TransferError("Connection dropped before full file received")
assert received == self.xfersize
def write_file(self, f):
def _write_file(self, f):
tmp_name = f.name
f.close()
os.rename(tmp_name, self.abs_destname)
self.msg(u"Received file written to %s" %
self._msg(u"Received file written to %s" %
os.path.basename(self.abs_destname))
def write_directory(self, f):
self.msg(u"Unpacking zipfile..")
def _write_directory(self, f):
self._msg(u"Unpacking zipfile..")
with self.args.timing.add("unpack zip"):
with zipfile.ZipFile(f, "r", zipfile.ZIP_DEFLATED) as zf:
zf.extractall(path=self.abs_destname)
@ -289,12 +289,12 @@ class TwistedReceiver:
# malicious pathnames. For example, "/tmp/oops" and
# "../tmp/oops" both do the same thing as the (safe)
# "tmp/oops".
self.msg(u"Received files written to %s/" %
self._msg(u"Received files written to %s/" %
os.path.basename(self.abs_destname))
f.close()
@inlineCallbacks
def close_transit(self, record_pipe):
def _close_transit(self, record_pipe):
with self.args.timing.add("send ack"):
yield record_pipe.send_record(b"ok\n")
yield record_pipe.close()