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
|
# only write to the current directory, and never overwrite anything
|
||||||
here = os.path.abspath(os.getcwd())
|
here = os.path.abspath(os.getcwd())
|
||||||
target = os.path.abspath(os.path.join(here, filename))
|
target = os.path.abspath(os.path.join(here, filename))
|
||||||
assert os.path.dirname(target) == here
|
if os.path.dirname(target) != here:
|
||||||
assert not os.path.exists(target)
|
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:
|
with open(target, "wb") as f:
|
||||||
f.write(decrypted)
|
f.write(decrypted)
|
||||||
print("Received file written to %s" % filename)
|
print("Received file written to %s" % filename)
|
||||||
skt.send("ok\n")
|
skt.send("ok\n")
|
||||||
|
skt.close()
|
||||||
|
sys.exit(0)
|
||||||
|
|
|
@ -53,9 +53,12 @@ print("Sending %d bytes.." % filesize)
|
||||||
skt.send(encrypted)
|
skt.send(encrypted)
|
||||||
|
|
||||||
print("File sent.. waiting for confirmation")
|
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":
|
if ack == "ok\n":
|
||||||
print("Confirmation received. Transfer complete.")
|
print("Confirmation received. Transfer complete.")
|
||||||
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
print("Transfer failed (remote says: '%r')" % ack)
|
print("Transfer failed (remote says: %r)" % ack)
|
||||||
skt.close()
|
sys.exit(1)
|
||||||
|
|
|
@ -120,10 +120,11 @@ def connector(owner, hint, send_handshake, expected_handshake):
|
||||||
if isinstance(hint, type(u"")):
|
if isinstance(hint, type(u"")):
|
||||||
hint = hint.encode("ascii")
|
hint = hint.encode("ascii")
|
||||||
addr,port = hint.split(",")
|
addr,port = hint.split(",")
|
||||||
|
skt = None
|
||||||
|
try:
|
||||||
skt = socket.create_connection((addr,port)) # timeout here
|
skt = socket.create_connection((addr,port)) # timeout here
|
||||||
skt.settimeout(TIMEOUT)
|
skt.settimeout(TIMEOUT)
|
||||||
#print("socket(%s) connected" % (hint,))
|
#print("socket(%s) connected" % (hint,))
|
||||||
try:
|
|
||||||
skt.send(send_handshake)
|
skt.send(send_handshake)
|
||||||
got = b""
|
got = b""
|
||||||
while len(got) < len(expected_handshake):
|
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" %
|
raise BadHandshake("got '%r' want '%r' on %s" %
|
||||||
(got, expected_handshake, hint))
|
(got, expected_handshake, hint))
|
||||||
#print("connector ready %r" % (hint,))
|
#print("connector ready %r" % (hint,))
|
||||||
except:
|
except Exception as e:
|
||||||
try:
|
try:
|
||||||
|
if skt:
|
||||||
skt.shutdown(socket.SHUT_WR)
|
skt.shutdown(socket.SHUT_WR)
|
||||||
except socket.error:
|
except socket.error:
|
||||||
pass
|
pass
|
||||||
|
if skt:
|
||||||
skt.close()
|
skt.close()
|
||||||
|
# ignore socket errors, warn about coding errors
|
||||||
|
if not isinstance(e, (socket.error, socket.timeout, BadHandshake)):
|
||||||
raise
|
raise
|
||||||
|
return
|
||||||
# owner is now responsible for the socket
|
# owner is now responsible for the socket
|
||||||
owner._negotiation_finished(skt) # note thread
|
owner._negotiation_finished(skt) # note thread
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user