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();
}
Aggregations