diff --git a/src/wormhole/blocking/transcribe.py b/src/wormhole/blocking/transcribe.py index 44baab0..2f50664 100644 --- a/src/wormhole/blocking/transcribe.py +++ b/src/wormhole/blocking/transcribe.py @@ -7,6 +7,7 @@ from nacl.exceptions import CryptoError from nacl import utils from .. import __version__ from .. import codes +from ..errors import ServerError from ..util.hkdf import HKDF SECOND = 1 @@ -98,12 +99,14 @@ class Common: version_warning_displayed = False def handle_welcome(self, welcome): - if self.version_warning_displayed: - return + if "error" in welcome: + raise ServerError(welcome["error"], self.relay) if "-" in __version__: # only warn if we're running a release version (e.g. 0.0.6, not # 0.0.6-DISTANCE-gHASH) return + if self.version_warning_displayed: + return if welcome["current_version"] != __version__: print("Warning: errors may occur unless both sides are running the same version", file=sys.stderr) print("Server claims %s is current, but ours is %s" @@ -139,7 +142,10 @@ class Common: def _allocate(self): r = requests.post(self.relay + "allocate") r.raise_for_status() - channel_id = r.json()["channel-id"] + data = r.json() + if "welcome" in data: + self.handle_welcome(data["welcome"]) + channel_id = data["channel-id"] return channel_id def _post_pake(self): diff --git a/src/wormhole/errors.py b/src/wormhole/errors.py new file mode 100644 index 0000000..2e8de3e --- /dev/null +++ b/src/wormhole/errors.py @@ -0,0 +1,18 @@ +import functools + +class ServerError(Exception): + def __init__(self, message, relay): + self.message = message + self.relay = relay + def __str__(self): + return self.message + +def handle_server_error(func): + @functools.wraps(func) + def _wrap(*args, **kwargs): + try: + return func(*args, **kwargs) + except ServerError as e: + print("Server error (from %s):\n%s" % (e.relay, e.message)) + return 1 + return _wrap diff --git a/src/wormhole/scripts/cmd_receive_file.py b/src/wormhole/scripts/cmd_receive_file.py index c072558..a24ea9e 100644 --- a/src/wormhole/scripts/cmd_receive_file.py +++ b/src/wormhole/scripts/cmd_receive_file.py @@ -1,12 +1,14 @@ from __future__ import print_function +import sys, os, json, binascii +from ..errors import handle_server_error APPID = "lothar.com/wormhole/file-xfer" +@handle_server_error def receive_file(args): # we're receiving - import sys, os, json, binascii - from wormhole.blocking.transcribe import Receiver, WrongPasswordError - from wormhole.blocking.transit import TransitReceiver, TransitError + from ..blocking.transcribe import Receiver, WrongPasswordError + from ..blocking.transit import TransitReceiver, TransitError from .progress import start_progress, update_progress, finish_progress transit_receiver = TransitReceiver(args.transit_helper) diff --git a/src/wormhole/scripts/cmd_receive_text.py b/src/wormhole/scripts/cmd_receive_text.py index 13c0a84..93cf95b 100644 --- a/src/wormhole/scripts/cmd_receive_text.py +++ b/src/wormhole/scripts/cmd_receive_text.py @@ -1,11 +1,13 @@ from __future__ import print_function +import sys, json, binascii +from ..errors import handle_server_error APPID = "lothar.com/wormhole/text-xfer" +@handle_server_error def receive_text(args): # we're receiving - import sys, json, binascii - from wormhole.blocking.transcribe import Receiver, WrongPasswordError + from ..blocking.transcribe import Receiver, WrongPasswordError r = Receiver(APPID, args.relay_url) code = args.code diff --git a/src/wormhole/scripts/cmd_send_file.py b/src/wormhole/scripts/cmd_send_file.py index a74cbf6..fc75a35 100644 --- a/src/wormhole/scripts/cmd_send_file.py +++ b/src/wormhole/scripts/cmd_send_file.py @@ -1,12 +1,14 @@ from __future__ import print_function +import os, sys, json, binascii +from ..errors import handle_server_error APPID = "lothar.com/wormhole/file-xfer" +@handle_server_error def send_file(args): # we're sending - import os, sys, json, binascii - from wormhole.blocking.transcribe import Initiator, WrongPasswordError - from wormhole.blocking.transit import TransitSender + from ..blocking.transcribe import Initiator, WrongPasswordError + from ..blocking.transit import TransitSender from .progress import start_progress, update_progress, finish_progress filename = args.filename diff --git a/src/wormhole/scripts/cmd_send_text.py b/src/wormhole/scripts/cmd_send_text.py index 881faef..c1385c4 100644 --- a/src/wormhole/scripts/cmd_send_text.py +++ b/src/wormhole/scripts/cmd_send_text.py @@ -1,11 +1,13 @@ from __future__ import print_function +import sys, json, binascii +from ..errors import handle_server_error APPID = "lothar.com/wormhole/text-xfer" +@handle_server_error def send_text(args): # we're sending - import sys, json, binascii - from wormhole.blocking.transcribe import Initiator, WrongPasswordError + from ..blocking.transcribe import Initiator, WrongPasswordError i = Initiator(APPID, args.relay_url) code = i.get_code(args.code_length) diff --git a/src/wormhole/servers/relay.py b/src/wormhole/servers/relay.py index 37fd291..57529a0 100644 --- a/src/wormhole/servers/relay.py +++ b/src/wormhole/servers/relay.py @@ -49,6 +49,8 @@ class EventsProtocol: WELCOME = { "current_version": __version__, "motd": "Welcome to the public relay.", + # adding .error will cause all clients to fail, with this message + #"error": "This server has been disabled, see URL for details.", } # relay URLs are: