2017-04-06 14:45:14 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2012-04-05 04:56:21 +02:00
|
|
|
import getpass
|
|
|
|
import sys
|
2018-05-18 12:56:54 +02:00
|
|
|
import re
|
2012-10-26 02:02:42 +02:00
|
|
|
from optparse import OptionParser
|
2014-10-08 19:12:37 +02:00
|
|
|
|
2015-04-01 17:43:16 +02:00
|
|
|
from minecraft import authentication
|
2015-04-01 23:38:10 +02:00
|
|
|
from minecraft.exceptions import YggdrasilError
|
2015-04-01 17:19:12 +02:00
|
|
|
from minecraft.networking.connection import Connection
|
2017-08-21 22:06:39 +02:00
|
|
|
from minecraft.networking.packets import Packet, clientbound, serverbound
|
2012-04-30 19:05:06 +02:00
|
|
|
|
2014-10-08 19:12:37 +02:00
|
|
|
|
2015-04-01 23:38:10 +02:00
|
|
|
def get_options():
|
2012-10-26 02:02:42 +02:00
|
|
|
parser = OptionParser()
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
parser.add_option("-u", "--username", dest="username", default=None,
|
2014-10-08 19:12:37 +02:00
|
|
|
help="username to log in with")
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
parser.add_option("-p", "--password", dest="password", default=None,
|
2014-10-08 19:12:37 +02:00
|
|
|
help="password to log in with")
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
parser.add_option("-s", "--server", dest="server", default=None,
|
2018-05-18 12:56:54 +02:00
|
|
|
help="server host or host:port "
|
|
|
|
"(enclose IPv6 addresses in square brackets)")
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2016-06-18 21:12:01 +02:00
|
|
|
parser.add_option("-o", "--offline", dest="offline", action="store_true",
|
2018-05-18 12:56:54 +02:00
|
|
|
help="connect to a server in offline mode "
|
|
|
|
"(no password required)")
|
2016-06-18 21:12:01 +02:00
|
|
|
|
2017-08-21 22:06:39 +02:00
|
|
|
parser.add_option("-d", "--dump-packets", dest="dump_packets",
|
|
|
|
action="store_true",
|
|
|
|
help="print sent and received packets to standard error")
|
|
|
|
|
2020-06-19 01:18:29 +02:00
|
|
|
parser.add_option("-v", "--dump-unknown-packets", dest="dump_unknown",
|
|
|
|
action="store_true",
|
|
|
|
help="include unknown packets in --dump-packets output")
|
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
(options, args) = parser.parse_args()
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
if not options.username:
|
2015-04-06 02:11:26 +02:00
|
|
|
options.username = input("Enter your username: ")
|
2012-12-31 03:20:06 +01:00
|
|
|
|
2016-06-18 21:12:01 +02:00
|
|
|
if not options.password and not options.offline:
|
2018-05-18 12:56:54 +02:00
|
|
|
options.password = getpass.getpass("Enter your password (leave "
|
|
|
|
"blank for offline mode): ")
|
|
|
|
options.offline = options.offline or (options.password == "")
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
if not options.server:
|
2018-05-18 12:56:54 +02:00
|
|
|
options.server = input("Enter server host or host:port "
|
|
|
|
"(enclose IPv6 addresses in square brackets): ")
|
2014-06-07 01:47:34 +02:00
|
|
|
# Try to split out port and address
|
2018-05-18 12:56:54 +02:00
|
|
|
match = re.match(r"((?P<host>[^\[\]:]+)|\[(?P<addr>[^\[\]]+)\])"
|
|
|
|
r"(:(?P<port>\d+))?$", options.server)
|
|
|
|
if match is None:
|
|
|
|
raise ValueError("Invalid server address: '%s'." % options.server)
|
|
|
|
options.address = match.group("host") or match.group("addr")
|
|
|
|
options.port = int(match.group("port") or 25565)
|
2015-04-01 23:38:10 +02:00
|
|
|
|
|
|
|
return options
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
options = get_options()
|
|
|
|
|
2016-06-18 21:12:01 +02:00
|
|
|
if options.offline:
|
2017-09-24 06:51:28 +02:00
|
|
|
print("Connecting in offline mode...")
|
2016-06-18 21:12:01 +02:00
|
|
|
connection = Connection(
|
|
|
|
options.address, options.port, username=options.username)
|
|
|
|
else:
|
|
|
|
auth_token = authentication.AuthenticationToken()
|
|
|
|
try:
|
|
|
|
auth_token.authenticate(options.username, options.password)
|
|
|
|
except YggdrasilError as e:
|
|
|
|
print(e)
|
|
|
|
sys.exit()
|
2017-09-24 06:51:28 +02:00
|
|
|
print("Logged in as %s..." % auth_token.username)
|
2016-06-18 21:12:01 +02:00
|
|
|
connection = Connection(
|
|
|
|
options.address, options.port, auth_token=auth_token)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2017-08-21 22:06:39 +02:00
|
|
|
if options.dump_packets:
|
|
|
|
def print_incoming(packet):
|
|
|
|
if type(packet) is Packet:
|
|
|
|
# This is a direct instance of the base Packet type, meaning
|
2020-08-17 05:39:48 +02:00
|
|
|
# that it is a packet of unknown type, so we do not print it
|
|
|
|
# unless explicitly requested by the user.
|
2020-06-19 01:18:29 +02:00
|
|
|
if options.dump_unknown:
|
2020-08-17 05:39:48 +02:00
|
|
|
print('--> [unknown packet] %s' % packet, file=sys.stderr)
|
|
|
|
else:
|
|
|
|
print('--> %s' % packet, file=sys.stderr)
|
2017-08-21 22:06:39 +02:00
|
|
|
|
|
|
|
def print_outgoing(packet):
|
|
|
|
print('<-- %s' % packet, file=sys.stderr)
|
|
|
|
|
|
|
|
connection.register_packet_listener(
|
|
|
|
print_incoming, Packet, early=True)
|
|
|
|
connection.register_packet_listener(
|
|
|
|
print_outgoing, Packet, outgoing=True)
|
|
|
|
|
2017-09-24 06:51:28 +02:00
|
|
|
def handle_join_game(join_game_packet):
|
|
|
|
print('Connected.')
|
|
|
|
|
|
|
|
connection.register_packet_listener(
|
|
|
|
handle_join_game, clientbound.play.JoinGamePacket)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2015-03-22 15:05:55 +01:00
|
|
|
def print_chat(chat_packet):
|
2017-09-24 06:51:28 +02:00
|
|
|
print("Message (%s): %s" % (
|
|
|
|
chat_packet.field_string('position'), chat_packet.json_data))
|
2015-03-22 15:05:55 +01:00
|
|
|
|
2017-08-21 22:06:39 +02:00
|
|
|
connection.register_packet_listener(
|
|
|
|
print_chat, clientbound.play.ChatMessagePacket)
|
2017-09-24 06:51:28 +02:00
|
|
|
|
|
|
|
connection.connect()
|
|
|
|
|
2012-10-26 15:09:16 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2015-04-06 02:11:26 +02:00
|
|
|
text = input()
|
2017-07-31 14:34:03 +02:00
|
|
|
if text == "/respawn":
|
|
|
|
print("respawning...")
|
2017-08-21 22:06:39 +02:00
|
|
|
packet = serverbound.play.ClientStatusPacket()
|
|
|
|
packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN
|
2017-07-31 14:34:03 +02:00
|
|
|
connection.write_packet(packet)
|
|
|
|
else:
|
2017-08-21 22:06:39 +02:00
|
|
|
packet = serverbound.play.ChatPacket()
|
2017-07-31 14:34:03 +02:00
|
|
|
packet.message = text
|
|
|
|
connection.write_packet(packet)
|
2014-06-07 01:47:34 +02:00
|
|
|
except KeyboardInterrupt:
|
2015-04-01 23:38:10 +02:00
|
|
|
print("Bye!")
|
2014-06-07 01:47:34 +02:00
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-04-06 02:11:26 +02:00
|
|
|
main()
|