Significant headway on the rewrite.

Handle crypto with the excellent crytography library, still a lot of TODOs to take care of but at least we can connect now
This commit is contained in:
Ammar Askar committed 2015-03-17 22:15:27 +05:00
1 parent f68206140b
commit 7e8df47352
8 files changed
+424 -96

No files matched your search

+86 -25
View File
@@ -1,4 +1,5 @@
from io import BytesIO
from zlib import compress
from types import *
@@ -9,15 +10,26 @@ class PacketBuffer(object):
self.b = BytesIO()
def send(self, value):
"""
Writes the given bytes to the buffer, designed to emulate socket.send
:param value: The bytes to write
"""
self.b.write(value)
def read(self, length):
return self.b.read(length)
def reset_cursor(self):
self.b.seek(0)
def get_writable(self):
return self.b.getvalue()
class Packet(object):
name = "base"
packet_name = "base"
id = -0x01
definition = []
def __init__(self):
@@ -26,12 +38,13 @@ class Packet(object):
def read(self, file_object):
for field in self.definition:
for var_name, data_type in field.iteritems():
setattr(self, var_name, data_type.read(file_object))
value = data_type.read(file_object)
setattr(self, var_name, value)
def write(self, socket):
def write(self, socket, compression_threshold=-1):
# buffer the data since we need to know the length of each packet's payload
packet_buffer = PacketBuffer()
# write off the id right off the bat
# write packet's id right off the bat in the header
VarInt.send(self.id, packet_buffer)
for field in self.definition:
@@ -39,6 +52,8 @@ class Packet(object):
data = getattr(self, var_name)
data_type.send(data, packet_buffer)
# TODO: implement compression
VarInt.send(len(packet_buffer.get_writable()), socket) # Packet Size
socket.send(packet_buffer.get_writable()) # Packet Payload
@@ -47,7 +62,7 @@ class Packet(object):
# ==============
class HandShakePacket(Packet):
id = 0x00
name = "handshake"
packet_name = "handshake"
definition = [
{'protocol_version': VarInt},
{'server_address': String},
@@ -64,21 +79,20 @@ state_handshake_serverbound = {
# Status State
#==============
# ==============
class ResponsePacket(Packet):
id = 0x00
name = "response"
packet_name = "response"
definition = [
{'json_response': String}]
class PingPacket(Packet):
id = 0x01
name = "ping"
packet_name = "ping"
definition = [
{'time': Long}]
state_status_clientbound = {
0x00: ResponsePacket,
0x01: PingPacket
@@ -87,71 +101,118 @@ state_status_clientbound = {
class RequestPacket(Packet):
id = 0x00
name = "request"
packet_name = "request"
definition = []
class PingPacket(Packet):
id = 0x01
name = "ping"
packet_name = "ping"
definition = [
{'time': Long}]
state_status_serverbound = {
0x00: RequestPacket,
0x01: PingPacket
}
# Login State
#==============
# ==============
class DisconnectPacket(Packet):
id = 0x00
name = "disconnect"
packet_name = "disconnect"
definition = [
{'json_data': String}]
class EncryptionRequestPacket(Packet):
id = 0x01
name = "encryption request"
packet_name = "encryption request"
definition = [
{'server_id': String},
{'public_key': ByteArray},
{'verify_token': ByteArray}]
{'public_key': VarIntPrefixedByteArray},
{'verify_token': VarIntPrefixedByteArray}]
class LoginSucessPacket(Packet):
class LoginSuccessPacket(Packet):
id = 0x02
name = "login success"
packet_name = "login success"
definition = [
{'UUID': String},
{'Username': String}]
class SetCompressionPacket(Packet):
id = 0x03
packet_name = "set compression"
definition = [
{'threshold': VarInt}]
state_login_clientbound = {
0x00: DisconnectPacket,
0x01: EncryptionRequestPacket,
0x02: LoginSucessPacket
0x02: LoginSuccessPacket,
0x03: SetCompressionPacket
}
class LoginStartPacket(Packet):
id = 0x00
name = "login start"
packet_name = "login start"
definition = [
{'name': String}]
class EncryptionResponsePacket(Packet):
id = 0x01
name = "encryption response"
packet_name = "encryption response"
definition = [
{'shared_secret': ByteArray},
{'verify_token': ByteArray}]
{'shared_secret': VarIntPrefixedByteArray},
{'verify_token': VarIntPrefixedByteArray}]
state_login_serverbound = {
0x00: LoginStartPacket,
0x01: EncryptionResponsePacket
}
# Playing State
# ==============
class KeepAlivePacket(Packet):
id = 0x00
packet_name = "keep alive"
definition = [
{'keep_alive_id': VarInt}]
class JoinGamePacket(Packet):
id = 0x01
packet_name = "join game"
definition = [
{'entity_id': Integer},
{'game_mode': UnsignedByte},
{'dimension': Byte},
{'difficulty': UnsignedByte},
{'max_players': UnsignedByte},
{'level_type': String},
{'reduced_debug_info': Boolean}]
class SetCompressionPacketPlayState(Packet):
id = 0x46
packet_name = "set compression"
definition = [
{'threshold': VarInt}]
state_playing_clientbound = {
0x00: KeepAlivePacket,
0x01: JoinGamePacket,
0x46: SetCompressionPacketPlayState
}
state_playing_serverbound = {
}