# Path: twistedbot/packets.py
# def ucs2(name):
#     def ucs2_encode(data, errors="replace"):
#         def encode(self, input, final=False):
#     def _encode(self, obj, context):
# def AlphaString(name):
# def Bool(*args, **kwargs):
# def ByteString(name, size_name, encoding=None):
#     def _decode(self, obj, context):
# def NBTdata(name, size_name):
# def itemstack_as_slotdata(itemstack):
#     def _decode(self, obj, context):
#     def _encode(self, obj, context):
# def packet_stream_print_header(context):
# def print_and_return(pv, rv):
# def parse_packets(bytestream):
# def parse_packets_incrementally(bytestream):
# def make_packet(packet, payload, template=None):
# def make_error_packet(message):
#     class UCS2IncrementalEncoder(IncrementalEncoder):
#     class UCS2IncrementalDecoder(BufferedIncrementalDecoder):
#     class UCS2StreamWriter(StreamWriter):
#     class UCS2StreamReader(StreamReader):
# class DoubleAdapter(LengthValueAdapter):
# class NBTAdapter(Adapter):
# class MetadataAdapter(Adapter):
# 
# Path: twistedbot/encryption.py
# def get_random_bytes(size=16):
# def make_aes(aes_key, aes_iv, segment_size=8):
# def gen_rsa_key(size=1024):
# def export_pubkey(key):
# def load_pubkey(s):
# def encrypt(message, pubkey):
# def decrypt(message, privkey):
# 
# Path: twistedbot/logbot.py
# def exit_on_error(_stuff=None, _why=None):
#     def __init__(self, f):
#     def formatTime(self, when):
#     def emit(self, eventDict):
#     def __init__(self, name):
#     def msg(self, *args, **kwargs):
#     def err(self, *args, **kwargs):
# def getlogger(name):
# def start_filelog(filename=None, kind="other_log"):
# def start_bot_filelog():
# def start_proxy_filelog():
# class MinecraftLogObserver(object):
# class Logger(object):
# 
# Path: twistedbot/config.py
# DEBUG = False
# WHISPER = False
# ONLINE_LOGIN = False
# USE_ENCRYPTION = ONLINE_LOGIN
# COMMANDER = "lukleh"
# COMMANDER = COMMANDER.lower()
# USERNAME = "twistedbot"
# PASSWORD = ""
# EMAIL = ""
# SERVER_HOST = "127.0.0.1"
# SERVER_PORT = 25565
# PROXY_PORT = 25566
# LOCALE = 'en_GB'
# VIEWDISTANCE = 0  # 0-3 far, normal, short, tiny
# DIFFICULTY = 0  # 0-3 peaceful, easy, normal, hard
# PROTOCOL_VERSION = 61
# CONNECTION_MAX_DELAY = 5
# CONNECTION_INITIAL_DELAY = 0.1
# KEEP_ALIVE_PERIOD = 300  # that is 6000 ticks
# WORLD_HEIGHT = 256
# CHUNK_SIDE_LEN = 16
# PLAYER_HEIGHT = 1.8
# PLAYER_EYELEVEL = 1.62
# PLAYER_RADIUS = 0.3
# PLAYER_WIDTH = 0.6
# MAX_JUMP_HEIGHT = 1.25
# MAX_STEP_HEIGHT = 0.5
# MAX_WATER_JUMP_HEIGHT = 0.67
# MAX_VINE_JUMP_HEIGHT = 0.35
# BLOCK_FALL = 0.08
# DRAG = 0.98
# SPEED_ON_GROUND = 0.1
# SPEED_IN_AIR = 0.02
# SPEED_JUMP = 0.42
# SPEED_LIQUID_JUMP = 0.04
# SPEED_CLIMB = 0.2
# TIME_STEP = 0.05
# COST_JUMP = 1.1
# COST_LADDER = 0.21 / \
#     0.15  # common speed on ground / max speed on ladder
# COST_FALL = 1.1
# COST_DIRECT = 1
# COST_DIAGONAL = math.sqrt(2) * COST_DIRECT
# PATHFIND_LIMIT = 400  # roughly in blocks
# HORIZONTAL_MOVE_DISTANCE_LIMIT = 2.83


import syspath_fix
import argparse
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, Factory
from twistedbot.packets import make_packet, parse_packets, packets
from twistedbot import encryption
from twistedbot import logbot
from twistedbot import config
                log.msg("packet %d cannot be unencrypted" % p[0])

    def on_encryption_key_responce(self, c):
        """
        STEP 3.1: decrypt client AES key with our RSA key
        """
        aeskey = encryption.decrypt(c.shared_secret, self.factory.rsakey)
        self.cipher = encryption.make_aes(aeskey, aeskey)
        self.decipher = encryption.make_aes(aeskey, aeskey)

    def send_encryption_key_request(self, c):
        """
        STEP 2.2: substitute our own key for the client side
        """
        pubkey = encryption.export_pubkey(self.factory.rsakey)
        c.public_key_length = len(pubkey)
        c.public_key = pubkey
        c.token_length = 4
        c.verify_token = encryption.get_random_bytes(c.token_length)
        self.sendData(chr(253) + packets[253].build(c))


class ProxyClientFactory(Factory):

    def __init__(self, host, port):
        self.mgsside = "CLIENT"
        self.log = logbot.getlogger(self.mgsside)
        self.host = host
        self.port = port
        self.log.msg("Generating RSA key pair")

# Path: twistedbot/packets.py
# def ucs2(name):
#     def ucs2_encode(data, errors="replace"):
#         def encode(self, input, final=False):
#     def _encode(self, obj, context):
# def AlphaString(name):
# def Bool(*args, **kwargs):
# def ByteString(name, size_name, encoding=None):
#     def _decode(self, obj, context):
# def NBTdata(name, size_name):
# def itemstack_as_slotdata(itemstack):
#     def _decode(self, obj, context):
#     def _encode(self, obj, context):
# def packet_stream_print_header(context):
# def print_and_return(pv, rv):
# def parse_packets(bytestream):
# def parse_packets_incrementally(bytestream):
# def make_packet(packet, payload, template=None):
# def make_error_packet(message):
#     class UCS2IncrementalEncoder(IncrementalEncoder):
#     class UCS2IncrementalDecoder(BufferedIncrementalDecoder):
#     class UCS2StreamWriter(StreamWriter):
#     class UCS2StreamReader(StreamReader):
# class DoubleAdapter(LengthValueAdapter):
# class NBTAdapter(Adapter):
# class MetadataAdapter(Adapter):
# 
# Path: twistedbot/encryption.py
# def get_random_bytes(size=16):
# def make_aes(aes_key, aes_iv, segment_size=8):
# def gen_rsa_key(size=1024):
# def export_pubkey(key):
# def load_pubkey(s):
# def encrypt(message, pubkey):
# def decrypt(message, privkey):
# 
# Path: twistedbot/logbot.py
# def exit_on_error(_stuff=None, _why=None):
#     def __init__(self, f):
#     def formatTime(self, when):
#     def emit(self, eventDict):
#     def __init__(self, name):
#     def msg(self, *args, **kwargs):
#     def err(self, *args, **kwargs):
# def getlogger(name):
# def start_filelog(filename=None, kind="other_log"):
# def start_bot_filelog():
# def start_proxy_filelog():
# class MinecraftLogObserver(object):
# class Logger(object):
# 
# Path: twistedbot/config.py
# DEBUG = False
# WHISPER = False
# ONLINE_LOGIN = False
# USE_ENCRYPTION = ONLINE_LOGIN
# COMMANDER = "lukleh"
# COMMANDER = COMMANDER.lower()
# USERNAME = "twistedbot"
# PASSWORD = ""
# EMAIL = ""
# SERVER_HOST = "127.0.0.1"
# SERVER_PORT = 25565
# PROXY_PORT = 25566
# LOCALE = 'en_GB'
# VIEWDISTANCE = 0  # 0-3 far, normal, short, tiny
# DIFFICULTY = 0  # 0-3 peaceful, easy, normal, hard
# PROTOCOL_VERSION = 61
# CONNECTION_MAX_DELAY = 5
# CONNECTION_INITIAL_DELAY = 0.1
# KEEP_ALIVE_PERIOD = 300  # that is 6000 ticks
# WORLD_HEIGHT = 256
# CHUNK_SIDE_LEN = 16
# PLAYER_HEIGHT = 1.8
# PLAYER_EYELEVEL = 1.62
# PLAYER_RADIUS = 0.3
# PLAYER_WIDTH = 0.6
# MAX_JUMP_HEIGHT = 1.25
# MAX_STEP_HEIGHT = 0.5
# MAX_WATER_JUMP_HEIGHT = 0.67
# MAX_VINE_JUMP_HEIGHT = 0.35
# BLOCK_FALL = 0.08
# DRAG = 0.98
# SPEED_ON_GROUND = 0.1
# SPEED_IN_AIR = 0.02
# SPEED_JUMP = 0.42
# SPEED_LIQUID_JUMP = 0.04
# SPEED_CLIMB = 0.2
# TIME_STEP = 0.05
# COST_JUMP = 1.1
# COST_LADDER = 0.21 / \
#     0.15  # common speed on ground / max speed on ladder
# COST_FALL = 1.1
# COST_DIRECT = 1
# COST_DIAGONAL = math.sqrt(2) * COST_DIRECT
# PATHFIND_LIMIT = 400  # roughly in blocks
# HORIZONTAL_MOVE_DISTANCE_LIMIT = 2.83


# Path: proxy.py
import syspath_fix
import argparse
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, Factory
from twistedbot.packets import make_packet, parse_packets, packets
from twistedbot import encryption
from twistedbot import logbot
from twistedbot import config

                log.msg("packet %d cannot be unencrypted" % p[0])

    def on_encryption_key_responce(self, c):
        """
        STEP 3.1: decrypt client AES key with our RSA key
        """
        aeskey = encryption.decrypt(c.shared_secret, self.factory.rsakey)
        self.cipher = encryption.make_aes(aeskey, aeskey)
        self.decipher = encryption.make_aes(aeskey, aeskey)

    def send_encryption_key_request(self, c):
        """
        STEP 2.2: substitute our own key for the client side
        """
        pubkey = encryption.export_pubkey(self.factory.rsakey)
        c.public_key_length = len(pubkey)
        c.public_key = pubkey
        c.token_length = 4
        c.verify_token = encryption.get_random_bytes(c.token_length)
        self.sendData(chr(253) + packets[253].build(c))


class ProxyClientFactory(Factory):

    def __init__(self, host, port):
        self.mgsside = "CLIENT"
        self.log = logbot.getlogger(self.mgsside)
        self.host = host
        self.port = port
        self.log.msg("Generating RSA key pair")

        self.rsakey = encryption.gen_rsa_key()