Minor cleanup, pep8 compliance
This commit is contained in:
1 parent
85112fba1d
commit
1982716f73
5 files changed
+74
-67
No files matched your search
+10
-9
@@ -1,15 +1,15 @@
|
|||||||
import urllib2
|
import urllib2
|
||||||
import urllib
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
BASE_URL = 'https://authserver.mojang.com/'
|
BASE_URL = 'https://authserver.mojang.com/'
|
||||||
AGENT_INFO = {"name": "Minecraft", "version": 1}
|
AGENT_INFO = {"name": "Minecraft", "version": 1}
|
||||||
|
|
||||||
|
|
||||||
"""Class to hold responses from Yggdrasil
|
class Response(object):
|
||||||
"""
|
"""Class to hold responses from Yggdrasil
|
||||||
class Response:
|
"""
|
||||||
error = False
|
error = False
|
||||||
|
payload = None
|
||||||
|
|
||||||
|
|
||||||
def make_request(url, payload):
|
def make_request(url, payload):
|
||||||
@@ -50,10 +50,11 @@ def make_request(url, payload):
|
|||||||
response.payload = json_response
|
response.payload = json_response
|
||||||
return response
|
return response
|
||||||
|
|
||||||
"""Yet another container class, this time to hold login info since it'll probably
|
|
||||||
be need to passed around a lot afterwards
|
class LoginResponse(object):
|
||||||
"""
|
"""Yet another container class, this time to hold login info since it'll probably
|
||||||
class LoginResponse:
|
be need to passed around a lot afterwards
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ def login_to_minecraft(username, password):
|
|||||||
If there is an error, it will be accompanied with a `human_error` field.
|
If there is an error, it will be accompanied with a `human_error` field.
|
||||||
Otherwise `access_token`, `profile_id` and `username` fields will be present in the response.
|
Otherwise `access_token`, `profile_id` and `username` fields will be present in the response.
|
||||||
"""
|
"""
|
||||||
payload = { "username": username, "password": password, "agent": AGENT_INFO }
|
payload = {"username": username, "password": password, "agent": AGENT_INFO}
|
||||||
response = make_request(BASE_URL + "authenticate", payload)
|
response = make_request(BASE_URL + "authenticate", payload)
|
||||||
|
|
||||||
login_response = LoginResponse()
|
login_response = LoginResponse()
|
||||||
|
|||||||
+10
-11
@@ -1,13 +1,14 @@
|
|||||||
from packets import *
|
|
||||||
from start import PROTOCOL_VERSION
|
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
from types import VarInt
|
|
||||||
import threading
|
import threading
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
import select
|
import select
|
||||||
|
|
||||||
|
from packets import *
|
||||||
|
from start import PROTOCOL_VERSION
|
||||||
|
from types import VarInt
|
||||||
|
|
||||||
|
|
||||||
class Connection:
|
class Connection:
|
||||||
"""This class represents a connection to a minecraft
|
"""This class represents a connection to a minecraft
|
||||||
@@ -62,7 +63,7 @@ class Connection:
|
|||||||
self._handshake()
|
self._handshake()
|
||||||
|
|
||||||
def _connect(self):
|
def _connect(self):
|
||||||
#Connect a socket to the server and create a file object from the socket
|
# 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"
|
#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
|
#to read the number of bytes specified, the socket itself will mostly be
|
||||||
#used to write data upstream to the server
|
#used to write data upstream to the server
|
||||||
@@ -108,7 +109,7 @@ class NetworkingThread(threading.Thread):
|
|||||||
|
|
||||||
# Read and react to as many as 50 packets
|
# Read and react to as many as 50 packets
|
||||||
num_packets = 0
|
num_packets = 0
|
||||||
packet = self.connection.reactor.read_packet(self.connection.socket, self.connection.file_object)
|
packet = self.connection.reactor.read_packet(self.connection.file_object)
|
||||||
while packet:
|
while packet:
|
||||||
num_packets += 1
|
num_packets += 1
|
||||||
|
|
||||||
@@ -116,13 +117,12 @@ class NetworkingThread(threading.Thread):
|
|||||||
if num_packets >= 50:
|
if num_packets >= 50:
|
||||||
break
|
break
|
||||||
|
|
||||||
packet = self.connection.reactor.read_packet(self.connection.socket, self.connection.file_object)
|
packet = self.connection.reactor.read_packet(self.connection.file_object)
|
||||||
|
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
|
|
||||||
|
|
||||||
class PacketReactor:
|
class PacketReactor:
|
||||||
|
|
||||||
state_name = None
|
state_name = None
|
||||||
clientbound_packets = None
|
clientbound_packets = None
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ class PacketReactor:
|
|||||||
def __init__(self, connection):
|
def __init__(self, connection):
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
|
|
||||||
def read_packet(self, socket, stream):
|
def read_packet(self, stream):
|
||||||
ready = select.select([self.connection.socket], [], [], 0.5)
|
ready = select.select([self.connection.socket], [], [], 0.5)
|
||||||
if ready[0]:
|
if ready[0]:
|
||||||
length = VarInt.read(stream)
|
length = VarInt.read(stream)
|
||||||
@@ -152,17 +152,16 @@ class PacketReactor:
|
|||||||
|
|
||||||
|
|
||||||
class HandshakeReactor(PacketReactor):
|
class HandshakeReactor(PacketReactor):
|
||||||
|
|
||||||
clientbound_packets = state_handshake_clientbound
|
clientbound_packets = state_handshake_clientbound
|
||||||
|
|
||||||
|
|
||||||
class StatusReactor(PacketReactor):
|
class StatusReactor(PacketReactor):
|
||||||
|
|
||||||
clientbound_packets = state_status_clientbound
|
clientbound_packets = state_status_clientbound
|
||||||
|
|
||||||
def react(self, packet):
|
def react(self, packet):
|
||||||
if packet.name == "response":
|
if packet.id == ResponsePacket.id:
|
||||||
import json
|
import json
|
||||||
|
|
||||||
print json.loads(packet.json_response)
|
print json.loads(packet.json_response)
|
||||||
|
|
||||||
ping_packet = PingPacket()
|
ping_packet = PingPacket()
|
||||||
|
|||||||
+40
-28
@@ -1,8 +1,9 @@
|
|||||||
from types import *
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
|
from types import *
|
||||||
|
|
||||||
class PacketBuffer:
|
|
||||||
|
class PacketBuffer(object):
|
||||||
b = BytesIO()
|
b = BytesIO()
|
||||||
|
|
||||||
def send(self, value):
|
def send(self, value):
|
||||||
@@ -11,11 +12,15 @@ class PacketBuffer:
|
|||||||
def get_writable(self):
|
def get_writable(self):
|
||||||
return self.b.getvalue()
|
return self.b.getvalue()
|
||||||
|
|
||||||
class Packet:
|
|
||||||
|
class Packet(object):
|
||||||
|
|
||||||
name = "base"
|
name = "base"
|
||||||
definition = []
|
definition = []
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def read(self, file_object):
|
def read(self, file_object):
|
||||||
for field in self.definition:
|
for field in self.definition:
|
||||||
for var_name, data_type in field.iteritems():
|
for var_name, data_type in field.iteritems():
|
||||||
@@ -32,21 +37,21 @@ class Packet:
|
|||||||
data = getattr(self, var_name)
|
data = getattr(self, var_name)
|
||||||
data_type.send(data, packet_buffer)
|
data_type.send(data, packet_buffer)
|
||||||
|
|
||||||
VarInt.send(len(packet_buffer.get_writable()), socket) # Packet Size
|
VarInt.send(len(packet_buffer.get_writable()), socket) # Packet Size
|
||||||
socket.send(packet_buffer.get_writable()) # Packet Payload
|
socket.send(packet_buffer.get_writable()) # Packet Payload
|
||||||
|
|
||||||
|
|
||||||
# Handshake State
|
# Handshake State
|
||||||
#==============
|
# ==============
|
||||||
class HandShakePacket(Packet):
|
class HandShakePacket(Packet):
|
||||||
|
|
||||||
id = 0x00
|
id = 0x00
|
||||||
name = "handshake"
|
name = "handshake"
|
||||||
definition = [
|
definition = [
|
||||||
{'protocol_version': VarInt},
|
{'protocol_version': VarInt},
|
||||||
{'server_address': String},
|
{'server_address': String},
|
||||||
{'server_port': UnsignedShort},
|
{'server_port': UnsignedShort},
|
||||||
{'next_state': VarInt}]
|
{'next_state': VarInt}]
|
||||||
|
|
||||||
|
|
||||||
state_handshake_clientbound = {
|
state_handshake_clientbound = {
|
||||||
|
|
||||||
@@ -55,35 +60,41 @@ state_handshake_serverbound = {
|
|||||||
0x00: HandShakePacket
|
0x00: HandShakePacket
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Status State
|
# Status State
|
||||||
#==============
|
#==============
|
||||||
class ResponsePacket(Packet):
|
class ResponsePacket(Packet):
|
||||||
id = 0x00
|
id = 0x00
|
||||||
name = "response"
|
name = "response"
|
||||||
definition = [
|
definition = [
|
||||||
{'json_response': String}]
|
{'json_response': String}]
|
||||||
|
|
||||||
|
|
||||||
class PingPacket(Packet):
|
class PingPacket(Packet):
|
||||||
id = 0x01
|
id = 0x01
|
||||||
name = "ping"
|
name = "ping"
|
||||||
definition = [
|
definition = [
|
||||||
{'time': Long}]
|
{'time': Long}]
|
||||||
|
|
||||||
|
|
||||||
state_status_clientbound = {
|
state_status_clientbound = {
|
||||||
0x00: ResponsePacket,
|
0x00: ResponsePacket,
|
||||||
0x01: PingPacket
|
0x01: PingPacket
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class RequestPacket(Packet):
|
class RequestPacket(Packet):
|
||||||
id = 0x00
|
id = 0x00
|
||||||
name = "request"
|
name = "request"
|
||||||
definition = []
|
definition = []
|
||||||
|
|
||||||
|
|
||||||
class PingPacket(Packet):
|
class PingPacket(Packet):
|
||||||
id = 0x01
|
id = 0x01
|
||||||
name = "ping"
|
name = "ping"
|
||||||
definition = [
|
definition = [
|
||||||
{'time': Long}]
|
{'time': Long}]
|
||||||
|
|
||||||
|
|
||||||
state_status_serverbound = {
|
state_status_serverbound = {
|
||||||
0x00: RequestPacket,
|
0x00: RequestPacket,
|
||||||
@@ -93,28 +104,28 @@ state_status_serverbound = {
|
|||||||
# Login State
|
# Login State
|
||||||
#==============
|
#==============
|
||||||
class DisconnectPacket(Packet):
|
class DisconnectPacket(Packet):
|
||||||
|
|
||||||
id = 0x00
|
id = 0x00
|
||||||
name = "disconnect"
|
name = "disconnect"
|
||||||
definition = [
|
definition = [
|
||||||
{'json_data': String}]
|
{'json_data': String}]
|
||||||
|
|
||||||
|
|
||||||
class EncryptionRequestPacket(Packet):
|
class EncryptionRequestPacket(Packet):
|
||||||
|
|
||||||
id = 0x01
|
id = 0x01
|
||||||
name = "encryption request"
|
name = "encryption request"
|
||||||
definition = [
|
definition = [
|
||||||
{'server_id': String},
|
{'server_id': String},
|
||||||
{'public_key': ByteArray},
|
{'public_key': ByteArray},
|
||||||
{'verify_token': ByteArray}]
|
{'verify_token': ByteArray}]
|
||||||
|
|
||||||
|
|
||||||
class LoginSucessPacket(Packet):
|
class LoginSucessPacket(Packet):
|
||||||
|
|
||||||
id = 0x02
|
id = 0x02
|
||||||
name = "login success"
|
name = "login success"
|
||||||
definition = [
|
definition = [
|
||||||
{'UUID': String},
|
{'UUID': String},
|
||||||
{'Username': String}]
|
{'Username': String}]
|
||||||
|
|
||||||
|
|
||||||
state_login_clientbound = {
|
state_login_clientbound = {
|
||||||
0x00: DisconnectPacket,
|
0x00: DisconnectPacket,
|
||||||
@@ -122,20 +133,21 @@ state_login_clientbound = {
|
|||||||
0x02: LoginSucessPacket
|
0x02: LoginSucessPacket
|
||||||
}
|
}
|
||||||
|
|
||||||
class LoginStartPacket(Packet):
|
|
||||||
|
|
||||||
|
class LoginStartPacket(Packet):
|
||||||
id = 0x00
|
id = 0x00
|
||||||
name = "login start"
|
name = "login start"
|
||||||
definition = [
|
definition = [
|
||||||
{'name': String}]
|
{'name': String}]
|
||||||
|
|
||||||
|
|
||||||
class EncryptionResponsePacket(Packet):
|
class EncryptionResponsePacket(Packet):
|
||||||
|
|
||||||
id = 0x01
|
id = 0x01
|
||||||
name = "encryption response"
|
name = "encryption response"
|
||||||
definition = [
|
definition = [
|
||||||
{'shared_secret': ByteArray},
|
{'shared_secret': ByteArray},
|
||||||
{'verify_token': ByteArray}]
|
{'verify_token': ByteArray}]
|
||||||
|
|
||||||
|
|
||||||
state_login_serverbound = {
|
state_login_serverbound = {
|
||||||
0x00: LoginStartPacket,
|
0x00: LoginStartPacket,
|
||||||
|
|||||||
+4
-13
@@ -4,8 +4,8 @@ These definitions and methods are used by the packet definitions
|
|||||||
"""
|
"""
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
class Type:
|
|
||||||
|
|
||||||
|
class Type:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
pass
|
pass
|
||||||
@@ -14,11 +14,11 @@ class Type:
|
|||||||
def send(value, socket):
|
def send(value, socket):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
#=========================================================
|
|
||||||
|
# =========================================================
|
||||||
|
|
||||||
|
|
||||||
class Boolean(Type):
|
class Boolean(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
return struct.unpack('?', file_object.read(1))[0]
|
return struct.unpack('?', file_object.read(1))[0]
|
||||||
@@ -29,7 +29,6 @@ class Boolean(Type):
|
|||||||
|
|
||||||
|
|
||||||
class Byte(Type):
|
class Byte(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
return struct.unpack('>b', file_object.read(1))[0]
|
return struct.unpack('>b', file_object.read(1))[0]
|
||||||
@@ -40,7 +39,6 @@ class Byte(Type):
|
|||||||
|
|
||||||
|
|
||||||
class Short(Type):
|
class Short(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
return struct.unpack('>h', file_object.read(2))[0]
|
return struct.unpack('>h', file_object.read(2))[0]
|
||||||
@@ -51,7 +49,6 @@ class Short(Type):
|
|||||||
|
|
||||||
|
|
||||||
class UnsignedShort(Type):
|
class UnsignedShort(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
return struct.unpack('>H', file_object.read(2))[0]
|
return struct.unpack('>H', file_object.read(2))[0]
|
||||||
@@ -62,7 +59,6 @@ class UnsignedShort(Type):
|
|||||||
|
|
||||||
|
|
||||||
class Integer(Type):
|
class Integer(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
return struct.unpack('>i', file_object.read(4))[0]
|
return struct.unpack('>i', file_object.read(4))[0]
|
||||||
@@ -73,7 +69,6 @@ class Integer(Type):
|
|||||||
|
|
||||||
|
|
||||||
class VarInt(Type):
|
class VarInt(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read_socket(s):
|
def read_socket(s):
|
||||||
d = 0
|
d = 0
|
||||||
@@ -107,7 +102,6 @@ class VarInt(Type):
|
|||||||
|
|
||||||
|
|
||||||
class Long(Type):
|
class Long(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
return struct.unpack('>q', file_object.read(8))[0]
|
return struct.unpack('>q', file_object.read(8))[0]
|
||||||
@@ -118,7 +112,6 @@ class Long(Type):
|
|||||||
|
|
||||||
|
|
||||||
class Float(Type):
|
class Float(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
return struct.unpack('>f', file_object.read(4))[0]
|
return struct.unpack('>f', file_object.read(4))[0]
|
||||||
@@ -127,8 +120,8 @@ class Float(Type):
|
|||||||
def send(svalue, socket):
|
def send(svalue, socket):
|
||||||
socket.send(struct.pack('>f', value))
|
socket.send(struct.pack('>f', value))
|
||||||
|
|
||||||
class Double(Type):
|
|
||||||
|
|
||||||
|
class Double(Type):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
return struct.unpack('>d', file_object.read(8))[0]
|
return struct.unpack('>d', file_object.read(8))[0]
|
||||||
@@ -139,7 +132,6 @@ class Double(Type):
|
|||||||
|
|
||||||
|
|
||||||
class ByteArray(Type):
|
class ByteArray(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object, length=None):
|
def read(file_object, length=None):
|
||||||
if length is None:
|
if length is None:
|
||||||
@@ -153,7 +145,6 @@ class ByteArray(Type):
|
|||||||
|
|
||||||
|
|
||||||
class String(Type):
|
class String(Type):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(file_object):
|
def read(file_object):
|
||||||
length = VarInt.read(file_object)
|
length = VarInt.read(file_object)
|
||||||
|
|||||||
@@ -1,25 +1,27 @@
|
|||||||
import getpass
|
import getpass
|
||||||
import sys
|
import sys
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
import authentication
|
import authentication
|
||||||
|
|
||||||
|
|
||||||
PROTOCOL_VERSION = 5
|
PROTOCOL_VERSION = 5
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = OptionParser()
|
parser = OptionParser()
|
||||||
|
|
||||||
parser.add_option("-u", "--username", dest="username", default=None,
|
parser.add_option("-u", "--username", dest="username", default=None,
|
||||||
help="username to log in with")
|
help="username to log in with")
|
||||||
|
|
||||||
parser.add_option("-p", "--password", dest="password", default=None,
|
parser.add_option("-p", "--password", dest="password", default=None,
|
||||||
help="password to log in with")
|
help="password to log in with")
|
||||||
|
|
||||||
parser.add_option("-s", "--server", dest="server", default=None,
|
parser.add_option("-s", "--server", dest="server", default=None,
|
||||||
help="server to connect to")
|
help="server to connect to")
|
||||||
|
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
if not options.username:
|
if not options.username:
|
||||||
options.username = raw_input("Enter your username: ")
|
options.username = raw_input("Enter your username: ")
|
||||||
|
|
||||||
@@ -27,8 +29,9 @@ def main():
|
|||||||
options.password = getpass.getpass("Enter your password: ")
|
options.password = getpass.getpass("Enter your password: ")
|
||||||
|
|
||||||
login_response = authentication.login_to_minecraft(options.username, options.password)
|
login_response = authentication.login_to_minecraft(options.username, options.password)
|
||||||
from pprint import pprint # TODO: remove debug
|
from pprint import pprint # TODO: remove debug
|
||||||
pprint(vars(login_response)) # TODO: remove debug
|
|
||||||
|
pprint(vars(login_response)) # TODO: remove debug
|
||||||
|
|
||||||
if login_response.error:
|
if login_response.error:
|
||||||
print login_response.human_error
|
print login_response.human_error
|
||||||
@@ -48,6 +51,7 @@ def main():
|
|||||||
port = 25565
|
port = 25565
|
||||||
|
|
||||||
from network.connection import Connection
|
from network.connection import Connection
|
||||||
|
|
||||||
connection = Connection(address, port, login_response)
|
connection = Connection(address, port, login_response)
|
||||||
connection.status()
|
connection.status()
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user