error out if server gives a "sorry we're closed" error

This commit is contained in:
Brian Warner 2015-04-09 12:29:26 -07:00
parent 3a728d4a06
commit e881d169a6
7 changed files with 47 additions and 13 deletions

View File

@ -7,6 +7,7 @@ from nacl.exceptions import CryptoError
from nacl import utils from nacl import utils
from .. import __version__ from .. import __version__
from .. import codes from .. import codes
from ..errors import ServerError
from ..util.hkdf import HKDF from ..util.hkdf import HKDF
SECOND = 1 SECOND = 1
@ -98,12 +99,14 @@ class Common:
version_warning_displayed = False version_warning_displayed = False
def handle_welcome(self, welcome): def handle_welcome(self, welcome):
if self.version_warning_displayed: if "error" in welcome:
return raise ServerError(welcome["error"], self.relay)
if "-" in __version__: if "-" in __version__:
# only warn if we're running a release version (e.g. 0.0.6, not # only warn if we're running a release version (e.g. 0.0.6, not
# 0.0.6-DISTANCE-gHASH) # 0.0.6-DISTANCE-gHASH)
return return
if self.version_warning_displayed:
return
if welcome["current_version"] != __version__: if welcome["current_version"] != __version__:
print("Warning: errors may occur unless both sides are running the same version", file=sys.stderr) 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" print("Server claims %s is current, but ours is %s"
@ -139,7 +142,10 @@ class Common:
def _allocate(self): def _allocate(self):
r = requests.post(self.relay + "allocate") r = requests.post(self.relay + "allocate")
r.raise_for_status() 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 return channel_id
def _post_pake(self): def _post_pake(self):

18
src/wormhole/errors.py Normal file
View File

@ -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

View File

@ -1,12 +1,14 @@
from __future__ import print_function from __future__ import print_function
import sys, os, json, binascii
from ..errors import handle_server_error
APPID = "lothar.com/wormhole/file-xfer" APPID = "lothar.com/wormhole/file-xfer"
@handle_server_error
def receive_file(args): def receive_file(args):
# we're receiving # we're receiving
import sys, os, json, binascii from ..blocking.transcribe import Receiver, WrongPasswordError
from wormhole.blocking.transcribe import Receiver, WrongPasswordError from ..blocking.transit import TransitReceiver, TransitError
from wormhole.blocking.transit import TransitReceiver, TransitError
from .progress import start_progress, update_progress, finish_progress from .progress import start_progress, update_progress, finish_progress
transit_receiver = TransitReceiver(args.transit_helper) transit_receiver = TransitReceiver(args.transit_helper)

View File

@ -1,11 +1,13 @@
from __future__ import print_function from __future__ import print_function
import sys, json, binascii
from ..errors import handle_server_error
APPID = "lothar.com/wormhole/text-xfer" APPID = "lothar.com/wormhole/text-xfer"
@handle_server_error
def receive_text(args): def receive_text(args):
# we're receiving # we're receiving
import sys, json, binascii from ..blocking.transcribe import Receiver, WrongPasswordError
from wormhole.blocking.transcribe import Receiver, WrongPasswordError
r = Receiver(APPID, args.relay_url) r = Receiver(APPID, args.relay_url)
code = args.code code = args.code

View File

@ -1,12 +1,14 @@
from __future__ import print_function from __future__ import print_function
import os, sys, json, binascii
from ..errors import handle_server_error
APPID = "lothar.com/wormhole/file-xfer" APPID = "lothar.com/wormhole/file-xfer"
@handle_server_error
def send_file(args): def send_file(args):
# we're sending # we're sending
import os, sys, json, binascii from ..blocking.transcribe import Initiator, WrongPasswordError
from wormhole.blocking.transcribe import Initiator, WrongPasswordError from ..blocking.transit import TransitSender
from wormhole.blocking.transit import TransitSender
from .progress import start_progress, update_progress, finish_progress from .progress import start_progress, update_progress, finish_progress
filename = args.filename filename = args.filename

View File

@ -1,11 +1,13 @@
from __future__ import print_function from __future__ import print_function
import sys, json, binascii
from ..errors import handle_server_error
APPID = "lothar.com/wormhole/text-xfer" APPID = "lothar.com/wormhole/text-xfer"
@handle_server_error
def send_text(args): def send_text(args):
# we're sending # we're sending
import sys, json, binascii from ..blocking.transcribe import Initiator, WrongPasswordError
from wormhole.blocking.transcribe import Initiator, WrongPasswordError
i = Initiator(APPID, args.relay_url) i = Initiator(APPID, args.relay_url)
code = i.get_code(args.code_length) code = i.get_code(args.code_length)

View File

@ -49,6 +49,8 @@ class EventsProtocol:
WELCOME = { WELCOME = {
"current_version": __version__, "current_version": __version__,
"motd": "Welcome to the public relay.", "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: # relay URLs are: