Search in sources :

Example 1 with DisconnectingEvent

use of com.github.steveice10.packetlib.event.session.DisconnectingEvent in project DragonProxy by DragonetMC.

the class PCDownstreamSession method connect.

public void connect(String addr, int port) {
    if (this.protocol == null) {
        // Clear the flags
        upstream.onConnected();
        upstream.disconnect("ERROR! ");
        return;
    }
    remoteClient = new Client(addr, port, protocol, new TcpSessionFactory());
    remoteClient.getSession().setConnectTimeout(5);
    remoteClient.getSession().setReadTimeout(5);
    remoteClient.getSession().setWriteTimeout(5);
    remoteClient.getSession().addListener(new SessionAdapter() {

        @Override
        public void connected(ConnectedEvent event) {
            proxy.getLogger().info(proxy.getLang().get(Lang.MESSAGE_REMOTE_CONNECTED, upstream.getUsername(), upstream.getRemoteAddress()));
            upstream.onConnected();
        }

        @Override
        public void packetSending(PacketSendingEvent event) {
            if (proxy.getAuthMode().equalsIgnoreCase("hybrid")) {
                if (protocol.getSubProtocol() == SubProtocol.HANDSHAKE && event.getPacket() instanceof HandshakePacket) {
                    HandshakePacket packet = event.getPacket();
                    String host = remoteClient.getSession().getHost() + "\0" + upstream.getProfile().getChainJWT();
                    packet = new HandshakePacket(packet.getProtocolVersion(), host, packet.getPort(), packet.getIntent());
                    event.setPacket(packet);
                }
            }
        }

        @Override
        public void disconnected(DisconnectedEvent event) {
            System.out.println("DisconnectedEvent " + event.getCause() + " " + event.getReason());
            upstream.disconnect(proxy.getLang().get(event.getReason()));
        }

        @Override
        public void disconnecting(DisconnectingEvent event) {
            System.out.println("DisconnectingEvent " + event.getCause() + " " + event.getReason());
            upstream.disconnect(proxy.getLang().get(event.getReason()));
        }

        @Override
        public void packetReceived(PacketReceivedEvent event) {
            // Handle the packet
            try {
                PEPacket[] packets = PacketTranslatorRegister.translateToPE(upstream, event.getPacket());
                if (packets == null) {
                    return;
                }
                if (packets.length <= 0) {
                    return;
                }
                if (packets.length == 1) {
                    upstream.sendPacket(packets[0]);
                } else {
                    upstream.sendAllPackets(packets, false);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    });
    remoteClient.getSession().connect();
}
Also used : PacketReceivedEvent(com.github.steveice10.packetlib.event.session.PacketReceivedEvent) HandshakePacket(com.github.steveice10.mc.protocol.packet.handshake.client.HandshakePacket) PacketSendingEvent(com.github.steveice10.packetlib.event.session.PacketSendingEvent) DisconnectedEvent(com.github.steveice10.packetlib.event.session.DisconnectedEvent) DisconnectingEvent(com.github.steveice10.packetlib.event.session.DisconnectingEvent) ConnectedEvent(com.github.steveice10.packetlib.event.session.ConnectedEvent) Client(com.github.steveice10.packetlib.Client) SessionAdapter(com.github.steveice10.packetlib.event.session.SessionAdapter) TcpSessionFactory(com.github.steveice10.packetlib.tcp.TcpSessionFactory)

Aggregations

HandshakePacket (com.github.steveice10.mc.protocol.packet.handshake.client.HandshakePacket)1 Client (com.github.steveice10.packetlib.Client)1 ConnectedEvent (com.github.steveice10.packetlib.event.session.ConnectedEvent)1 DisconnectedEvent (com.github.steveice10.packetlib.event.session.DisconnectedEvent)1 DisconnectingEvent (com.github.steveice10.packetlib.event.session.DisconnectingEvent)1 PacketReceivedEvent (com.github.steveice10.packetlib.event.session.PacketReceivedEvent)1 PacketSendingEvent (com.github.steveice10.packetlib.event.session.PacketSendingEvent)1 SessionAdapter (com.github.steveice10.packetlib.event.session.SessionAdapter)1 TcpSessionFactory (com.github.steveice10.packetlib.tcp.TcpSessionFactory)1