pyCraft/networking/NetworkManager.py

353 lines
16 KiB
Python
Raw Normal View History

2012-04-07 16:02:46 +02:00
import socket
2012-04-15 18:49:47 +02:00
import PacketListenerManager
2012-04-07 16:02:46 +02:00
import urllib2
import urllib
2012-04-07 16:02:46 +02:00
import traceback
2012-04-10 21:14:32 +02:00
import threading
import hashlib
import string
import Utils
from networking import PacketSenderManager
from Crypto.Random import _UserFriendlyRNG
from Crypto.Util import asn1
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES
from Crypto.Cipher import PKCS1_v1_5
try:
import wx
#Eclipse pyDev error fix
wx=wx
except ImportError:
pass
2012-04-07 16:02:46 +02:00
EntityID = 0
class ServerConnection(threading.Thread):
2012-04-07 16:02:46 +02:00
def __init__(self, window, username, password, sessionID, server, port):
threading.Thread.__init__(self)
2012-04-07 16:02:46 +02:00
self.username = username
self.password = password
self.sessionID = sessionID
self.server = server
self.port = port
if(window == None):
self.NoGUI = True
2012-05-31 14:50:01 +02:00
else:
self.NoGUI = False
2012-04-07 16:02:46 +02:00
self.window = window
2012-07-17 23:42:42 +02:00
def setWindow(self, window):
self.window = window
2012-04-15 18:49:47 +02:00
def grabSocket(self):
return self.socket
def run(self):
2012-04-07 16:02:46 +02:00
try:
#Create the socket and fileobject
self.socket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
2012-04-07 16:02:46 +02:00
self.socket.connect ( ( self.server, self.port ) )
2012-04-15 16:51:00 +02:00
self.FileObject = self.socket.makefile()
#Send out the handshake packet
PacketSenderManager.sendHandshake(self.socket, self.username, self.server, self.port)
#Receive the encryption packet id
packetid = self.socket.recv(1)
#Sanity check the packet id
if (packetid != "\xFD"):
if(self.NoGUI == False):
self.window.ConnectPanel.Status.SetFont(wx.Font(15, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Minecraft"))
self.window.ConnectPanel.Status.SetLabel("Server responded with malformed packet")
else:
print "Server responded with malformed packet"
return False
#Parse the packet
packetFD = PacketListenerManager.handleFD(self.FileObject)
#Import the server's public key
self.pubkey = RSA.importKey(packetFD['Public Key'])
#Generate a 16 byte (128 bit) shared secret
self.sharedSecret = _UserFriendlyRNG.get_random_bytes(16)
#Grab the server id
sha1 = hashlib.sha1()
sha1.update(packetFD['ServerID'])
sha1.update(self.sharedSecret)
sha1.update(packetFD['Public Key'])
#lovely java style hex digest by SirCmpwn
sha1 = sha1.hexdigest()
negative = (int(sha1[0], 16) & 0x80) == 0x80
if(negative):
sha1 = Utils.TwosCompliment(sha1.digest())
#else:
# sha1 = sha1.digest()
Utils.trimStart(str(sha1), '0')
if (negative):
sha1 = '-' + sha1
serverid = sha1
#Authenticate the server from sessions.minecraft.net
2012-04-07 16:02:46 +02:00
if(serverid != '-'):
2012-07-17 23:42:42 +02:00
try:
#Open up the url with the appropriate get parameters
2012-07-17 23:42:42 +02:00
url = "http://session.minecraft.net/game/joinserver.jsp?user=" + self.username + "&sessionId=" + self.sessionID + "&serverId=" + serverid
response = urllib2.urlopen(url).read()
2012-07-17 23:42:42 +02:00
if(response != "OK"):
#handle gui errors
2012-07-17 23:42:42 +02:00
if(self.NoGUI == False):
self.window.ConnectPanel.Status.SetFont(wx.Font(15, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Minecraft"))
self.window.ConnectPanel.Status.SetLabel("Response from sessions.minecraft.net wasn't OK")
else:
print "Response from sessions.minecraft.net wasn't OK, it was " + response
return False
#Success \o/ We can now begin sending our stuff to the server
#Instantiate our main packet listener
PacketListener(self, self.window, self.socket, self.FileObject).start()
#Encrypt the verification token from earlier along with our shared secret with the server's rsa key
self.RSACipher = PKCS1_v1_5.new(self.pubkey)
encryptedSanityToken = self.RSACipher.encrypt(str(packetFD['Token']))
encryptedSharedSecret = self.RSACipher.encrypt(str(self.sharedSecret))
#Send out a a packet FC to the server
PacketSenderManager.sendFC(self.socket, encryptedSharedSecret, encryptedSanityToken)
#GUI handling
if(self.NoGUI == False):
2012-07-17 23:42:42 +02:00
self.window.ConnectPanel.callbackAfterConnect()
#Wait for server screen to be ready
2012-07-17 23:42:42 +02:00
while(not hasattr(self.window, 'text')):
continue
2012-07-17 23:42:42 +02:00
except Exception, e:
#handle gui errors
2012-07-17 23:42:42 +02:00
if(self.NoGUI == False and hasattr(self.window, 'ConnectPanel')):
self.window.ConnectPanel.Status.SetForegroundColour(wx.RED)
self.window.ConnectPanel.Status.SetLabel("Conection to sessions.mc.net failed")
self.window.ConnectPanel.RotationThread.Kill = True
traceback.print_exc()
else:
2012-05-31 14:50:01 +02:00
if(self.NoGUI):
print "Server is in offline mode"
#TODO: handle offline mod servers
2012-04-07 16:02:46 +02:00
except Exception, e:
2012-07-17 23:42:42 +02:00
if(self.NoGUI == False and self.window):
2012-05-31 14:50:01 +02:00
self.window.ConnectPanel.Status.SetForegroundColour(wx.RED)
self.window.ConnectPanel.Status.SetLabel("Connection to server failed")
self.window.ConnectPanel.RotationThread.Kill = True
else:
print "Connection to server failed"
2012-04-07 16:02:46 +02:00
traceback.print_exc()
return False
2012-07-17 23:42:42 +02:00
#self.window.Status.SetLabel("Connected to " + self.server + "!")
class EncryptedFileObjectHandler():
def __init__(self, fileobject, cipher):
self.fileobject = fileobject
self.cipher = cipher
def read(self, length):
rawData = self.fileobject.read(length)
unencryptedData = self.cipher.decrypt(rawData)
return unencryptedData
class EncryptedSocketObjectHandler():
def __init__(self, socket, cipher):
self.socket = socket
self.cipher = cipher
def send(self, stuff):
self.socket.send(self.cipher.encrypt(stuff))
def close(self):
self.socket.close()
2012-04-15 18:49:47 +02:00
2012-04-10 21:14:32 +02:00
class PacketListener(threading.Thread):
2012-04-15 18:49:47 +02:00
def __init__(self, connection, window, socket, FileObject):
2012-04-10 21:14:32 +02:00
threading.Thread.__init__(self)
self.connection = connection
2012-04-10 21:14:32 +02:00
self.socket = socket
2012-04-15 16:51:00 +02:00
self.FileObject = FileObject
self.window = window
self.encryptedConnection = False
def enableEncryption(self):
#Create an AES cipher from the previously obtained public key
self.cipher = AES.new(self.connection.sharedSecret, AES.MODE_CFB, IV=self.connection.sharedSecret)
self.decipher = AES.new(self.connection.sharedSecret, AES.MODE_CFB, IV=self.connection.sharedSecret)
self.rawsocket = self.socket
self.socket = EncryptedSocketObjectHandler(self.rawsocket, self.cipher)
self.rawFileObject = self.FileObject
self.FileObject = EncryptedFileObjectHandler(self.rawFileObject, self.decipher)
self.encryptedConnection = True
2012-04-10 21:14:32 +02:00
def run(self):
while True:
try:
response = self.FileObject.read(1)
if (response == ""):
continue
2012-07-17 23:42:42 +02:00
except Exception, e:
if(self.window):
self.window.Status.SetLabel("Ping timeout")
else:
print "Ping timeout"
traceback.print_exc()
2012-04-10 21:14:32 +02:00
break
if(response == "\x00"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle00(self.FileObject, self.socket)
elif(response == "\x01"):
packet01 = PacketListenerManager.handle01(self.FileObject)
print "Logged in \o/ Received an entity id of " + str(packet01['EntityID'])
elif(response == "\x03"):
2012-05-31 14:50:01 +02:00
message = PacketListenerManager.handle03(self.FileObject)
if(self.connection.NoGUI):
filtered_string = filter(lambda x: x in string.printable, message)
#print message.replace(u'\xa7', '&')
print filtered_string
2012-07-17 23:42:42 +02:00
elif(self.window):
self.window.handleChat(message)
elif(response == "\x04"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle04(self.FileObject)
elif(response == "\x05"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle05(self.FileObject)
elif(response == "\x06"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle06(self.FileObject)
elif(response == "\x07"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle07(self.FileObject)
elif(response == "\x08"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle08(self.FileObject)
elif(response == "\x09"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle09(self.FileObject)
elif(response == "\x0D"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle0D(self.FileObject)
elif(response == "\x11"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle11(self.FileObject)
elif(response == "\x12"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle12(self.FileObject)
elif(response == "\x14"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle14(self.FileObject)
elif(response == "\x15"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle15(self.FileObject)
elif(response == "\x16"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle16(self.FileObject)
elif(response == "\x17"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle17(self.FileObject)
elif(response == "\x18"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle18(self.FileObject)
elif(response == "\x19"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle19(self.FileObject)
elif(response == "\x1A"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1A(self.FileObject)
elif(response == "\x1C"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1C(self.FileObject)
elif(response == "\x1D"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1D(self.FileObject)
elif(response == "\x1E"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1E(self.FileObject)
elif(response == "\x1F"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle1F(self.FileObject)
elif(response == "\x20"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle20(self.FileObject)
elif(response == "\x21"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle21(self.FileObject)
elif(response == "\x22"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle22(self.FileObject)
elif(response == "\x23"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle23(self.FileObject)
elif(response == "\x26"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle26(self.FileObject)
elif(response == "\x27"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle27(self.FileObject)
elif(response == "\x28"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle28(self.FileObject)
elif(response == "\x29"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle29(self.FileObject)
elif(response == "\x2A"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle2A(self.FileObject)
elif(response == "\x2B"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle2B(self.FileObject)
elif(response == "\x33"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle33(self.FileObject)
elif(response == "\x34"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle34(self.FileObject)
elif(response == "\x35"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle35(self.FileObject)
elif(response == "\x36"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle36(self.FileObject)
elif(response == "\x37"):
PacketListenerManager.handle37(self.FileObject)
elif(response == "\x38"):
PacketListenerManager.handle38(self.FileObject)
elif(response == "\x3C"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle3C(self.FileObject)
elif(response == "\x3D"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle3D(self.FileObject)
elif(response == "\x3E"):
PacketListenerManager.handle3E(self.FileObject)
elif(response == "\x46"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle46(self.FileObject)
elif(response == "\x47"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle47(self.FileObject)
elif(response == "\x64"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle64(self.FileObject)
elif(response == "\x65"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle65(self.FileObject)
elif(response == "\x67"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle67(self.FileObject)
elif(response == "\x68"):
2012-09-06 12:08:03 +02:00
PacketListenerManager.handle68(self.FileObject)
elif(response == "\x69"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle69(self.FileObject)
elif(response == "\x6A"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle6A(self.FileObject)
elif(response == "\x6B"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle6B(self.FileObject)
elif(response == "\x82"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle82(self.FileObject)
elif(response == "\x83"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle83(self.FileObject)
elif(response == "\x84"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handle84(self.FileObject)
elif(response == "\xC8"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handleC8(self.FileObject)
elif(response == "\xC9"):
2012-09-06 12:08:03 +02:00
PacketListenerManager.handleC9(self.FileObject)
elif(response == "\xCA"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handleCA(self.FileObject)
elif(response == "\xCB"):
PacketListenerManager.handleCB(self.FileObject)
elif(response == "\xFA"):
2012-04-15 18:49:47 +02:00
PacketListenerManager.handleFA(self.FileObject)
elif(response == "\xFC"):
PacketListenerManager.handleFC(self.FileObject)
if (not self.encryptedConnection):
self.enableEncryption()
PacketSenderManager.sendCD(self.socket, 0)
elif(response == "\xFF"):
2012-04-15 18:49:47 +02:00
DisconMessage = PacketListenerManager.handleFF(self.FileObject)
2012-04-14 17:29:04 +02:00
if(self.window == None):
print "Disconnected: " + DisconMessage
if(self.window):
2012-07-17 23:42:42 +02:00
if(hasattr(self.window, 'ChatPanel')):
self.window.ChatPanel.Status.SetLabel("Disconnected: " + DisconMessage)
if(hasattr(self.window, 'Status')):
2012-07-17 23:42:42 +02:00
self.window.Status.SetLabel("Disconnected: " + DisconMessage)
self.socket.close()
2012-04-14 17:29:04 +02:00
break
else:
if(self.window == None):
print "Protocol error: " + hex(ord(response))
self.socket.close()
break