2016-03-07 03:40:25 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
from collections import deque
|
2017-08-21 22:06:28 +02:00
|
|
|
from threading import RLock
|
2017-08-08 20:29:25 +02:00
|
|
|
import zlib
|
2014-06-07 01:47:34 +02:00
|
|
|
import threading
|
|
|
|
import socket
|
2016-11-20 06:03:23 +01:00
|
|
|
import timeit
|
2014-06-07 01:47:34 +02:00
|
|
|
import select
|
2015-10-07 06:51:40 +02:00
|
|
|
import sys
|
2016-03-07 03:40:25 +01:00
|
|
|
import json
|
2018-05-28 18:42:08 +02:00
|
|
|
import re
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2016-06-17 14:54:24 +02:00
|
|
|
from future.utils import raise_
|
|
|
|
|
2015-04-03 00:13:22 +02:00
|
|
|
from .types import VarInt
|
2017-08-09 21:53:54 +02:00
|
|
|
from .packets import clientbound, serverbound
|
2015-04-03 00:13:22 +02:00
|
|
|
from . import packets
|
2015-04-03 19:04:45 +02:00
|
|
|
from . import encryption
|
2018-05-28 18:42:08 +02:00
|
|
|
from .. import SUPPORTED_PROTOCOL_VERSIONS, SUPPORTED_MINECRAFT_VERSIONS
|
2018-05-29 02:14:23 +02:00
|
|
|
from ..exceptions import (
|
|
|
|
VersionMismatch, LoginDisconnect, IgnorePacket, InvalidState
|
|
|
|
)
|
2016-11-22 13:13:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
STATE_STATUS = 1
|
|
|
|
STATE_PLAYING = 2
|
2016-03-07 03:40:25 +01:00
|
|
|
|
2016-06-18 19:22:18 +02:00
|
|
|
|
2016-03-07 03:40:25 +01:00
|
|
|
class ConnectionContext(object):
|
|
|
|
"""A ConnectionContext encapsulates the static configuration parameters
|
|
|
|
shared by the Connection class with other classes, such as Packet.
|
|
|
|
Importantly, it can be used without knowing the interface of Connection.
|
|
|
|
"""
|
|
|
|
def __init__(self, **kwds):
|
|
|
|
self.protocol_version = kwds.get('protocol_version')
|
2015-03-17 18:15:27 +01:00
|
|
|
|
2016-06-18 19:22:18 +02:00
|
|
|
|
2015-04-03 00:13:22 +02:00
|
|
|
class _ConnectionOptions(object):
|
2016-06-18 19:22:18 +02:00
|
|
|
def __init__(self, address=None, port=None, compression_threshold=-1,
|
|
|
|
compression_enabled=False):
|
2015-10-07 06:51:40 +02:00
|
|
|
self.address = address
|
|
|
|
self.port = port
|
|
|
|
self.compression_threshold = compression_threshold
|
|
|
|
self.compression_enabled = compression_enabled
|
2015-03-17 18:15:27 +01:00
|
|
|
|
2016-06-18 19:22:18 +02:00
|
|
|
|
2015-03-17 18:15:27 +01:00
|
|
|
class Connection(object):
|
2014-06-07 01:47:34 +02:00
|
|
|
"""This class represents a connection to a minecraft
|
2015-03-17 18:15:27 +01:00
|
|
|
server, it handles everything from connecting, sending packets to
|
2014-06-07 01:47:34 +02:00
|
|
|
handling default network behaviour
|
|
|
|
"""
|
2016-03-05 08:28:14 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
address,
|
2016-11-20 06:03:23 +01:00
|
|
|
port=25565,
|
2016-06-18 19:22:18 +02:00
|
|
|
auth_token=None,
|
|
|
|
username=None,
|
|
|
|
initial_version=None,
|
|
|
|
allowed_versions=None,
|
2016-11-20 07:02:57 +01:00
|
|
|
handle_exception=None,
|
2018-05-27 08:40:13 +02:00
|
|
|
handle_exit=None,
|
2016-03-05 08:28:14 +01:00
|
|
|
):
|
2015-04-02 22:44:03 +02:00
|
|
|
"""Sets up an instance of this object to be able to connect to a
|
|
|
|
minecraft server.
|
|
|
|
|
|
|
|
The connect method needs to be called in order to actually begin
|
|
|
|
the connection
|
2015-03-17 18:15:27 +01:00
|
|
|
|
|
|
|
:param address: address of the server to connect to
|
|
|
|
:param port(int): port of the server to connect to
|
2016-12-19 11:41:28 +01:00
|
|
|
:param auth_token: :class:`minecraft.authentication.AuthenticationToken`
|
|
|
|
object. If None, no authentication is attempted and
|
|
|
|
the server is assumed to be running in offline mode.
|
2016-06-18 19:22:18 +02:00
|
|
|
:param username: Username string; only applicable in offline mode.
|
|
|
|
:param initial_version: A Minecraft version string or protocol version
|
2016-11-22 13:13:09 +01:00
|
|
|
number to use if the server's protocol version
|
|
|
|
cannot be determined. (Although it is now
|
|
|
|
somewhat inaccurate, this name is retained for
|
|
|
|
backward compatibility.)
|
2016-06-18 19:22:18 +02:00
|
|
|
:param allowed_versions: A set of versions, each being a Minecraft
|
|
|
|
version string or protocol version number,
|
|
|
|
restricting the versions that the client may
|
|
|
|
use in connecting to the server.
|
2016-11-20 07:02:57 +01:00
|
|
|
:param handle_exception: A function to be called when an exception
|
|
|
|
occurs in the client's networking thread,
|
2016-12-19 11:26:12 +01:00
|
|
|
taking 2 arguments: the exception object 'e'
|
|
|
|
as in 'except Exception as e', and a 3-tuple
|
2016-11-20 07:02:57 +01:00
|
|
|
given by sys.exc_info(); or None for the
|
|
|
|
default behaviour of raising the exception
|
|
|
|
from its original context; or False for no
|
|
|
|
action. In any case, the networking thread
|
|
|
|
will terminate, the exception will be
|
2016-12-19 11:26:12 +01:00
|
|
|
available via the 'exception' and 'exc_info'
|
|
|
|
attributes of the 'Connection' instance.
|
2018-05-27 08:40:13 +02:00
|
|
|
:param handle_exit: A function to be called when a connection to a
|
|
|
|
server terminates, not caused by an exception,
|
|
|
|
and not with the intention to automatically
|
|
|
|
reconnect. Exceptions raised in this handler
|
|
|
|
will be handled by handle_exception.
|
2016-12-19 11:41:28 +01:00
|
|
|
""" # NOQA
|
2015-10-07 06:51:40 +02:00
|
|
|
|
2017-08-21 22:06:28 +02:00
|
|
|
# This lock is re-entrant because it may be acquired in a re-entrant
|
|
|
|
# manner from within an outgoing packet listener
|
|
|
|
self._write_lock = RLock()
|
|
|
|
|
2015-10-07 06:51:40 +02:00
|
|
|
self.networking_thread = None
|
2018-05-29 02:14:23 +02:00
|
|
|
self.new_networking_thread = None
|
2015-10-07 06:51:40 +02:00
|
|
|
self.packet_listeners = []
|
2017-08-21 22:06:28 +02:00
|
|
|
self.early_packet_listeners = []
|
|
|
|
self.outgoing_packet_listeners = []
|
|
|
|
self.early_outgoing_packet_listeners = []
|
2016-03-07 03:40:25 +01:00
|
|
|
|
|
|
|
def proto_version(version):
|
|
|
|
if isinstance(version, str):
|
|
|
|
proto_version = SUPPORTED_MINECRAFT_VERSIONS.get(version)
|
|
|
|
elif isinstance(version, int):
|
|
|
|
proto_version = version
|
|
|
|
else:
|
|
|
|
proto_version = None
|
|
|
|
if proto_version not in SUPPORTED_PROTOCOL_VERSIONS:
|
|
|
|
raise ValueError('Unsupported version number: %r.' % version)
|
|
|
|
return proto_version
|
|
|
|
|
|
|
|
if allowed_versions is None:
|
2016-10-01 16:52:17 +02:00
|
|
|
self.allowed_proto_versions = set(SUPPORTED_PROTOCOL_VERSIONS)
|
2016-03-07 03:40:25 +01:00
|
|
|
else:
|
2016-11-22 13:13:09 +01:00
|
|
|
allowed_versions = set(map(proto_version, allowed_versions))
|
|
|
|
self.allowed_proto_versions = allowed_versions
|
2016-03-07 03:40:25 +01:00
|
|
|
|
|
|
|
if initial_version is None:
|
2016-11-22 13:13:09 +01:00
|
|
|
self.default_proto_version = max(self.allowed_proto_versions)
|
2016-03-07 03:40:25 +01:00
|
|
|
else:
|
2016-11-22 13:13:09 +01:00
|
|
|
self.default_proto_version = proto_version(initial_version)
|
|
|
|
|
2016-03-07 03:40:25 +01:00
|
|
|
self.context = ConnectionContext(
|
2016-11-22 13:13:09 +01:00
|
|
|
protocol_version=max(self.allowed_proto_versions))
|
2015-10-07 06:51:40 +02:00
|
|
|
|
|
|
|
self.options = _ConnectionOptions()
|
2015-03-17 18:15:27 +01:00
|
|
|
self.options.address = address
|
|
|
|
self.options.port = port
|
2015-04-01 23:38:10 +02:00
|
|
|
self.auth_token = auth_token
|
2016-06-18 19:22:18 +02:00
|
|
|
self.username = username
|
2018-05-27 08:40:13 +02:00
|
|
|
self.connected = False
|
2015-10-07 06:51:40 +02:00
|
|
|
|
2016-11-20 07:02:57 +01:00
|
|
|
self.handle_exception = handle_exception
|
|
|
|
self.exception, self.exc_info = None, None
|
2018-05-27 08:40:13 +02:00
|
|
|
self.handle_exit = handle_exit
|
2016-11-20 07:02:57 +01:00
|
|
|
|
2015-10-07 06:51:40 +02:00
|
|
|
# The reactor handles all the default responses to packets,
|
|
|
|
# it should be changed per networking state
|
2015-03-17 18:15:27 +01:00
|
|
|
self.reactor = PacketReactor(self)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
|
|
|
def _start_network_thread(self):
|
2018-05-29 02:14:23 +02:00
|
|
|
with self._write_lock:
|
|
|
|
if self.networking_thread is not None and \
|
|
|
|
not self.networking_thread.interrupt or \
|
|
|
|
self.new_networking_thread is not None:
|
|
|
|
raise InvalidState('A networking thread is already running.')
|
|
|
|
elif self.networking_thread is None:
|
|
|
|
self.networking_thread = NetworkingThread(self)
|
|
|
|
self.networking_thread.start()
|
|
|
|
else:
|
|
|
|
# This thread will wait until the existing thread exits, and
|
|
|
|
# then set 'networking_thread' to itself and
|
|
|
|
# 'new_networking_thread' to None.
|
|
|
|
self.new_networking_thread \
|
|
|
|
= NetworkingThread(self, previous=self.networking_thread)
|
|
|
|
self.new_networking_thread.start()
|
2014-06-07 01:47:34 +02:00
|
|
|
|
|
|
|
def write_packet(self, packet, force=False):
|
2015-03-17 18:15:27 +01:00
|
|
|
"""Writes a packet to the server.
|
2015-04-02 22:44:03 +02:00
|
|
|
|
|
|
|
If force is set to true, the method attempts to acquire the write lock
|
|
|
|
and write the packet out immediately, and as such may block.
|
|
|
|
|
|
|
|
If force is false then the packet will be added to the end of the
|
|
|
|
packet writing queue to be sent 'as soon as possible'
|
2015-03-17 18:15:27 +01:00
|
|
|
|
2015-03-21 23:08:44 +01:00
|
|
|
:param packet: The :class:`network.packets.Packet` to write
|
2015-03-17 18:15:27 +01:00
|
|
|
:param force(bool): Specifies if the packet write should be immediate
|
|
|
|
"""
|
2016-03-05 08:28:14 +01:00
|
|
|
packet.context = self.context
|
2014-06-07 01:47:34 +02:00
|
|
|
if force:
|
2017-08-21 22:06:28 +02:00
|
|
|
with self._write_lock:
|
|
|
|
self._write_packet(packet)
|
2014-06-07 01:47:34 +02:00
|
|
|
else:
|
2015-03-17 18:15:27 +01:00
|
|
|
self._outgoing_packet_queue.append(packet)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2018-08-13 02:57:16 +02:00
|
|
|
def listener(self, *packet_types, **kwds):
|
|
|
|
"""
|
|
|
|
Shorthand decorator to register a function as a packet listener.
|
|
|
|
"""
|
|
|
|
def _method_func(method):
|
|
|
|
self.register_packet_listener(method, *packet_types, **kwds)
|
2018-08-15 21:53:13 +02:00
|
|
|
return method
|
2018-08-13 02:57:16 +02:00
|
|
|
|
|
|
|
return _method_func
|
|
|
|
|
2017-08-21 22:06:28 +02:00
|
|
|
def register_packet_listener(self, method, *packet_types, **kwds):
|
2015-03-22 15:05:55 +01:00
|
|
|
"""
|
|
|
|
Registers a listener method which will be notified when a packet of
|
2017-08-21 22:06:28 +02:00
|
|
|
a selected type is received.
|
|
|
|
|
|
|
|
If :class:`minecraft.networking.connection.IgnorePacket` is raised from
|
|
|
|
within this method, no subsequent handlers will be called. If
|
|
|
|
'early=True', this has the additional effect of preventing the default
|
|
|
|
in-built action; this could break the internal state of the
|
|
|
|
'Connection', so should be done with care. If, in addition,
|
|
|
|
'outgoing=True', this will prevent the packet from being written to the
|
|
|
|
network.
|
2015-03-22 15:05:55 +01:00
|
|
|
|
|
|
|
:param method: The method which will be called back with the packet
|
2017-08-21 22:06:28 +02:00
|
|
|
:param packet_types: The packets to listen for
|
|
|
|
:param outgoing: If 'True', this listener will be called on outgoing
|
|
|
|
packets just after they are sent to the server, rather
|
|
|
|
than on incoming packets.
|
|
|
|
:param early: If 'True', this listener will be called before any
|
|
|
|
built-in default action is carried out, and before any
|
|
|
|
listeners with 'early=False' are called. If
|
|
|
|
'outgoing=True', the listener will be called before the
|
|
|
|
packet is written to the network, rather than afterwards.
|
2015-03-22 15:05:55 +01:00
|
|
|
"""
|
2017-08-21 22:06:28 +02:00
|
|
|
outgoing = kwds.pop('outgoing', False)
|
|
|
|
early = kwds.pop('early', False)
|
|
|
|
target = self.packet_listeners if not early and not outgoing \
|
|
|
|
else self.early_packet_listeners if early and not outgoing \
|
|
|
|
else self.outgoing_packet_listeners if not early \
|
|
|
|
else self.early_outgoing_packet_listeners
|
|
|
|
target.append(packets.PacketListener(method, *packet_types, **kwds))
|
2015-03-22 15:05:55 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
def _pop_packet(self):
|
2015-04-02 22:44:03 +02:00
|
|
|
# Pops the topmost packet off the outgoing queue and writes it out
|
|
|
|
# through the socket
|
2015-03-17 18:15:27 +01:00
|
|
|
#
|
2015-04-02 22:44:03 +02:00
|
|
|
# Mostly an internal convenience function, caller should make sure
|
|
|
|
# they have the write lock acquired to avoid issues caused by
|
|
|
|
# asynchronous access to the socket.
|
|
|
|
# This should be the only method that removes elements from the
|
|
|
|
# outbound queue
|
2015-03-17 18:15:27 +01:00
|
|
|
if len(self._outgoing_packet_queue) == 0:
|
2014-06-07 01:47:34 +02:00
|
|
|
return False
|
|
|
|
else:
|
2017-08-21 22:06:28 +02:00
|
|
|
self._write_packet(self._outgoing_packet_queue.popleft())
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _write_packet(self, packet):
|
|
|
|
# Immediately writes the given packet to the network. The caller must
|
|
|
|
# have the write lock acquired before calling this method.
|
|
|
|
try:
|
|
|
|
for listener in self.early_outgoing_packet_listeners:
|
|
|
|
listener.call_packet(packet)
|
|
|
|
|
2015-03-17 18:15:27 +01:00
|
|
|
if self.options.compression_enabled:
|
|
|
|
packet.write(self.socket, self.options.compression_threshold)
|
|
|
|
else:
|
|
|
|
packet.write(self.socket)
|
2017-08-21 22:06:28 +02:00
|
|
|
|
|
|
|
for listener in self.outgoing_packet_listeners:
|
|
|
|
listener.call_packet(packet)
|
|
|
|
except IgnorePacket:
|
|
|
|
pass
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2016-11-20 06:03:23 +01:00
|
|
|
def status(self, handle_status=None, handle_ping=False):
|
|
|
|
"""Issue a status request to the server and then disconnect.
|
|
|
|
|
|
|
|
:param handle_status: a function to be called with the status
|
|
|
|
dictionary None for the default behaviour of
|
|
|
|
printing the dictionary to standard output, or
|
|
|
|
False to ignore the result.
|
|
|
|
:param handle_ping: a function to be called with the measured latency
|
|
|
|
in milliseconds, None for the default handler,
|
|
|
|
which prints the latency to standard outout, or
|
|
|
|
False, to prevent measurement of the latency.
|
|
|
|
"""
|
2018-05-29 02:14:23 +02:00
|
|
|
with self._write_lock: # pylint: disable=not-context-manager
|
|
|
|
self._check_connection()
|
|
|
|
|
|
|
|
self._connect()
|
|
|
|
self._handshake(next_state=STATE_STATUS)
|
|
|
|
self._start_network_thread()
|
2016-11-20 06:03:23 +01:00
|
|
|
|
2018-05-29 02:14:23 +02:00
|
|
|
do_ping = handle_ping is not False
|
|
|
|
self.reactor = StatusReactor(self, do_ping=do_ping)
|
2016-11-20 06:03:23 +01:00
|
|
|
|
2018-05-29 02:14:23 +02:00
|
|
|
if handle_status is False:
|
|
|
|
self.reactor.handle_status = lambda *args, **kwds: None
|
|
|
|
elif handle_status is not None:
|
|
|
|
self.reactor.handle_status = handle_status
|
2016-11-20 06:03:23 +01:00
|
|
|
|
2018-05-29 02:14:23 +02:00
|
|
|
if handle_ping is False:
|
|
|
|
self.reactor.handle_ping = lambda *args, **kwds: None
|
|
|
|
elif handle_ping is not None:
|
|
|
|
self.reactor.handle_ping = handle_ping
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2018-05-29 02:14:23 +02:00
|
|
|
request_packet = serverbound.status.RequestPacket()
|
|
|
|
self.write_packet(request_packet)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
|
|
|
def connect(self):
|
2015-03-17 18:15:27 +01:00
|
|
|
"""
|
2016-03-07 03:40:25 +01:00
|
|
|
Attempt to begin connecting to the server.
|
|
|
|
May safely be called multiple times after the first, i.e. to reconnect.
|
|
|
|
"""
|
2016-11-22 13:13:09 +01:00
|
|
|
# Hold the lock throughout, in case connect() is called from the
|
|
|
|
# networking thread while another connection is in progress.
|
2017-04-16 05:19:42 +02:00
|
|
|
with self._write_lock: # pylint: disable=not-context-manager
|
2018-05-29 02:14:23 +02:00
|
|
|
self._check_connection()
|
2016-11-22 13:13:09 +01:00
|
|
|
|
|
|
|
# It is important that this is set correctly even when connecting
|
|
|
|
# in status mode, as some servers, e.g. SpigotMC with the
|
|
|
|
# ProtocolSupport plugin, use it to determine the correct response.
|
|
|
|
self.context.protocol_version = max(self.allowed_proto_versions)
|
2016-09-02 01:26:12 +02:00
|
|
|
|
|
|
|
self.spawned = False
|
|
|
|
self._connect()
|
2016-11-22 13:13:09 +01:00
|
|
|
if len(self.allowed_proto_versions) == 1:
|
|
|
|
# There is exactly one allowed protocol version, so skip the
|
|
|
|
# process of determining the server's version, and immediately
|
|
|
|
# connect.
|
|
|
|
self._handshake(next_state=STATE_PLAYING)
|
2017-08-09 21:53:54 +02:00
|
|
|
login_start_packet = serverbound.login.LoginStartPacket()
|
2016-11-22 13:13:09 +01:00
|
|
|
if self.auth_token:
|
|
|
|
login_start_packet.name = self.auth_token.profile.name
|
|
|
|
else:
|
|
|
|
login_start_packet.name = self.username
|
|
|
|
self.write_packet(login_start_packet)
|
|
|
|
self.reactor = LoginReactor(self)
|
2016-09-02 01:26:12 +02:00
|
|
|
else:
|
2016-11-22 13:13:09 +01:00
|
|
|
# Determine the server's protocol version by first performing a
|
|
|
|
# status query.
|
|
|
|
self._handshake(next_state=STATE_STATUS)
|
2017-08-09 21:53:54 +02:00
|
|
|
self.write_packet(serverbound.status.RequestPacket())
|
2016-11-22 13:13:09 +01:00
|
|
|
self.reactor = PlayingStatusReactor(self)
|
2016-09-02 01:26:12 +02:00
|
|
|
self._start_network_thread()
|
2015-03-17 18:15:27 +01:00
|
|
|
|
2018-05-29 02:14:23 +02:00
|
|
|
def _check_connection(self):
|
|
|
|
if self.networking_thread is not None and \
|
|
|
|
not self.networking_thread.interrupt or \
|
|
|
|
self.new_networking_thread is not None:
|
|
|
|
raise InvalidState('There is an existing connection.')
|
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
def _connect(self):
|
2015-04-02 22:44:03 +02:00
|
|
|
# Connect a socket to the server and create a file object from the
|
|
|
|
# socket.
|
|
|
|
# The file object is used to read any and all data from the socket
|
|
|
|
# since it's "guaranteed" to read the number of bytes specified,
|
|
|
|
# the socket itself will mostly be used to write data upstream to
|
|
|
|
# the server.
|
2016-11-20 06:03:23 +01:00
|
|
|
self._outgoing_packet_queue = deque()
|
2018-05-18 12:38:27 +02:00
|
|
|
|
|
|
|
info = socket.getaddrinfo(self.options.address, self.options.port,
|
|
|
|
0, socket.SOCK_STREAM)
|
|
|
|
|
|
|
|
# Prefer to use IPv4 (for backward compatibility with previous
|
|
|
|
# versions that always resolved hostnames to IPv4 addresses),
|
|
|
|
# then IPv6, then other address families.
|
|
|
|
def key(ai):
|
|
|
|
return 0 if ai[0] == socket.AF_INET else \
|
|
|
|
1 if ai[0] == socket.AF_INET6 else 2
|
|
|
|
ai_faml, ai_type, ai_prot, _ai_cnam, ai_addr = min(info, key=key)
|
|
|
|
|
|
|
|
self.socket = socket.socket(ai_faml, ai_type, ai_prot)
|
|
|
|
self.socket.connect(ai_addr)
|
2016-06-18 19:22:18 +02:00
|
|
|
self.file_object = self.socket.makefile("rb", 0)
|
2018-10-12 17:47:55 +02:00
|
|
|
self.options.compression_enabled = False
|
|
|
|
self.options.compression_threshold = -1
|
2018-05-27 08:40:13 +02:00
|
|
|
self.connected = True
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2018-05-29 02:14:23 +02:00
|
|
|
def disconnect(self, immediate=False):
|
|
|
|
"""Terminate the existing server connection, if there is one.
|
|
|
|
If 'immediate' is True, do not attempt to write any packets.
|
|
|
|
"""
|
2017-08-22 18:50:16 +02:00
|
|
|
with self._write_lock: # pylint: disable=not-context-manager
|
2018-05-29 02:14:23 +02:00
|
|
|
self.connected = False
|
|
|
|
|
|
|
|
if not immediate and self.socket is not None:
|
2017-08-22 18:50:16 +02:00
|
|
|
# Flush any packets remaining in the queue.
|
|
|
|
while self._pop_packet():
|
|
|
|
pass
|
|
|
|
|
|
|
|
if self.networking_thread is not None:
|
2016-11-22 13:13:09 +01:00
|
|
|
self.networking_thread.interrupt = True
|
2016-11-20 06:03:23 +01:00
|
|
|
|
2018-05-29 02:14:23 +02:00
|
|
|
if self.socket is not None:
|
|
|
|
try:
|
|
|
|
self.socket.shutdown(socket.SHUT_RDWR)
|
|
|
|
except socket.error:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.socket.close()
|
|
|
|
self.socket = None
|
2016-11-20 06:03:23 +01:00
|
|
|
|
2016-11-22 13:13:09 +01:00
|
|
|
def _handshake(self, next_state=STATE_PLAYING):
|
2017-08-09 21:53:54 +02:00
|
|
|
handshake = serverbound.handshake.HandShakePacket()
|
2016-03-05 08:28:14 +01:00
|
|
|
handshake.protocol_version = self.context.protocol_version
|
2015-03-17 18:15:27 +01:00
|
|
|
handshake.server_address = self.options.address
|
|
|
|
handshake.server_port = self.options.port
|
2014-06-07 01:47:34 +02:00
|
|
|
handshake.next_state = next_state
|
|
|
|
|
2014-11-10 16:25:30 +01:00
|
|
|
self.write_packet(handshake)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2016-11-20 07:02:57 +01:00
|
|
|
def _handle_exception(self, exc, exc_info):
|
2018-05-27 08:40:13 +02:00
|
|
|
handle_exception = self.handle_exception
|
|
|
|
try:
|
|
|
|
if self.reactor.handle_exception(exc, exc_info):
|
|
|
|
return
|
|
|
|
if handle_exception not in (None, False):
|
|
|
|
self.handle_exception(exc, exc_info)
|
|
|
|
except BaseException as new_exc:
|
|
|
|
exc, exc_info = new_exc, sys.exc_info()
|
|
|
|
|
2016-11-20 07:02:57 +01:00
|
|
|
try:
|
|
|
|
exc.exc_info = exc_info # For backward compatibility.
|
|
|
|
except (TypeError, AttributeError):
|
|
|
|
pass
|
2016-11-22 13:13:09 +01:00
|
|
|
|
2016-11-20 07:02:57 +01:00
|
|
|
self.exception, self.exc_info = exc, exc_info
|
2018-05-29 02:14:23 +02:00
|
|
|
with self._write_lock:
|
|
|
|
if self.networking_thread and not self.networking_thread.interrupt:
|
|
|
|
self.disconnect(immediate=True)
|
2018-05-27 08:40:13 +02:00
|
|
|
if handle_exception is None:
|
2016-11-20 07:02:57 +01:00
|
|
|
raise_(*exc_info)
|
2018-05-27 08:40:13 +02:00
|
|
|
|
2018-05-28 18:42:08 +02:00
|
|
|
def _version_mismatch(self, server_protocol=None, server_version=None):
|
|
|
|
if server_protocol is None:
|
|
|
|
server_protocol = SUPPORTED_MINECRAFT_VERSIONS.get(server_version)
|
|
|
|
|
|
|
|
if server_protocol is None:
|
|
|
|
vs = 'version' if server_version is None else \
|
|
|
|
('version of %s' % server_version)
|
|
|
|
else:
|
|
|
|
vs = ('protocol version of %d' % server_protocol) + \
|
|
|
|
('' if server_version is None else ' (%s)' % server_version)
|
|
|
|
ss = 'supported, but not allowed for this connection' \
|
|
|
|
if server_protocol in SUPPORTED_PROTOCOL_VERSIONS \
|
|
|
|
else 'not supported'
|
|
|
|
raise VersionMismatch("Server's %s is %s." % (vs, ss))
|
|
|
|
|
2018-05-27 08:40:13 +02:00
|
|
|
def _handle_exit(self):
|
|
|
|
if not self.connected and self.handle_exit is not None:
|
|
|
|
self.handle_exit()
|
2016-11-20 07:02:57 +01:00
|
|
|
|
2016-11-22 13:13:09 +01:00
|
|
|
def _react(self, packet):
|
2017-10-10 05:36:50 +02:00
|
|
|
try:
|
|
|
|
for listener in self.early_packet_listeners:
|
|
|
|
listener.call_packet(packet)
|
|
|
|
self.reactor.react(packet)
|
|
|
|
for listener in self.packet_listeners:
|
|
|
|
listener.call_packet(packet)
|
|
|
|
except IgnorePacket:
|
|
|
|
pass
|
2016-11-22 13:13:09 +01:00
|
|
|
|
2016-06-18 19:22:18 +02:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
class NetworkingThread(threading.Thread):
|
2016-11-22 13:13:09 +01:00
|
|
|
def __init__(self, connection, previous=None):
|
2014-06-07 01:47:34 +02:00
|
|
|
threading.Thread.__init__(self)
|
2015-10-07 06:51:40 +02:00
|
|
|
self.interrupt = False
|
2014-06-07 01:47:34 +02:00
|
|
|
self.connection = connection
|
|
|
|
self.name = "Networking Thread"
|
|
|
|
self.daemon = True
|
|
|
|
|
2016-11-22 13:13:09 +01:00
|
|
|
self.previous_thread = previous
|
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
def run(self):
|
2015-10-05 04:14:48 +02:00
|
|
|
try:
|
2017-10-10 05:36:50 +02:00
|
|
|
if self.previous_thread is not None:
|
|
|
|
if self.previous_thread.is_alive():
|
|
|
|
self.previous_thread.join()
|
2018-05-29 02:14:23 +02:00
|
|
|
with self.connection._write_lock:
|
|
|
|
self.connection.networking_thread = self
|
|
|
|
self.connection.new_networking_thread = None
|
2015-10-05 04:14:48 +02:00
|
|
|
self._run()
|
2018-05-27 08:40:13 +02:00
|
|
|
self.connection._handle_exit()
|
2016-11-20 07:02:57 +01:00
|
|
|
except BaseException as e:
|
2018-05-29 02:14:23 +02:00
|
|
|
self.interrupt = True
|
2016-11-20 07:02:57 +01:00
|
|
|
self.connection._handle_exception(e, sys.exc_info())
|
2016-11-20 06:03:23 +01:00
|
|
|
finally:
|
2018-05-29 02:14:23 +02:00
|
|
|
with self.connection._write_lock:
|
|
|
|
self.connection.networking_thread = None
|
2015-10-05 04:14:48 +02:00
|
|
|
|
|
|
|
def _run(self):
|
2016-11-22 13:13:09 +01:00
|
|
|
while not self.interrupt:
|
2017-10-10 05:36:50 +02:00
|
|
|
# Attempt to write out as many as 300 packets.
|
2014-06-07 01:47:34 +02:00
|
|
|
num_packets = 0
|
2017-10-10 05:36:50 +02:00
|
|
|
with self.connection._write_lock:
|
|
|
|
try:
|
|
|
|
while not self.interrupt and self.connection._pop_packet():
|
|
|
|
num_packets += 1
|
|
|
|
if num_packets >= 300:
|
|
|
|
break
|
|
|
|
exc_info = None
|
|
|
|
except IOError:
|
|
|
|
exc_info = sys.exc_info()
|
|
|
|
|
|
|
|
# If any packets remain to be written, resume writing as soon
|
|
|
|
# as possible after reading any available packets; otherwise,
|
|
|
|
# wait for up to 50ms (1 tick) for new packets to arrive.
|
|
|
|
if self.connection._outgoing_packet_queue:
|
|
|
|
read_timeout = 0
|
|
|
|
else:
|
|
|
|
read_timeout = 0.05
|
|
|
|
|
|
|
|
# Read and react to as many as 50 packets.
|
2016-11-22 13:13:09 +01:00
|
|
|
while num_packets < 50 and not self.interrupt:
|
|
|
|
packet = self.connection.reactor.read_packet(
|
2017-10-10 05:36:50 +02:00
|
|
|
self.connection.file_object, timeout=read_timeout)
|
2016-11-22 13:13:09 +01:00
|
|
|
if not packet:
|
|
|
|
break
|
2014-06-07 01:47:34 +02:00
|
|
|
num_packets += 1
|
2017-10-10 05:36:50 +02:00
|
|
|
self.connection._react(packet)
|
|
|
|
read_timeout = 0
|
2016-06-18 19:22:18 +02:00
|
|
|
|
2017-10-10 05:36:50 +02:00
|
|
|
# Ignore the earlier exception if a disconnect packet is
|
|
|
|
# received, as it may have been caused by trying to write to
|
2018-10-12 17:47:55 +02:00
|
|
|
# the closed socket, which does not represent a program error.
|
2018-01-21 01:45:41 +01:00
|
|
|
if exc_info is not None and packet.packet_name == "disconnect":
|
2016-06-18 19:22:18 +02:00
|
|
|
exc_info = None
|
|
|
|
|
2016-03-28 06:08:01 +02:00
|
|
|
if exc_info is not None:
|
2016-06-17 14:54:24 +02:00
|
|
|
raise_(*exc_info)
|
2016-03-28 06:08:01 +02:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2015-03-17 18:15:27 +01:00
|
|
|
class PacketReactor(object):
|
2015-03-22 15:05:55 +01:00
|
|
|
"""
|
|
|
|
Reads and reacts to packets
|
|
|
|
"""
|
2014-06-07 01:47:34 +02:00
|
|
|
state_name = None
|
|
|
|
|
2017-07-16 10:14:18 +02:00
|
|
|
# Handshaking is considered the "default" state
|
2017-08-09 21:53:54 +02:00
|
|
|
get_clientbound_packets = staticmethod(clientbound.handshake.get_packets)
|
2016-03-05 08:28:14 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
def __init__(self, connection):
|
|
|
|
self.connection = connection
|
2016-03-05 08:28:14 +01:00
|
|
|
context = self.connection.context
|
|
|
|
self.clientbound_packets = {
|
|
|
|
packet.get_id(context): packet
|
|
|
|
for packet in self.__class__.get_clientbound_packets(context)}
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2017-10-10 05:36:50 +02:00
|
|
|
def read_packet(self, stream, timeout=0):
|
|
|
|
# Block for up to `timeout' seconds waiting for `stream' to become
|
|
|
|
# readable, returning `None' if the timeout elapses.
|
|
|
|
ready_to_read = select.select([stream], [], [], timeout)[0]
|
2015-03-17 18:15:27 +01:00
|
|
|
|
2016-06-18 19:22:18 +02:00
|
|
|
if ready_to_read:
|
2016-06-17 14:54:24 +02:00
|
|
|
length = VarInt.read(stream)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2015-04-02 22:44:03 +02:00
|
|
|
packet_data = packets.PacketBuffer()
|
2015-03-22 13:39:15 +01:00
|
|
|
packet_data.send(stream.read(length))
|
|
|
|
# Ensure we read all the packet
|
|
|
|
while len(packet_data.get_writable()) < length:
|
2015-04-02 22:44:03 +02:00
|
|
|
packet_data.send(
|
|
|
|
stream.read(length - len(packet_data.get_writable())))
|
2015-03-22 13:39:15 +01:00
|
|
|
packet_data.reset_cursor()
|
|
|
|
|
2015-03-17 18:15:27 +01:00
|
|
|
if self.connection.options.compression_enabled:
|
2016-09-27 12:32:39 +02:00
|
|
|
decompressed_size = VarInt.read(packet_data)
|
2017-08-08 20:09:04 +02:00
|
|
|
if decompressed_size > 0:
|
2017-08-08 20:29:25 +02:00
|
|
|
decompressor = zlib.decompressobj()
|
|
|
|
decompressed_packet = decompressor.decompress(
|
|
|
|
packet_data.read())
|
2017-08-08 20:09:04 +02:00
|
|
|
assert len(decompressed_packet) == decompressed_size, \
|
|
|
|
'decompressed length %d, but expected %d' % \
|
|
|
|
(len(decompressed_packet), decompressed_size)
|
|
|
|
packet_data.reset()
|
|
|
|
packet_data.send(decompressed_packet)
|
|
|
|
packet_data.reset_cursor()
|
2015-03-17 18:15:27 +01:00
|
|
|
|
2015-03-22 13:39:15 +01:00
|
|
|
packet_id = VarInt.read(packet_data)
|
2015-03-17 18:15:27 +01:00
|
|
|
|
|
|
|
# If we know the structure of the packet, attempt to parse it
|
|
|
|
# otherwise just skip it
|
2014-06-07 01:47:34 +02:00
|
|
|
if packet_id in self.clientbound_packets:
|
|
|
|
packet = self.clientbound_packets[packet_id]()
|
2016-03-05 08:28:14 +01:00
|
|
|
packet.context = self.connection.context
|
2015-03-22 13:39:15 +01:00
|
|
|
packet.read(packet_data)
|
2014-06-07 01:47:34 +02:00
|
|
|
return packet
|
|
|
|
else:
|
2016-03-05 08:28:14 +01:00
|
|
|
return packets.Packet(context=self.connection.context)
|
2014-06-07 01:47:34 +02:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def react(self, packet):
|
2018-07-19 02:59:48 +02:00
|
|
|
"""Called with each incoming packet after early packet listeners are
|
|
|
|
run (if none of them raise 'IgnorePacket'), but before regular
|
|
|
|
packet listeners are run. If this method raises 'IgnorePacket', no
|
|
|
|
subsequent packet listeners will be called for this packet.
|
|
|
|
"""
|
2015-03-17 18:15:27 +01:00
|
|
|
raise NotImplementedError("Call to base reactor")
|
|
|
|
|
2016-11-22 13:13:09 +01:00
|
|
|
def handle_exception(self, exc, exc_info):
|
2018-07-19 02:59:48 +02:00
|
|
|
"""Called when an exception is raised in the networking thread. If this
|
|
|
|
method returns True, the default action will be prevented and the
|
|
|
|
exception ignored (but the networking thread will still terminate).
|
|
|
|
"""
|
2016-11-22 13:13:09 +01:00
|
|
|
return False
|
|
|
|
|
2015-03-17 18:15:27 +01:00
|
|
|
|
|
|
|
class LoginReactor(PacketReactor):
|
2017-08-09 21:53:54 +02:00
|
|
|
get_clientbound_packets = staticmethod(clientbound.login.get_packets)
|
2015-03-17 18:15:27 +01:00
|
|
|
|
|
|
|
def react(self, packet):
|
|
|
|
if packet.packet_name == "encryption request":
|
|
|
|
|
|
|
|
secret = encryption.generate_shared_secret()
|
2015-04-02 22:44:03 +02:00
|
|
|
token, encrypted_secret = encryption.encrypt_token_and_secret(
|
|
|
|
packet.public_key, packet.verify_token, secret)
|
2015-03-17 18:15:27 +01:00
|
|
|
|
|
|
|
# A server id of '-' means the server is in offline mode
|
|
|
|
if packet.server_id != '-':
|
2015-04-02 22:44:03 +02:00
|
|
|
server_id = encryption.generate_verification_hash(
|
|
|
|
packet.server_id, secret, packet.public_key)
|
2016-06-18 19:22:18 +02:00
|
|
|
if self.connection.auth_token is not None:
|
|
|
|
self.connection.auth_token.join(server_id)
|
2015-04-02 22:44:03 +02:00
|
|
|
|
2017-08-09 21:53:54 +02:00
|
|
|
encryption_response = serverbound.login.EncryptionResponsePacket()
|
2015-03-17 18:15:27 +01:00
|
|
|
encryption_response.shared_secret = encrypted_secret
|
2015-04-02 22:44:03 +02:00
|
|
|
encryption_response.verify_token = token
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2015-04-02 22:44:03 +02:00
|
|
|
# Forced because we'll have encrypted the connection by the time
|
|
|
|
# it reaches the outgoing queue
|
2015-03-17 18:15:27 +01:00
|
|
|
self.connection.write_packet(encryption_response, force=True)
|
|
|
|
|
|
|
|
# Enable the encryption
|
|
|
|
cipher = encryption.create_AES_cipher(secret)
|
|
|
|
encryptor = cipher.encryptor()
|
|
|
|
decryptor = cipher.decryptor()
|
2015-04-02 22:44:03 +02:00
|
|
|
self.connection.socket = encryption.EncryptedSocketWrapper(
|
|
|
|
self.connection.socket, encryptor, decryptor)
|
2015-04-02 23:25:34 +02:00
|
|
|
self.connection.file_object = \
|
|
|
|
encryption.EncryptedFileObjectWrapper(
|
|
|
|
self.connection.file_object, decryptor)
|
2015-03-17 18:15:27 +01:00
|
|
|
|
2018-07-19 02:59:48 +02:00
|
|
|
elif packet.packet_name == "disconnect":
|
2018-05-28 18:42:08 +02:00
|
|
|
# Receiving a disconnect packet in the login state indicates an
|
|
|
|
# abnormal condition. Raise an exception explaining the situation.
|
|
|
|
try:
|
|
|
|
msg = json.loads(packet.json_data)['text']
|
|
|
|
except (ValueError, TypeError, KeyError):
|
|
|
|
msg = packet.json_data
|
|
|
|
match = re.match(r"Outdated (client! Please use|server!"
|
|
|
|
r" I'm still on) (?P<ver>\S+)$", msg)
|
|
|
|
if match:
|
|
|
|
ver = match.group('ver')
|
|
|
|
self.connection._version_mismatch(server_version=ver)
|
|
|
|
raise LoginDisconnect('The server rejected our login attempt '
|
|
|
|
'with: "%s".' % msg)
|
2015-03-17 18:15:27 +01:00
|
|
|
|
2018-07-19 02:59:48 +02:00
|
|
|
elif packet.packet_name == "login success":
|
2015-03-17 18:15:27 +01:00
|
|
|
self.connection.reactor = PlayingReactor(self.connection)
|
|
|
|
|
2018-07-19 02:59:48 +02:00
|
|
|
elif packet.packet_name == "set compression":
|
2015-03-17 18:15:27 +01:00
|
|
|
self.connection.options.compression_threshold = packet.threshold
|
|
|
|
self.connection.options.compression_enabled = True
|
|
|
|
|
2018-07-19 02:59:48 +02:00
|
|
|
elif packet.packet_name == "login plugin request":
|
|
|
|
self.connection.write_packet(
|
|
|
|
serverbound.login.PluginResponsePacket(
|
|
|
|
message_id=packet.message_id, successful=False))
|
|
|
|
|
2016-06-18 19:22:18 +02:00
|
|
|
|
2015-03-17 18:15:27 +01:00
|
|
|
class PlayingReactor(PacketReactor):
|
2017-08-09 21:53:54 +02:00
|
|
|
get_clientbound_packets = staticmethod(clientbound.play.get_packets)
|
2015-03-17 18:15:27 +01:00
|
|
|
|
|
|
|
def react(self, packet):
|
|
|
|
if packet.packet_name == "set compression":
|
|
|
|
self.connection.options.compression_threshold = packet.threshold
|
|
|
|
self.connection.options.compression_enabled = True
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2018-07-19 02:59:48 +02:00
|
|
|
elif packet.packet_name == "keep alive":
|
2017-08-09 21:53:54 +02:00
|
|
|
keep_alive_packet = serverbound.play.KeepAlivePacket()
|
2015-03-22 13:39:15 +01:00
|
|
|
keep_alive_packet.keep_alive_id = packet.keep_alive_id
|
|
|
|
self.connection.write_packet(keep_alive_packet)
|
|
|
|
|
2018-07-19 02:59:48 +02:00
|
|
|
elif packet.packet_name == "player position and look":
|
2016-11-22 13:13:09 +01:00
|
|
|
if self.connection.context.protocol_version >= 107:
|
2017-08-09 21:53:54 +02:00
|
|
|
teleport_confirm = serverbound.play.TeleportConfirmPacket()
|
2016-11-22 13:13:09 +01:00
|
|
|
teleport_confirm.teleport_id = packet.teleport_id
|
|
|
|
self.connection.write_packet(teleport_confirm)
|
|
|
|
else:
|
2017-08-09 21:53:54 +02:00
|
|
|
position_response = serverbound.play.PositionAndLookPacket()
|
2016-11-22 13:13:09 +01:00
|
|
|
position_response.x = packet.x
|
|
|
|
position_response.feet_y = packet.y
|
|
|
|
position_response.z = packet.z
|
|
|
|
position_response.yaw = packet.yaw
|
|
|
|
position_response.pitch = packet.pitch
|
|
|
|
position_response.on_ground = True
|
|
|
|
self.connection.write_packet(position_response)
|
2015-03-22 14:20:01 +01:00
|
|
|
self.connection.spawned = True
|
2015-03-22 14:16:47 +01:00
|
|
|
|
2018-07-19 02:59:48 +02:00
|
|
|
elif packet.packet_name == "disconnect":
|
2016-11-20 06:03:23 +01:00
|
|
|
self.connection.disconnect()
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2016-06-18 19:22:18 +02:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
class StatusReactor(PacketReactor):
|
2017-08-09 21:53:54 +02:00
|
|
|
get_clientbound_packets = staticmethod(clientbound.status.get_packets)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2016-11-20 06:03:23 +01:00
|
|
|
def __init__(self, connection, do_ping=False):
|
|
|
|
super(StatusReactor, self).__init__(connection)
|
|
|
|
self.do_ping = do_ping
|
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
def react(self, packet):
|
2016-03-07 03:40:25 +01:00
|
|
|
if packet.packet_name == "response":
|
2016-11-22 13:13:09 +01:00
|
|
|
status_dict = json.loads(packet.json_response)
|
2016-11-20 06:03:23 +01:00
|
|
|
if self.do_ping:
|
2017-08-09 21:53:54 +02:00
|
|
|
ping_packet = serverbound.status.PingPacket()
|
2016-11-20 06:03:23 +01:00
|
|
|
# NOTE: it may be better to depend on the `monotonic' package
|
|
|
|
# or something similar for more accurate time measurement.
|
|
|
|
ping_packet.time = int(1000 * timeit.default_timer())
|
|
|
|
self.connection.write_packet(ping_packet)
|
|
|
|
else:
|
|
|
|
self.connection.disconnect()
|
2016-11-22 13:13:09 +01:00
|
|
|
self.handle_status(status_dict)
|
2016-11-20 06:03:23 +01:00
|
|
|
|
2018-07-19 02:59:48 +02:00
|
|
|
elif packet.packet_name == "ping":
|
|
|
|
if self.do_ping:
|
|
|
|
now = int(1000 * timeit.default_timer())
|
|
|
|
self.connection.disconnect()
|
|
|
|
self.handle_ping(now - packet.time)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2016-11-20 06:03:23 +01:00
|
|
|
def handle_status(self, status_dict):
|
|
|
|
print(status_dict)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2016-11-20 06:03:23 +01:00
|
|
|
def handle_ping(self, latency_ms):
|
|
|
|
print('Ping: %d ms' % latency_ms)
|
2016-11-22 13:13:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PlayingStatusReactor(StatusReactor):
|
|
|
|
def __init__(self, connection):
|
|
|
|
super(PlayingStatusReactor, self).__init__(connection, do_ping=False)
|
|
|
|
|
|
|
|
def handle_status(self, status):
|
|
|
|
if status == {}:
|
|
|
|
# This can occur when we connect to a Mojang server while it is
|
|
|
|
# still initialising, so it must not cause the client to connect
|
|
|
|
# with the default version.
|
|
|
|
raise IOError('Invalid server status.')
|
|
|
|
elif 'version' not in status or 'protocol' not in status['version']:
|
|
|
|
return self.handle_failure()
|
|
|
|
|
|
|
|
proto = status['version']['protocol']
|
|
|
|
if proto not in self.connection.allowed_proto_versions:
|
2018-05-28 18:42:08 +02:00
|
|
|
self.connection._version_mismatch(
|
|
|
|
server_protocol=proto,
|
|
|
|
server_version=status['version'].get('name'))
|
2016-11-22 13:13:09 +01:00
|
|
|
|
|
|
|
self.handle_proto_version(proto)
|
|
|
|
|
|
|
|
def handle_proto_version(self, proto_version):
|
|
|
|
self.connection.allowed_proto_versions = {proto_version}
|
|
|
|
self.connection.connect()
|
|
|
|
|
|
|
|
def handle_failure(self):
|
|
|
|
self.handle_proto_version(self.connection.default_proto_version)
|
|
|
|
|
|
|
|
def handle_exception(self, exc, exc_info):
|
|
|
|
if isinstance(exc, EOFError):
|
|
|
|
# An exception of this type may indicate that the server does not
|
|
|
|
# properly support status queries, so we treat it as non-fatal.
|
2018-05-29 02:14:23 +02:00
|
|
|
self.connection.disconnect(immediate=True)
|
2016-11-22 13:13:09 +01:00
|
|
|
self.handle_failure()
|
|
|
|
return True
|