2012-04-05 04:56:21 +02:00
|
|
|
import urllib
|
|
|
|
import urllib2
|
|
|
|
import getpass
|
|
|
|
import socket
|
|
|
|
import sys
|
2012-04-15 18:49:47 +02:00
|
|
|
import PacketListenerManager
|
|
|
|
import PacketSenderManager
|
2012-04-06 12:25:30 +02:00
|
|
|
import NetworkManager
|
2012-04-11 22:22:58 +02:00
|
|
|
import NoGUIstuff
|
2012-04-06 01:04:51 +02:00
|
|
|
import time
|
2012-04-05 04:56:21 +02:00
|
|
|
import threading
|
2012-04-07 16:02:46 +02:00
|
|
|
import thread
|
2012-04-06 01:04:51 +02:00
|
|
|
import wx
|
|
|
|
import wxPython
|
2012-04-05 04:56:21 +02:00
|
|
|
|
|
|
|
sessionid = ""
|
|
|
|
username = ""
|
2012-04-06 01:04:51 +02:00
|
|
|
|
|
|
|
#Eclipse pyDev error fix
|
|
|
|
wx = wx
|
|
|
|
|
|
|
|
class Window(wx.Frame):
|
|
|
|
|
|
|
|
def __init__(self, parent, title):
|
|
|
|
self.username = ""
|
|
|
|
self.sessionID = ""
|
2012-04-07 16:02:46 +02:00
|
|
|
self.password = ""
|
2012-04-06 01:04:51 +02:00
|
|
|
self.LoggedIn = False
|
|
|
|
|
|
|
|
super(Window, self).__init__(parent, title=title,
|
|
|
|
size=(400, 100))
|
|
|
|
|
2012-04-06 12:25:30 +02:00
|
|
|
self.initialize()
|
2012-04-06 01:04:51 +02:00
|
|
|
self.Show()
|
2012-04-05 04:56:21 +02:00
|
|
|
|
|
|
|
def initialize(self):
|
|
|
|
|
2012-04-06 01:04:51 +02:00
|
|
|
self.sizer = wx.GridBagSizer()
|
|
|
|
|
|
|
|
self.label = wx.StaticText(self, -1, label=u'Username')
|
|
|
|
self.sizer.Add( self.label, (0,0),(1,1), wx.EXPAND )
|
2012-04-05 04:56:21 +02:00
|
|
|
|
2012-04-07 16:02:46 +02:00
|
|
|
self.entry = wx.TextCtrl(self,-1, size=(200,23))
|
|
|
|
self.sizer.Add(self.entry,(0,1),(1, 4),wx.EXPAND | wx.ALIGN_LEFT)
|
2012-04-06 01:04:51 +02:00
|
|
|
self.Bind(wx.EVT_TEXT_ENTER, self.onPressEnterOnFields, self.entry)
|
2012-04-05 04:56:21 +02:00
|
|
|
|
2012-04-06 01:04:51 +02:00
|
|
|
self.label2 = wx.StaticText(self, -1, label=u'Password')
|
|
|
|
self.sizer.Add( self.label2, (2,0),(1,2), wx.EXPAND)
|
|
|
|
|
2012-04-07 16:02:46 +02:00
|
|
|
self.entry2 = wx.TextCtrl(self, -1, size=(200,23), style = wx.TE_PASSWORD)
|
|
|
|
self.sizer.Add(self.entry2,(2,2),(2,4), wx.EXPAND | wx.ALIGN_LEFT)
|
2012-04-06 01:04:51 +02:00
|
|
|
self.Bind(wx.EVT_TEXT_ENTER, self.onPressEnterOnFields, self.entry2)
|
|
|
|
|
|
|
|
button = wx.Button(self,-1,label="Login")
|
|
|
|
self.sizer.Add(button, (5,0))
|
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, button)
|
|
|
|
|
|
|
|
self.status = wx.StaticText(self, -1,)
|
2012-04-07 16:02:46 +02:00
|
|
|
self.sizer.Add(self.status,(5,2),(5,15), wx.EXPAND)
|
2012-04-06 01:04:51 +02:00
|
|
|
|
|
|
|
self.sizer.AddGrowableCol(0)
|
|
|
|
self.SetSizerAndFit(self.sizer)
|
|
|
|
|
|
|
|
def InitializeServerBrowser(self):
|
|
|
|
|
|
|
|
self.sizer = wx.GridBagSizer()
|
2012-04-05 04:56:21 +02:00
|
|
|
|
|
|
|
#Top label
|
2012-04-06 01:04:51 +02:00
|
|
|
self.topLabel = wx.StaticText(self, -1, label=u'Logged in as ' + self.username + '! (Session id: ' + self.sessionID + ')')
|
|
|
|
self.sizer.Add(self.topLabel, (0,0),(1,1), wx.EXPAND)
|
2012-04-05 04:56:21 +02:00
|
|
|
#Top label
|
|
|
|
|
|
|
|
#Address label
|
2012-04-06 01:04:51 +02:00
|
|
|
self.addressLabel = wx.StaticText(self, -1, label=u'Server address')
|
|
|
|
self.sizer.Add(self.addressLabel, (2,0), (1,2), wx.EXPAND)
|
2012-04-05 04:56:21 +02:00
|
|
|
#Address lable
|
|
|
|
|
|
|
|
#Address entry box
|
2012-04-06 01:04:51 +02:00
|
|
|
self.AddressEntry = wx.TextCtrl(self, -1)
|
|
|
|
self.sizer.Add(self.AddressEntry,(2,2),(2,2), wx.EXPAND)
|
2012-04-06 12:25:30 +02:00
|
|
|
self.Bind(wx.EVT_TEXT_ENTER, self.onConnectClick, self.AddressEntry)
|
2012-04-05 04:56:21 +02:00
|
|
|
#Address entry box
|
|
|
|
|
2012-04-06 01:04:51 +02:00
|
|
|
self.connectbutton = wx.Button(self, -1, label="Connect")
|
|
|
|
self.sizer.Add(self.connectbutton,(4,0))
|
2012-04-06 12:25:30 +02:00
|
|
|
self.Bind(wx.EVT_BUTTON, self.onConnectClick, self.connectbutton)
|
2012-04-06 01:04:51 +02:00
|
|
|
|
|
|
|
self.sizer.AddGrowableCol(0)
|
|
|
|
self.SetSizerAndFit(self.sizer)
|
|
|
|
|
2012-04-06 12:25:30 +02:00
|
|
|
def onConnectClick(self, event):
|
2012-04-07 16:02:46 +02:00
|
|
|
StuffEnteredIntoBox = self.AddressEntry.GetValue()
|
2012-04-10 21:14:32 +02:00
|
|
|
if(StuffEnteredIntoBox == ""):
|
|
|
|
self.topLabel.SetLabel("Logged in as " + self.username + "! Enter a server address")
|
2012-04-06 12:25:30 +02:00
|
|
|
self.sizer.DeleteWindows()
|
|
|
|
self.sizer = wx.GridBagSizer()
|
|
|
|
self.connectStatus = wx.StaticText(self, -1, label=u'Connecting ...')
|
|
|
|
self.connectStatus.SetForegroundColour(wx.BLUE)
|
|
|
|
self.sizer.Add(self.connectStatus, (0,0), (0,0))
|
|
|
|
self.connectStatus.Center()
|
2012-04-07 16:02:46 +02:00
|
|
|
if ':' in StuffEnteredIntoBox:
|
|
|
|
StuffEnteredIntoBox = StuffEnteredIntoBox.split(":")
|
|
|
|
host = StuffEnteredIntoBox[0]
|
2012-04-14 17:29:04 +02:00
|
|
|
port = int(StuffEnteredIntoBox[1])
|
2012-04-07 16:02:46 +02:00
|
|
|
else:
|
|
|
|
host = StuffEnteredIntoBox
|
|
|
|
port = 25565
|
|
|
|
self.connection = NetworkManager.ServerConnection(self, self.username, self.password, self.sessionID, host, port)
|
2012-04-11 22:22:58 +02:00
|
|
|
self.connection.start()
|
2012-04-06 12:25:30 +02:00
|
|
|
|
2012-04-06 01:04:51 +02:00
|
|
|
def OnButtonClick(self, event):
|
|
|
|
if(self.entry.GetValue() == ""):
|
2012-04-07 16:02:46 +02:00
|
|
|
self.status.SetLabel("Enter a username sherlock")
|
2012-04-05 04:56:21 +02:00
|
|
|
return
|
2012-04-06 01:04:51 +02:00
|
|
|
if(self.entry2.GetValue() == ""):
|
2012-04-07 16:02:46 +02:00
|
|
|
self.status.SetLabel("Enter a password you derp")
|
2012-04-05 04:56:21 +02:00
|
|
|
return
|
2012-04-06 01:04:51 +02:00
|
|
|
password = self.entry2.GetValue()
|
|
|
|
username = self.entry.GetValue()
|
|
|
|
self.status.SetForegroundColour(wx.BLUE)
|
|
|
|
self.status.SetLabel("Logging in...")
|
2012-04-05 04:56:21 +02:00
|
|
|
thread = MinecraftLoginThread(self, username, password)
|
|
|
|
thread.start()
|
2012-04-06 01:04:51 +02:00
|
|
|
while(thread.is_alive() == True):
|
|
|
|
pass
|
|
|
|
self.sizer.DeleteWindows()
|
|
|
|
self.InitializeServerBrowser()
|
2012-04-05 04:56:21 +02:00
|
|
|
|
|
|
|
def onPressEnterOnFields(self, event):
|
2012-04-06 01:04:51 +02:00
|
|
|
if(self.entry.GetValue() == ""):
|
2012-04-07 16:02:46 +02:00
|
|
|
self.status.SetLabel("Enter a username sherlock")
|
2012-04-05 04:56:21 +02:00
|
|
|
return
|
2012-04-06 01:04:51 +02:00
|
|
|
if(self.entry2.GetValue() == ""):
|
2012-04-07 16:02:46 +02:00
|
|
|
self.status.SetLabel("Enter a password you derp")
|
2012-04-05 04:56:21 +02:00
|
|
|
return
|
2012-04-06 01:04:51 +02:00
|
|
|
password = self.entry2.GetValue()
|
|
|
|
username = self.entry.GetValue()
|
|
|
|
self.status.SetForegroundColour(wx.BLUE)
|
|
|
|
self.status.SetLabel("Logging in...")
|
2012-04-05 04:56:21 +02:00
|
|
|
thread = MinecraftLoginThread(self, username, password)
|
|
|
|
thread.start()
|
2012-04-06 01:04:51 +02:00
|
|
|
while(thread.is_alive() == True):
|
|
|
|
pass
|
|
|
|
self.sizer.DeleteWindows()
|
|
|
|
self.InitializeServerBrowser()
|
2012-04-05 04:56:21 +02:00
|
|
|
|
|
|
|
class MinecraftLoginThread(threading.Thread):
|
|
|
|
|
|
|
|
def __init__(self, window, username, password):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.window = window
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
url = 'https://login.minecraft.net'
|
|
|
|
header = {'Content-Type' : 'application/x-www-form-urlencoded'}
|
|
|
|
data = {'user' : self.username,
|
|
|
|
'password' : self.password,
|
|
|
|
'version' : '13'}
|
2012-04-06 12:25:30 +02:00
|
|
|
try:
|
|
|
|
data = urllib.urlencode(data)
|
|
|
|
req = urllib2.Request(url, data, header)
|
|
|
|
opener = urllib2.build_opener()
|
|
|
|
response = opener.open(req)
|
|
|
|
response = response.read()
|
|
|
|
except urllib2.URLError:
|
|
|
|
popup = wx.MessageBox('Connection to minecraft.net failed', 'Warning',
|
|
|
|
wx.OK | wx.ICON_ERROR)
|
|
|
|
popup.ShowModal()
|
|
|
|
return
|
2012-04-05 04:56:21 +02:00
|
|
|
if(response == "Bad login"):
|
2012-04-06 01:04:51 +02:00
|
|
|
self.window.status.SetForegroundColour(wx.RED)
|
|
|
|
self.window.status.SetLabel("Incorrect username or password!")
|
2012-04-05 04:56:21 +02:00
|
|
|
return
|
|
|
|
response = response.split(":")
|
2012-04-06 01:04:51 +02:00
|
|
|
self.window.username = response[2]
|
2012-04-07 16:02:46 +02:00
|
|
|
self.window.password = self.password
|
2012-04-06 01:04:51 +02:00
|
|
|
self.window.sessionID = response[3]
|
|
|
|
self.window.LoggedIn = True
|
|
|
|
KeepConnectionAlive(self.username, self.password).start()
|
|
|
|
|
|
|
|
class KeepConnectionAlive(threading.Thread):
|
|
|
|
|
|
|
|
def __init__(self, username, password):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
|
|
|
|
def run(self):
|
2012-04-07 16:02:46 +02:00
|
|
|
while True:
|
|
|
|
time.sleep(300)
|
2012-04-06 01:04:51 +02:00
|
|
|
url = 'https://login.minecraft.net'
|
|
|
|
header = {'Content-Type' : 'application/x-www-form-urlencoded'}
|
|
|
|
data = {'user' : self.username,
|
|
|
|
'password' : self.password,
|
|
|
|
'version' : '13'}
|
2012-04-06 12:25:30 +02:00
|
|
|
try:
|
|
|
|
data = urllib.urlencode(data)
|
|
|
|
req = urllib2.Request(url, data, header)
|
|
|
|
opener = urllib2.build_opener()
|
|
|
|
opener.open(req)
|
|
|
|
except urllib2.URLError:
|
|
|
|
popup = wx.MessageBox('Keep alive to minecraft.net failed', 'Warning',
|
|
|
|
wx.OK | wx.ICON_ERROR)
|
|
|
|
popup.ShowModal()
|
2012-04-05 04:56:21 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2012-04-11 22:22:58 +02:00
|
|
|
if (len(sys.argv) > 1):
|
|
|
|
if(sys.argv[1] == "nogui"):
|
|
|
|
user = raw_input("Enter your username: ")
|
|
|
|
passwd = getpass.getpass("Enter your password: ")
|
|
|
|
derp = NoGUIstuff.loginToMinecraft(user, passwd)
|
|
|
|
if(derp['Response'] == "Incorrect username/password" or derp['Response'] == "Can't connect to minecraft.net"):
|
|
|
|
print derp['Response']
|
|
|
|
sys.exit()
|
|
|
|
sessionid = derp['SessionID']
|
|
|
|
print "Logged in as " + derp['Username'] + "! Your session id is: " + sessionid
|
|
|
|
stuff = raw_input("Enter host and port if any: ")
|
|
|
|
if ':' in stuff:
|
|
|
|
StuffEnteredIntoBox = stuff.split(":")
|
2012-04-14 17:29:04 +02:00
|
|
|
host = StuffEnteredIntoBox[0]
|
|
|
|
port = int(StuffEnteredIntoBox[1])
|
2012-04-11 22:22:58 +02:00
|
|
|
else:
|
|
|
|
host = stuff
|
|
|
|
port = 25565
|
|
|
|
connection = NetworkManager.ServerConnection(None, derp['Username'], passwd, sessionid, host, port)
|
|
|
|
connection.start()
|
2012-04-15 18:49:47 +02:00
|
|
|
raw_input()
|
|
|
|
PacketSenderManager.send03(connection.grabSocket(), "/report Test report from pyCraft, hope this works :3")
|
2012-04-11 22:22:58 +02:00
|
|
|
else:
|
|
|
|
app = wx.App(0)
|
|
|
|
Window(None, title='pyCraft')
|
|
|
|
app.MainLoop()
|
2012-04-05 04:56:21 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
url = 'https://login.minecraft.net'
|
|
|
|
header = {'Content-Type' : 'application/x-www-form-urlencoded'}
|
|
|
|
username = raw_input("Enter your username: ")
|
|
|
|
password = getpass.getpass("Enter your password: ")
|
|
|
|
data = {'user' : username,
|
|
|
|
'password' : password,
|
|
|
|
'version' : '13'}
|
|
|
|
data = urllib.urlencode(data)
|
|
|
|
req = urllib2.Request(url, data, header)
|
|
|
|
opener = urllib2.build_opener()
|
|
|
|
response = opener.open(req)
|
|
|
|
response = response.read()
|
|
|
|
response = response.split(":")
|
|
|
|
sessionid = response[3]
|
|
|
|
print "Your session id is: " + sessionid
|
|
|
|
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
|
|
|
|
host = raw_input("Please enter host: ");
|
|
|
|
port = raw_input("Please enter port (return for 25565): ");
|
|
|
|
if(port == ""):
|
|
|
|
port = 25565
|
|
|
|
else:
|
|
|
|
port = int(port)
|
|
|
|
mySocket.connect ( ( host, port ) )
|
|
|
|
PacketManager.sendString("\x02", username + host + ":" + str(port), mySocket)
|
|
|
|
response = PacketManager.readStringFromSocket(mySocket)
|
|
|
|
print "Server id is: " + response['string']
|
|
|
|
serverid = response['string']
|
|
|
|
url = "http://session.minecraft.net/game/joinserver.jsp?user=" + username + "&sessionId=" + sessionid + "&serverId=" + serverid
|
|
|
|
response = urllib2.urlopen(url).read()
|
|
|
|
print "Response: " + response
|
|
|
|
if(response != "OK"):
|
|
|
|
print "OH GOD RESPONSE IS NOT OK. QUITING NOW."
|
|
|
|
sys.exit()
|
|
|
|
PacketManager.sendLoginRequest(mySocket, username)
|
|
|
|
while True:
|
|
|
|
PacketManager.handleIncomingPacket(mySocket)
|
|
|
|
"""
|
|
|
|
|