don't complain about unconnectable sockets, improve error messages
This commit is contained in:
parent
3ccd7bd61e
commit
50e466b581
|
@ -45,9 +45,20 @@ decrypted = SecretBox(xfer_key).decrypt(encrypted)
|
|||
# only write to the current directory, and never overwrite anything
|
||||
here = os.path.abspath(os.getcwd())
|
||||
target = os.path.abspath(os.path.join(here, filename))
|
||||
assert os.path.dirname(target) == here
|
||||
assert not os.path.exists(target)
|
||||
if os.path.dirname(target) != here:
|
||||
print("Error: suggested filename (%s) would be outside current directory"
|
||||
% (filename,))
|
||||
skt.send("bad filename\n")
|
||||
skt.close()
|
||||
sys.exit(1)
|
||||
if os.path.exists(target):
|
||||
print("Error: refusing to overwrite existing file %s" % (filename,))
|
||||
skt.send("file already exists\n")
|
||||
skt.close()
|
||||
sys.exit(1)
|
||||
with open(target, "wb") as f:
|
||||
f.write(decrypted)
|
||||
print("Received file written to %s" % filename)
|
||||
skt.send("ok\n")
|
||||
skt.close()
|
||||
sys.exit(0)
|
||||
|
|
|
@ -53,9 +53,12 @@ print("Sending %d bytes.." % filesize)
|
|||
skt.send(encrypted)
|
||||
|
||||
print("File sent.. waiting for confirmation")
|
||||
ack = skt.recv(3)
|
||||
# ack is a short newline-terminated string, followed by socket close. A long
|
||||
# read is probably good enough.
|
||||
ack = skt.recv(300)
|
||||
if ack == "ok\n":
|
||||
print("Confirmation received. Transfer complete.")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print("Transfer failed (remote says: '%r')" % ack)
|
||||
skt.close()
|
||||
print("Transfer failed (remote says: %r)" % ack)
|
||||
sys.exit(1)
|
||||
|
|
|
@ -120,10 +120,11 @@ def connector(owner, hint, send_handshake, expected_handshake):
|
|||
if isinstance(hint, type(u"")):
|
||||
hint = hint.encode("ascii")
|
||||
addr,port = hint.split(",")
|
||||
skt = socket.create_connection((addr,port)) # timeout here
|
||||
skt.settimeout(TIMEOUT)
|
||||
#print("socket(%s) connected" % (hint,))
|
||||
skt = None
|
||||
try:
|
||||
skt = socket.create_connection((addr,port)) # timeout here
|
||||
skt.settimeout(TIMEOUT)
|
||||
#print("socket(%s) connected" % (hint,))
|
||||
skt.send(send_handshake)
|
||||
got = b""
|
||||
while len(got) < len(expected_handshake):
|
||||
|
@ -132,13 +133,18 @@ def connector(owner, hint, send_handshake, expected_handshake):
|
|||
raise BadHandshake("got '%r' want '%r' on %s" %
|
||||
(got, expected_handshake, hint))
|
||||
#print("connector ready %r" % (hint,))
|
||||
except:
|
||||
except Exception as e:
|
||||
try:
|
||||
skt.shutdown(socket.SHUT_WR)
|
||||
if skt:
|
||||
skt.shutdown(socket.SHUT_WR)
|
||||
except socket.error:
|
||||
pass
|
||||
skt.close()
|
||||
raise
|
||||
if skt:
|
||||
skt.close()
|
||||
# ignore socket errors, warn about coding errors
|
||||
if not isinstance(e, (socket.error, socket.timeout, BadHandshake)):
|
||||
raise
|
||||
return
|
||||
# owner is now responsible for the socket
|
||||
owner._negotiation_finished(skt) # note thread
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user